211 lines
6.0 KiB
PHP
211 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\Tickets;
|
|
use app\common\model\CompanyOperators;
|
|
use app\common\model\Companys;
|
|
use app\common\model\User;
|
|
|
|
class TicketService
|
|
{
|
|
/**
|
|
* 获取工单列表业务逻辑
|
|
* @param array $params 过滤参数
|
|
* @param int $page 页码
|
|
* @param int $limit 每页数量
|
|
* @param string $order 排序方式
|
|
*/
|
|
public function queryTickets(array $params, int $page, int $limit, string $order)
|
|
{
|
|
try {
|
|
// 查询构造器
|
|
$query = Tickets::with(['ticketStaff', 'ticketCompany', 'ticketOperator']);
|
|
|
|
$equalFilters = ['operator_id', 'user_id', 'status'];
|
|
foreach ($equalFilters as $field) {
|
|
if (isset($params[$field]) && $params[$field] !== '') {
|
|
$query->where($field, $params[$field]);
|
|
}
|
|
}
|
|
|
|
if (!empty($params['searchKeyword'])) { // 模糊搜索(标题,备注)
|
|
$query->where(function ($q) use ($params) {
|
|
$q->where('website', 'like', '%' . $params['searchKeyword'] . '%')
|
|
->whereOr('remark', 'like', '%' . $params['searchKeyword'] . '%');
|
|
});
|
|
}
|
|
|
|
$list = $query->order('create_time', $order)
|
|
->hidden(['ticketStaff', 'ticketCompany', 'ticketOperator', 'company_id', 'operator_id', 'user_id'])
|
|
->append(['username', 'company_name', 'operator_name'])
|
|
->paginate(
|
|
$limit,
|
|
false,
|
|
[
|
|
'page' => $page,
|
|
'query' => $params
|
|
]
|
|
);
|
|
|
|
return $list;
|
|
} catch (\Exception $e) { //数据库连接失败,表不存在
|
|
throw new \Exception('查询工单列表失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 新增工单
|
|
*
|
|
*/
|
|
public function createTicket(array $data)
|
|
{
|
|
if (isset($data['website']) && $data['website'] === '') { // isset检查键存在且不为null
|
|
throw new \Exception('网站不能为空');
|
|
}
|
|
|
|
$website = trim($data['website']);
|
|
|
|
$unfinishedTicket = Tickets::where('website', $website) // 查找是否有未完成的同网站工单
|
|
->whereIn('status', [0, 1])
|
|
->find();
|
|
|
|
if ($unfinishedTicket) {
|
|
throw new \Exception('已存在未完成的同网站工单,无法创建');
|
|
}
|
|
|
|
// 当前无相同网站
|
|
$ticketResult = Tickets::create($data);
|
|
|
|
// 通知前端实时刷新(静默,广播服务不可用不影响主流程)
|
|
TicketNotifier::notify($ticketResult->id, 'create');
|
|
|
|
return $ticketResult;
|
|
}
|
|
|
|
|
|
/**
|
|
* 更新工单
|
|
* @param $id 工单id
|
|
* @param $data 外部传入的更新数据
|
|
* @param $uid 当前登录用户id
|
|
* @param $role 当前登录用户角色
|
|
* @return array
|
|
*/
|
|
public function updateTicket(int $id, array $data, array $allowFields, $isRole, $isSuper = false)
|
|
{
|
|
$ticket = Tickets::where('id', $id)->find();
|
|
|
|
if (!$ticket) {
|
|
throw new \Exception('工单不存在');
|
|
}
|
|
|
|
// 提取 $allowFields 允许修改的字段
|
|
$updateData = [];
|
|
foreach ($allowFields as $field) {
|
|
if (array_key_exists($field, $data)) {
|
|
$updateData[$field] = $data[$field];
|
|
}
|
|
}
|
|
|
|
// 如果没有有效数据需要更新
|
|
if (empty($updateData)) {
|
|
throw new \Exception('没有合法的更新字段');
|
|
}
|
|
|
|
|
|
return $ticket->save($updateData);
|
|
}
|
|
|
|
/**
|
|
* 获取所有运营人员名称 (下拉筛选用)-仅限后台使用
|
|
*
|
|
*/
|
|
public function getAllOperators()
|
|
{
|
|
$operators = CompanyOperators::with('company')->select();
|
|
|
|
$operators->each(function ($item) {
|
|
$item->hidden(['company_id', 'company', 'create_time', 'status']);
|
|
|
|
$item->append(['company_name']);
|
|
return $item;
|
|
});
|
|
|
|
return $operators;
|
|
}
|
|
|
|
|
|
public function TestgetAllOperators()
|
|
{
|
|
$operators = Companys::with('companyoperators')->select();
|
|
return $operators;
|
|
}
|
|
|
|
|
|
/**
|
|
* 统计各状态工单数量
|
|
*/
|
|
public function getTicketDashboardStats()
|
|
{
|
|
return [
|
|
'total' => Tickets::count(),
|
|
'status_counts' => Tickets::field('status, COUNT(*) as count')
|
|
->group('status')
|
|
->select()
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 批量更新工单(事务内执行)
|
|
*/
|
|
public function batchUpdateTickets(array $ids, array $data, array $allowFields, $uid, $isSuper = false)
|
|
{
|
|
if (empty($ids)) {
|
|
throw new \Exception('请选择要更新的工单');
|
|
}
|
|
|
|
$updateData = [];
|
|
foreach ($allowFields as $field) {
|
|
if (array_key_exists($field, $data)) {
|
|
$updateData[$field] = $data[$field];
|
|
}
|
|
}
|
|
|
|
if (empty($updateData)) {
|
|
throw new \Exception('没有合法的更新字段');
|
|
}
|
|
|
|
$tickets = Tickets::whereIn('id', $ids)->select();
|
|
|
|
if ($tickets->isEmpty()) {
|
|
throw new \Exception('未找到任何工单');
|
|
}
|
|
|
|
\think\facade\Db::transaction(function () use ($tickets, $updateData, $uid, $isSuper) {
|
|
foreach ($tickets as $ticket) {
|
|
if (!$isSuper) {
|
|
if (!is_null($ticket->user_id) && $ticket->user_id != $uid) {
|
|
throw new \Exception("工单 #{$ticket->id} 没有权限修改");
|
|
}
|
|
}
|
|
$ticket->save($updateData);
|
|
}
|
|
});
|
|
|
|
return ['updated_count' => count($tickets)];
|
|
}
|
|
|
|
public function getStaffList()
|
|
{
|
|
$map = [
|
|
'role' => ['user', 'admin'],
|
|
'status' => 1
|
|
];
|
|
|
|
$staffs = User::where($map)->field('id, username', 'real_name')->select();
|
|
return $staffs;
|
|
}
|
|
}
|