Files
hyperf_service/app/Service/Admin/User/EmployeeService.php
2025-04-07 15:50:38 +08:00

276 lines
8.8 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Constants\Admin\UserCode;
use App\Constants\Common\RoleCode;
use App\Exception\ErrException;
use App\Extend\StringUtil;
use App\Lib\Crypto\CryptoFactory;
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;
use Exception;
use Hyperf\Di\Annotation\Inject;
use function Hyperf\Config\config;
class EmployeeService extends BaseService
{
use RoleMembersTrait;
/**
* 注入用户模型
* @var AdminUser $adminUserModel
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* @var AdminRole
*/
#[Inject]
protected AdminRole $adminRoleModel;
/**
* @var User
*/
#[Inject]
protected User $userModel;
/**
* 注入加密工厂
* @var CryptoFactory $cryptoFactory
*/
#[Inject]
protected CryptoFactory $cryptoFactory;
/**
* @var array|string[]
*/
private array $filed = ['id','username','avatar','chinese_name','mobile','status','last_login_ip','last_login_time','role_id'];
/**
* 列表
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$list = $this->adminUserModel->where('is_del',UserCode::ENABLE)->paginate($limit,$this->filed)->toArray();
$roleIds = array_column($list['data'], 'role_id');
$roleList = $this->adminRoleModel->getDataByIds($roleIds);
foreach ($list['data'] as &$item) {
$item['role_name'] = $roleList[$item['role_id']]['name'] ?? '普通管理员';
}
return $this->return->success('success', ['list' => $list]);
}
/**
* 添加
* @return array
* @throws Exception
*/
public function add(): array
{
$name = $this->request->input('chinese_name');
$account = $this->request->input('account');
$oldAccount = $this->adminUserModel->getAdminInfoByAccount($account);
$oldName = $this->adminUserModel->getAdminInfoByName($name);
if (!empty($oldName) && !empty($oldAccount)) throw new ErrException('账号或者员工已存在');
$bindUserId = (int)$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');
$model = new AdminUser();
$model->username = $account;
$model->mobile = $account;
$model->chinese_name = $name;
$model->salt = $salt;
$model->password = $this->cryptoFactory->cryptoClass('admin-password',$defaultPassword,$salt)->encrypt();
$model->status = $this->request->input('status', 1);
$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('账号添加失败');
//写入关联表
$res = match ($model->role_id) {
RoleCode::DRIVER => $this->addDriver($model->id,$model->chinese_name),
RoleCode::CHEF => $this->addChef($model->id),
RoleCode::WAREHOUSE => $this->addWarehouseKeeper($model->id),
RoleCode::OPTION_CATERING, RoleCode::MEAL_CATERING => $this->addCaterer($model->id,$model->role_id),
default => true,
};
if (!$res) throw new ErrException('关联表添加失败');
return $this->return->success();
}
/**
* 修改
* @return array
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$name = $this->request->input('chinese_name');
$account = $this->request->input('account');
$roleId = (int)$this->request->input('role_id', 0);
$info = $this->adminUserModel->getAdminInfoById($id);
if (empty($info)) throw new ErrException('数据不存在');
$oldAccount = $this->adminUserModel->getAdminInfoByAccount($account);
$oldName = $this->adminUserModel->getAdminInfoByName($name);
if (!empty($oldName) && $oldName->id != $info->id) throw new ErrException('员工已存在');
if (!empty($oldAccount) && $oldAccount->id != $info->id) throw new ErrException('账号已存在');
$bindUserId = (int)$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) {
RoleCode::DRIVER => $this->delDriver($info->id),
RoleCode::CHEF => $this->delChef($info->id),
RoleCode::WAREHOUSE => $this->delWarehouseKeeper($info->id),
RoleCode::OPTION_CATERING, RoleCode::MEAL_CATERING => $this->delCaterer($info->id),
default => true,
};
$add = match ($roleId) {
RoleCode::DRIVER => $this->addDriver($info->id,$info->chinese_name),
RoleCode::CHEF => $this->addChef($info->id),
RoleCode::WAREHOUSE => $this->addWarehouseKeeper($info->id),
RoleCode::OPTION_CATERING, RoleCode::MEAL_CATERING => $this->addCaterer($info->id,$roleId),
default => true,
};
if (!$del || !$add) throw new ErrException('关联表修改失败');
}
$info->username = $account;
$info->mobile = $account;
$info->chinese_name = $name;
$info->status = $this->request->input('status', 1);
$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('账号修改失败');
return $this->return->success();
}
/**
* 删除
* @return array
*/
public function delete(): array
{
$id = (int)$this->request->input('id');
if ($this->adminId == $id) throw new ErrException('不可删除自己');
$info = $this->adminUserModel->getAdminInfoById($id);
if (empty($info)) throw new ErrException('员工不存在');
$info->is_del = UserCode::IS_DEL;
$del = match ($info->role_id) {
RoleCode::DRIVER => $this->delDriver($info->id),
RoleCode::CHEF => $this->delChef($info->id),
RoleCode::WAREHOUSE => $this->delWarehouseKeeper($info->id),
RoleCode::OPTION_CATERING, RoleCode::MEAL_CATERING => $this->delCaterer($info->id),
default => true,
};
if (!$info->save() || !$del) throw new ErrException('账号删除失败');
return $this->return->success();
}
/**
* 详情
* @return array
*/
public function get(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminUserModel->where('id', $id)->where('is_del',UserCode::ENABLE)->first($this->filed);
if (empty($info)) throw new ErrException('员工不存在');
$roleName = $this->adminRoleModel->where('id', $info->role_id)->value('name');
$res = $info->toArray();
$res['role_name'] = $roleName ?? '普通管理员';
return $this->return->success('success', ['info' => $res]);
}
/**
* 重置密码
* @return array
* @throws Exception
*/
public function resetPassword(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminUserModel->getAdminInfoById($id);
if (empty($info)) throw new ErrException('员工不存在');
$salt = StringUtil::randStr(6);
$defaultPassword = config('system.admin_default_password');
$info->salt = $salt;
$info->password = $this->cryptoFactory->cryptoClass('admin-password',$defaultPassword,$salt)->encrypt();
if (!$info->save()) throw new ErrException('密码重置失败');
return $this->return->success();
}
}