122 lines
3.6 KiB
Go
122 lines
3.6 KiB
Go
package gas
|
|
|
|
import (
|
|
"git.apinb.com/bsm-sdk/core/errcode"
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func ticketQuery(gasID uint64) *gorm.DB {
|
|
return common.ActiveRecords(impl.DBService.Model(&models.CsTicket{})).
|
|
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = cs_ticket.user_account_id AND user_service_relation.status <> ?",
|
|
common.StatusArchived).
|
|
Where("user_service_relation.gas_basic_id = ?", gasID)
|
|
}
|
|
|
|
func ListTicket(ctx *gin.Context) {
|
|
station, ok := currentGas(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
listScoped(ctx, &models.CsTicket{}, ticketQuery(station.ID), "cs_ticket.created_at desc")
|
|
}
|
|
|
|
func GetTicket(ctx *gin.Context) {
|
|
station, ok := currentGas(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var ticket models.CsTicket
|
|
respondScopedRecord(ctx, ticketQuery(station.ID).Where("cs_ticket.identity = ?", ctx.Param("identity")), &ticket)
|
|
}
|
|
|
|
type ticketRequest struct {
|
|
UserAccountIdentity string `json:"user_account_identity" binding:"required"`
|
|
TicketNo string `json:"ticket_no" binding:"required,max=64"`
|
|
Category string `json:"category" binding:"required,max=64"`
|
|
Priority string `json:"priority" binding:"required,max=16"`
|
|
}
|
|
|
|
func CreateTicket(ctx *gin.Context) {
|
|
station, ok := currentGas(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var request ticketRequest
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
user, _, ok := requireUser(ctx, request.UserAccountIdentity, station.ID)
|
|
if !ok {
|
|
return
|
|
}
|
|
ticket := models.CsTicket{
|
|
Entity: common.NewEntity(common.StatusEnable), TicketStatus: common.StatusOpen,
|
|
TicketNo: request.TicketNo, UserAccountID: user.ID, Category: request.Category, Priority: request.Priority,
|
|
}
|
|
if err := impl.DBService.Create(&ticket).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
common.RespondCreatedResource(ctx, ticket)
|
|
}
|
|
|
|
func UpdateTicket(ctx *gin.Context) {
|
|
station, ok := currentGas(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var ticket models.CsTicket
|
|
if err := ticketQuery(station.ID).Where("cs_ticket.identity = ?", ctx.Param("identity")).First(&ticket).Error; err != nil {
|
|
common.RespondRecordError(ctx, err)
|
|
return
|
|
}
|
|
var request ticketRequest
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
user, _, ok := requireUser(ctx, request.UserAccountIdentity, station.ID)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := impl.DBService.Model(&ticket).Updates(map[string]any{
|
|
"user_account_id": user.ID, "ticket_no": request.TicketNo, "category": request.Category, "priority": request.Priority,
|
|
}).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"updated": true})
|
|
}
|
|
|
|
func UpdateTicketStatus(ctx *gin.Context) {
|
|
station, ok := currentGas(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var count int64
|
|
if err := ticketQuery(station.ID).Where("cs_ticket.identity = ?", ctx.Param("identity")).Count(&count).Error; err != nil || count != 1 {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
common.UpdateRecordStatus(ctx, &models.CsTicket{})
|
|
}
|
|
|
|
func ArchiveTicket(ctx *gin.Context) {
|
|
station, ok := currentGas(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var count int64
|
|
if err := ticketQuery(station.ID).Where("cs_ticket.identity = ?", ctx.Param("identity")).Count(&count).Error; err != nil || count != 1 {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
common.ArchiveRecord(ctx, &models.CsTicket{})
|
|
}
|