- 修复登录参数 username→account - 修复前后端字段名不匹配 (ticketno/contactname/createtime等) - 修复AI分析GLM返回markdown包裹和priority类型问题 - 添加AutoMigrate自动建表 - 统一API路由为 /api/auth/ 前缀 - 添加config.example.yaml,.gitignore排除config.yaml
150 lines
3.9 KiB
Go
150 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/casehub/ticket-workbench/internal/model"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type TicketListResult struct {
|
|
Total int64 `json:"total"`
|
|
Rows []model.TicketInfo `json:"rows"`
|
|
}
|
|
|
|
func ListTickets(db *gorm.DB, status *int16, category string, priority *int16, keyword string, page, pageSize int) (*TicketListResult, error) {
|
|
var total int64
|
|
var tickets []model.TicketInfo
|
|
|
|
query := db.Model(&model.TicketInfo{})
|
|
|
|
if status != nil {
|
|
query = query.Where("status = ?", *status)
|
|
}
|
|
if category != "" {
|
|
query = query.Where("category = ?", category)
|
|
}
|
|
if priority != nil {
|
|
query = query.Where("priority = ?", *priority)
|
|
}
|
|
if keyword != "" {
|
|
query = query.Where("title LIKE ? OR content LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
|
|
}
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
offset := (page - 1) * pageSize
|
|
if err := query.Offset(offset).Limit(pageSize).Order("createtime DESC").Find(&tickets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &TicketListResult{Total: total, Rows: tickets}, nil
|
|
}
|
|
|
|
func GetTicketByID(db *gorm.DB, ticketid int) (*model.TicketInfo, error) {
|
|
var ticket model.TicketInfo
|
|
err := db.Where("ticketid = ?", ticketid).First(&ticket).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ticket, nil
|
|
}
|
|
|
|
func CreateTicket(db *gorm.DB, title, content, contactname, contactphone, source, category string, priority int16, submitterid int) (*model.TicketInfo, error) {
|
|
ticketno := fmt.Sprintf("TKT%s", uuid.New().String()[:8])
|
|
now := time.Now()
|
|
|
|
ticket := &model.TicketInfo{
|
|
Ticketno: ticketno,
|
|
Title: title,
|
|
Content: content,
|
|
Contactname: contactname,
|
|
Contactphone: contactphone,
|
|
Source: source,
|
|
Category: category,
|
|
Priority: priority,
|
|
Submitterid: submitterid,
|
|
Status: 0,
|
|
Createtime: now,
|
|
Updatetime: now,
|
|
}
|
|
|
|
if err := db.Create(ticket).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
AddOperationLog(db, ticket.Ticketid, submitterid, "create", "创建工单")
|
|
|
|
return ticket, nil
|
|
}
|
|
|
|
func UpdateTicket(db *gorm.DB, ticketid int, title, content, contactname, contactphone, category string, priority int16, handlerid *int, operatorid int) error {
|
|
updates := map[string]interface{}{
|
|
"updatetime": time.Now(),
|
|
}
|
|
if title != "" {
|
|
updates["title"] = title
|
|
}
|
|
if content != "" {
|
|
updates["content"] = content
|
|
}
|
|
if contactname != "" {
|
|
updates["contactname"] = contactname
|
|
}
|
|
if contactphone != "" {
|
|
updates["contactphone"] = contactphone
|
|
}
|
|
if category != "" {
|
|
updates["category"] = category
|
|
}
|
|
if priority >= 0 {
|
|
updates["priority"] = priority
|
|
}
|
|
if handlerid != nil {
|
|
updates["handlerid"] = handlerid
|
|
}
|
|
|
|
if err := db.Model(&model.TicketInfo{}).Where("ticketid = ?", ticketid).Updates(updates).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
AddOperationLog(db, ticketid, operatorid, "update", "更新工单信息")
|
|
|
|
return nil
|
|
}
|
|
|
|
func UpdateTicketStatus(db *gorm.DB, ticketid int, status int16, operatorid int) error {
|
|
if err := db.Model(&model.TicketInfo{}).Where("ticketid = ?", ticketid).Updates(map[string]interface{}{
|
|
"status": status,
|
|
"updatetime": time.Now(),
|
|
}).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
statusText := map[int16]string{0: "待处理", 1: "分析中", 2: "已确认", 3: "处理中", 4: "已关闭"}
|
|
AddOperationLog(db, ticketid, operatorid, "status", "状态变更为: "+statusText[status])
|
|
|
|
return nil
|
|
}
|
|
|
|
func AddOperationLog(db *gorm.DB, ticketid, operatorid int, action, detail string) error {
|
|
log := &model.TicketOperationLog{
|
|
Ticketid: ticketid,
|
|
Operatorid: operatorid,
|
|
Action: action,
|
|
Detail: detail,
|
|
Createtime: time.Now(),
|
|
}
|
|
return db.Create(log).Error
|
|
}
|
|
|
|
func GetOperationLogs(db *gorm.DB, ticketid int) ([]model.TicketOperationLog, error) {
|
|
var logs []model.TicketOperationLog
|
|
err := db.Where("ticketid = ?", ticketid).Order("createtime ASC").Find(&logs).Error
|
|
return logs, err
|
|
}
|