Vehicle.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace catchAdmin\yunying\model;
  3. use catchAdmin\permissions\controller\User;
  4. use catcher\base\CatchModel as Model;
  5. use catchAdmin\permissions\model\DataRangScopeTrait;
  6. use catchAdmin\permissions\model\Department;
  7. use catchAdmin\permissions\model\Users;
  8. use catchAdmin\yunying\model\VehicleBrand;
  9. use catchAdmin\yunying\model\VehicleColor;
  10. use catchAdmin\yunying\model\StolenVehicle;
  11. class Vehicle extends Model
  12. {
  13. use DataRangScopeTrait;
  14. // 表名
  15. public $name = 'vehicles';
  16. // 数据库字段映射
  17. public $field = array(
  18. 'id',
  19. // 车牌号码
  20. 'license_plate',
  21. // 所属部门
  22. 'department_id',
  23. // 用户id
  24. 'user_id',
  25. // 上牌时间
  26. 'license_time',
  27. // 车架号
  28. 'frame_number',
  29. // 电机号
  30. 'motor_number',
  31. // 车辆品牌
  32. 'brand_id',
  33. // 车辆颜色
  34. 'color_id',
  35. // 备注
  36. 'remark',
  37. // 购车日期
  38. 'buydate',
  39. // 上牌员工
  40. 'open_user_id',
  41. // 创建人ID
  42. 'creator_id',
  43. // 创建时间
  44. 'created_at',
  45. // 更新时间
  46. 'updated_at',
  47. // 软删除
  48. 'deleted_at',
  49. );
  50. /**
  51. * 获取车辆列表
  52. */
  53. public function getVehicleList($field, $order)
  54. {
  55. $res = $this->dataRange()
  56. ->catchSearch()
  57. ->append(['depart_name', 'vehicle_state', 'owner', 'brand_name', 'color_name', 'open_user_name'])
  58. ->order($this->aliasField($field), $order)
  59. ->paginate();
  60. return $res;
  61. }
  62. /**
  63. * 获取车辆导出列表
  64. */
  65. public function getVehiclesExportList()
  66. {
  67. $res = $this->dataRange()
  68. ->catchSearch()
  69. ->append(['depart_name', 'vehicle_state', 'owner', 'brand_name', 'color_name', 'open_user_name'])
  70. ->order($this->aliasField('id'), 'desc')
  71. ->select();
  72. return $res;
  73. }
  74. //根据部门搜索
  75. public function searchDepartmentIdAttr($query, $value, $data)
  76. {
  77. if (count($value)) {
  78. $value = $value[count($value) - 1];
  79. return $query->where('department_id', $value);
  80. } else {
  81. return $query;
  82. }
  83. }
  84. //根据车牌搜索
  85. public function searchLicensePlateAttr($query, $value, $data)
  86. {
  87. return $query->where('license_plate', 'like', '%' . $value . '%');
  88. }
  89. //根据车架号搜索
  90. public function searchFrameNumberAttr($query, $value, $data)
  91. {
  92. return $query->where('frame_number', 'like', '%' . $value . '%');
  93. }
  94. //根据电机号搜索
  95. public function searchMotorNumberAttr($query, $value, $data)
  96. {
  97. return $query->where('motor_number', 'like', '%' . $value . '%');
  98. }
  99. //根据上牌时间搜索
  100. public function searchLicenseTimeAttr($query, $value, $data)
  101. {
  102. if (empty($value)) {
  103. return $query;
  104. }
  105. return $query->where('license_time', 'between', $value);
  106. }
  107. //获取部门名称
  108. public function getDepartNameAttr($value)
  109. {
  110. $depart_id = $this->getData('department_id');
  111. return (new Department)->where('id', $depart_id)->value('department_name');
  112. }
  113. //获取车主名称
  114. public function getOwnerAttr($value)
  115. {
  116. $uid = $this->getData('user_id');
  117. return (new Users)->where('id', $uid)->value('realname');
  118. }
  119. //获取开户员工账号
  120. public function getOpenUserNameAttr($value)
  121. {
  122. $uid = $this->getData('open_user_id');
  123. return (new Users)->where('id', $uid)->value('username');
  124. }
  125. //获取车辆品牌名称
  126. public function getBrandNameAttr($value)
  127. {
  128. $bid = $this->getData('brand_id');
  129. return (new VehicleBrand())->where('id', $bid)->value('name');
  130. }
  131. //获取车辆颜色名称
  132. public function getColorNameAttr($value)
  133. {
  134. $cid = $this->getData('color_id');
  135. return (new VehicleColor())->where('id', $cid)->value('name');
  136. }
  137. //获取车辆状态名称
  138. public function getVehicleStateAttr($value)
  139. {
  140. $id = $this->getData('id');
  141. $stolen_info = (new StolenVehicle())->where('vehicle_id', $id)->find();
  142. if (!$stolen_info || in_array($stolen_info['result'], [2, 4])) { //0-待处理 1-已受理 2-已找回 3-已理赔 4-误报
  143. $value = 0;
  144. } else {
  145. $value = 1;
  146. }
  147. return $value;
  148. }
  149. }