Files

107 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2026-08-31 11:33:59 +08:00
<?php
declare(strict_types=1);
/**
*+------------------
* madong
*+------------------
* Copyright (c) https://gitee.com/motion-code All rights reserved.
*+------------------
* Author: Mr. April (405784684@qq.com)
*+------------------
* Official Website: http://www.madong.tech
*/
namespace app\service\admin\org;
use app\dao\org\DeptDao;
use core\base\BaseService;
use core\exception\handler\AdminException;
use support\Container;
/**
* @method getChildIdsIncludingSelf(mixed $deptId)
*/
class DeptService extends BaseService
{
public function __construct(DeptDao $dao)
{
$this->dao = $dao;
}
/**
* save
*
* @param array $data
*
* @return mixed
* @throws \core\exception\handler\AdminException
*/
public function save(array $data): mixed
{
try {
return $this->transaction(function () use ($data) {
$leaders = $data['leader_id_list'] ?? [];//部门领导
$model = $this->dao->save($data);
if (!empty($leaders)) {
$model->leader()->sync($leaders);
}
return $model;
});
} catch (\Throwable $e) {
throw new AdminException($e->getMessage());
}
}
/**
* update
*
* @param $id
* @param $data
*
* @return void
* @throws \core\exception\handler\AdminException
*/
public function update($id, $data): void
{
try {
$this->transaction(function () use ($id, $data) {
$leaders = $data['leader_id_list'] ?? [];//部门领导
unset($data['leader_id_list']);
$this->dao->update($id, $data);
$model = $this->get($id);
$model->leader()->sync($leaders);
});
} catch (\Throwable $e) {
throw new AdminException($e->getMessage());
}
}
/**
* destroy
*
* @param $id
* @param $force
*
* @return void
* @throws \core\exception\handler\AdminException
*/
public function destroy($id, $force): void
{
try {
$this->transaction(function () use ($id, $force) {
$res = $this->dao->count(['pid' => $id], true);
if ($res > 0) {
throw new AdminException('该部门下存在子部门,请先删除子部门');
}
$this->dao->destroy($id);
$systemDeptLeaderService = Container::make(DeptLeaderService::class);
$systemDeptLeaderService->dao->delete(['dept_id' => $id]);
});
} catch (\Throwable $e) {
throw new AdminException($e->getMessage());
}
}
}