UserCar.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $params = [
  32. 'user_id' => $userId,
  33. 'company_id' => $companyId,
  34. 'car_id' => $ids,
  35. ];
  36. $orderService = new OrderService();
  37. $orderRes = $orderService->getCarNextDate($params);
  38. foreach ($result as $key => &$value) {
  39. $value['next_date'] = isset($orderRes['data'][$value['id']]) ? $orderRes['data'][$value['id']]['next_date'] : '';
  40. }
  41. }
  42. $this->success('获取成功',$result);
  43. } catch (Exception $e) {
  44. $this->error($e->getMessage());
  45. }
  46. }
  47. /**
  48. * 车辆详情
  49. * @return void
  50. */
  51. public function getInfo()
  52. {
  53. try {
  54. $id = $this->request->param('id',0);
  55. $userId = $this->auth->id;
  56. $companyId = $this->auth->company_id;
  57. $field = 'id,car_number,car_model';
  58. $where['user_id'] = $userId;
  59. $where['id'] = $id;
  60. $result = $this->model->field($field)->where($where)->find();
  61. if (!empty($result)) {
  62. $params = [
  63. 'user_id' => $userId,
  64. 'company_id' => $companyId,
  65. 'car_id' => [$id],
  66. ];
  67. $orderService = new OrderService();
  68. $orderRes = $orderService->getCarNextDate($params);
  69. $result['next_date'] = isset($orderRes['data'][$id]) ? $orderRes['data'][$id]['next_date'] : '';
  70. }
  71. $this->success('获取成功',$result);
  72. } catch (Exception $e) {
  73. $this->error($e->getMessage());
  74. }
  75. }
  76. /**
  77. * 保存
  78. * @return void
  79. */
  80. public function save()
  81. {
  82. try {
  83. //验证参数
  84. $id = $this->request->param('id',0);
  85. $userId = $this->auth->id;
  86. $scene = !empty($id) ? 'edit' : 'add';
  87. $validate = validate('UserCar');
  88. if(!$validate->check($this->request->param(),[],$scene)){
  89. throw new Exception($validate->getError());
  90. }
  91. $time = time();
  92. $data = [
  93. 'car_number' => $this->request->param('car_number', ''),
  94. 'car_model' => $this->request->param('car_model', ''),
  95. ];
  96. if (empty($id)) {
  97. $data['user_id'] = $userId;
  98. $data['createtime'] = $time;
  99. $res = $this->model->insertGetId($data);
  100. } else {
  101. $data['updatetime'] = $time;
  102. $where['id'] = $id;
  103. $where['user_id'] = $userId;
  104. $res = $this->model->where($where)->update($data);
  105. }
  106. if (!$res) {
  107. throw new Exception('操作失败');
  108. }
  109. $this->success('操作成功');
  110. } catch (Exception $e) {
  111. $this->error($e->getMessage());
  112. }
  113. }
  114. /**
  115. * 删除
  116. * @return void
  117. */
  118. public function del()
  119. {
  120. try {
  121. //验证参数
  122. $id = $this->request->param('id',0);
  123. $userId = $this->auth->id;
  124. $where['id'] = $id;
  125. $where['user_id'] = $userId;
  126. $userCar = $this->model->where($where)->find();
  127. if (empty($userCar)) {
  128. throw new Exception('未找到车辆信息');
  129. }
  130. $res = $this->model->where($where)->delete();
  131. if (!$res) {
  132. throw new Exception('操作失败');
  133. }
  134. $this->success('操作成功');
  135. } catch (Exception $e) {
  136. $this->error($e->getMessage());
  137. }
  138. }
  139. }