Demo.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 示例接口
  7. */
  8. class Demo extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. public function get_car_price(){
  13. $car_id = 1;
  14. $car_info = Db::name('car')->where('id',$car_id)->find();
  15. //最终结构
  16. if(empty($car_info['price_json'])){
  17. $car_info['price_json'] = [];
  18. }else{
  19. $car_info['price_json'] = json_decode($car_info['price_json'],true);
  20. }
  21. $all_price = $this->rili_price($car_info['price'],$car_info['price_json']);
  22. //dump($all_price);
  23. $this->success(1,$all_price);
  24. }
  25. private function rili_price($default_price,$car_price)
  26. {
  27. $month_num = 12; //预算3个月,不可以超过12
  28. $toyear = date('Y'); //今天 年份
  29. $tomonth = date('n'); //今天 几月
  30. $today = date('j'); //今天 几号
  31. //今年每月天数
  32. $toyear_month_days = [
  33. 1 => 31,
  34. 2 => $toyear%4 == 0 ? 29 : 28,
  35. 3 => 31,
  36. 4 => 30,
  37. 5 => 31,
  38. 6 => 30,
  39. 7 => 31,
  40. 8 => 31,
  41. 9 => 30,
  42. 10 => 31,
  43. 11 => 30,
  44. 12 => 31,
  45. ];
  46. //明年每月天数
  47. $nextyear_month_days = [
  48. 1 => 31,
  49. 2 => ($toyear+1)%4 == 0 ? 29 : 28,
  50. 3 => 31,
  51. 4 => 30,
  52. 5 => 31,
  53. 6 => 30,
  54. 7 => 31,
  55. 8 => 31,
  56. 9 => 30,
  57. 10 => 31,
  58. 11 => 30,
  59. 12 => 31,
  60. ];
  61. //今年假期,明年假期
  62. $toyear_holiday = Db::name('car_price_holiday')->where('year',$toyear)->select();
  63. $nextyear_holiday = Db::name('car_price_holiday')->where('year',$toyear+1)->select();
  64. //提前解析,不在循环里解析
  65. foreach($toyear_holiday as $key => $val){
  66. $toyear_holiday[$key]['date_json'] = json_decode($val['date_json'],true);
  67. }
  68. foreach($nextyear_holiday as $key => $val){
  69. $nextyear_holiday[$key]['date_json'] = json_decode($val['date_json'],true);
  70. }
  71. //本月剩余日期
  72. $this_month = [];
  73. $tomonth_days = $toyear_month_days[$tomonth]; //本月天数
  74. for($i = 1;$i <= $tomonth_days; $i++){
  75. //新的一天
  76. $date = $toyear.'-'.$tomonth.'-'.$i;
  77. $newday = [];
  78. $newday = ['name'=>$i,'date'=>$date,'val'=>strtotime($date),'hasTips'=>'false','tips'=>'','istoday'=>-1,'price'=>$default_price,'jq_id'=>0];
  79. $newday['date'] = date('Y-m-d',$newday['val']); //日期格式化
  80. $newday['week'] = date('w',$newday['val']); //周几
  81. //追加价格
  82. foreach($car_price as $key => $val){
  83. if($key == $newday['date']){
  84. $newday['price'] = $val;
  85. }
  86. }
  87. //追加假期
  88. foreach($toyear_holiday as $key => $val){
  89. foreach($val['date_json'] as $k => $v){
  90. if($v == $newday['date']){
  91. $newday['hasTips'] = 'true';
  92. $newday['tips'] = $val['title'];
  93. $newday['jq_id'] = $val['id'];
  94. }
  95. }
  96. }
  97. //关于今天,放在最后优先级高于假期
  98. if($i == $today){
  99. $newday['hasTips'] = 'true';
  100. $newday['tips'] = '今天';
  101. $newday['istoday'] = 1;
  102. }
  103. if($i > $today){
  104. $newday['istoday'] = 2;
  105. }
  106. //当本月的第一天不是周天,前追N条数据
  107. if($i == 1 && $newday['week'] != 0){
  108. for($n = 1;$n <= $newday['week'];$n++){
  109. $this_month[] = ['name'=>''];
  110. }
  111. }
  112. $this_month[] = $newday;
  113. }
  114. //追加本月
  115. $all_month[] = [
  116. 'monthTxt' => $toyear.'-'.$tomonth,
  117. 'list' => $this_month,
  118. ];
  119. //dump($all_month);exit;
  120. //剩余几个月
  121. for($m = 1;$m <= $month_num; $m++){
  122. $new_month = [];
  123. $m_yeah = $tomonth+$m > 12 ? $toyear+1 : $toyear; //年份,大于12进入下一年
  124. $m_month = $tomonth+$m > 12 ? $tomonth+$m-12 : $tomonth+$m; //月份,大于12进入下一年
  125. //dump($m_yeah);
  126. //dump($m_month);
  127. $m_month_days = $m_yeah == $toyear ? $toyear_month_days[$m_month] : $nextyear_month_days[$m_month];//本月天数
  128. //dump($m_month_days);
  129. //循环这个月
  130. for($d = 1;$d <= $m_month_days;$d++){
  131. //新的一天
  132. $date = $m_yeah.'-'.$m_month.'-'.$d;
  133. $newday = [];
  134. $newday = ['name'=>$d,'date'=>$date,'val'=>strtotime($date),'hasTips'=>'false','tips'=>'','istoday'=>2,'price'=>$default_price,'jq_id'=>0];
  135. $newday['date'] = date('Y-m-d',$newday['val']);//日期格式化
  136. $newday['week'] = date('w',$newday['val']);//周几
  137. //追加价格
  138. foreach($car_price as $key => $val){
  139. if($key == $newday['date']){
  140. $newday['price'] = $val;
  141. }
  142. }
  143. //追加假期
  144. $holiday = $m_yeah == $toyear ? $toyear_holiday : $nextyear_holiday;
  145. foreach($holiday as $key => $val){
  146. foreach($val['date_json'] as $k => $v){
  147. if($v == $newday['date']){
  148. $newday['hasTips'] = 'true';
  149. $newday['tips'] = $val['title'];
  150. $newday['jq_id'] = $val['id'];
  151. }
  152. }
  153. }
  154. //当本月的第一天不是周天,前追N条数据
  155. if($d == 1 && $newday['week'] != 0){
  156. for($n = 1;$n <= $newday['week'];$n++){
  157. $new_month[] = ['name'=>''];
  158. }
  159. }
  160. $new_month[] = $newday;
  161. }
  162. //追加未来几个月
  163. $all_month[] = [
  164. 'monthTxt' => $m_yeah.'-'.$m_month,
  165. 'list' => $new_month,
  166. ];
  167. }
  168. //价格日历结果
  169. //dump($all_month);
  170. //假期结果
  171. $holiday = array_merge($toyear_holiday,$nextyear_holiday);
  172. //dump($holiday);exit;
  173. return [
  174. 'rili'=>$all_month,
  175. 'holiday' => $holiday,
  176. ];
  177. }
  178. }