Car.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\cache\driver\Redis;
  6. use app\common\library\Area;
  7. /**
  8. * 汽车接口
  9. */
  10. class Car extends Api
  11. {
  12. protected $noNeedLogin = ['index','showall','car_info','car_comment','get_car_price'];
  13. protected $noNeedRight = ['*'];
  14. public function index(){
  15. $where = [];
  16. $where['car.status'] = 1;
  17. $where['car.audit_status'] = 1;
  18. //车型
  19. $type_id = input('type_id',0);
  20. if($type_id){
  21. $where['p.type_id'] = $type_id;
  22. }
  23. //品牌
  24. $brand_id = input('brand_id',0);
  25. if($brand_id){
  26. $where['p.brand_id'] = ['IN',$brand_id];
  27. }
  28. //变速箱
  29. $gear_id = input('gear_id',0);
  30. if($gear_id){
  31. $where['p.gear_id'] = $gear_id;
  32. }
  33. //座位数
  34. $seat_id = input('seat_id',0);
  35. if($seat_id){
  36. $where['p.seat_id'] = ['IN',$seat_id];
  37. }
  38. //车辆配置
  39. $config_ids = input('config_ids','');
  40. if($config_ids){
  41. $config_ids = explode(',',$config_ids);
  42. foreach($config_ids as $ck => $cv){
  43. //$where[]=['exp','FIND_IN_SET('.$cv.',car.config_ids)'];
  44. $where[]=['exp',Db::raw("FIND_IN_SET('".$cv."',car.config_ids)")];
  45. }
  46. }
  47. //便捷取还
  48. $back_ids = input('back_ids','');
  49. if($back_ids){
  50. $back_ids = explode(',',$back_ids);
  51. foreach($back_ids as $sk => $sv){
  52. //$where[] = ['exp','FIND_IN_SET('.$back_ids.',company.back_ids)'];
  53. $where[] = ['exp',Db::raw("FIND_IN_SET('".$sv."',company.back_ids)")];
  54. }
  55. }
  56. //门店服务
  57. $service_ids = input('service_ids','');
  58. if($service_ids){
  59. $service_ids = explode(',',$service_ids);
  60. foreach($service_ids as $sk => $sv){
  61. //$where[]=['exp','FIND_IN_SET('.$sv.',company.service_ids)'];
  62. $where[]=['exp',Db::raw("FIND_IN_SET('".$sv."',company.service_ids)")];
  63. }
  64. }
  65. //双坐标都符合的公司ids
  66. $get_longitude = input('get_longitude',0);
  67. $get_latitude = input('get_latitude',0);
  68. $back_longitude = input('back_longitude',0);
  69. $back_latitude = input('back_latitude',0);
  70. //符合公司ids
  71. $company_can = $this->area_check($get_longitude,$get_latitude,$back_longitude,$back_latitude);
  72. $where['car.company_id'] = ['IN',$company_can];
  73. //双坐标都符合的公司ids
  74. //排序
  75. $ordertype = input('ordertype',0);
  76. if($ordertype == 1){
  77. $orderby = 'price_min asc';
  78. }elseif($ordertype == 2){
  79. $orderby = 'price_min desc';
  80. }else{
  81. $orderby = 'car.id desc';
  82. }
  83. //列表
  84. $list = Db::name('car')
  85. ->field('
  86. car.*,
  87. min(car.pricetoday) price_min,count(car.id) as baojianum,group_concat(car.id) as concat_car_id,
  88. p.type_id,p.brand_id,p.model_id,p.image,p.images,p.gear_id,p.pailiang,p.seat_id,p.banben,
  89. company.back_ids,company.service_ids,
  90. brand.name as brand_name,model.name as model_name,gear.name as gear_name,seat.name as seat_name
  91. ')
  92. ->join('car_public p','car.carpublic_id = p.id','LEFT')
  93. ->join('company','car.company_id = company.id','LEFT')
  94. ->join('store','car.store_id = store.id','LEFT')
  95. ->join('car_enum_brand brand','p.brand_id = brand.id','LEFT')
  96. ->join('car_enum_model model','p.model_id = model.id','LEFT')
  97. ->join('car_enum_gear gear','p.gear_id = gear.id','LEFT')
  98. ->join('car_enum_seat seat','p.seat_id = seat.id','LEFT')
  99. ->where($where)->order($orderby)->group('carpublic_id')->autopage()->select();
  100. $list = list_domain_image($list,['image','images']);
  101. $this->success('success',$list);
  102. }
  103. //双坐标都符合的公司ids
  104. private function area_check($get_longitude,$get_latitude,$back_longitude,$back_latitude){
  105. $company_can = [];//符合公司ids
  106. $companys = Db::name('company')->column('id');
  107. foreach($companys as $key => $val){
  108. $store_zuobiao = Db::name('store_zuobiao')->where('company_id',$val)->column('zuobiao');//某公司下所有门店的坐标
  109. if(empty($store_zuobiao)){
  110. continue;
  111. }
  112. //dump($store_zuobiao);
  113. $newArr = []; //坐标集合
  114. foreach($store_zuobiao as $k => $v){
  115. if(empty($v)){ continue;}
  116. $v = json_decode($v, true);
  117. if(!is_array($v)){ continue;}
  118. /*if(isset($v[0]) && is_array($v[0])){
  119. $newArr[] = $v[0];
  120. }*/
  121. $newArr[] = $v;
  122. }
  123. //dump($newArr);
  124. if(empty($newArr)){
  125. continue;
  126. }
  127. $area = new Area($newArr);
  128. $get_bol = $area->checkPoint($get_longitude, $get_latitude);
  129. $back_bol = $area->checkPoint($back_longitude, $back_latitude);
  130. //dump($get_bol);
  131. //dump($back_bol);
  132. //借车点和还车点都满足
  133. if($get_bol !== false && $back_bol !== false && $get_bol >= 0 && $back_bol >= 0){
  134. //echo $val;
  135. $company_can[] = $val;
  136. }
  137. //echo '<hr>';
  138. }
  139. return $company_can;
  140. //dump($company_can);
  141. //取车坐标
  142. }
  143. public function test(){
  144. //大兴取116.339878,39.730733
  145. //昌平还116.197299,40.234052
  146. //北京中心还116.372074,39.898331
  147. $get_back_fee = $this->store_fanwei_check(56,'116.339878','39.730733','116.372074','39.898331');
  148. dump($get_back_fee);
  149. }
  150. //车辆取还费
  151. private function store_fanwei_check($car_id,$get_longitude,$get_latitude,$back_longitude,$back_latitude){
  152. $return_fee = ['get_price'=>0,'back_price'=>0,'get_zuobiao_id'=>0,'back_zuobiao_id'=>0];
  153. $company_id = Db::name('car')->where('id',$car_id)->value('company_id');
  154. $store_zuobiao = Db::name('store_zuobiao')->where('company_id',$company_id)->column('id,zuobiao');//某公司下所有门店的坐标
  155. $store_fee = Db::name('store_zuobiao')->where('company_id',$company_id)->column('id,get_price,back_price');//某公司下所有门店的坐标
  156. if(empty($store_zuobiao)){
  157. return $return_fee;
  158. }
  159. //dump($store_zuobiao);
  160. $newArr = []; //坐标集合
  161. foreach($store_zuobiao as $k => $v){
  162. if(empty($v)){ continue;}
  163. $v = json_decode($v, true);
  164. if(!is_array($v)){ continue;}
  165. $newArr[$k] = $v;
  166. }
  167. //dump($newArr);
  168. if(empty($newArr)){
  169. return $return_fee;
  170. }
  171. $area = new Area($newArr);
  172. $get_bol = $area->checkPoint($get_longitude, $get_latitude);
  173. $back_bol = $area->checkPoint($back_longitude, $back_latitude);
  174. //dump($get_bol);//取
  175. //dump($back_bol);//还
  176. //借车点和还车点都满足
  177. if($get_bol !== false && $get_bol >= 0){
  178. $return_fee['get_price'] = $store_fee[$get_bol]['get_price'];
  179. $return_fee['get_zuobiao_id'] = $get_bol;
  180. }
  181. if($back_bol !== false && $back_bol >= 0){
  182. $return_fee['back_price'] = $store_fee[$back_bol]['back_price'];
  183. $return_fee['back_zuobiao_id'] = $back_bol;
  184. }
  185. //dump($return_fee);
  186. return $return_fee;
  187. }
  188. //展开车
  189. public function showall(){
  190. $id = input('concat_car_id',0);
  191. $address = input('address','');
  192. $starttime = input('starttime');
  193. $endtime = input('endtime');
  194. $days = bcdiv(($endtime - $starttime),86400,2);
  195. $longitude = input('longitude','');
  196. $latitude = input('latitude','');
  197. $where = [];
  198. $where['car.status'] = 1;
  199. $where['car.audit_status'] = 1;
  200. $where['car.id'] = ['IN',$id];
  201. $list = Db::name('car')
  202. ->field('
  203. car.*,
  204. p.type_id,p.brand_id,p.model_id,p.image,p.images,p.gear_id,p.seat_id,p.pailiang,p.banben,
  205. company.name as company_name,company.back_ids,company.service_ids,
  206. store.longitude as store_longitude,store.latitude as store_latitude,
  207. brand.name as brand_name,model.name as model_name,gear.name as gear_name,seat.name as seat_name
  208. ')
  209. ->join('car_public p','car.carpublic_id = p.id','LEFT')
  210. ->join('company','car.company_id = company.id','LEFT')
  211. ->join('store','car.store_id = store.id','LEFT')
  212. ->join('car_enum_brand brand','p.brand_id = brand.id','LEFT')
  213. ->join('car_enum_model model','p.model_id = model.id','LEFT')
  214. ->join('car_enum_gear gear','p.gear_id = gear.id','LEFT')
  215. ->join('car_enum_seat seat','p.seat_id = seat.id','LEFT')
  216. ->where($where)->order('car.pricetoday asc')->select();
  217. $list = list_domain_image($list,['image','images']);
  218. $redis = new Redis ();
  219. $redis->handler()->geoAdd("cs", $longitude, $latitude, 'kaishi_address');
  220. foreach($list as $key => &$val){
  221. //退还标签
  222. $val['backs'] = Db::name('store_enum_back')->where('id','IN',$val['back_ids'])->column('name');
  223. //服务标签
  224. $val['service'] = Db::name('store_enum_service')->where('id','IN',$val['service_ids'])->column('name');
  225. //配置标签
  226. $val['config'] = Db::name('car_enum_config')->where('id','IN',$val['config_ids'])->column('name');
  227. //取车点
  228. $val['get_address'] = $address;
  229. $val['days'] = $days;
  230. $val['price'] = $val['pricetoday']; //今日价格
  231. $val['price_total'] = bcmul($val['pricetoday'],$days,2);
  232. //门店直线距离
  233. $rediskey = 'juli' . $val['store_id'];
  234. $redis->handler()->geoAdd("cs", $val['store_longitude'], $val['store_latitude'], $rediskey);
  235. $distance = $redis->handler()->geoDist("cs", 'kaishi_address', $rediskey, 'km');
  236. $val['distance'] = $distance !== false ? $distance : 0;
  237. //合并image到images
  238. $val['images'] .= ','.$val['image'];
  239. //追加客服图片与客服电话
  240. $val['kefu_mobile'] = config('site.kefu_mobile');
  241. $val['kefu_weixin'] = localpath_to_netpath(config('site.kefu_weixin'));
  242. }
  243. $this->success('success',$list);
  244. }
  245. //单车信息
  246. public function car_info(){
  247. $id = input('id');
  248. $where = [];
  249. $where['car.id'] = $id;
  250. $info = Db::name('car')
  251. ->field('
  252. car.*,
  253. p.type_id,p.brand_id,p.model_id,p.image,p.images,p.gear_id,p.seat_id,p.pailiang,p.banben,
  254. company.name as company_name,company.back_ids,company.service_ids,
  255. store.longitude as store_longitude,store.latitude as store_latitude,
  256. brand.name as brand_name,model.name as model_name,gear.name as gear_name,seat.name as seat_name
  257. ')
  258. ->join('car_public p','car.carpublic_id = p.id','LEFT')
  259. ->join('company','car.company_id = company.id','LEFT')
  260. ->join('store','car.store_id = store.id','LEFT')
  261. ->join('car_enum_brand brand','p.brand_id = brand.id','LEFT')
  262. ->join('car_enum_model model','p.model_id = model.id','LEFT')
  263. ->join('car_enum_gear gear','p.gear_id = gear.id','LEFT')
  264. ->join('car_enum_seat seat','p.seat_id = seat.id','LEFT')
  265. ->where($where)->find();
  266. $info = info_domain_image($info,['image','images']);
  267. $info['price'] = $info['pricetoday'];//今日价格
  268. //退还标签
  269. $info['backs'] = Db::name('store_enum_back')->where('id','IN',$info['back_ids'])->column('name');
  270. //服务标签
  271. $info['service'] = Db::name('store_enum_service')->where('id','IN',$info['service_ids'])->column('name');
  272. //配置标签
  273. $info['config'] = Db::name('car_enum_config')->where('id','IN',$info['config_ids'])->column('name');
  274. $this->success('success',$info);
  275. }
  276. //汽车评价
  277. public function car_comment(){
  278. $id = input('car_id',0);
  279. //评价
  280. $comment = Db::name('order_comment')->alias('c')
  281. ->field('c.*,user.avatar,user.mobile')
  282. ->join('user','c.user_id = user.id','LEFT')
  283. ->where('c.car_id',$id)->order('id desc')->autopage()->select();
  284. foreach($comment as $key => &$val){
  285. $val['avatar'] = localpath_to_netpath($val['avatar']);
  286. $val['mobile'] = str_replace(substr($val['mobile'],3,5),'****',$val['mobile']);
  287. }
  288. $this->success('success',$comment);
  289. }
  290. //确认订单
  291. public function confirmorder(){
  292. $id = input('id');
  293. $starttime = input('starttime');
  294. $endtime = input('endtime');
  295. $days = bcdiv(($endtime - $starttime),86400,2);
  296. $coupon_id = input('coupon_id',0);
  297. $yajin_type = input('yajin_type','zhima');
  298. //双坐标都符合的公司ids
  299. $get_longitude = input('get_longitude',0);
  300. $get_latitude = input('get_latitude',0);
  301. $back_longitude = input('back_longitude',0);
  302. $back_latitude = input('back_latitude',0);
  303. $get_back_fee = $this->store_fanwei_check($id,$get_longitude,$get_latitude,$back_longitude,$back_latitude);
  304. //
  305. $where = [];
  306. $where['car.id'] = $id;
  307. $info = Db::name('car')
  308. ->field('
  309. car.*,
  310. p.type_id,p.brand_id,p.model_id,p.image,p.images,p.gear_id,p.seat_id,p.pailiang,
  311. com.name as company_name,com.back_ids,com.service_ids,com.service_price,
  312. store.longitude as store_longitude,store.latitude as store_latitude,
  313. brand.name as brand_name,model.name as model_name,gear.name as gear_name,seat.name as seat_name
  314. ')
  315. ->join('car_public p','car.carpublic_id = p.id','LEFT')
  316. ->join('company com','car.company_id = company.id','LEFT')
  317. ->join('store','car.store_id = store.id','LEFT')
  318. ->join('car_enum_brand brand','p.brand_id = brand.id','LEFT')
  319. ->join('car_enum_model model','p.model_id = model.id','LEFT')
  320. ->join('car_enum_gear gear','p.gear_id = gear.id','LEFT')
  321. ->join('car_enum_seat seat','p.seat_id = seat.id','LEFT')
  322. ->where($where)->find();
  323. $info['weizhang_price'] = config('site.public_weizhang_price');
  324. $info = info_domain_image($info,['image','images']);
  325. // $info['lease_price'] = bcmul($info['price'],$days,2);
  326. $price_data = $this->get_car_price_do($starttime,$endtime,$info);
  327. $info['lease_price'] = $price_data['lease_price'];
  328. $info['price_data'] = $price_data['price_data'];
  329. $info['base_price'] = bcmul($info['base_price'],$days,2);
  330. $info['get_price'] = $get_back_fee['get_price']; //取车费
  331. $info['back_price'] = $get_back_fee['back_price']; //还车费
  332. $info['total_fee'] = $info['lease_price'] + $info['base_price'] + $info['service_price'] + $info['get_price'] + $info['back_price'];
  333. //芝麻免押才计算押金,否则走线下
  334. if($yajin_type == 'zhima'){
  335. //$info['total_fee'] += $info['yajin_price'] + $info['weizhang_price'];
  336. }
  337. $info['total_fee'] = bcadd($info['total_fee'],0,2);
  338. $coupon_price = Db::name('user_coupons')->where('id',$coupon_id)->value('amount');
  339. $info['coupon_price'] = $coupon_price > 0 ? $coupon_price : 0;
  340. $info['pay_fee'] = $info['total_fee'] - $info['coupon_price'];
  341. //可用优惠券
  342. $coupon_list = Db::name('user_coupons')->where(
  343. [
  344. 'user_id' => $this->auth->id,
  345. 'enough' => ['elt',$info['total_fee']],
  346. 'status' => 0,
  347. 'usetimeend' => ['gt',time()],
  348. 'company_id' => ['IN',[0,$info['company_id']]],
  349. ]
  350. )->select();
  351. $info['coupons'] = $coupon_list;
  352. $this->success('success',$info);
  353. }
  354. //下订单
  355. public function createorder(){
  356. $car_id = input('car_id',0);
  357. $starttime = input('starttime');
  358. $endtime = input('endtime');
  359. $days = bcdiv(($endtime - $starttime),86400,2);
  360. $get_province_name = input('get_province_name','');
  361. $get_city_name = input('get_city_name','');
  362. $get_area_name = input('get_area_name','');
  363. $get_address = input('get_address','');
  364. $get_longitude = input('get_longitude','');
  365. $get_latitude = input('get_latitude','');
  366. $back_province_name = input('back_province_name','');
  367. $back_city_name = input('back_city_name','');
  368. $back_area_name = input('back_area_name','');
  369. $back_address = input('back_address','');
  370. $back_longitude = input('back_longitude','');
  371. $back_latitude = input('back_latitude','');
  372. $user_truename = input('user_truename','');
  373. $user_mobile = input('user_mobile','');
  374. $user_idcard = input('user_idcard','');
  375. $coupon_id = input('coupon_id',0);
  376. $yajin_type = input('yajin_type','offline');
  377. //车辆信息
  378. $car_info = Db::name('car')
  379. ->field('
  380. car.*,
  381. p.shortname,p.image as public_image,p.pailiang,p.banben,
  382. store.longitude as store_longitude,store.latitude as store_latitude,
  383. brand.name as brand_name,model.name as model_name,gear.name as gear_name,seat.name as seat_name,
  384. company.service_price
  385. ')
  386. ->join('car_public p','car.carpublic_id = p.id','LEFT')
  387. ->join('store','car.store_id = store.id','LEFT')
  388. ->join('car_enum_brand brand','p.brand_id = brand.id','LEFT')
  389. ->join('car_enum_model model','p.model_id = model.id','LEFT')
  390. ->join('car_enum_gear gear','p.gear_id = gear.id','LEFT')
  391. ->join('car_enum_seat seat','p.seat_id = seat.id','LEFT')
  392. ->join('company','car.company_id = company.id','LEFT')
  393. ->where('car.id',$car_id)->find();
  394. if($car_info['audit_status'] != 1){
  395. $this->error('不存在的车辆,请重新选择');
  396. }
  397. if($car_info['status'] != 1){
  398. $this->error('该车辆已经被占用');
  399. }
  400. $car_info['weizhang_price'] = config('site.public_weizhang_price') ?: 0;
  401. //数据
  402. $data = [
  403. 'orderno'=> createUniqueNo('N',$this->auth->id),
  404. 'starttime' => $starttime,
  405. 'endtime' => $endtime,
  406. 'company_id' => $car_info['company_id'],
  407. 'get_store_id' => $car_info['store_id'],
  408. 'back_store_id' => $car_info['store_id'], //先默认同上
  409. 'car_id' => $car_id,
  410. 'car_image' => $car_info['public_image'],
  411. 'car_shortname' => $car_info['brand_name'].' '.$car_info['model_name'],
  412. 'car_infoname' => $car_info['gear_name'].'|'.$car_info['seat_name'].'|'.$car_info['pailiang'].'|'.$car_info['banben'],
  413. 'car_chepai' => $car_info['chepai'],
  414. 'price' => $car_info['price'],
  415. 'days' => $days,
  416. //'lease_price' => bcmul($car_info['price'], $days,2), //借出费
  417. 'base_price' => bcmul($car_info['base_price'],$days,2), //基础保障费
  418. 'service_price' => $car_info['service_price'], //手续费
  419. 'yajin_price' => $car_info['yajin_price'], //车辆押金
  420. 'weizhang_price' => $car_info['weizhang_price'], //违章押金
  421. 'coupon_id' => 0,
  422. 'coupon_price' => 0,
  423. 'pay_fee' => 0,
  424. 'get_province_name' => $get_province_name,
  425. 'get_city_name' => $get_city_name,
  426. 'get_area_name' => $get_area_name,
  427. 'get_address' => $get_address,
  428. 'get_longitude' => $get_longitude,
  429. 'get_latitude' => $get_latitude,
  430. 'back_province_name' => $back_province_name,
  431. 'back_city_name' => $back_city_name,
  432. 'back_area_name' => $back_area_name,
  433. 'back_address' => $back_address,
  434. 'back_longitude' => $back_longitude,
  435. 'back_latitude' => $back_latitude,
  436. 'user_id' => $this->auth->id,
  437. 'user_truename' => $user_truename,
  438. 'user_mobile' => $user_mobile,
  439. 'user_idcard' => $user_idcard,
  440. 'status' => 0,
  441. 'createtime' => time(),
  442. ];
  443. //另写价格
  444. $price_data = $this->get_car_price_do($starttime,$endtime,$car_info);
  445. $data['lease_price'] = $price_data['lease_price'];
  446. $data['price_data'] = $price_data['price_data'];
  447. //双坐标都符合的公司ids
  448. $get_back_fee = $this->store_fanwei_check($car_id,$get_longitude,$get_latitude,$back_longitude,$back_latitude);
  449. $data['get_price'] = $get_back_fee['get_price']; //取车费
  450. $data['get_zuobiao_id'] = $get_back_fee['get_zuobiao_id']; //取车范围
  451. $data['back_price'] = $get_back_fee['back_price']; //还车费
  452. $data['back_zuobiao_id'] = $get_back_fee['back_zuobiao_id']; //还车范围
  453. //总计
  454. $data['total_fee'] = $data['lease_price'] + $data['base_price'] + $data['service_price'] + $data['get_price'] + $data['back_price'];
  455. //芝麻免押才计算押金,否则走线下
  456. if($yajin_type == 'zhima'){
  457. //$data['total_fee'] += $data['yajin_price'] + $data['weizhang_price'];
  458. $data['yajin_type'] = 1;
  459. }else{
  460. $data['yajin_type'] = 2;
  461. }
  462. $data['total_fee'] = bcadd($data['total_fee'],0,2);
  463. $data['coupon_id'] = $coupon_id;
  464. $data['coupon_price'] = Db::name('user_coupons')->where('id',$coupon_id)->value('amount') ?: 0; //优惠券减免费
  465. $data['pay_fee'] = $data['total_fee'] - $data['coupon_price']; //实际最终需要支付费
  466. //临时测试使用
  467. //$data['pay_fee'] = 0.09; //测试强制修改
  468. $order_id = Db::name('order')->insertGetId($data);
  469. //优惠券改为已使用
  470. $coupon_rs = Db::name('user_coupons')->where('id',$coupon_id)->update(['status'=>1,'usetime'=>time(),'use_order_id'=>$order_id]);
  471. if($coupon_rs === false){
  472. $this->error('优惠券使用失败');
  473. }
  474. //修改此车辆状态
  475. $car_rs = Db::name('car')->where('id',$car_id)->update(['status'=>0]);
  476. //
  477. $rs = [
  478. 'orderid' => $order_id,
  479. 'pay_fee' => $data['pay_fee'],
  480. ];
  481. $this->success('下单成功',$rs);
  482. }
  483. //测试
  484. public function get_car_price(){
  485. $car_id = 1;
  486. $info = Db::name('car')->where('id',$car_id)->find();
  487. $starttime = '1682825400';
  488. $endtime = '1683171000';//2023-04-05 11:30
  489. $rs = $this->get_car_price_do($starttime,$endtime,$info);
  490. dump($rs);
  491. }
  492. //获得价格
  493. private function get_car_price_do($starttime,$endtime,$info){
  494. if(empty($info['price_json'])){
  495. $price_json = [];
  496. }else{
  497. $price_json = json_decode($info['price_json'],true);
  498. }
  499. $price_default = $info['price'] ? $info['price'] : 0;
  500. $startdate = date('Y-m-d',$starttime);
  501. $startdatetime = strtotime($startdate); //第一天凌晨
  502. $enddate = date('Y-m-d',$endtime);
  503. //开始日期和结束日期是同一天
  504. if($startdate == $enddate){
  505. $start_price = isset($price_json[$startdate]) ? $price_json[$startdate] : $price_default;
  506. $lease_price = bcmul(bcdiv(($endtime - $starttime),86400,4),$start_price,2);
  507. return [
  508. 'lease_price'=> $lease_price,
  509. 'price_data' => json_encode([['date' => $startdate,'price' => $start_price,'fee' => $lease_price]]),
  510. ];
  511. }
  512. //开始日期和结束日期不是同一天
  513. $lease_price = 0;
  514. $price_data = [];
  515. $firstdate = ($startdatetime + 86400 - $starttime) / 3600; //(晚上0点 - 开始) / 3600,第一天小时数
  516. $lastdate = ($endtime - strtotime(date('Y-m-d',$endtime))) / 3600; //(结束 - 凌晨0点) / 3600,最后一天小时数
  517. $days = (strtotime(date('Y-m-d',$endtime)) - ($startdatetime + 86400)) / 86400;//中间天数
  518. // dump($firstdate);
  519. // dump($lastdate);
  520. // dump($days);
  521. //追加第一天
  522. $start_price = isset($price_json[$startdate]) ? $price_json[$startdate] : $price_default; //第一天价格
  523. $start_fee = bcmul(bcdiv($firstdate,24,4),$start_price,2);
  524. $lease_price = bcadd($lease_price,$start_fee,4); //第一天小时数 / 24 *第一天价格
  525. $price_data[] = ['date' => $startdate,'price' => $start_price,'fee'=> $start_fee];
  526. //追加中间天数
  527. for($i=1;$i<=$days;$i++){
  528. $i_time = $startdatetime + (86400 * $i);
  529. $i_date = date('Y-m-d',$i_time);
  530. $i_price = isset($price_json[$i_date]) ? $price_json[$i_date] : $price_default;
  531. $lease_price = bcadd($lease_price,$i_price,4);
  532. $price_data[] = ['date' => $i_date,'price' => $i_price, 'fee' => $i_price];
  533. }
  534. //追加最后一天
  535. $end_price = isset($price_json[$enddate]) ? $price_json[$enddate] : $price_default; //
  536. $end_fee = bcmul(bcdiv($lastdate,24,4),$end_price,2); //最后一天小时数 / 24 * 最后一天价格
  537. $lease_price = bcadd($lease_price,$end_fee,2);
  538. $price_data[] = ['date' => $enddate,'price' => $end_price, 'fee' => $end_fee];
  539. //结束
  540. return [
  541. 'lease_price'=> $lease_price,
  542. 'price_data' => json_encode($price_data),
  543. ];
  544. }
  545. }