You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

443 lines
16 KiB

  1. <?php
  2. namespace backend\modules\motorcade\models;
  3. use backend\modules\api\models\BaseUser;
  4. use backend\modules\api\models\DictType;
  5. use backend\modules\zzcs\models\BaseResource;
  6. use backend\modules\zzcs\models\BaseSupplier;
  7. use common\models\zModel;
  8. use Yii;
  9. use yii\db\Query;
  10. use yii\helpers\ArrayHelper;
  11. /**
  12. * This is the model class for table "base_bus".
  13. *
  14. * @property integer $BUS_ID
  15. * @property integer $CREATE_USER_ID
  16. * @property string $CREATE_TIME
  17. * @property integer $UPDATE_USER_ID
  18. * @property string $UPDATE_TIME
  19. * @property integer $CANCEL_FLAG
  20. * @property string $BUS_NO
  21. * @property integer $BUS_TYPE_RES_ID
  22. * @property integer $BRAND_ID
  23. * @property integer $MAIN_CORP_ID
  24. * @property integer $ORG_ID
  25. * @property string $SEAT_DESC
  26. * @property string $BUY_DATE
  27. * @property string $BUS_LICENSE
  28. * @property integer $SEAT_COUNT
  29. * @property string $DRIVER_COUNT
  30. * @property string $TOUR_COUNT
  31. * @property string $EXTRA_COUNT
  32. * @property string $MPG
  33. * @property string $COST
  34. * @property integer $BUS_STATE
  35. * @property integer $SEAT_TYPE
  36. * @property integer $BUS_COLOR
  37. * @property string $BUS_IMG_PATH
  38. * @property string $BUS_IMG_TYPE
  39. * @property string $REGISTER_TIME
  40. * @property string $BUS_IMG_PATH_ORI
  41. * @property string $BUS_BELONG
  42. *
  43. * @property BusOrder[] $busOrders
  44. */
  45. class BaseBus extends zModel
  46. {
  47. public $is_my;
  48. // 判断是否新车,图片显示判断用,0为新车,1为老车
  49. public $is_new;
  50. const BUS_STATUS_NORMAL = 336; //正常
  51. const BUS_STATUS_FIXING = 337; //维修
  52. const BUS_BELONG_OWN = 523; //自有车辆
  53. const BUS_BELONG_ATTACHED = 524; //挂靠车辆
  54. const BUS_BELONG_OUTSIDE = 525; //外部车辆
  55. // 经营属性
  56. const BUS_BUSINESS_TYPE = array(
  57. 1 => '省际',
  58. 2 => '市际',
  59. 3 => '其他',
  60. );
  61. // 车身属性
  62. const BUS_SURFACE_TYPE = array(
  63. 1 => '蜘蛛车身',
  64. 2 => '恒栋车身',
  65. 3 => '自在车身',
  66. 4 => '迪士尼车身',
  67. 5 => '奕欧来车身',
  68. 6 => '广告车身',
  69. 7 => '小黑鱼车身',
  70. 10 => '其他',
  71. );
  72. /**
  73. * @inheritdoc
  74. */
  75. public static function tableName()
  76. {
  77. return 'base_bus';
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function rules()
  83. {
  84. return [
  85. [['CREATE_USER_ID', 'UPDATE_USER_ID', 'CANCEL_FLAG', 'SEAT_MATRIX_ID', 'BUS_TYPE_RES_ID', 'BRAND_ID', 'MAIN_CORP_ID', 'ORG_ID', 'SEAT_COUNT', 'BUS_STATE', 'SEAT_TYPE', 'BUS_COLOR'], 'integer'],
  86. [['CREATE_TIME', 'BUS_NO', 'REGISTER_TIME', 'MAX_COUNT', 'FUEL_FORM', 'BUY_DATE', 'BRAND_ID'], 'required'],
  87. [['BUS_NO'], 'unique', 'targetAttribute' => ['BUS_NO', 'CANCEL_FLAG', 'MAIN_CORP_ID', 'ORG_ID'], 'message' => '该车辆已存在'],
  88. [['UPDATE_TIME', 'BUS_BELONG', 'is_new'], 'safe'],
  89. [['BUSINESS_TYPE', 'SURFACE_TYPE'], 'string'],
  90. [['CREATE_TIME', 'BUS_NO', 'BUY_DATE', 'REGISTER_TIME'], 'string', 'max' => 20],
  91. [['SEAT_DESC', 'BUS_LICENSE'], 'string', 'max' => 50],
  92. [['DRIVER_COUNT', 'TOUR_COUNT', 'MPG', 'COST'], 'string', 'max' => 10],
  93. [['EXTRA_COUNT'], 'string', 'max' => 11],
  94. [['BUS_IMG_PATH', 'BUS_IMG_PATH_ORI'], 'string', 'max' => 1024],
  95. [['BUS_IMG_TYPE'], 'string', 'max' => 64],
  96. ];
  97. }
  98. /**
  99. * @inheritdoc
  100. */
  101. public function attributeLabels()
  102. {
  103. return [
  104. 'FUEL_FORM' => '燃料形式',
  105. 'BUS_BELONG' => '自有或外部',
  106. 'BUS_ID' => '车辆',
  107. 'CREATE_USER_ID' => '记录创建用户ID',
  108. 'CREATE_TIME' => '记录创建时间',
  109. 'UPDATE_USER_ID' => '记录最后更新用户ID',
  110. 'UPDATE_TIME' => '记录最后更新时间',
  111. 'CANCEL_FLAG' => '记录有效性标记,CANCEL_FLAG=0记录有效;CANCEL_FLAG=1,记录已删除',
  112. 'BUS_NO' => '车牌号',
  113. 'BUS_TYPE_RES_ID' => '车型',
  114. 'SEAT_MATRIX_ID' => '座位图ID',
  115. 'BRAND_ID' => '品牌',
  116. 'MAIN_CORP_ID' => '运营主体ID',
  117. 'ORG_ID' => '所属公司ID',
  118. 'SEAT_DESC' => '座位',
  119. 'BUY_DATE' => '购车日期',
  120. 'BUS_LICENSE' => '型号',
  121. 'SEAT_COUNT' => ' 座位数',
  122. 'DRIVER_COUNT' => ' 司机座位数',
  123. 'TOUR_COUNT' => '导游座位数',
  124. 'EXTRA_COUNT' => '辅座数',
  125. 'MPG' => '油耗',
  126. 'COST' => ' 固定成本,单位元/天',
  127. 'BUS_STATE' => '车辆状态',
  128. 'SEAT_TYPE' => '座位类型',
  129. 'BUS_COLOR' => 'Bus Color',
  130. 'BUS_IMG_PATH' => '图片路径 对应的图片类型为BUS_IMG_TYPE里相应数字',
  131. 'BUS_IMG_TYPE' => '图片对应的类型,dict_type 360',
  132. 'REGISTER_TIME' => '注册登记日期',
  133. 'BUS_IMG_PATH_ORI' => '图片路径 对应的图片类型为BUS_IMG_TYPE里相应数字',
  134. 'MAX_COUNT' => '核载数',
  135. 'BUSINESS_TYPE' => '经营属性',
  136. 'SURFACE_TYPE' => '车身属性',
  137. ];
  138. }
  139. /**
  140. * User: wangxj
  141. *
  142. * 函数作用
  143. *
  144. * @param $post
  145. * @param $formName
  146. * @return \yii\db\ActiveQuery
  147. */
  148. public function load($post, $formName = null)
  149. {
  150. // $this->start_date = date('Y-m-01', time());
  151. // $this->end_date = date('Y-m-t', time());
  152. // $this->op = isset($post['op']) ? $post['op'] : $this->op;
  153. // $this->org_sale_id = isset($post['org_sale_id']) ? $post['org_sale_id'] : $this->org_sale_id;
  154. // $this->bus_user = isset($post['bus_user']) ? isset($post['bus_user']) :$this ->bus_user;
  155. // $this->line_name = isset($post['line_name']) ? isset($post['line_name']) :$this ->line_name;
  156. // $this->line_type = isset($post['line_type']) ? isset($post['line_type']) :$this ->line_type;
  157. return parent::load($post);
  158. }
  159. /**
  160. * @return \yii\db\ActiveQuery 车辆所属公司
  161. */
  162. public function getOrg()
  163. {
  164. return $this->hasOne(BaseSupplier::className(), ['ID' => 'ORG_ID']);
  165. }
  166. //所属品牌
  167. public function getBrand()
  168. {
  169. return $this->hasOne(BaseResource::className(), ['RES_ID' => 'BRAND_ID']);
  170. }
  171. public static function getBrands()
  172. {
  173. $data = (new Query())
  174. ->select(['res_id', 'res_name'])
  175. ->from('base_resource')
  176. ->where(['cancel_flag' => '0', 'res_type_id' => '134'])
  177. ->all();
  178. return $data;
  179. }
  180. public static function getFuel()
  181. {
  182. $data = (new Query())
  183. ->select(['id', 'type_name'])
  184. ->from('dict_type')
  185. ->where(['parent_id' => '485'])
  186. ->all();
  187. return $data;
  188. }
  189. public function getBusOrders()
  190. {
  191. return $this->hasMany(BusOrder::className(), ['send_bus_res_id' => 'BUS_ID']);
  192. }
  193. public function getBaseDocument()
  194. {
  195. return $this->hasMany(BaseDocument::className(), ['DOC_ID' => 'BUS_ID']);
  196. }
  197. /**
  198. * User:Steven
  199. * 获取车辆列表
  200. * @param $key_word
  201. * @param $date
  202. * @param $key
  203. * @return array
  204. */
  205. public function GetBusList($key_word, $key, $date)
  206. {
  207. $main_corp_id = Yii::$app->user->identity->MAIN_CORP_ID2; //运营主体
  208. if (!empty(trim($key_word))) {
  209. $key_word = ' and a.BUS_NO LIKE \'%' . $key_word . '%\'';
  210. }
  211. if (trim($key) != '') {
  212. $key = ' and a.ORG_ID=' . $key;
  213. }
  214. $sql = "select
  215. a.bus_id,
  216. a.bus_no,
  217. a.seat_count,
  218. a.org_id,
  219. b.supplier_name res_name,
  220. a.surface_type,
  221. count(bo.id) used_time
  222. from
  223. base_bus a
  224. left join base_supplier b on a.org_id = b.id
  225. left join bus_order bo on bo.send_bus_res_id = a.bus_id and bo.cancel_flag = 0 and bo.run_status <> 439 and bo.run_date = '$date'
  226. where
  227. a.cancel_flag = 0
  228. and a.MAIN_CORP_ID=$main_corp_id $key_word $key
  229. group by bus_id";
  230. // $sql = "select a.bus_id,a.bus_no,a.seat_count,a.org_id,b.res_name from base_bus a left join base_resource b on a.org_id=b.res_id where a.cancel_flag=0 and a.MAIN_CORP_ID=$main_corp_id" . $key_word . $key;
  231. $conn = Yii::$app->db;
  232. $data = $conn->createCommand($sql)->queryAll();
  233. return ['model' => $this, 'data' => $data];
  234. }
  235. /**
  236. * User:Steven
  237. * 获取车队列表
  238. * @return array
  239. */
  240. public function getConvoy()
  241. {
  242. $main_corp_id = Yii::$app->user->identity->MAIN_CORP_ID2; //运营主体
  243. $sql = "select distinct a.org_id,b.supplier_name res_name from base_bus a left join base_supplier b on a.org_id=b.id where a.cancel_flag=0 and a.MAIN_CORP_ID=$main_corp_id";
  244. $conn = Yii::$app->db;
  245. $data = $conn->createCommand($sql)->queryAll();
  246. return ['model' => $this, 'data' => $data];
  247. }
  248. /**
  249. * User:Steven
  250. * 获取司机列表
  251. * @param $key_word
  252. * @param $date
  253. * @param $key
  254. * @return array
  255. */
  256. public function getDriver($key_word, $date, $key)
  257. {
  258. $main_corp_id = Yii::$app->user->identity->MAIN_CORP_ID2; //运营主体
  259. if (!empty(trim($key_word))) {
  260. $key_word = ' and (a.driver_name LIKE \'%' . $key_word . '%\' or a.phone_no LIKE \'%' . $key_word . '%\')';
  261. }
  262. if (trim($key) != '') {
  263. $key = ' and a.ORG_ID=' . $key;
  264. }
  265. $sql = "select
  266. a.driver_id,
  267. a.driver_name,
  268. a.phone_no,
  269. s.supplier_name as res_name,
  270. count(bo.id) used_time
  271. from
  272. base_driver a
  273. left join base_supplier s on a.org_id = s.id
  274. left join bus_order bo on bo.send_bus_driver_res_id = a.driver_id and bo.run_date = '$date'
  275. and bo.cancel_flag = 0
  276. and bo.run_status <> 439
  277. where
  278. a.cancel_flag = 0
  279. and a.MAIN_CORP_ID=$main_corp_id $key_word $key
  280. group by driver_id order by convert(a.driver_name using gbk)";
  281. // $sql = "select a.driver_id,a.driver_name,a.phone_no,b.res_name from base_driver a left join base_resource b on a.org_id=b.res_id where a.cancel_flag=0 and a.MAIN_CORP_ID=$main_corp_id" . $key_word; and a.MAIN_CORP_ID=$main_corp_id" . $key_word;
  282. $conn = Yii::$app->db;
  283. $data = $conn->createCommand($sql)->queryAll();
  284. return ['model' => $this, 'data' => $data];
  285. }
  286. public function getSeatCount($seat_type = '-1')
  287. {
  288. $data = (new Query())
  289. ->select(['res_id', 'res_name'])
  290. ->from('base_resource')
  291. ->where(['parent_id' => $seat_type, 'res_type_id' => '69', 'cancel_flag' => '0'])
  292. ->all();
  293. return $data;
  294. }
  295. public static function is_my()
  296. {
  297. $bus_belong = BaseBus::findAll(['CANCEL_FLAG' => 0, 'BUS_ID' => $_REQUEST['bus_id']]);
  298. return $bus_belong;
  299. }
  300. public static function getBusBelong()
  301. {
  302. $obj = DictType::findAll(['PARENT_ID' => 522]);
  303. return $obj;
  304. }
  305. public function baoyanglicheng()
  306. {
  307. //当前里程数
  308. $result[0] = (new Query())->select('max(end_value) as min')->from('run_bus_expenses')->where(['bus_res_id' => $_REQUEST['bus_id']])->one();
  309. //下次保养里程数
  310. $result[1] = (new Query())->select('max(last_maintain) as max')->from('base_document')->where(['CANCEL_FLAG' => 0, 'DOC_ID' => $_REQUEST['bus_id'], 'DOC_OPTION' => 515])->one();
  311. return $result;
  312. }
  313. public function getPicture()
  314. {
  315. $result = [
  316. 506 => [new BaseDocument(['scenario' => 'bus']), new BaseDocument(['scenario' => 'bus'])],
  317. 507 => [new BaseDocument(['scenario' => 'bus']), new BaseDocument(['scenario' => 'bus'])],
  318. 509 => [new BaseDocument(['scenario' => 'bus']), new BaseDocument(['scenario' => 'bus'])],
  319. 510 => [new BaseDocument(['scenario' => 'bus']), new BaseDocument(['scenario' => 'bus'])],
  320. ];
  321. //以506 507 509 510为数组元素返回 506 507为车辆行驶证 509 510为道路运输证 511为保单 515为保养
  322. $documents = BaseDocument::findAll(['CANCEL_FLAG' => 0, 'DOC_TYPE' => BaseDocument::BUS, 'DOC_ID' => $this->BUS_ID = $_REQUEST['bus_id'], 'DOC_STATUS' => 0, 'DOC_OPTION' => [506, 507]]);
  323. if (count($documents) > 0) {
  324. foreach ($documents as $document) {
  325. if ($document->DOC_OPTION == '506') {
  326. if ($document->IMG_OPTION == BaseDocument::IMG_FRONT) {
  327. $result[506][0] = $document;
  328. $result[506][0]['is_new'] = 1;
  329. }
  330. }
  331. if ($document->DOC_OPTION == '507') {
  332. if ($document->IMG_OPTION == BaseDocument::IMG_FRONT) {
  333. $result[507][0] = $document;
  334. $result[507][0]['is_new'] = 1;
  335. }
  336. }
  337. }
  338. }
  339. $documents1 = BaseDocument::findAll(['CANCEL_FLAG' => 0, 'DOC_TYPE' => BaseDocument::BUS, 'DOC_ID' => $this->BUS_ID = $_REQUEST['bus_id'], 'DOC_STATUS' => 0, 'DOC_OPTION' => [509, 510]]);
  340. if (count($documents1) > 0) {
  341. foreach ($documents1 as $document) {
  342. if ($document->DOC_OPTION == '509') {
  343. if ($document->IMG_OPTION == BaseDocument::IMG_FRONT) {
  344. $result[509][0] = $document;
  345. $result[509][0]['is_new'] = 1;
  346. }
  347. }
  348. if ($document->DOC_OPTION == '510') {
  349. if ($document->IMG_OPTION == BaseDocument::IMG_FRONT) {
  350. $result[510][0] = $document;
  351. $result[510][0]['is_new'] = 1;
  352. }
  353. }
  354. }
  355. }
  356. return $result;
  357. }
  358. /**
  359. * User: wangxj
  360. *
  361. * 函数作用
  362. *
  363. * @param $org_id
  364. * @param $type string 返回类型为数组或对象
  365. *
  366. * @return array|BaseBus
  367. */
  368. public static function getBusListByOrg($org_id = '', $type = 'array')
  369. {
  370. $res = BaseBus::find()->where(['MAIN_CORP_ID' => Yii::$app->user->identity->MAIN_CORP_ID2, 'CANCEL_FLAG' => 0]);
  371. if ($org_id !== '') {
  372. $res->andFilterWhere(['ORG_ID' => $org_id]);
  373. }
  374. if ($type === 'array') {
  375. $map = true;
  376. $res->asArray();
  377. }
  378. $data = $res->all();
  379. if (isset($map)) {
  380. $data = ArrayHelper::map($data, 'BUS_ID', 'BUS_NO');
  381. }
  382. return $data;
  383. }
  384. public static function getBaseUser($org_id = '', $type = 'array')
  385. {
  386. $res = BaseDriver::find()->where(['MAIN_CORP_ID' => Yii::$app->user->identity->MAIN_CORP_ID2, 'CANCEL_FLAG' => 0]);
  387. if ($org_id !== '') {
  388. $res->andFilterWhere(['ORG_ID' => $org_id]);
  389. }
  390. if ($type === 'array') {
  391. $map = true;
  392. $res->asArray();
  393. }
  394. $data = $res->all();
  395. if (isset($map)) {
  396. $data = ArrayHelper::map($data, 'DRIVER_ID', 'DRIVER_NAME');
  397. }
  398. return $data;
  399. }
  400. public static function getBaseResource($org_id = '', $type = 'array')
  401. {
  402. $res = BaseResource::find()->where(['RES_TYPE_ID' => [520, 516]]);
  403. if ($type === 'array') {
  404. $map = true;
  405. $res->asArray();
  406. }
  407. $data = $res->all();
  408. if (isset($map)) {
  409. $data = ArrayHelper::map($data, 'RES_ID', 'RES_NAME');
  410. }
  411. return $data;
  412. }
  413. }