123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 示例接口
- */
- class Demo extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function get_car_price(){
- $car_id = 1;
- $car_info = Db::name('car')->where('id',$car_id)->find();
- //最终结构
- if(empty($car_info['price_json'])){
- $car_info['price_json'] = [];
- }else{
- $car_info['price_json'] = json_decode($car_info['price_json'],true);
- }
- $all_price = $this->rili_price($car_info['price'],$car_info['price_json']);
- //dump($all_price);
- $this->success(1,$all_price);
- }
- private function rili_price($default_price,$car_price)
- {
- $month_num = 12; //预算3个月,不可以超过12
- $toyear = date('Y'); //今天 年份
- $tomonth = date('n'); //今天 几月
- $today = date('j'); //今天 几号
- //今年每月天数
- $toyear_month_days = [
- 1 => 31,
- 2 => $toyear%4 == 0 ? 29 : 28,
- 3 => 31,
- 4 => 30,
- 5 => 31,
- 6 => 30,
- 7 => 31,
- 8 => 31,
- 9 => 30,
- 10 => 31,
- 11 => 30,
- 12 => 31,
- ];
- //明年每月天数
- $nextyear_month_days = [
- 1 => 31,
- 2 => ($toyear+1)%4 == 0 ? 29 : 28,
- 3 => 31,
- 4 => 30,
- 5 => 31,
- 6 => 30,
- 7 => 31,
- 8 => 31,
- 9 => 30,
- 10 => 31,
- 11 => 30,
- 12 => 31,
- ];
- //今年假期,明年假期
- $toyear_holiday = Db::name('car_price_holiday')->where('year',$toyear)->select();
- $nextyear_holiday = Db::name('car_price_holiday')->where('year',$toyear+1)->select();
- //提前解析,不在循环里解析
- foreach($toyear_holiday as $key => $val){
- $toyear_holiday[$key]['date_json'] = json_decode($val['date_json'],true);
- }
- foreach($nextyear_holiday as $key => $val){
- $nextyear_holiday[$key]['date_json'] = json_decode($val['date_json'],true);
- }
- //本月剩余日期
- $this_month = [];
- $tomonth_days = $toyear_month_days[$tomonth]; //本月天数
- for($i = 1;$i <= $tomonth_days; $i++){
- //新的一天
- $date = $toyear.'-'.$tomonth.'-'.$i;
- $newday = [];
- $newday = ['name'=>$i,'date'=>$date,'val'=>strtotime($date),'hasTips'=>'false','tips'=>'','istoday'=>-1,'price'=>$default_price,'jq_id'=>0];
- $newday['date'] = date('Y-m-d',$newday['val']); //日期格式化
- $newday['week'] = date('w',$newday['val']); //周几
- //追加价格
- foreach($car_price as $key => $val){
- if($key == $newday['date']){
- $newday['price'] = $val;
- }
- }
- //追加假期
- foreach($toyear_holiday as $key => $val){
- foreach($val['date_json'] as $k => $v){
- if($v == $newday['date']){
- $newday['hasTips'] = 'true';
- $newday['tips'] = $val['title'];
- $newday['jq_id'] = $val['id'];
- }
- }
- }
- //关于今天,放在最后优先级高于假期
- if($i == $today){
- $newday['hasTips'] = 'true';
- $newday['tips'] = '今天';
- $newday['istoday'] = 1;
- }
- if($i > $today){
- $newday['istoday'] = 2;
- }
- //当本月的第一天不是周天,前追N条数据
- if($i == 1 && $newday['week'] != 0){
- for($n = 1;$n <= $newday['week'];$n++){
- $this_month[] = ['name'=>''];
- }
- }
- $this_month[] = $newday;
- }
- //追加本月
- $all_month[] = [
- 'monthTxt' => $toyear.'-'.$tomonth,
- 'list' => $this_month,
- ];
- //dump($all_month);exit;
- //剩余几个月
- for($m = 1;$m <= $month_num; $m++){
- $new_month = [];
- $m_yeah = $tomonth+$m > 12 ? $toyear+1 : $toyear; //年份,大于12进入下一年
- $m_month = $tomonth+$m > 12 ? $tomonth+$m-12 : $tomonth+$m; //月份,大于12进入下一年
- //dump($m_yeah);
- //dump($m_month);
- $m_month_days = $m_yeah == $toyear ? $toyear_month_days[$m_month] : $nextyear_month_days[$m_month];//本月天数
- //dump($m_month_days);
- //循环这个月
- for($d = 1;$d <= $m_month_days;$d++){
- //新的一天
- $date = $m_yeah.'-'.$m_month.'-'.$d;
- $newday = [];
- $newday = ['name'=>$d,'date'=>$date,'val'=>strtotime($date),'hasTips'=>'false','tips'=>'','istoday'=>2,'price'=>$default_price,'jq_id'=>0];
- $newday['date'] = date('Y-m-d',$newday['val']);//日期格式化
- $newday['week'] = date('w',$newday['val']);//周几
- //追加价格
- foreach($car_price as $key => $val){
- if($key == $newday['date']){
- $newday['price'] = $val;
- }
- }
- //追加假期
- $holiday = $m_yeah == $toyear ? $toyear_holiday : $nextyear_holiday;
- foreach($holiday as $key => $val){
- foreach($val['date_json'] as $k => $v){
- if($v == $newday['date']){
- $newday['hasTips'] = 'true';
- $newday['tips'] = $val['title'];
- $newday['jq_id'] = $val['id'];
- }
- }
- }
- //当本月的第一天不是周天,前追N条数据
- if($d == 1 && $newday['week'] != 0){
- for($n = 1;$n <= $newday['week'];$n++){
- $new_month[] = ['name'=>''];
- }
- }
- $new_month[] = $newday;
- }
- //追加未来几个月
- $all_month[] = [
- 'monthTxt' => $m_yeah.'-'.$m_month,
- 'list' => $new_month,
- ];
- }
- //价格日历结果
- //dump($all_month);
- //假期结果
- $holiday = array_merge($toyear_holiday,$nextyear_holiday);
- //dump($holiday);exit;
- return [
- 'rili'=>$all_month,
- 'holiday' => $holiday,
- ];
- }
- }
|