UserCar.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\OrderService;
  5. use think\Db;
  6. use think\Exception;
  7. class UserCar 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('user_car');
  16. }
  17. /**
  18. * 车辆列表
  19. * @return void
  20. */
  21. public function getList()
  22. {
  23. try {
  24. $userId = $this->auth->id;
  25. $companyId = $this->auth->company_id;
  26. $field = 'id,car_number,car_model';
  27. $where['user_id'] = $userId;
  28. $result = $this->model->field($field)->where($where)->order('createtime desc')->autopage()->select();
  29. if (!empty($result)) {
  30. $ids = array_column($result,'id');
  31. $carNos = array_column($result,'car_number');
  32. $params = [
  33. 'user_id' => $userId,
  34. 'company_id' => $companyId,
  35. 'car_id' => $ids,
  36. 'car_number' => $carNos,
  37. ];
  38. $orderService = new OrderService();
  39. $orderRes = $orderService->getCarNextDate($params);
  40. foreach ($result as $key => &$value) {
  41. $value['next_date'] = isset($orderRes['data'][$value['car_number']]) ? $orderRes['data'][$value['car_number']]['next_date'] : '';
  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 getInfo()
  54. {
  55. try {
  56. $id = $this->request->param('id',0);
  57. $userId = $this->auth->id;
  58. $companyId = $this->auth->company_id;
  59. $field = 'id,car_number,car_model';
  60. $where['user_id'] = $userId;
  61. $where['id'] = $id;
  62. $result = $this->model->field($field)->where($where)->find();
  63. if (!empty($result)) {
  64. $params = [
  65. 'user_id' => $userId,
  66. 'company_id' => $companyId,
  67. 'car_id' => [$id],
  68. ];
  69. $orderService = new OrderService();
  70. $orderRes = $orderService->getCarNextDate($params);
  71. $result['next_date'] = isset($orderRes['data'][$result['car_number']]) ? $orderRes['data'][$result['car_number']]['next_date'] : '';
  72. }
  73. $this->success('获取成功',$result);
  74. } catch (Exception $e) {
  75. $this->error($e->getMessage());
  76. }
  77. }
  78. /**
  79. * 保存
  80. * @return void
  81. */
  82. public function save()
  83. {
  84. try {
  85. //验证参数
  86. $id = $this->request->param('id',0);
  87. $userId = $this->auth->id;
  88. $scene = !empty($id) ? 'edit' : 'add';
  89. $validate = validate('UserCar');
  90. if(!$validate->check($this->request->param(),[],$scene)){
  91. throw new Exception($validate->getError());
  92. }
  93. $time = time();
  94. $data = [
  95. 'car_number' => $this->request->param('car_number', ''),
  96. 'car_model' => $this->request->param('car_model', ''),
  97. ];
  98. if (empty($id)) {
  99. $data['user_id'] = $userId;
  100. $data['createtime'] = $time;
  101. $res = $this->model->insertGetId($data);
  102. } else {
  103. $data['updatetime'] = $time;
  104. $where['id'] = $id;
  105. $where['user_id'] = $userId;
  106. $res = $this->model->where($where)->update($data);
  107. }
  108. if (!$res) {
  109. throw new Exception('操作失败');
  110. }
  111. $this->success('操作成功');
  112. } catch (Exception $e) {
  113. $this->error($e->getMessage());
  114. }
  115. }
  116. /**
  117. * 删除
  118. * @return void
  119. */
  120. public function del()
  121. {
  122. try {
  123. //验证参数
  124. $id = $this->request->param('id',0);
  125. $userId = $this->auth->id;
  126. $where['id'] = $id;
  127. $where['user_id'] = $userId;
  128. $userCar = $this->model->where($where)->find();
  129. if (empty($userCar)) {
  130. throw new Exception('未找到车辆信息');
  131. }
  132. $res = $this->model->where($where)->delete();
  133. if (!$res) {
  134. throw new Exception('操作失败');
  135. }
  136. $this->success('操作成功');
  137. } catch (Exception $e) {
  138. $this->error($e->getMessage());
  139. }
  140. }
  141. }