Order.php 8.4 KB

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