PreOrder.php 7.3 KB

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