rts-sim-testing-service/service/drafting.go

146 lines
4.2 KiB
Go

package service
import (
"fmt"
"time"
"joylink.club/bj-rtsts-server/db/dbquery"
"joylink.club/bj-rtsts-server/db/model"
"joylink.club/bj-rtsts-server/dto"
"joylink.club/bj-rtsts-server/sys_error"
)
// 查询草稿列表
func PageDraftingQuery(query *dto.PageDraftingReqDto) *dto.PageDto[*dto.DraftingResDto] {
d, u := dbquery.Drafting, dbquery.User
dq := d.LeftJoin(u, u.ID.EqCol(d.CreatorID)).
Select(d.ID, d.Name, d.Category, d.Type, d.CreatorID, d.CreatedAt, d.UpdateAt, u.Name.As("creator")).
Where()
if query.Name != "" {
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
var records []*dto.DraftingResDto
total, err := dq.Debug().Order(d.UpdateAt.Desc()).ScanByPage(&records, query.Offset(), query.Size)
if err != nil {
panic(sys_error.New("草稿查询错误", err))
}
return &dto.PageDto[*dto.DraftingResDto]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
}
func ListDraftingQuery(query *dto.ListDraftingReqDto) []*model.Drafting {
d := dbquery.Drafting
dq := d.Where()
if query.Type != 0 {
dq = dq.Where(d.Type.Eq(query.Type))
}
records, err := dq.Debug().Omit(d.Proto).Order(d.UpdateAt.Desc()).Find()
if err != nil {
panic(sys_error.New("草稿查询错误", err))
}
return records
}
// 创建草稿
func CreateDrafting(createId int32, dd *dto.DraftingDto) *model.Drafting {
if err := checkDraftingInfo(dd.Name); err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
d := model.Drafting{
Name: dd.Name,
Category: dd.Category,
Type: int32(dd.Type),
Proto: dd.Proto,
CreatorID: createId,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
}
err := dbquery.Drafting.Save(&d)
if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
data, err := dbquery.Drafting.Where(
dbquery.Drafting.Name.Eq(dd.Name),
dbquery.Drafting.CreatorID.Eq(createId),
).Order(dbquery.Drafting.CreatedAt).Debug().First()
if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
return data
}
// 另存为
func SaveAsDrafting(createId int32, oldId int32, dd *dto.DraftingDto) *model.Drafting {
if err := checkDraftingInfo(dd.Name); err != nil {
return nil
}
oldD, err := dbquery.Drafting.Where(dbquery.Drafting.ID.Eq(oldId)).Debug().First()
if oldD == nil || err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
newD := model.Drafting{
Name: dd.Name,
Proto: oldD.Proto,
CreatorID: createId,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
Category: oldD.Category,
Type: oldD.Type,
}
if err = dbquery.Drafting.Save(&newD); err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
data, err := dbquery.Drafting.Where(
dbquery.Drafting.Name.Eq(dd.Name),
dbquery.Drafting.CreatorID.Eq(createId),
).Order(dbquery.Drafting.CreatedAt).Debug().First()
if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
return data
}
func QueryDrafting(id int32) *model.Drafting {
query := dbquery.Drafting
data, err := query.Where(query.ID.Eq(id)).Debug().First()
if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
return data
}
func UpdateDrafting(id int32, dd *dto.DraftingDto) bool {
findOldQuery := dbquery.Drafting
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
if oldD == nil || err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
if dd.Proto != nil {
oldD.Proto = dd.Proto
}
if len(dd.Name) > 0 {
oldD.Name = dd.Name
}
oldD.UpdateAt = time.Now()
_, err2 := dbquery.Drafting.Updates(oldD)
if err2 != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err2.Error()})
}
return true
}
func DeleteDraftingById(id int) {
_, _ = dbquery.Drafting.Debug().Where(dbquery.Drafting.ID.Eq(int32(id))).Delete()
}
func checkDraftingInfo(name string) error {
findNameQuery := dbquery.Drafting
count, err := findNameQuery.Where(findNameQuery.Name.Eq(name)).Debug().Count()
if err != nil {
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
}
if count > 0 {
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "名称已存在"})
}
return err
}