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

101 lines
2.4 KiB
Go
Raw Normal View History

2023-08-03 17:01:28 +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 PageCategoryQuery(query *dto.PageCategoryReqDto) (*dto.PageDto, error) {
d := dbquery.Category
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).FindByPage(query.Offset(), query.Size)
if err != nil {
panic(err)
}
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
}
// 查询草稿列表
func ListCategoryQuery(query *dto.PageCategoryReqDto) ([]*model.Category, error) {
d := dbquery.Category
dq := d.Where()
if query.Name != "" {
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
records, err := dq.Debug().Select(d.ID, d.Name, d.UpdateAt, d.CreatedAt).Find()
if err != nil {
panic(err)
}
return records, nil
}
// 创建草稿
func CreateCategory(dto *dto.CategoryDto) (*model.Category, error) {
if err := checkCategoryInfo(dto.Name); err != nil {
panic(err)
}
d := model.Category{
Name: dto.Name,
Config: dto.Config,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
}
err := dbquery.Category.Save(&d)
if err != nil {
panic(err)
}
return dbquery.Category.Where(dbquery.Category.Name.Eq(dto.Name)).Order(dbquery.Category.CreatedAt).Debug().First()
2023-08-03 17:01:28 +08:00
}
func QueryCategory(id int32) *model.Category {
data, err := dbquery.Category.Where(dbquery.Category.ID.Eq(id)).Debug().First()
if err != nil {
panic(err)
}
return data
}
func UpdateCategory(id int32, dto *dto.CategoryDto) bool {
findOldQuery := dbquery.Category
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
if oldD == nil || err != nil {
panic(err)
}
if dto.Config != "" {
oldD.Config = dto.Config
}
if len(dto.Name) > 0 {
oldD.Name = dto.Name
}
oldD.UpdateAt = time.Now()
_, error := dbquery.Category.Updates(oldD)
if error != nil {
panic(error)
}
return true
}
func DeleteCategoryById(id int) {
_, _ = dbquery.Category.Debug().Where(dbquery.Category.ID.Eq(int32(id))).Delete()
}
func checkCategoryInfo(name string) error {
findNameQuery := dbquery.Category
count, err := findNameQuery.Where(findNameQuery.Name.Eq(name)).Debug().Count()
if err != nil {
panic(err)
}
if count > 0 {
panic("名称已存在")
}
return err
}