Order.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use app\common\service\SmsService;
  5. use app\common\service\UserService;
  6. use think\Db;
  7. use alipaysdkphpallmaster\aop\AopClient;
  8. use alipaysdkphpallmaster\aop\request\AlipayTradePayRequest;
  9. use think\Log;
  10. /**
  11. * 订单管理
  12. */
  13. class Order extends Apic
  14. {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = '*';
  17. private function status_text($status){
  18. $arr = [
  19. 1 => '待核销',
  20. 2 => '已接单',
  21. 3 => '已完成',
  22. 4 => '已取消',
  23. ];
  24. return isset($arr[$status]) ? $arr[$status] : '';
  25. }
  26. //
  27. public function lists(){
  28. $keyword = input('keyword','');
  29. $starttime = input('starttime',0);
  30. $endtime = input('endtime',0);
  31. $servicetype_id = intval(input('servicetype_id',0));
  32. $status = intval(input('status',0));
  33. $order = 'order.id desc';
  34. $where = [
  35. 'company_id' => $this->auth->company_id,
  36. ];
  37. //员工
  38. if($this->auth->type == 2){
  39. $where['staff_id'] = $this->auth->id;
  40. }//员工
  41. if($starttime || $endtime){
  42. $where['createtime'] = ['between',$starttime,$endtime];
  43. }
  44. if($servicetype_id){
  45. $where['servicetype_id'] = $servicetype_id;
  46. }
  47. if($status){
  48. $where['status'] = $status; //状态:2=已接单,3=已完成,4=已取消
  49. if($status > 2){
  50. $order = 'order.finish_time desc';
  51. }
  52. }
  53. if(!empty($keyword))
  54. {
  55. $where['user_car_number|user_mobile'] = ['LIKE','%'.$keyword.'%'];
  56. }
  57. $list = Db::name('order')->alias('order')
  58. ->join('servicetype','order.servicetype_id = servicetype.id','LEFT')
  59. ->field('order.id,orderno,ordertype,user_name,user_mobile,user_car_number,createtime,servicetype_id,server_info,server_time,status,finish_time,cancel_reason,hexiao_time,cancel_time,servicetype.title as servicetype_title,servicetype.baoyang_switch')->where($where)->order($order)->autopage()->select();
  60. foreach($list as $key => &$val){
  61. $val['status_text'] = $this->status_text($val['status']);
  62. }
  63. $this->success(1,$list);
  64. }
  65. //详情
  66. public function info(){
  67. $id = input('id',0);
  68. $info = Db::name('order')->alias('order')
  69. ->join('servicetype','order.servicetype_id = servicetype.id','LEFT')
  70. ->field('order.*,servicetype.title as servicetype_title,servicetype.baoyang_switch')
  71. ->where('order.id',$id)->find();
  72. $info = info_domain_image($info,['server_images']);
  73. $info['status_text'] = $this->status_text($info['status']);
  74. $info['total_fee'] = bcadd($info['pay_fee'],$info['appen_fee'],2);
  75. $info['appen_list'] = Db::name('order_appen')->where('order_id',$id)->select();
  76. $this->success(1,$info);
  77. }
  78. //取消
  79. public function cancel(){
  80. $id = input('id',0);
  81. $reason = input('reason','');
  82. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  83. if($info['status'] == 4){
  84. $this->error('当前订单已经取消');
  85. }
  86. if($info['status'] != 2){
  87. $this->error('当前订单状态不能取消');
  88. }
  89. if($info['ordertype'] == 3){
  90. $this->error('套餐订单不能取消');
  91. }
  92. $time = time();
  93. //同步到预约单,这一步不该有
  94. if (!empty($info['pre_order_id'])) {
  95. $preOrderWhere['id'] = $info['pre_order_id'];
  96. $preOrder = Db::name('pre_order')->where($preOrderWhere)->find();
  97. if (!empty($preOrder) && $preOrder['pre_order_status'] != 0) {
  98. $preOrderData = ['pre_order_status'=>0,'cancel_time'=>$time,'cancel_reason'=>$reason];
  99. $preOrderRes = Db::name('pre_order')->where($preOrderWhere)->update($preOrderData);
  100. if (!$preOrderRes) {
  101. $this->error('预约单取消失败');
  102. }
  103. }
  104. }
  105. //取消
  106. $rs = Db::name('order')->where('id',$id)->update(['status'=>4,'cancel_time'=>time(),'finish_time'=>time(),'cancel_reason'=>$reason]);
  107. if($rs === false){
  108. $this->error('取消失败');
  109. }
  110. //订单取消发送消息
  111. $userService = new UserService();
  112. $params['order_id'] = $id;
  113. $userService->msgOrder($params);
  114. $this->success('取消成功');
  115. }
  116. //完成
  117. public function finish(){
  118. $id = input('id',0);
  119. Db::startTrans();
  120. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  121. if($info['status'] != 2){
  122. Db::rollback();
  123. $this->error('当前订单不能完成');
  124. }
  125. //完成
  126. $time = time();
  127. $updateData = ['status'=>3,'finish_time'=>time()];
  128. $rs = Db::name('order')->where('id',$id)->update($updateData);
  129. if($rs === false){
  130. Db::rollback();
  131. $this->error('操作失败');
  132. }
  133. //给门店加钱
  134. if($info['ordertype'] == 3 && $info['paytype'] == 3 && $info['pay_fee'] > 0){
  135. $wallet_rs = model('walletcompany')->lockChangeAccountRemain($this->auth->company_id,'money',$info['pay_fee'],203,$remark='套餐订单完成服务','order',$id);
  136. if($wallet_rs['status'] === false){
  137. Db::rollback();
  138. $this->error($wallet_rs['msg']);
  139. }
  140. }
  141. Db::commit();
  142. //订单取消发送消息
  143. $userService = new UserService();
  144. $params['order_id'] = $id;
  145. $userService->msgOrder($params);
  146. //是否弹出保养
  147. $baoyang_switch = Db::name('servicetype')->where('id',$info['servicetype_id'])->value('baoyang_switch');
  148. $this->success('操作成功',$baoyang_switch);
  149. }
  150. //设置保养时间提醒
  151. public function baoyang(){
  152. $id = input('id',0);
  153. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  154. if(!$info){
  155. $this->error('不存在的订单');
  156. }
  157. //更新
  158. $next_date = input('next_date','');
  159. $next_carlicheng = input('next_carlicheng','');
  160. $data = [
  161. 'next_date' => $next_date,
  162. 'next_carlicheng' => $next_carlicheng,
  163. ];
  164. Db::name('order')->where('id',$id)->update($data);
  165. //记录保养时间(后续提醒客户保养)
  166. $smsService = new SmsService();
  167. $nextDate = !empty($next_date) ? strtotime($next_date) : 0;
  168. $params = [
  169. 'order_id' => $info['id'],
  170. 'company_id' => $info['company_id'],
  171. 'user_id' => $info['user_id'],
  172. 'car_id' => $info['user_car_id'],
  173. 'car_number' => $info['user_car_number'],
  174. 'upkeep_time' => $nextDate,
  175. ];
  176. $orderRes = $smsService->carRemindSave($params);
  177. if (!$orderRes['status']) {
  178. Log::error('记录保养短信提醒失败:params:'.json_encode($params));
  179. }
  180. $this->success('操作成功');
  181. }
  182. //追加列表
  183. public function appen_lists(){
  184. $id = input('id',0);
  185. $info = Db::name('order')->field('id,orderno,pay_fee,appen_fee')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  186. if(!$info){
  187. $this->error('不存在的订单');
  188. }
  189. $info['appen_list'] = Db::name('order_appen')->where('order_id',$id)->select();
  190. $this->success(1,$info);
  191. }
  192. //追加新费用
  193. public function appen_newone(){
  194. $id = input('id',0);
  195. Db::startTrans();
  196. $info = Db::name('order')->field('id,orderno,pay_fee,appen_fee')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  197. if(!$info){
  198. $this->error('不存在的订单');
  199. }
  200. //加入新的一条
  201. $name = input('name','');
  202. $price = input('price',0);
  203. if($price <= 0){
  204. Db::rollback();
  205. $this->error('请填写正确的金额');
  206. }
  207. $data = [
  208. 'company_id' => $this->auth->company_id,
  209. 'order_id' => $id,
  210. 'name' => $name,
  211. 'price' => $price,
  212. 'createtime' => time(),
  213. ];
  214. $log_id = Db::name('order_appen')->insertGetId($data);
  215. if(!$log_id){
  216. Db::rollback();
  217. $this->error('操作失败');
  218. }
  219. //计算追加总额做冗余
  220. $sum_price = Db::name('order_appen')->where('order_id',$id)->sum('price');
  221. $update = [
  222. 'appen_fee'=>$sum_price,
  223. 'total_fee'=>bcadd($sum_price,$info['pay_fee'],2),
  224. ];
  225. $rs = Db::name('order')->where('id',$id)->update($update);
  226. if($rs === false){
  227. Db::rollback();
  228. $this->error('操作失败');
  229. }
  230. //结束
  231. Db::commit();
  232. $this->success();
  233. }
  234. }