PreOrder.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $smsService = new SmsService();
  125. $smsParams = [
  126. 'id' => $id,//ID
  127. 'company_id' => $companyId,//门店ID
  128. ];
  129. $smsService->preOrderToShop($smsParams);
  130. } else {
  131. $data['updatetime'] = $time;
  132. $where['id'] = $id;
  133. $where['user_id'] = $userId;
  134. $res = $this->model->where($where)->update($data);
  135. }
  136. if (!$res) {
  137. throw new Exception('操作失败');
  138. }
  139. $result = [
  140. 'pre_order_id' => $id,
  141. 'template_ids' => config('param.wechat_template_ids'),//微信消息模版ID
  142. ];
  143. $this->success('操作成功',$result);
  144. } catch (Exception $e) {
  145. $this->error($e->getMessage());
  146. }
  147. }
  148. /**
  149. * 微信消息通知
  150. * @return void
  151. */
  152. public function wxMessage()
  153. {
  154. try {
  155. $preOrderId = $this->request->param('pre_order_id',0);
  156. $userService = new UserService();
  157. $params = ['pre_order_id' => $preOrderId];
  158. $wechatMsgRes = $userService->wechatMessageSend($params);
  159. if (!$wechatMsgRes['status']) {
  160. throw new Exception($wechatMsgRes['msg']);
  161. }
  162. $this->success('操作成功');
  163. } catch (Exception $e) {
  164. $this->error($e->getMessage());
  165. }
  166. }
  167. /**
  168. * 详情
  169. * @return void
  170. */
  171. public function getInfo()
  172. {
  173. try {
  174. $id = $this->request->param('id',0);
  175. $po = 'pre_order';
  176. $st = 'servicetype';
  177. $o = 'order';
  178. $where[$po.'.user_id'] = $this->auth->id;
  179. $where[$po.'.company_id'] = $this->auth->company_id;
  180. $where[$po.'.id'] = $id;
  181. $field = $po.'.id,name,mobile,address,remark,'.$po.'.car_id,'.$po.'.car_number,pre_time,order_time,'.
  182. $po.'.cancel_time,'.$po.'.cancel_reason,'.$po.'.createtime,'.$po.'.servicetype_id,'.$st.
  183. '.title as `servicetype_title`,pre_order_status,'.$o.'.id as `order_id`';
  184. $result = $this->model->alias($po)->field($field)
  185. ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
  186. ->join($o,$o.'.pre_order_id = '.$po.'.id','LEFT')
  187. ->where($where)->order($po.'.createtime desc')->find();
  188. if (!empty($result)) {
  189. $model = model('PreOrder');
  190. $statusArr = $model->getPreOrderStatusList();
  191. $timeArr = ['pre_time','order_time','cancel_time','createtime'];
  192. foreach ($timeArr as $k => $v) {
  193. $result[$v] = !empty($result[$v]) ? date('Y年m月d日 H:i:s', $result[$v]) : '';
  194. }
  195. $result['order_id'] = empty($result['order_id']) ? 0 : $result['order_id'];
  196. $result['pre_order_status_text'] = isset($statusArr[$result['pre_order_status']]) ? $statusArr[$result['pre_order_status']] : '';
  197. }
  198. $this->success('获取成功',$result);
  199. } catch (Exception $e) {
  200. $this->error($e->getMessage());
  201. }
  202. }
  203. /**
  204. * 取消
  205. * @return void
  206. */
  207. public function cancel()
  208. {
  209. try {
  210. $id = $this->request->param('id',0);
  211. $cancelReason = $this->request->param('cancel_reason','');
  212. $time = time();
  213. $where['user_id'] = $this->auth->id;
  214. $where['id'] = $id;
  215. $modelData = model('PreOrder')->where($where)->find();
  216. if (empty($modelData)) {
  217. throw new Exception('未找到预约信息');
  218. }
  219. if ($modelData['pre_order_status'] != 1) {
  220. throw new Exception('该预约单无法取消或已取消');
  221. }
  222. $modelData->pre_order_status = 0;
  223. $modelData->cancel_reason = $cancelReason;
  224. $modelData->cancel_time = $time;
  225. $res = $modelData->save();
  226. if (!$res) {
  227. throw new Exception('操作失败');
  228. }
  229. $this->success('操作成功');
  230. } catch (Exception $e) {
  231. $this->error($e->getMessage());
  232. }
  233. }
  234. }