PreOrder.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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`';
  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'];
  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. } else {
  99. $data['updatetime'] = $time;
  100. $where['id'] = $id;
  101. $where['user_id'] = $userId;
  102. $res = $this->model->where($where)->update($data);
  103. }
  104. if (!$res) {
  105. throw new Exception('操作失败');
  106. }
  107. $result = [
  108. 'pre_order_id' => $id,
  109. 'template_ids' => config('param.wechat_template_ids'),//微信消息模版ID
  110. ];
  111. $this->success('操作成功',$result);
  112. } catch (Exception $e) {
  113. $this->error($e->getMessage());
  114. }
  115. }
  116. /**
  117. * 微信消息通知
  118. * @return void
  119. */
  120. public function wxMessage()
  121. {
  122. try {
  123. $preOrderId = $this->request->param('pre_order_id',0);
  124. $userService = new UserService();
  125. $params = ['pre_order_id' => $preOrderId];
  126. $wechatMsgRes = $userService->wechatMessageSend($params);
  127. if (!$wechatMsgRes['status']) {
  128. throw new Exception($wechatMsgRes['msg']);
  129. }
  130. $this->success('操作成功');
  131. } catch (Exception $e) {
  132. $this->error($e->getMessage());
  133. }
  134. }
  135. /**
  136. * 详情
  137. * @return void
  138. */
  139. public function getInfo()
  140. {
  141. try {
  142. $id = $this->request->param('id',0);
  143. $po = 'pre_order';
  144. $st = 'servicetype';
  145. $o = 'order';
  146. $where[$po.'.user_id'] = $this->auth->id;
  147. $where[$po.'.id'] = $id;
  148. $field = $po.'.id,name,mobile,address,remark,'.$po.'.car_id,'.$po.'.car_number,pre_time,order_time,'.
  149. $po.'.cancel_time,'.$po.'.cancel_reason,'.$po.'.createtime,'.$po.'.servicetype_id,'.$st.
  150. '.title as `servicetype_title`,pre_order_status,'.$o.'.id as `order_id`';
  151. $result = $this->model->alias($po)->field($field)
  152. ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
  153. ->join($o,$o.'.pre_order_id = '.$po.'.id','LEFT')
  154. ->where($where)->order($po.'.createtime desc')->find();
  155. if (!empty($result)) {
  156. $model = model('PreOrder');
  157. $statusArr = $model->getPreOrderStatusList();
  158. $timeArr = ['pre_time','order_time','cancel_time','createtime'];
  159. foreach ($timeArr as $k => $v) {
  160. $result[$v] = !empty($result[$v]) ? date('Y年m月d日 H:i:s', $result[$v]) : '';
  161. }
  162. $result['order_id'] = empty($result['order_id']) ? 0 : $result['order_id'];
  163. $result['pre_order_status_text'] = isset($statusArr[$result['pre_order_status']]) ? $statusArr[$result['pre_order_status']] : '';
  164. }
  165. $this->success('获取成功',$result);
  166. } catch (Exception $e) {
  167. $this->error($e->getMessage());
  168. }
  169. }
  170. /**
  171. * 取消
  172. * @return void
  173. */
  174. public function cancel()
  175. {
  176. try {
  177. $id = $this->request->param('id',0);
  178. $cancelReason = $this->request->param('cancel_reason','');
  179. $time = time();
  180. $where['user_id'] = $this->auth->id;
  181. $where['id'] = $id;
  182. $modelData = model('PreOrder')->where($where)->find();
  183. if (empty($modelData)) {
  184. throw new Exception('未找到预约信息');
  185. }
  186. if ($modelData['pre_order_status'] != 1) {
  187. throw new Exception('该预约单无法取消或已取消');
  188. }
  189. $modelData->pre_order_status = 0;
  190. $modelData->cancel_reason = $cancelReason;
  191. $modelData->cancel_time = $time;
  192. $res = $modelData->save();
  193. if (!$res) {
  194. throw new Exception('操作失败');
  195. }
  196. $this->success('操作成功');
  197. } catch (Exception $e) {
  198. $this->error($e->getMessage());
  199. }
  200. }
  201. }