AgentFrontStaff.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. use yii\db\ActiveRecord;
  5. /**
  6. * This is the model class for table "{{%agent_front_staff}}".
  7. *
  8. * @property integer $id
  9. * @property integer $front_agent_admin_id
  10. * @property integer $saas_id
  11. * @property string $username
  12. * @property string $password
  13. * @property integer $status
  14. * @property integer $is_delete
  15. * @property integer $created_at
  16. * @property integer $updated_at
  17. */
  18. class AgentFrontStaff extends \yii\db\ActiveRecord
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%agent_front_staff}}';
  26. }
  27. public function behaviors()
  28. {
  29. return [
  30. [
  31. 'class' => TimestampBehavior::class,
  32. 'attributes' => [
  33. ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'created_at'],
  34. ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'
  35. ]
  36. ]
  37. ];
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['id', 'saas_id', 'front_agent_admin_id', 'status', 'created_at', 'updated_at', 'is_delete'], 'integer'],
  46. [['username', 'password'], 'string']
  47. ];
  48. }
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => '',
  53. 'front_agent_admin_id' => '前置仓admin ID',
  54. 'saas_id' => '绑定用户(平台用户)',
  55. 'username' => '登录账户',
  56. 'password' => '账户密码',
  57. 'status' => '状态:0禁用 1启用',
  58. 'is_delete' => '',
  59. 'created_at' => '',
  60. 'updated_at' => ''
  61. ];
  62. }
  63. public function beforeSave($insert)
  64. {
  65. if (parent::beforeSave($insert)) {
  66. if (empty($this->username) || empty($this->password)) {
  67. $this->addError('username', '缺少用户名或密码');
  68. return false;
  69. }
  70. if (!in_array($this->status, [0, 1])) {
  71. $this->addError('status', '状态错误');
  72. return false;
  73. }
  74. $saas_user = SaasUser::findOne(['id' => $this->saas_id, 'is_delete' => 0]);
  75. if (!$saas_user) {
  76. $this->addError('saas_user', '用户信息不存在');
  77. return false;
  78. }
  79. $front_agent_admin = Admin::findOne(['id' => $this->front_agent_admin_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT, 'is_delete' => 0]);
  80. if (!$front_agent_admin) {
  81. $this->addError('front_agent_admin', '前置仓不存在');
  82. return false;
  83. }
  84. if ($insert) {
  85. $staff = self::findOne(['saas_id' => $this->saas_id, 'is_delete' => 0]);
  86. if ($staff) {
  87. $this->addError('staff', '当前用户已经绑定过仓库员工');
  88. return false;
  89. }
  90. $staff = self::findOne(['username' => $this->username, 'is_delete' => 0]);
  91. if ($staff) {
  92. $this->addError('staff', '当前账户已经存在');
  93. return false;
  94. }
  95. // $other_admin = Admin::findOne(['username' => $this->username, 'is_delete' => 0]);
  96. // if ($other_admin) {//用户名不可重复
  97. // $this->addError('admin', '当前账户用户名已经存在 请更换用户名');
  98. // return false;
  99. // }
  100. }
  101. $other_admin = Admin::find()->where(['username' => $this->username, 'is_delete' => 0])->asArray()->all();
  102. foreach ($other_admin as $item) {
  103. if (!($item['type'] === Admin::ADMIN_TYPE_FRONT_AGENT_STAFF && intval($item['type_id']) === intval($this->id))) {
  104. $this->addError('admin', '当前账户已经存在 请更换用户名');
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. return false;
  111. }
  112. public function afterSave($insert, $changedAttributes)
  113. {
  114. parent::afterSave($insert, $changedAttributes);
  115. //创建后台员工登录账户
  116. if ($this->is_delete) {
  117. $admin = Admin::findOne(['type' => Admin::ADMIN_TYPE_FRONT_AGENT_STAFF, 'type_id' => $this->id, 'is_delete' => 0]);
  118. if ($admin) {
  119. $admin->is_delete = 1;
  120. }
  121. } else {
  122. $saas_user = SaasUser::findOne(['id' => $this->saas_id, 'is_delete' => 0]);
  123. $admin = Admin::findOne(['type' => Admin::ADMIN_TYPE_FRONT_AGENT_STAFF, 'type_id' => $this->id, 'is_delete' => 0]);
  124. if ($insert || !$admin) {
  125. // 自动创建admin账号
  126. $admin = new Admin();
  127. $admin->access_token = \Yii::$app->security->generateRandomString();
  128. $admin->type = Admin::ADMIN_TYPE_FRONT_AGENT_STAFF;
  129. $admin->type_id = $this->id;
  130. }
  131. $admin->username = $this->username;
  132. if ($insert || !empty(trim($changedAttributes['password']))) {
  133. $admin->password = \Yii::$app->security->generatePasswordHash($this->password);
  134. }
  135. $admin->mobile = $saas_user->mobile;
  136. $admin->name = '仓库员工-' . $saas_user->name;
  137. }
  138. if ($admin) {
  139. $admin->save();
  140. }
  141. }
  142. }