PreOrder.php 6.8 KB

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