PreOrder.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class PreOrder extends Api
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = Db::name('pre_order');
  15. }
  16. /**
  17. * 列表
  18. * @return void
  19. */
  20. public function getList(){
  21. try {
  22. $preOrderStatus = input('pre_order_status','');
  23. $where['user_id'] = $this->auth->id;
  24. if ($preOrderStatus != '') {
  25. $where['pre_order_status'] = $preOrderStatus;
  26. }
  27. $po = 'pre_order';
  28. $st = 'servicetype';
  29. $field = $po.'.id,name,mobile,car_number,pre_time,order_time,cancel_time,'.$st.'.title,pre_order_status';
  30. $result = $this->model->alias($po)->field($field)
  31. ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
  32. ->where($where)->order($po.'.createtime desc')->autopage()->select();
  33. if (!empty($result)) {
  34. $model = model('PreOrder');
  35. $statusArr = $model->getPreOrderStatusList();
  36. $timeArr = ['pre_time','order_time','cancel_time'];
  37. foreach ($result as $key => &$value) {
  38. foreach ($timeArr as $k => $v) {
  39. $value[$v] = !empty($value[$v]) ? date('Y年m月d日 H:i:s', $value[$v]) : '';
  40. }
  41. $value['pre_order_status_text'] = isset($statusArr[$value['pre_order_status']]) ? $statusArr[$value['pre_order_status']] : '';
  42. }
  43. }
  44. $this->success('获取成功', $result);
  45. } catch (Exception $e) {
  46. $this->error($e->getMessage());
  47. }
  48. }
  49. /**
  50. * 保存
  51. * @return void
  52. */
  53. public function save()
  54. {
  55. try {
  56. //验证参数
  57. $id = $this->request->param('id',0);
  58. $carId = $this->request->param('car_id',0);
  59. $preTime = $this->request->param('pre_time','');
  60. $userId = $this->auth->id;
  61. $scene = !empty($id) ? 'edit' : 'add';
  62. $validate = validate('PreOrder');
  63. if(!$validate->check($this->request->param(),[],$scene)){
  64. throw new Exception($validate->getError());
  65. }
  66. $preTime = strtotime($preTime);
  67. //获取车辆信息
  68. $userCarWhere['user_id'] = $userId;
  69. $userCarWhere['id'] = $carId;
  70. $userCar = Db::name('user_car')->where($userCarWhere)->find();
  71. if (empty($userCar)) {
  72. throw new Exception('未找到车辆信息');
  73. }
  74. $time = time();
  75. $data = [
  76. 'company_id' => $this->request->param('company_id', 0),
  77. 'name' => $this->request->param('name', ''),
  78. 'mobile' => $this->request->param('mobile', ''),
  79. 'address' => $this->request->param('address', ''),
  80. 'remark' => $this->request->param('remark', ''),
  81. 'car_id' => $carId,
  82. 'car_number' => isset($userCar['car_number']) ? $userCar['car_number'] : '',
  83. 'servicetype_id' => $this->request->param('servicetype_id', 0),
  84. 'pre_time' => $preTime,
  85. ];
  86. if (empty($id)) {
  87. $data['user_id'] = $userId;
  88. $data['createtime'] = $time;
  89. $res = $this->model->insertGetId($data);
  90. } else {
  91. $data['updatetime'] = $time;
  92. $where['id'] = $id;
  93. $where['user_id'] = $userId;
  94. $res = $this->model->where($where)->update($data);
  95. }
  96. if (!$res) {
  97. throw new Exception('操作失败');
  98. }
  99. $this->success('操作成功');
  100. } catch (Exception $e) {
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. /**
  105. * 详情
  106. * @return void
  107. */
  108. public function getInfo()
  109. {
  110. try {
  111. $id = $this->request->param('id',0);
  112. $po = 'pre_order';
  113. $st = 'servicetype';
  114. $where[$po.'.user_id'] = $this->auth->id;
  115. $where[$po.'.id'] = $id;
  116. $field = $po.'.id,name,mobile,address,remark,car_number,pre_time,order_time,cancel_time,cancel_reason,'.
  117. $po.'.createtime,'.$st.'.title,pre_order_status';
  118. $result = $this->model->alias($po)->field($field)
  119. ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
  120. ->where($where)->order($po.'.createtime desc')->find();
  121. if (!empty($result)) {
  122. $model = model('PreOrder');
  123. $statusArr = $model->getPreOrderStatusList();
  124. $timeArr = ['pre_time','order_time','cancel_time','createtime'];
  125. foreach ($timeArr as $k => $v) {
  126. $result[$v] = !empty($result[$v]) ? date('Y年m月d日 H:i:s', $result[$v]) : '';
  127. }
  128. $result['pre_order_status_text'] = isset($statusArr[$result['pre_order_status']]) ? $statusArr[$result['pre_order_status']] : '';
  129. }
  130. $this->success('获取成功',$result);
  131. } catch (Exception $e) {
  132. $this->error($e->getMessage());
  133. }
  134. }
  135. /**
  136. * 取消
  137. * @return void
  138. */
  139. public function cancel()
  140. {
  141. try {
  142. $id = $this->request->param('id',0);
  143. $cancelReason = $this->request->param('cancel_reason','');
  144. $time = time();
  145. $where['user_id'] = $this->auth->id;
  146. $where['id'] = $id;
  147. $modelData = model('PreOrder')->where($where)->find();
  148. if (empty($modelData)) {
  149. throw new Exception('未找到预约信息');
  150. }
  151. if ($modelData['pre_order_status'] != 1) {
  152. throw new Exception('该预约单无法取消或已取消');
  153. }
  154. $modelData->pre_order_status = 0;
  155. $modelData->cancel_reason = $cancelReason;
  156. $modelData->cancel_time = $time;
  157. $res = $modelData->save();
  158. if (!$res) {
  159. throw new Exception('操作失败');
  160. }
  161. $this->success('操作成功');
  162. } catch (Exception $e) {
  163. $this->error($e->getMessage());
  164. }
  165. }
  166. }