PreOrder.php 7.0 KB

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