diff --git a/app/Model/AdminUser.php b/app/Model/AdminUser.php index 896f44f..b1c05db 100644 --- a/app/Model/AdminUser.php +++ b/app/Model/AdminUser.php @@ -9,7 +9,8 @@ use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Model\Model; /** - * @property int $id + * @property int $id + * @property int $bind_user_id * @property string $username * @property string $password * @property string $salt @@ -43,7 +44,7 @@ class AdminUser extends Model /** * The attributes that should be cast to native types. */ - protected array $casts = ['id' => 'integer', 'avatar' => 'integer', 'status' => 'integer', 'is_del' => 'integer', 'role_id' => 'integer']; + protected array $casts = ['id' => 'integer', 'avatar' => 'integer', 'status' => 'integer', 'is_del' => 'integer', 'role_id' => 'integer','bind_user_id' => 'integer']; const CREATED_AT = 'create_time'; @@ -76,6 +77,15 @@ class AdminUser extends Model return $this->where('id', $id)->where('is_del',UserCode::IS_NO_DEL)->first(); } + /** + * @param int $userId + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getAdminInfoByBindUserId(int $userId): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('bind_user_id', $userId)->where('is_del',UserCode::IS_NO_DEL)->first(); + } + /** * 获取所有数据 * @param array $ids diff --git a/app/Service/Admin/User/EmployeeService.php b/app/Service/Admin/User/EmployeeService.php index 0a0ff70..f6a2e71 100644 --- a/app/Service/Admin/User/EmployeeService.php +++ b/app/Service/Admin/User/EmployeeService.php @@ -19,6 +19,7 @@ use App\Model\AdminRole; use App\Model\AdminUser; use App\Model\Chef; use App\Model\DriverSequence; +use App\Model\User; use App\Model\WarehouseKeeper; use App\Service\Admin\BaseService; use App\Service\ServiceTrait\Admin\RoleMembersTrait; @@ -43,6 +44,13 @@ class EmployeeService extends BaseService #[Inject] protected AdminRole $adminRoleModel; + /** + * @var User + */ + #[Inject] + protected User $userModel; + + /** * 注入加密工厂 * @var CryptoFactory $cryptoFactory @@ -90,6 +98,12 @@ class EmployeeService extends BaseService if (!empty($oldName) && !empty($oldAccount)) throw new ErrException('账号或者员工已存在'); + $bindUserId = $this->request->input('bind_user_id', 0); + if ($bindUserId > 0) { + $oldBindUserId = $this->adminUserModel->getAdminInfoByBindUserId($bindUserId); + if (!empty($oldBindUserId)) throw new ErrException('绑定用户已存在'); + } + $salt = StringUtil::randStr(6); $defaultPassword = config('system.admin_default_password'); @@ -104,6 +118,7 @@ class EmployeeService extends BaseService $model->avatar = $this->request->input('avatar',0); $model->role_id = $this->request->input('role_id', 0); $model->city_id = $this->request->input('city_id', 0); + $model->bind_user_id = $bindUserId; // $model->section_id = $this->request->input('section_id', 0); if (!$model->save()) throw new ErrException('账号添加失败'); @@ -143,6 +158,13 @@ class EmployeeService extends BaseService if (!empty($oldName) && $oldName->id != $info->id) throw new ErrException('员工已存在'); if (!empty($oldAccount) && $oldAccount->id != $info->id) throw new ErrException('账号已存在'); + $bindUserId = $this->request->input('bind_user_id', 0); + if ($bindUserId > 0) { + $oldBindUserId = $this->adminUserModel->getAdminInfoByBindUserId($bindUserId); + if (!empty($oldBindUserId) && $info->bind_user_id != $oldBindUserId) throw new ErrException('绑定用户已存在'); + } + + if ($info->role_id != $roleId) { //写入关联表 $del = match ($info->role_id) { @@ -171,6 +193,7 @@ class EmployeeService extends BaseService $info->avatar = $this->request->input('avatar',0); $info->role_id = $roleId; $info->city_id = $this->request->input('city_id', 0); + $info->bind_user_id = $bindUserId; // $info->section_id = $this->request->input('section_id', 0); if (!$info->save()) throw new ErrException('账号修改失败');