This commit is contained in:
2025-03-14 09:27:29 +08:00
parent a51f77bbf5
commit fbcfef4ff0
12 changed files with 768 additions and 5 deletions

View File

@ -3,6 +3,9 @@
namespace plugin\ticket\controller;
use plugin\inspection\model\InspectionStaff;
use plugin\ticket\model\ApprovalInstance;
use plugin\ticket\model\ApprovalProcess;
use plugin\ticket\model\ApprovalStep;
use plugin\ticket\model\TicketDept;
use plugin\ticket\model\TicketReply;
use plugin\ticket\model\TicketTicket;
@ -10,6 +13,7 @@ use plugin\ticket\model\TicketType;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;
use think\exception\HttpResponseException;
/**
* 工单管理
@ -36,7 +40,7 @@ class Ticket extends Controller
$query->like(['title|content|contact_name|ticket_address|contact_phone#keyword'])
->dateBetween(['create_at'])
->equal(['status', 'type_id']);
$query->append(['imgs_arr', 'source_type_name', 'status_text', 'type_name', 'last_reply']);
$query->append(['imgs_arr', 'source_type_name', 'status_text', 'type_name', 'last_reply'])->with('approval');
});
}
@ -61,7 +65,8 @@ class Ticket extends Controller
if ($this->request->isPost()) {
$data['user_id'] = 0;
}
$data['status'] = 0;
ApprovalInstance::query()->where('oid', '=', $data['id'])->delete();
$data['status'] = -1;
}
/**
@ -90,6 +95,7 @@ class Ticket extends Controller
public function edit()
{
$this->title = '编辑工单';
$this->id = $this->request->get('id');
$this->types = TicketType::mk()->scope('active')->select();
TicketTicket::mForm('form');
}
@ -162,4 +168,160 @@ class Ticket extends Controller
$this->fetch();
}
}
/**
* 审核页面
* @auth true
* @menu true
*/
public function approve()
{
$this->title = '审核流程';
[
'id'=>$id
] = $this->_vali([
'id.require'=>'请指定回复ID!'
]);
$this->vo = TicketTicket::query()->with(['user', 'type'])->append(['imgs_arr', 'status_text', 'type_name'])->find($id);
$process = ApprovalProcess::where('type', '=', 'GDSH')->order('id', 'asc')->findOrEmpty();
if ($process->isEmpty()) {
$this->error('未找到可用的审核流程');
}
$this->process = $process;
$instance = ApprovalInstance::query()->with(['steps', 'logs'])->where('process_id', '=', $process->id)->where('oid', '=', $id)->findOrEmpty();
$this->instance = $instance;
if ($instance->isEmpty()) {
$this->step_index = -1;
$this->current_step = $process->steps[0];
} else {
$this->step_index = $instance->current_step;
if (sizeof($instance->steps) <= ($instance->current_step + 1)) {
$this->current_step = ['approver_type' => 0];
} else {
$this->current_step = $instance->steps[$instance->current_step + 1];
}
}
$this->users = SystemUser::query()->field('id,username,nickname')->select();
$this->fetch();
}
public function create_approval()
{
$data = $this->_vali([
'id.require'=>'请指定工单ID!',
'title.default'=>'工单审核',
'content.default'=>'',
]);
$process = ApprovalProcess::where('type', '=', 'GDSH')->order('id', 'asc')->findOrEmpty();
if ($process->isEmpty()) {
$this->error('未找到可用的审核流程');
}
$adminInfo = $this->request->session('user');
ApprovalInstance::mk()->startTrans();
try {
$instance = ApprovalInstance::create([
'process_id' => $process->id,
'oid' => $data['id'],
'title' => $data['title'],
'content' => $data['content'],
'status' => 0,
'current_step' => 0,
'create_time' => date('Y-m-d H:i:s'),
'create_by' => $adminInfo['id'],
'create_name' => $adminInfo['username'],
]);
foreach ($process->steps as $index => $step) {
$approver_id = $step['approver_id'] ?? 0;
if ($index == 0) {
if ($step['approver_type'] == 3) {
$approve_data = $this->_vali([
'approver_id.require' => '请指定审核人!',
]);
$approver_id = $approve_data['approver_id'];
}
}
ApprovalStep::create([
'instance_id' => $instance->id,
'step_number' => $index,
'approver_type' => $step['approver_type'],
'approver_id' => $approver_id,
'status' => 0,
]);
}
ApprovalInstance::mk()->commit();
} catch (HttpResponseException $e) {
ApprovalInstance::mk()->rollback();
throw $e;
} catch (\Exception $e) {
ApprovalInstance::mk()->rollback();
// $this->error("创建失败");
throw $e;
}
$this->success('创建成功!');
}
public function do_approve()
{
$data = $this->_vali([
'id.require'=>'请指定工单ID!',
'instance_id.require'=>'请指定当前审核ID!',
'step_index.require'=>'请指定当前步骤!',
'status.require'=>'请指定审核结果!',
'content.default'=>'',
]);
$ticket = TicketTicket::query()->findOrEmpty($data['id']);
if ($ticket->isEmpty()) {
$this->error('工单不存在!');
}
$adminInfo = $this->request->session('user');
$instance = ApprovalInstance::query()->with('steps')->findOrEmpty($data['instance_id']);
if ($instance->isEmpty()) {
$this->error('审核流程不存在!');
}
if ($instance->status == 1) {
$this->error('工单已通过,请勿重复操作!');
} else if ($instance->status == 2) {
$this->error('工单已驳回,请勿重复操作!');
}
if ($instance->current_step != $data['step_index']) {
$this->error('当前步骤不正确!');
}
if ($instance->current->approver_id != $adminInfo['id']) {
$this->error('您不是当前审核人,请勿操作!');
}
ApprovalInstance::mk()->startTrans();
try {
$instance->current->status = $data['status'];
$instance->current->content = $data['content'];
$instance->current->save();
if ($data['status'] == 2) {
$instance->status = 2;
} else {
$instance->current_step++;
if ($instance->current_step >= count($instance->steps)) {
$instance->status = 1;
$ticket->status = 0;
} else {
$step = $instance->steps[$instance->current_step];
if ($step['approver_type'] == 3) {
$approve_data = $this->_vali([
'approver_id.require' => '请指定审核人!',
]);
$step->approver_id = $approve_data['approver_id'];
$step->save();
}
}
}
$instance->save();
ApprovalInstance::mk()->commit();
} catch (HttpResponseException $e) {
ApprovalInstance::mk()->rollback();
throw $e;
} catch (\Exception $e) {
ApprovalInstance::mk()->rollback();
// $this->error("审核失败");
throw $e;
}
$this->success('审核成功!');
}
}