PreOrder.php 9.1 KB

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