|
- <?php
-
- namespace backend\modules\motorcade\models;
-
- use backend\modules\zzcs\models\BaseResource;
- use common\models\zModel;
- use Yii;
-
- /**
- * This is the model class for table "base_driver".
- *
- * @property integer $DRIVER_ID
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $CANCEL_FLAG
- * @property string $DRIVER_NAME
- * @property string $DRIVER_NUMBER
- * @property integer $SEX
- * @property integer $AGE
- * @property string $PHONE_NO
- * @property integer $ORG_ID
- * @property string $ID_NUMBER
- * @property string $LICENSE_NO
- * @property integer $BELONG
- * @property integer $DRIVER_STATUS
- * @property string $DRIVER_BIRTHDAY
- * @property string $FIRST_LICENSE_DATE
- * @property integer $AREA_ID
- * @property string $ADDRESS
- * @property string $LICENSE_START_DATE
- * @property integer $VALID_YEARS
- *
- * @property BusOrder[] $busOrders
- */
- class BaseDriver extends zModel
- {
- const DRIVER_STATUS_NORMAL = 0;
- const DRIVER_STATUS_UNNORMAL = 1;
-
- const BELONG_OWN = 523; //自有
- const BELONG_ATTACHED = 524; //挂靠
- const BELONG_OUTSIDE = 525; //外部
- public $province = 791;
- public $city;
- public $country;
- public $outMotorcade = 1;
-
- public function __construct(array $config = [])
- {
- $this->CANCEL_FLAG = 0;
- parent::__construct($config);
- }
-
- public function init()
- {
- $this->DRIVER_BIRTHDAY = date('Y-m-d');
- $this->DRIVER_STATUS = 0;
- $this->SEX = 1;
- $this->DRIVER_STATUS = 0;
- $this->ORG_ID = Yii::$app->user->identity->ORG_ID;
- $this->MAIN_CORP_ID = Yii::$app->user->identity->MAIN_CORP_ID2;
- parent::init();
- }
-
- /**
- * @inheritdoc
- */
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- $scenarios['default'] = ['CANCEL_FLAG'];
- $scenarios['create'] = ['province', 'city', 'country', 'outMotorcade', 'BELONG', 'CREATE_USER_ID', 'CREATE_TIME', 'DRIVER_NAME', 'ORG_ID', 'DRIVER_STATUS', 'DRIVER_NUMBER', 'SEX', 'ID_NUMBER', 'DRIVER_BIRTHDAY', 'PHONE_NO', 'AREA_ID', 'ADDRESS','MAIN_CORP_ID'];
- $scenarios['update'] = ['province', 'city', 'country', 'outMotorcade', 'BELONG', 'UPDATE_USER_ID', 'UPDATE_TIME', 'ORG_ID', 'DRIVER_STATUS', 'DRIVER_NUMBER', 'SEX', 'ID_NUMBER', 'DRIVER_BIRTHDAY', 'PHONE_NO', 'AREA_ID', 'ADDRESS','MAIN_CORP_ID'];
- $scenarios['delete'] = ['CANCEL_FLAG'];
- return $scenarios;
- }
-
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'base_driver';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['CREATE_USER_ID', 'UPDATE_USER_ID', 'CANCEL_FLAG', 'SEX', 'AGE', 'ORG_ID', 'DRIVER_STATUS', 'AREA_ID', 'VALID_YEARS', 'BELONG', ], 'integer'],
- [['DRIVER_NAME', 'ID_NUMBER','PHONE_NO','DRIVER_BIRTHDAY', 'ADDRESS', 'SEX'], 'required'],
- [['PHONE_NO'], 'required', 'message' => '请填写联系手机号'],
- [['DRIVER_NAME', 'ADDRESS'], 'trim'],
- [['ORG_ID'], 'integer', 'skipOnEmpty' => true],
- [['province', 'city', 'country', 'outMotorcade'], 'safe'],
- // [['DRIVER_NAME'], 'unique', 'targetAttribute' => ['DRIVER_NAME', 'CANCEL_FLAG'], 'message' => '司机已经存在'],
- [['ID_NUMBER'], 'unique', 'targetAttribute' => ['ID_NUMBER', 'CANCEL_FLAG'], 'message' => '身份证已经存在'],
- [['PHONE_NO'], 'unique', 'targetAttribute' => ['PHONE_NO', 'CANCEL_FLAG'], 'message' => '手机号已经存在'],
- [['PHONE_NO'], 'match', 'pattern' => '/^\d{11}$/', 'message' => '请正确填写联系手机号'],
- [['ID_NUMBER'], 'match', 'pattern' => '/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/', 'message' => '请正确填写身份证号'],
- [['DRIVER_BIRTHDAY', 'FIRST_LICENSE_DATE', 'LICENSE_START_DATE'], 'string', 'max' => 20],
- [['LICENSE_NO'], 'string', 'max' => 50],
- [['ADDRESS', 'DRIVER_NUMBER'], 'string', 'max' => 30],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'DRIVER_ID' => '司机',
- 'CREATE_USER_ID' => '添加用户',
- 'CREATE_TIME' => '添加时间',
- 'UPDATE_USER_ID' => '用户',
- 'UPDATE_TIME' => '更新时间',
- 'CANCEL_FLAG' => '标识',
- 'DRIVER_NAME' => '姓名',
- 'DRIVER_NUMBER' => '工号',
- 'SEX' => '性别',
- 'AGE' => '年龄',
- 'PHONE_NO' => '手机号',
- 'ORG_ID' => '车队',
- 'ID_NUMBER' => '身份证',
- 'LICENSE_NO' => '驾驶证',
- 'DRIVER_STATUS' => '司机状态',
- 'DRIVER_BIRTHDAY' => '出生日期',
- 'FIRST_LICENSE_DATE' => '初次领证日期',
- 'AREA_ID' => '区域',
- 'ADDRESS' => '详细地址',
- 'LICENSE_START_DATE' => '有效起始日期',
- 'VALID_YEARS' => '有效期限',
- ];
- }
-
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getBusOrders()
- {
- return $this->hasMany(BusOrder::className(), ['send_bus_driver_res_id' => 'DRIVER_ID']);
- }
-
- public function getBaseDocument()
- {
- return $this->hasMany(BaseDocument::className(), ['DOC_ID' => 'DRIVER_ID']);
- }
-
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getOrganization()
- {
- return $this->hasOne(BaseResource::className(), ['RES_ID' => 'ORG_ID']);
- }
-
- /**
- * 证件资料
- * @return array
- */
- public function getDocument()
- {
- $result = [502 => [], 503 => [], 504 => []];
- //以502 503 504为数组元素返回
- $documents = BaseDocument::findAll(['CANCEL_FLAG' => 0, 'DOC_TYPE' => BaseDocument::DRIVER, 'DOC_ID' => $this->DRIVER_ID]);
- if (count($documents) > 0) {
- foreach ($documents as $document) {
- $document->setScenario('driver');
- if ($document->DOC_OPTION == '502') {
- $result[502][] = $document;
- }
- if ($document->DOC_OPTION == '503') {
- $result[503][] = $document;
- }
- if ($document->DOC_OPTION == '504') {
- $result[504][] = $document;
- }
- }
- }
- while (count($result[502]) < 2) {
- $result[502][] = new BaseDocument(['scenario' => 'driver']);
- }
- while (count($result[503]) < 1) {
- $result[503][] = new BaseDocument(['scenario' => 'driver']);
- }
- while (count($result[504]) < 2) {
- $result[504][] = new BaseDocument(['scenario' => 'driver']);
- }
-
- // else {
- // $result[502] = [new BaseDocument(), new BaseDocument()];
- // $result[503] = [new BaseDocument()];
- // $result[504] = [new BaseDocument(), new BaseDocument()];
- // }
- return $result;
- // return $this->hasMany(BaseDocument::className(), ['DOC_ID' => 'DRIVER_ID'])->where(['DOC_TYPE' => BaseDocument::DRIVER]);
- }
-
- /**
- * User: wangxj
- *
- * 根据form加载数据
- *
- * @params $data
- *
- * @return
- */
- // public function load($data, $formName = null)
- // {
- // parent::load($data, $formName);
- //
- // $form = $data['BaseDriver'];
- // $this->setAttributes($form);
- //// $this->province = $form['pri'];
- // }
-
- public function beforeSave($insert)
- {
- //联系地址的省市
- $this->AREA_ID = $this->country ? $this->country : $this->city;
- $this->ORG_ID = $this->outMotorcade == 1 ? Yii::$app->user->identity->ORG_ID : $this->ORG_ID;
- $this->CREATE_TIME = date('Y-m-d H:i:s', time());
- $this->CREATE_USER_ID = Yii::$app->user->id;
- $this->UPDATE_USER_ID = Yii::$app->user->id;
- return parent::beforeSave($insert);
- }
- }
|