PreOrder.php 7.1 KB

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