Files
guangan/plugs/think-plugs-ticket/src/controller/Ticket.php
2025-03-27 00:48:16 +08:00

510 lines
18 KiB
PHP

<?php
namespace plugin\ticket\controller;
use app\custom\model\CustomMessage;
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\TicketRepair;
use plugin\ticket\model\TicketReply;
use plugin\ticket\model\TicketTicket;
use plugin\ticket\model\TicketType;
use plugin\ticket\model\TicketView;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;
use think\exception\HttpResponseException;
use \PhpOffice\PhpSpreadsheet\IOFactory;
/**
* 工单管理
*/
class Ticket extends Controller
{
/**
* 工单列表
* @auth true
* @menu true
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->title = '工单中心';
$this->type_list = TicketType::getList();
$this->user_id = $this->request->session('user')['id'];
TicketTicket::mQuery()->layTable(function () {
}, function (QueryHelper $query) {
$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', 'type_name', 'last_reply'])->with(['user_shares', 'views', 'repairs', 'verifys', 'view_process', 'repair_process', 'verify_process']);
});
}
/**
* 添加工单
* @auth true
* @menu true
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function add()
{
$this->title = '添加工单';
$this->types = TicketType::mk()->scope('active')->select();
TicketTicket::mForm('form');
}
public function _form_filter(&$data)
{
if ($this->request->isPost()) {
$data['user_type'] = 'admin';
$data['user_id'] = $this->request->session('user')['id'];
}
}
/**
* 查看工单
* @auth true
* @menu true
* @return void
*/
public function detail()
{
$this->title = '工单详情';
['id' => $id] = $this->_vali(['id.require' => '请指定工单ID!']);
$this->vo = TicketTicket::mk()->with(['user_shares', 'views', 'repairs', 'verifys', 'view_process', 'repair_process', 'verify_process'])->append(['imgs_arr', 'type_name'])->find($id);
$this->ticket = $this->vo;
$this->fetch();
}
/**
* 编辑工单
* @auth true
* @menu true
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit()
{
$this->title = '编辑工单';
$this->id = $this->request->get('id');
$this->types = TicketType::mk()->scope('active')->select();
TicketTicket::mForm('form');
}
/**
* 删除工单
* @auth true
* @menu true
* @return void
*/
public function remove()
{
TicketTicket::mDelete();
}
public function reply()
{
$this->title = '回复工单';
[
'ticket_id'=>$ticket_id,
'id'=>$id
] = $this->_vali([
'ticket_id.require'=>'请指定工单ID!',
'id.require'=>'请指定回复ID!'
]);
$this->vo = TicketTicket::mk()->with(['user', 'type'])->append(['imgs_arr', 'type_name'])->find($ticket_id);
TicketReply::mForm('reply');
}
public function _reply_form_filter(&$data)
{
if ($this->request->isPost()) {
$this->_applyFormToken();
$data['type'] = 1;
$adminInfo = $this->request->session('user');
$data['user_id'] = $adminInfo['id'];
$data['username'] = $adminInfo['nickname'];
$data['contact'] = $adminInfo['contact_phone'];
}
}
/**
* 工单分配
* @auth true
* @menu true
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function move()
{
$this->title = "工单分配";
$id = $this->request->param('id');
$ticket = TicketTicket::mk()->where('id', '=', $id)->findOrEmpty();
if ($ticket->isEmpty()) $this->error('工单不存在!');
if ($this->request->isPost()) {
// 提交
$data = $this->_vali([
'id.require' => '请指定工单ID!',
'type.require' => '请指定分配类型!',
'dept_id.require' => '请指定处理部门!',
'user_id.default' => '0',
]);
$ticket->fz_user_id = $data['user_id'];
$ticket->fz_dept_id = $data['dept_id'];
if ($data['type'] == 1) {
// 内部
$ticket->state = 1;
} else {
// 外部
$ticket->state = 2;
}
$ticket->status = 1;
$ticket->save();
$this->success('工单分配成功!');
} else {
$this->dept_list = TicketDept::query()->scope('avail')->select();
$this->user_list = SystemUser::query()->select();
$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', '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();
}
/**
* @return void
* @throws \Exception
* 审核提交
* @auth true
* @menu true
*/
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 view_process_create()
{
$data = $this->_vali([
'ticket_id.require'=>'请指定工单ID!',
]);
$process = ApprovalProcess::where('type', '=', 'HSSH')->order('id', 'asc')->findOrEmpty();
if ($process->isEmpty()) {
$this->error('未找到可用的审核流程');
}
$adminInfo = $this->request->session('user');
$ticket = TicketTicket::query()->with(['user', 'type'])->append(['imgs_arr', 'type_name'])->where('id', '=', $data['ticket_id'])->findOrEmpty();
if ($ticket->isEmpty()) {
$this->error('未找到工单信息');
}
if ($this->request->isPost()) {
$instance_data = $this->_vali([
'title.default'=>'核验工单审核',
'content.default'=>'',
]);
ApprovalInstance::mk()->startTrans();
try {
$instance = ApprovalInstance::create([
'process_id' => $process->id,
'oid' => $ticket->id,
'title' => $instance_data['title'],
'content' => $instance_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,
]);
}
$ticket->view_pid = $instance->id;
$ticket->save();
ApprovalInstance::mk()->commit();
} catch (HttpResponseException $e) {
ApprovalInstance::mk()->rollback();
throw $e;
} catch (\Exception $e) {
ApprovalInstance::mk()->rollback();
$this->error("创建失败");
}
$this->success('创建成功!');
} else {
$this->vo = $ticket;
$this->process = $process;
$this->step_index = -1;
$this->current_step = $process->steps[0];
$this->users = SystemUser::query()->field('id,username,nickname')->select();
$this->fetch();
}
}
public function view_ticket_create()
{
$data = $this->_vali([
'ticket_id.require'=>'请指定工单ID!',
]);
$ticket = TicketTicket::query()->with(['view_process'])->append(['imgs_arr', 'type_name'])->where('id', '=', $data['ticket_id'])->findOrEmpty();
$staffs = InspectionStaff::query()->field('id,name,phone')->select();
if ($ticket->isEmpty()) {
$this->error('未找到工单信息');
}
if ($this->request->isPost()) {
$adminInfo = $this->request->session('user');
$instance_data = $this->_vali([
'staff_id.require'=>'请指定维修人员!',
]);
$staff = InspectionStaff::query()->where('id', '=', $instance_data['staff_id'])->findOrEmpty();
if ($staff->isEmpty()) {
$this->error('未找到维修人员信息');
}
$ticket->gc_content = '';
$ticket->zf_content = '';
$ticket->ys_content = '';
$ticket->work_days = null;
$view = $ticket->views()->save([
'staff_id'=>$instance_data['staff_id'],
'status'=>0,
'create_id'=>$adminInfo['id'],
]);
$staff->messages()->save([
'status'=>0,
'title'=>'您有新的核验工单需要处理',
'content'=>'您有新的核验工单需要处理,请及时处理。',
'type' => 'TICKET_VIEW',
'oid' => $view->id,
]);
$ticket->save();
$this->success('创建成功!', $view);
} else {
$this->vo = $ticket;
$this->staffs = $staffs;
$this->fetch();
}
}
public function my() {
$this->title = '待我审核';
$this->type_list = TicketType::getList();
$this->user_id = $this->request->session('user')['id'];
TicketTicket::mQuery()->layTable(function () {
}, function (QueryHelper $query) {
$query->like(['title|content|contact_name|ticket_address|contact_phone#keyword'])
->dateBetween(['create_at'])
->equal(['status', 'type_id']);
$query->whereIn("id", ApprovalStep::query()->where(['approver_id' => $this->user_id, 'status' => 0, 'approver_type' => 1, 'instance.status' => 0])->field("oid")->select())
->append(['imgs_arr', 'source_type_name', 'type_name', 'last_reply'])->with('approval');
});
}
/**
* @return void
* @auth true
*/
public function import() {
$file = $this->app->request->post('file');
if (!$file) {
// 创建一个临时Excel模板用于下载
$this->redirect('/static/excel/ticket_import_template.xlsx');
};
$file = '.' . str_replace($this->app->request->domain(), '', $file);
//表格字段对应
$fields = [
'A' => 'title',
'B' => 'type_id',
'C' => 'content',
'D' => 'contact_name',
'E' => 'contact_phone',
'F' => 'contact_address',
'G' => 'ticket_address',
];
//加载文件
$spreadsheet = IOFactory::load($file);
$sheet = $spreadsheet->getActiveSheet(); // 获取表格
$highestRow = $sheet->getHighestRow(); // 取得总行数
$sheetData = [];
for ($row = 2; $row <= $highestRow; $row++) { // $row表示从第几行开始读取
foreach ($fields as $cell => $field) {
$value = $sheet->getCell($cell . $row)->getValue();
$value = trim($value);
$sheetData[$row][$field] = $value;
}
}
$sheetData = array_values($sheetData);
foreach ($sheetData as $key => $value) {
if (!empty($value['type_id'])) {
$type = TicketType::query()->where(['id' => $value['type_id']])->find();
if (empty($type)) {
$this->error('工单类型【'.$value['type_id'].'】不存在');
} else {
$sheetData[$key]['type_id'] = $type['id'];
}
}
$sheetData[$key]['create_at'] = date('Y-m-d H:i:s');
$sheetData[$key]['user_id'] = $this->request->session('user')['id'];
$sheetData[$key]['status'] = 0;
}
TicketTicket::mk()->saveAll($sheetData);
$this->success("成功");
}
public function comment()
{
$data = $this->_vali([
'id.require'=>'请指定工单ID!',
]);
$ticket = TicketTicket::query()->with(['user_shares'])->where('id', '=', $data['id'])->findOrEmpty();
if ($ticket->isEmpty()) {
$this->error('未找到工单信息');
}
if (!$ticket->user_shares || sizeof($ticket->user_shares) == 0) {
$this->error("该工单无需评论");
}
if ($this->request->isPost()) {
$adminInfo = $this->request->session('user');
$comment_data = $this->_vali([
'content.require'=>'请输入评论内容!',
]);
TicketTicket::mk()->startTrans();
try {
foreach ($ticket->user_shares as $share) {
$share->logs()->save([
'op_name' => $adminInfo["username"],
'content' => $comment_data['content'],
'result' => "后台用户反馈",
]);
CustomMessage::create([
'user_id' => $share->user_id,
'title' => "随手拍反馈",
'content' => "您的随手拍有新的回复了,请及时查看。",
'type' => 'USER_SHARE',
'oid' => $share->id,
]);
}
TicketTicket::mk()->commit();
} catch (\Exception $e) {
TicketTicket::mk()->rollback();
$this->error("保存失败");
}
} else {
$this->vo = $ticket;
$this->ticket = $ticket;
$this->fetch();
}
}
}