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

123 lines
2.9 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)))
}
2023-08-10 17:22:42 +08:00
records, total, err := dq.Debug().Select(d.ID, d.Code, d.Name, d.UpdateAt, d.CreatedAt).FindByPage(query.Offset(), query.Size)
2023-08-03 17:01:28 +08:00
if err != nil {
panic(err)
}
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, nil
}
// 查询草稿列表
func ListCategoryQuery(query *dto.CategoryReqDto) ([]*model.Category, error) {
2023-08-03 17:01:28 +08:00
d := dbquery.Category
dq := d.Where()
if query.Name != "" {
dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name)))
}
2023-08-10 14:18:55 +08:00
records, err := dq.Debug().Select(d.ID, d.Code, d.Name, d.UpdateAt, d.CreatedAt).Find()
2023-08-03 17:01:28 +08:00
if err != nil {
panic(err)
}
return records, nil
}
// 创建草稿
func CreateCategory(dto *dto.CategoryDto) (*model.Category, error) {
2023-08-10 17:22:42 +08:00
if err := checkCategoryInfo(dto.Name, dto.Code, 0); err != nil {
2023-08-03 17:01:28 +08:00
panic(err)
}
d := model.Category{
Name: dto.Name,
2023-08-10 14:18:55 +08:00
Code: dto.Code,
2023-08-03 17:01:28 +08:00
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 {
2023-08-10 17:22:42 +08:00
if err := checkCategoryInfo(dto.Name, dto.Code, id); err != nil {
panic(err)
}
2023-08-03 17:01:28 +08:00
findOldQuery := dbquery.Category
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
if oldD == nil || err != nil {
panic(err)
}
2023-08-10 17:22:42 +08:00
if dto.Code != "" {
oldD.Code = dto.Code
}
2023-08-03 17:01:28 +08:00
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()
}
2023-08-10 17:22:42 +08:00
func checkCategoryInfo(name string, code string, id int32) error {
2023-08-03 17:01:28 +08:00
findNameQuery := dbquery.Category
2023-08-10 17:22:42 +08:00
w := findNameQuery.Where()
if id != 0 {
w = w.Where(findNameQuery.ID.NotIn(id))
2023-08-10 17:22:42 +08:00
}
if name != "" {
count, err := w.Where(findNameQuery.Name.Eq(name)).Debug().Count()
if err != nil {
panic(err)
}
if count > 0 {
panic("名称已存在")
}
2023-08-03 17:01:28 +08:00
}
2023-08-10 17:22:42 +08:00
if code != "" {
count, err := w.Where(findNameQuery.Code.Eq(code)).Debug().Count()
if err != nil {
panic(err)
}
if count > 0 {
panic("编码已存在")
}
2023-08-03 17:01:28 +08:00
}
2023-08-10 17:22:42 +08:00
return nil
2023-08-03 17:01:28 +08:00
}