Order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use alipaysdkphpallmaster\aop\AopClient;
  6. use alipaysdkphpallmaster\aop\request\AlipayTradePayRequest;
  7. /**
  8. * 订单管理
  9. */
  10. class Order extends Apic
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = '*';
  14. //
  15. public function lists(){
  16. $keyword = input('keyword','');
  17. $status = input('status','all');
  18. $where = [
  19. 'company_id' => $this->auth->id,
  20. ];
  21. if($status !== 'all'){
  22. $where['status'] = $status;
  23. }
  24. if($status == 20){ //待还车
  25. $where['status'] = 20;
  26. $where['endtime'] = ['gt',time()];
  27. }
  28. if($status == 200){ //逾期
  29. $where['status'] = 20;
  30. $where['endtime'] = ['elt',time()];
  31. }
  32. if($status == 30){
  33. $where['status'] = ['IN',[-1,-2,30]];
  34. }
  35. if(!empty($keyword))
  36. {
  37. $where['user_truename|user_mobile|orderno'] = ['LIKE','%'.$keyword.'%'];
  38. }
  39. $list = Db::name('order')->where($where)->order('id desc')->autopage()->select();
  40. $list = list_domain_image($list,['car_image','idcard_images','driver_images','get_yibiao_images','get_carvideo','back_yibiao_images','back_carvideo']);
  41. foreach($list as $key => &$val){
  42. $val['status_text'] = $this->status_text($val['status'],$val['endtime']);
  43. if($val['status'] == 20 && time() >= $val['endtime']){
  44. $val['status'] = 200; //強制改掉
  45. }
  46. //追加取消
  47. $val['cancel_info'] = (object)[];
  48. if($val['status'] == -1 || $val['status'] == -2){
  49. //取消订单追加取消原因
  50. $val['cancel_info'] = Db::name('order_cancel')->where('order_id',$val['id'])->find();
  51. }
  52. //追加评价
  53. $val['comment_info'] = (object)[];
  54. if($val['status'] == 30){
  55. //完成订单追加评价
  56. $comment_info = Db::name('order_comment')->where('order_id',$val['id'])->find();
  57. if($comment_info){
  58. $val['comment_info'] = $comment_info;
  59. }
  60. }
  61. }
  62. $this->success(1,$list);
  63. }
  64. //各个状态的订单数量
  65. public function status_each_number(){
  66. //全部
  67. $where = [
  68. 'company_id' => $this->auth->id,
  69. ];
  70. $all = Db::name('order')->where($where)->count();
  71. //待取车
  72. $where = [
  73. 'company_id' => $this->auth->id,
  74. 'status' => 10
  75. ];
  76. $daiquche = Db::name('order')->where($where)->count();
  77. //待还车
  78. $where = [
  79. 'company_id' => $this->auth->id,
  80. 'status' => 20,
  81. 'endtime' => ['gt',time()],
  82. ];
  83. $daihuanche = Db::name('order')->where($where)->count();
  84. //逾期
  85. $where = [
  86. 'company_id' => $this->auth->id,
  87. 'status' => 20,
  88. 'endtime' => ['elt',time()],
  89. ];
  90. $yuqi = Db::name('order')->where($where)->count();
  91. //完成/取消
  92. $where = [
  93. 'company_id' => $this->auth->id,
  94. 'status' => ['IN',[-1,-2,30]],
  95. ];
  96. $wancheng = Db::name('order')->where($where)->count();
  97. $result = [
  98. 'all' => $all,
  99. 'daiquche' => $daiquche,
  100. 'daihuanche' => $daihuanche,
  101. 'yuqi' => $yuqi,
  102. 'wancheng' => $wancheng,
  103. ];
  104. $this->success(1,$result);
  105. }
  106. //状态
  107. private function status_text($status,$endtime){
  108. $arr = [
  109. -1 => '未付款被取消',
  110. -2 => '已付款被取消',
  111. 0 => '待付款',
  112. 10 => '已付款',
  113. 20 => '已取车',
  114. 30 => '完成',
  115. ];
  116. $status_text = isset($arr[$status]) ? $arr[$status] : $status;
  117. //取走了车没还,时间却超过了还车时间
  118. if($status == 20 && time() >= $endtime){
  119. $status_text = '已逾期';
  120. }
  121. return $status_text;
  122. }
  123. //详情
  124. public function info(){
  125. $id = input('id',0);
  126. $info = Db::name('order')->where('id',$id)->find();
  127. $info = info_domain_image($info,['car_image','idcard_images','driver_images','get_yibiao_images','get_carvideo','back_yibiao_images','back_carvideo']);
  128. $info['status_text'] = $this->status_text($info['status'],$info['endtime']);
  129. if($info['status'] == 20 && time() >= $info['endtime']){
  130. $info['status'] = 200; //強制改掉
  131. }
  132. //完成订单追加评价
  133. $comment_info = Db::name('order_comment')->alias('c')
  134. ->field('c.*,user.avatar,user.mobile')
  135. ->join('user','c.user_id = user.id','LEFT')
  136. ->where('c.order_id',$id)->find();
  137. $comment_info = info_domain_image($comment_info,['avatar']);
  138. if(!empty($comment_info)){
  139. $comment_info['mobile'] = str_replace(substr($comment_info['mobile'],3,5),'****',$comment_info['mobile']);
  140. }
  141. $info['comment_info'] = $comment_info;
  142. //取消订单追加取消原因
  143. $info['cancel_info'] = Db::name('order_cancel')->where('order_id',$id)->find();
  144. $this->success(1,$info);
  145. }
  146. //取消理由
  147. public function cancel_config(){
  148. $list = Db::name('company_cancel_config')->order('id asc')->select();
  149. $this->success(1,$list);
  150. }
  151. //取消
  152. public function cancel(){
  153. $id = input('id',0);
  154. $reason = input('reason','');
  155. Db::startTrans();
  156. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->id)->lock(true)->find();
  157. if($info['status'] == -2){
  158. Db::rollback();
  159. $this->error('当前订单已经申请取消,已付款订单审核后可退款');
  160. }
  161. if($info['status'] != 0 && $info['status'] != 10){
  162. Db::rollback();
  163. $this->error('当前订单状态不能取消');
  164. }
  165. //未付款取消
  166. if($info['status'] == 0){
  167. $rs = Db::name('order')->where('id',$id)->update(['status'=>-1]);
  168. }
  169. //已付款取消
  170. if($info['status'] == 10){
  171. $rs = Db::name('order')->where('id',$id)->update(['status'=>-2]);
  172. //扣除车行收益
  173. //从计划任务里走
  174. }
  175. if($rs === false){
  176. Db::rollback();
  177. $this->error('申请取消失败');
  178. }
  179. $data = [
  180. 'user_id' => $info['user_id'],
  181. 'company_id' => $info['company_id'],
  182. 'order_id' => $info['id'],
  183. 'reason' => '商家取消:'.$reason,
  184. 'refund_price' => 0,
  185. 'type' => $info['status'], //分类:0=未付款,10=已付款
  186. 'createtime' => time(),
  187. 'status' => $info['status'] == 0 ? 2 : 0, //状态:0=未处理,1=已处理,2=无需处理
  188. 'from' => 2, //取消方:1=用户取消,2=商户取消
  189. ];
  190. $rs_cancle = Db::name('order_cancel')->insertGetId($data);
  191. if(!$rs_cancle){
  192. Db::rollback();
  193. $this->error('申请取消失败');
  194. }
  195. //解除汽车占用
  196. $rs_car = Db::name('car')->where('id',$info['car_id'])->update(['status'=>1]);
  197. if(!$rs_car){
  198. Db::rollback();
  199. $this->error('申请取消失败');
  200. }
  201. Db::commit();
  202. $this->success('取消成功');
  203. }
  204. //确认取车
  205. public function get_car(){
  206. $id = input('id',0);
  207. Db::startTrans();
  208. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->id)->lock(true)->find();
  209. if($info['status'] != 10){
  210. Db::rollback();
  211. $this->error('当前订单状态不能取车');
  212. }
  213. //
  214. $data = [
  215. 'idcard_images' => input('idcard_images',''),
  216. 'driver_images' => input('driver_images',''),
  217. 'get_yibiao_images' => input('get_yibiao_images',''),
  218. 'get_carvideo' => input('get_carvideo',''),
  219. 'status' => 20,
  220. 'getcartime' => time(),
  221. ];
  222. Db::name('order')->where('id',$id)->update($data);
  223. Db::commit();
  224. $this->success('取车完成');
  225. }
  226. //还车车伤确认,配置
  227. public function back_config(){
  228. $list = Db::name('order_back_config')->order('id asc')->select();
  229. $this->success(1,$list);
  230. }
  231. //还车
  232. public function back_car(){
  233. $id = input('id',0);
  234. Db::startTrans();
  235. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->id)->lock(true)->find();
  236. if($info['status'] != 20){
  237. Db::rollback();
  238. $this->error('当前订单状态不能还车');
  239. }
  240. //还车
  241. $data = [
  242. 'back_yibiao_images' => input('back_yibiao_images',''),
  243. 'back_carvideo' => input('back_carvideo',''),
  244. 'back_remark' => input('back_remark',''),
  245. 'status' => 30,
  246. 'backcartime' => input('backcartime',''),
  247. 'back_oilfee' => input('back_oilfee',0),
  248. 'back_damage' => input('back_damage',''),
  249. ];
  250. $rs_update = Db::name('order')->where('id',$id)->update($data);
  251. if($rs_update === false){
  252. Db::rollback();
  253. $this->error('还车失败');
  254. }
  255. //汽车解除占用
  256. $rs_car = Db::name('car')->where('id',$info['car_id'])->update(['status'=>1]);
  257. if($rs_car === false){
  258. Db::rollback();
  259. $this->error('还车失败');
  260. }
  261. //给上一级用户赠送一张满100减100的券
  262. $intro_user = Db::name('user')->where('id',$info['user_id'])->value('intro_uid');
  263. if($intro_user){
  264. //检查送过没有
  265. $where = [
  266. 'user_id' => $info['user_id'],
  267. 'status' => 30,
  268. 'id' => ['neq',$info['id']],
  269. ];
  270. $check = Db::name('order')->where($where)->find();
  271. if(!$check){
  272. //赠送一个
  273. $only_coupon = Db::name('coupons')->where('id',1)->lock(true)->find();
  274. if($only_coupon['stock'] > 1){
  275. //送到
  276. $data = [
  277. 'user_id' => $intro_user,
  278. 'company_id' => $only_coupon['company_id'],
  279. 'enough' => $only_coupon['enough'],
  280. 'amount' => $only_coupon['amount'],
  281. 'type' => $only_coupon['type'],
  282. 'coupons_id' => $only_coupon['id'],
  283. 'createtime' => time(),
  284. 'usetimestart' => $only_coupon['usetimestart'],
  285. 'usetimeend' => $only_coupon['usetimeend'],
  286. ];
  287. $rs_user_coupon = Db::name('user_coupons')->insertGetId($data);
  288. //减库存
  289. $rs_only_coupon = Db::name('coupons')->where('id',1)->update(['stock'=>$only_coupon['stock']-1]);
  290. if(!$rs_user_coupon || $rs_only_coupon === false){
  291. Db::rollback();
  292. $this->error('还车失败了');
  293. }
  294. }
  295. //给上级奖励money
  296. $intro_money = config('site.yaoqing_user_backcar_money');
  297. if($intro_money > 0){
  298. $wallet = new \app\common\model\Wallet;
  299. $wallet_rs = $wallet->lockChangeAccountRemain($intro_user,'money',$intro_money,3,'邀请新用户下单('.$info['user_id'].')','order',$info['id']);
  300. if($wallet_rs['status'] === false){
  301. Db::rollback();
  302. $this->error($wallet_rs['msg']);
  303. }
  304. }
  305. }
  306. }
  307. Db::commit();
  308. $this->success('还车完成');
  309. }
  310. //回复评价
  311. public function comment_reply(){
  312. $comment_id = input('comment_id',0);
  313. $reply = input('reply','');
  314. $data = [
  315. 'reply' => $reply,
  316. 'replytime' => time(),
  317. ];
  318. Db::name('order_comment')->where('id',$comment_id)->update($data);
  319. $this->success('回复完成');
  320. }
  321. }