PreOrder.php 9.1 KB

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