| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%agent_front_staff}}".
- *
- * @property integer $id
- * @property integer $front_agent_admin_id
- * @property integer $saas_id
- * @property string $username
- * @property string $password
- * @property integer $status
- * @property integer $is_delete
- * @property integer $created_at
- * @property integer $updated_at
- */
- class AgentFrontStaff extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%agent_front_staff}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'
- ]
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'saas_id', 'front_agent_admin_id', 'status', 'created_at', 'updated_at', 'is_delete'], 'integer'],
- [['username', 'password'], 'string']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'front_agent_admin_id' => '前置仓admin ID',
- 'saas_id' => '绑定用户(平台用户)',
- 'username' => '登录账户',
- 'password' => '账户密码',
- 'status' => '状态:0禁用 1启用',
- 'is_delete' => '',
- 'created_at' => '',
- 'updated_at' => ''
- ];
- }
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- if (empty($this->username) || empty($this->password)) {
- $this->addError('username', '缺少用户名或密码');
- return false;
- }
- if (!in_array($this->status, [0, 1])) {
- $this->addError('status', '状态错误');
- return false;
- }
- $saas_user = SaasUser::findOne(['id' => $this->saas_id, 'is_delete' => 0]);
- if (!$saas_user) {
- $this->addError('saas_user', '用户信息不存在');
- return false;
- }
- $front_agent_admin = Admin::findOne(['id' => $this->front_agent_admin_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT, 'is_delete' => 0]);
- if (!$front_agent_admin) {
- $this->addError('front_agent_admin', '前置仓不存在');
- return false;
- }
- if ($insert) {
- $staff = self::findOne(['saas_id' => $this->saas_id, 'is_delete' => 0]);
- if ($staff) {
- $this->addError('staff', '当前用户已经绑定过仓库员工');
- return false;
- }
- $staff = self::findOne(['username' => $this->username, 'is_delete' => 0]);
- if ($staff) {
- $this->addError('staff', '当前账户已经存在');
- return false;
- }
- // $other_admin = Admin::findOne(['username' => $this->username, 'is_delete' => 0]);
- // if ($other_admin) {//用户名不可重复
- // $this->addError('admin', '当前账户用户名已经存在 请更换用户名');
- // return false;
- // }
- }
- $other_admin = Admin::find()->where(['username' => $this->username, 'is_delete' => 0])->asArray()->all();
- foreach ($other_admin as $item) {
- if (!($item['type'] === Admin::ADMIN_TYPE_FRONT_AGENT_STAFF && intval($item['type_id']) === intval($this->id))) {
- $this->addError('admin', '当前账户已经存在 请更换用户名');
- return false;
- }
- }
- return true;
- }
- return false;
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- //创建后台员工登录账户
- if ($this->is_delete) {
- $admin = Admin::findOne(['type' => Admin::ADMIN_TYPE_FRONT_AGENT_STAFF, 'type_id' => $this->id, 'is_delete' => 0]);
- if ($admin) {
- $admin->is_delete = 1;
- }
- } else {
- $saas_user = SaasUser::findOne(['id' => $this->saas_id, 'is_delete' => 0]);
- $admin = Admin::findOne(['type' => Admin::ADMIN_TYPE_FRONT_AGENT_STAFF, 'type_id' => $this->id, 'is_delete' => 0]);
- if ($insert || !$admin) {
- // 自动创建admin账号
- $admin = new Admin();
- $admin->access_token = \Yii::$app->security->generateRandomString();
- $admin->type = Admin::ADMIN_TYPE_FRONT_AGENT_STAFF;
- $admin->type_id = $this->id;
- }
- $admin->username = $this->username;
- if ($insert || !empty(trim($changedAttributes['password']))) {
- $admin->password = \Yii::$app->security->generatePasswordHash($this->password);
- }
- $admin->mobile = $saas_user->mobile;
- $admin->name = '仓库员工-' . $saas_user->name;
- }
- if ($admin) {
- $admin->save();
- }
- }
- }
|