PreOrder.php 9.0 KB

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