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

67 lines
2.1 KiB
Go

package service
import (
"fmt"
"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/dto/publishedGi"
"time"
)
func PageQueryPublishedGi(req *publishedGi.PublishedGiReqDto) *dto.PageDto {
where := dbquery.PublishedGi.Where()
if req.Name != "" {
where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
result, count, err := where.Debug().FindByPage(req.Offset(), req.Size)
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
return &dto.PageDto{
Total: int(count),
PageQueryDto: req.PageQueryDto,
Records: publishedGi.ConvertFromSlice(result),
}
}
func ListQueryPublishedGi(req *publishedGi.PublishedGiReqDto) []*publishedGi.PublishedGiDto {
where := dbquery.PublishedGi.Where()
if req.Name != "" {
where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
find, err := where.Debug().Find()
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
return publishedGi.ConvertFromSlice(find)
}
func GetPublishedGiById(id int) (*model.PublishedGi, error) {
return dbquery.PublishedGi.Where(dbquery.PublishedGi.ID.Eq(int32(id))).Debug().First()
}
func PublishFormDraft(req *publishedGi.PublishReqDto, user *model.User) {
draft := QueryDrafting(req.DraftId)
if draft.Proto == nil || len(draft.Proto) == 0 {
panic(fmt.Sprintf("草稿[%v]绘图数据信息为空", req.DraftId))
}
entity := model.PublishedGi{
Name: req.Name,
Proto: draft.Proto,
UserID: user.ID,
PublishAt: time.Now(),
}
//删除同名数据
dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Name.Eq(req.Name)).Delete()
//插入新数据
err := dbquery.PublishedGi.Debug().Create(&entity)
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: fmt.Sprintf("数据创建失败:\n%s", err.Error())})
}
}
func DeletePublishedGiById(id int) {
_, _ = dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.ID.Eq(int32(id))).Delete()
}