You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
5.4 KiB
186 lines
5.4 KiB
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\service\school_approval;
|
|
|
|
use app\model\school_approval\SchoolApprovalConfig;
|
|
use app\model\school_approval\SchoolApprovalConfigNode;
|
|
use think\Exception;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 审批流配置服务
|
|
* Class SchoolApprovalConfigService
|
|
* @package app\service\school_approval
|
|
*/
|
|
class SchoolApprovalConfigService
|
|
{
|
|
/**
|
|
* 获取审批流配置列表
|
|
* @param array $where
|
|
* @param int $page
|
|
* @param int $limit
|
|
* @return array
|
|
*/
|
|
public function getList(array $where = [], int $page = 1, int $limit = 10): array
|
|
{
|
|
$field = 'id, config_name, description, status, creator_id, created_at, updated_at';
|
|
$order = 'id desc';
|
|
|
|
$list = (new SchoolApprovalConfig())
|
|
->where($where)
|
|
->field($field)
|
|
->order($order)
|
|
->page($page, $limit)
|
|
->select()
|
|
->toArray();
|
|
|
|
$count = (new SchoolApprovalConfig())->where($where)->count();
|
|
|
|
return [
|
|
'list' => $list,
|
|
'count' => $count
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取审批流配置详情
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
public function getInfo(int $id): array
|
|
{
|
|
$info = (new SchoolApprovalConfig())->with(['nodes'])->where(['id' => $id])->find();
|
|
|
|
|
|
if (empty($info)) {
|
|
return [];
|
|
}
|
|
return $info->toArray();
|
|
}
|
|
|
|
/**
|
|
* 添加审批流配置
|
|
* @param array $data
|
|
* @return int
|
|
* @throws \Exception
|
|
*/
|
|
public function add(array $data): int
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
$config = [
|
|
'config_name' => $data['config_name'],
|
|
'description' => $data['description'] ?? '',
|
|
'status' => $data['status'] ?? 1,
|
|
'creator_id' => $data['creator_id']
|
|
];
|
|
|
|
$config_id = (new SchoolApprovalConfig())->insertGetId($config);
|
|
|
|
// 添加节点
|
|
if (!empty($data['nodes'])) {
|
|
$nodes = [];
|
|
foreach ($data['nodes'] as $sequence => $node) {
|
|
$nodes[] = [
|
|
'config_id' => $config_id,
|
|
'node_name' => $node['node_name'],
|
|
'approver_type' => $node['approver_type'],
|
|
'approver_ids' => is_array($node['approver_ids']) ? implode(',', $node['approver_ids']) : $node['approver_ids'],
|
|
'sign_type' => $node['sign_type'] ?? SchoolApprovalConfigNode::SIGN_TYPE_OR,
|
|
'sequence' => $sequence + 1
|
|
];
|
|
}
|
|
|
|
(new SchoolApprovalConfigNode())->insertAll($nodes);
|
|
}
|
|
|
|
Db::commit();
|
|
return $config_id;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
throw new Exception($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 编辑审批流配置
|
|
* @param array $data
|
|
* @return bool
|
|
* @throws \Exception
|
|
*/
|
|
public function edit(array $data): bool
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
$config = [
|
|
'config_name' => $data['config_name'],
|
|
'description' => $data['description'] ?? '',
|
|
'status' => $data['status'] ?? 1
|
|
];
|
|
|
|
(new SchoolApprovalConfig())->where(['id' => $data['id']])->update($config);
|
|
|
|
// 先删除原有节点
|
|
(new SchoolApprovalConfigNode())->where(['config_id' => $data['id']])->delete();
|
|
|
|
// 添加新节点
|
|
if (!empty($data['nodes'])) {
|
|
$nodes = [];
|
|
foreach ($data['nodes'] as $sequence => $node) {
|
|
$nodes[] = [
|
|
'config_id' => $data['id'],
|
|
'node_name' => $node['node_name'],
|
|
'approver_type' => $node['approver_type'],
|
|
'approver_ids' => is_array($node['approver_ids']) ? implode(',', $node['approver_ids']) : $node['approver_ids'],
|
|
'sign_type' => $node['sign_type'] ?? SchoolApprovalConfigNode::SIGN_TYPE_OR,
|
|
'sequence' => $sequence + 1
|
|
];
|
|
}
|
|
|
|
(new SchoolApprovalConfigNode())->insertAll($nodes);
|
|
}
|
|
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
throw new Exception($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除审批流配置
|
|
* @param int $id
|
|
* @return bool
|
|
* @throws \Exception
|
|
*/
|
|
public function delete(int $id): bool
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
// 删除配置
|
|
(new SchoolApprovalConfig())->where(['id' => $id])->delete();
|
|
|
|
// 删除节点
|
|
(new SchoolApprovalConfigNode())->where(['config_id' => $id])->delete();
|
|
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
throw new Exception($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 修改状态
|
|
* @param int $id
|
|
* @param int $status
|
|
* @return bool
|
|
*/
|
|
public function changeStatus(int $id, int $status): bool
|
|
{
|
|
return (new SchoolApprovalConfig())->where(['id' => $id])->update(['status' => $status]) !== false;
|
|
}
|
|
}
|
|
|