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

120 lines
2.9 KiB
Go
Raw Normal View History

2023-07-14 16:47:59 +08:00
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"
)
// 查询草稿列表
func PageDraftingQuery(query *dto.PageDraftingReqDto) (*dto.PageDto, error) {
d := dbquery.Drafting
dq := d.Where()
if query.Name != "" {
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
2023-08-28 14:16:14 +08:00
records, total, err := dq.Debug().Omit(d.Proto).FindByPage(query.Offset(), query.Size)
2023-07-14 16:47:59 +08:00
if err != nil {
panic(err)
}
2023-07-28 14:13:03 +08:00
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
2023-07-14 16:47:59 +08:00
}
// 创建草稿
2023-07-28 14:13:03 +08:00
func CreateDrafting(createId int32, dto *dto.DraftingDto) (*model.Drafting, error) {
if err := checkDraftingInfo(dto.Name); err != nil {
panic(err)
}
2023-07-14 16:47:59 +08:00
d := model.Drafting{
Name: dto.Name,
2023-08-25 15:37:29 +08:00
Category: dto.Category,
Type: int32(dto.Type),
2023-07-14 16:47:59 +08:00
Proto: dto.Proto,
CreatorID: createId,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
}
2023-07-28 14:13:03 +08:00
err := dbquery.Drafting.Save(&d)
if err != nil {
panic(err)
}
return dbquery.Drafting.Where(
dbquery.Drafting.Name.Eq(dto.Name),
dbquery.Drafting.CreatorID.Eq(createId),
).Order(dbquery.Drafting.CreatedAt).Debug().First()
2023-07-14 16:47:59 +08:00
}
// 另存为
2023-07-28 14:13:03 +08:00
func SaveAsDrafting(createId int32, oldId int32, dto *dto.DraftingDto) (*model.Drafting, error) {
if err := checkDraftingInfo(dto.Name); err != nil {
return nil, err
2023-07-14 16:47:59 +08:00
}
2023-07-28 14:13:03 +08:00
oldD, err := dbquery.Drafting.Where(dbquery.Drafting.ID.Eq(oldId)).Debug().First()
2023-07-14 16:47:59 +08:00
if oldD == nil || err != nil {
panic(err)
}
newD := model.Drafting{
Name: dto.Name,
Proto: oldD.Proto,
CreatorID: createId,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
2023-08-03 17:01:28 +08:00
Category: oldD.Category,
2023-07-14 16:47:59 +08:00
}
2023-07-28 14:13:03 +08:00
if err = dbquery.Drafting.Save(&newD); err != nil {
panic(err)
}
return dbquery.Drafting.Where(
dbquery.Drafting.Name.Eq(dto.Name),
dbquery.Drafting.CreatorID.Eq(createId),
).Order(dbquery.Drafting.CreatedAt).Debug().First()
2023-07-14 16:47:59 +08:00
}
2023-07-14 18:09:07 +08:00
2023-07-28 14:13:03 +08:00
func QueryDrafting(id int32) *model.Drafting {
2023-07-14 18:09:07 +08:00
query := dbquery.Drafting
data, err := query.Where(query.ID.Eq(id)).Debug().First()
if err != nil {
panic(err)
}
2023-07-28 14:13:03 +08:00
return data
2023-07-14 18:09:07 +08:00
}
2023-07-28 14:13:03 +08:00
func UpdateDrafting(id int32, dto *dto.DraftingDto) bool {
2023-07-14 18:09:07 +08:00
findOldQuery := dbquery.Drafting
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
if oldD == nil || err != nil {
panic(err)
}
if dto.Proto != nil {
oldD.Proto = dto.Proto
}
if len(dto.Name) > 0 {
oldD.Name = dto.Name
}
oldD.UpdateAt = time.Now()
2023-08-28 14:16:14 +08:00
_, err2 := dbquery.Drafting.Updates(oldD)
if err2 != nil {
panic(err2)
2023-07-28 14:13:03 +08:00
}
return true
2023-07-14 18:09:07 +08:00
}
func DeleteDraftingById(id int) {
_, _ = dbquery.Drafting.Debug().Where(dbquery.Drafting.ID.Eq(int32(id))).Delete()
}
2023-07-28 14:13:03 +08:00
func checkDraftingInfo(name string) error {
findNameQuery := dbquery.Drafting
count, err := findNameQuery.Where(findNameQuery.Name.Eq(name)).Debug().Count()
if err != nil {
panic(err)
}
if count > 0 {
panic("名称已存在")
}
return err
}