117 lines
2.8 KiB
Go
117 lines
2.8 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"
|
|
)
|
|
|
|
// 查询草稿列表
|
|
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().Select(d.ID, d.Name, d.UpdateAt, d.CreatedAt, d.CreatorID).FindByPage(query.Offset(), query.Size)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
|
|
}
|
|
|
|
// 创建草稿
|
|
func CreateDrafting(createId int32, dto *dto.DraftingDto) (*model.Drafting, error) {
|
|
if err := checkDraftingInfo(dto.Name); err != nil {
|
|
panic(err)
|
|
}
|
|
d := model.Drafting{
|
|
Name: dto.Name,
|
|
Proto: dto.Proto,
|
|
CreatorID: createId,
|
|
CreatedAt: time.Now(),
|
|
UpdateAt: time.Now(),
|
|
}
|
|
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()
|
|
}
|
|
|
|
// 另存为
|
|
func SaveAsDrafting(createId int32, oldId int32, dto *dto.DraftingDto) (*model.Drafting, error) {
|
|
if err := checkDraftingInfo(dto.Name); err != nil {
|
|
return nil, err
|
|
}
|
|
oldD, err := dbquery.Drafting.Where(dbquery.Drafting.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(),
|
|
}
|
|
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()
|
|
}
|
|
|
|
func QueryDrafting(id int32) *model.Drafting {
|
|
query := dbquery.Drafting
|
|
data, err := query.Where(query.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return data
|
|
}
|
|
|
|
func UpdateDrafting(id int32, dto *dto.DraftingDto) bool {
|
|
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()
|
|
_, error := dbquery.Drafting.Updates(oldD)
|
|
if error != nil {
|
|
panic(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(err)
|
|
}
|
|
if count > 0 {
|
|
panic("名称已存在")
|
|
}
|
|
return err
|
|
}
|