PreOrder.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\UserService;
  5. use think\Db;
  6. use think\Exception;
  7. class PreOrder extends Api
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = '*';
  11. protected $model = null;
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->model = Db::name('pre_order');
  16. }
  17. /**
  18. * 列表
  19. * @return void
  20. */
  21. public function getList(){
  22. try {
  23. $preOrderStatus = input('pre_order_status','');
  24. $po = 'pre_order';
  25. $st = 'servicetype';
  26. $o = 'order';
  27. $where[$po.'.user_id'] = $this->auth->id;
  28. if ($preOrderStatus != '') {
  29. $where[$po.'.pre_order_status'] = $preOrderStatus;
  30. }
  31. $field = $po.'.id,name,mobile,address,remark,'.$po.'.car_id,'.$po.'.car_number,pre_time,order_time,'.$po.'.cancel_time,'.
  32. $po.'.cancel_reason,'.$st.'.title,pre_order_status,'.$o.'.id as `order_id`,'.$o.'.server_time';
  33. $result = $this->model->alias($po)->field($field)
  34. ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
  35. ->join($o,$o.'.pre_order_id = '.$po.'.id','LEFT')
  36. ->where($where)->order($po.'.createtime desc')->autopage()->select();
  37. if (!empty($result)) {
  38. $model = model('PreOrder');
  39. $statusArr = $model->getPreOrderStatusList();
  40. $timeArr = ['pre_time','order_time','cancel_time','server_time'];
  41. foreach ($result as $key => &$value) {
  42. foreach ($timeArr as $k => $v) {
  43. $value[$v] = !empty($value[$v]) ? date('Y年m月d日 H:i:s', $value[$v]) : '';
  44. }
  45. $value['order_id'] = empty($value['order_id']) ? 0 : $value['order_id'];
  46. $value['pre_order_status_text'] = isset($statusArr[$value['pre_order_status']]) ? $statusArr[$value['pre_order_status']] : '';
  47. }
  48. }
  49. $this->success('获取成功', $result);
  50. } catch (Exception $e) {
  51. $this->error($e->getMessage());
  52. }
  53. }
  54. /**
  55. * 保存
  56. * @return void
  57. */
  58. public function save()
  59. {
  60. try {
  61. //验证参数
  62. $id = $this->request->param('id',0);
  63. $carId = $this->request->param('car_id',0);
  64. $preTime = $this->request->param('pre_time','');
  65. $userId = $this->auth->id;
  66. $companyId = $this->auth->company_id;
  67. $scene = !empty($id) ? 'edit' : 'add';
  68. $validate = validate('PreOrder');
  69. if(!$validate->check($this->request->param(),[],$scene)){
  70. throw new Exception($validate->getError());
  71. }
  72. $preTime = strtotime($preTime);
  73. //获取车辆信息
  74. $userCarWhere['user_id'] = $userId;
  75. $userCarWhere['id'] = $carId;
  76. $userCar = Db::name('user_car')->where($userCarWhere)->find();
  77. if (empty($userCar)) {
  78. throw new Exception('未找到车辆信息');
  79. }
  80. $time = time();
  81. $data = [
  82. 'name' => $this->request->param('name', ''),
  83. 'mobile' => $this->request->param('mobile', ''),
  84. 'address' => $this->request->param('address', ''),
  85. 'remark' => $this->request->param('remark', ''),
  86. 'car_id' => $carId,
  87. 'car_number' => isset($userCar['car_number']) ? $userCar['car_number'] : '',
  88. 'servicetype_id' => $this->request->param('servicetype_id', 0),
  89. 'pre_time' => $preTime,
  90. ];
  91. if (empty($id)) {
  92. $data['company_id'] = $companyId;
  93. $data['pre_order_no'] = createUniqueNo('PO',$userId);
  94. $data['user_id'] = $userId;
  95. $data['createtime'] = $time;
  96. $res = $this->model->insertGetId($data);
  97. $id = $res;
  98. //绑定门店
  99. $userService = new UserService();
  100. $userParams = [
  101. 'user_id' => $userId,
  102. 'company_id' => $companyId,
  103. 'comefrom' => '',//来源
  104. ];
  105. $userBindRes = $userService->userWallet($userParams);
  106. if (!$userBindRes['status']) {
  107. throw new Exception($userBindRes['msg']);
  108. }
  109. } else {
  110. $data['updatetime'] = $time;
  111. $where['id'] = $id;
  112. $where['user_id'] = $userId;
  113. $res = $this->model->where($where)->update($data);
  114. }
  115. if (!$res) {
  116. throw new Exception('操作失败');
  117. }
  118. $result = [
  119. 'pre_order_id' => $id,
  120. 'template_ids' => config('param.wechat_template_ids'),//微信消息模版ID
  121. ];
  122. $this->success('操作成功',$result);
  123. } catch (Exception $e) {
  124. $this->error($e->getMessage());
  125. }
  126. }
  127. /**
  128. * 微信消息通知
  129. * @return void
  130. */
  131. public function wxMessage()
  132. {
  133. try {
  134. $preOrderId = $this->request->param('pre_order_id',0);
  135. $userService = new UserService();
  136. $params = ['pre_order_id' => $preOrderId];
  137. $wechatMsgRes = $userService->wechatMessageSend($params);
  138. if (!$wechatMsgRes['status']) {
  139. throw new Exception($wechatMsgRes['msg']);
  140. }
  141. $this->success('操作成功');
  142. } catch (Exception $e) {
  143. $this->error($e->getMessage());
  144. }
  145. }
  146. /**
  147. * 详情
  148. * @return void
  149. */
  150. public function getInfo()
  151. {
  152. try {
  153. $id = $this->request->param('id',0);
  154. $po = 'pre_order';
  155. $st = 'servicetype';
  156. $o = 'order';
  157. $where[$po.'.user_id'] = $this->auth->id;
  158. $where[$po.'.id'] = $id;
  159. $field = $po.'.id,name,mobile,address,remark,'.$po.'.car_id,'.$po.'.car_number,pre_time,order_time,'.
  160. $po.'.cancel_time,'.$po.'.cancel_reason,'.$po.'.createtime,'.$po.'.servicetype_id,'.$st.
  161. '.title as `servicetype_title`,pre_order_status,'.$o.'.id as `order_id`';
  162. $result = $this->model->alias($po)->field($field)
  163. ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
  164. ->join($o,$o.'.pre_order_id = '.$po.'.id','LEFT')
  165. ->where($where)->order($po.'.createtime desc')->find();
  166. if (!empty($result)) {
  167. $model = model('PreOrder');
  168. $statusArr = $model->getPreOrderStatusList();
  169. $timeArr = ['pre_time','order_time','cancel_time','createtime'];
  170. foreach ($timeArr as $k => $v) {
  171. $result[$v] = !empty($result[$v]) ? date('Y年m月d日 H:i:s', $result[$v]) : '';
  172. }
  173. $result['order_id'] = empty($result['order_id']) ? 0 : $result['order_id'];
  174. $result['pre_order_status_text'] = isset($statusArr[$result['pre_order_status']]) ? $statusArr[$result['pre_order_status']] : '';
  175. }
  176. $this->success('获取成功',$result);
  177. } catch (Exception $e) {
  178. $this->error($e->getMessage());
  179. }
  180. }
  181. /**
  182. * 取消
  183. * @return void
  184. */
  185. public function cancel()
  186. {
  187. try {
  188. $id = $this->request->param('id',0);
  189. $cancelReason = $this->request->param('cancel_reason','');
  190. $time = time();
  191. $where['user_id'] = $this->auth->id;
  192. $where['id'] = $id;
  193. $modelData = model('PreOrder')->where($where)->find();
  194. if (empty($modelData)) {
  195. throw new Exception('未找到预约信息');
  196. }
  197. if ($modelData['pre_order_status'] != 1) {
  198. throw new Exception('该预约单无法取消或已取消');
  199. }
  200. $modelData->pre_order_status = 0;
  201. $modelData->cancel_reason = $cancelReason;
  202. $modelData->cancel_time = $time;
  203. $res = $modelData->save();
  204. if (!$res) {
  205. throw new Exception('操作失败');
  206. }
  207. $this->success('操作成功');
  208. } catch (Exception $e) {
  209. $this->error($e->getMessage());
  210. }
  211. }
  212. }