Order.php 12 KB

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