Files
hyperf_service/app/Service/Admin/User/EmployeeService.php
LAPTOP-7SGDREK0\shiweijun c96ecc2594 feat:warehouseKeeper
2025-02-11 17:54:00 +08:00

264 lines
8.2 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\WarehouseKeeper;
use App\Service\Admin\BaseService;
use Exception;
use Hyperf\Di\Annotation\Inject;
use function Hyperf\Config\config;
class EmployeeService extends BaseService
{
/**
* 注入用户模型
* @var AdminUser $adminUserModel
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* @var AdminRole
*/
#[Inject]
protected AdminRole $adminRoleModel;
/**
* 注入加密工厂
* @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('账号或者员工已存在');
$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->section_id = $this->request->input('section_id', 0);
if (!$model->save()) throw new ErrException('账号添加失败');
//写入关联表
$res = match ($model->role_id) {
RoleCode::DRIVER =>
(new DriverSequence)->insert([
'driver_id' => $model->id,
]),
RoleCode::CHEF =>
$this->addChef($model->id),
RoleCode::WAREHOUSE => $this->addWarehouseKeeper($model->id),
default => true,
};
if (!$res) throw new ErrException('关联表添加失败');
return $this->return->success();
}
public function addChef($id): bool
{
$chef = new Chef();
$chef->user_id = $id;
return $chef->save();
}
public function addWarehouseKeeper($id): bool
{
$warehouseKeeper = new WarehouseKeeper();
$warehouseKeeper->user_id = $id;
return $warehouseKeeper->save();
}
/**
* 修改
* @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('账号已存在');
if ($info->role_id != $roleId) {
//写入关联表
$del = match ($info->role_id) {
RoleCode::DRIVER => (new DriverSequence)->where('driver_id', $info->id)->delete(),
RoleCode::CHEF => (new Chef)->where('user_id', $info->id)->delete(),
RoleCode::WAREHOUSE => (new WarehouseKeeper)->where('user_id', $info->id)->delete(),
default => true,
};
$add = match ($roleId) {
RoleCode::DRIVER => (new DriverSequence)->insert([
'driver_id' => $info->id,
]),
RoleCode::CHEF => $this->addChef($info->id),
RoleCode::WAREHOUSE => $this->addWarehouseKeeper($info->id),
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->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 => (new DriverSequence)->where('driver_id', $info->id)->update(['is_del' => UserCode::IS_DEL]),
RoleCode::CHEF => (new Chef)->where('user_id', $info->id)->update(['is_del' => UserCode::IS_DEL]),
RoleCode::WAREHOUSE => (new WarehouseKeeper)->where('user_id', $info->id)->update(['is_del' => UserCode::IS_DEL]),
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();
}
}