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

59 lines
1.4 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)))
}
records, total, err := dq.Debug().FindByPage(query.Offset(), query.Size)
if err != nil {
panic(err)
}
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, err
}
// 创建草稿
func CreateDrafting(createId int32, dto *dto.DraftingDto) error {
d := model.Drafting{
Name: dto.Name,
Proto: dto.Proto,
CreatorID: createId,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
}
return dbquery.Drafting.Save(&d)
}
// 另存为
func SaveAsDrafting(createId int32, oldId int32, dto *dto.DraftingDto) error {
findNameQuery := dbquery.Drafting
count, err := findNameQuery.Where(findNameQuery.Name.Eq(dto.Name)).Debug().Count()
if count > 0 || err != nil {
panic("名称已存在")
}
findOldQuery := dbquery.Drafting
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(oldId)).Debug().First()
if oldD == nil || err != nil {
panic(err)
}
newD := model.Drafting{
Name: dto.Name,
Proto: oldD.Proto,
CreatorID: createId,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
}
return dbquery.Drafting.Save(&newD)
}