123 lines
2.9 KiB
Go
123 lines
2.9 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 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.Code, 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.CategoryReqDto) ([]*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.Code, 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, dto.Code, 0); err != nil {
|
|
panic(err)
|
|
}
|
|
d := model.Category{
|
|
Name: dto.Name,
|
|
Code: dto.Code,
|
|
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()
|
|
}
|
|
|
|
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 {
|
|
if err := checkCategoryInfo(dto.Name, dto.Code, id); err != nil {
|
|
panic(err)
|
|
}
|
|
findOldQuery := dbquery.Category
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(err)
|
|
}
|
|
if dto.Code != "" {
|
|
oldD.Code = dto.Code
|
|
}
|
|
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, code string, id int32) error {
|
|
findNameQuery := dbquery.Category
|
|
w := findNameQuery.Where()
|
|
if id != 0 {
|
|
w = w.Where(findNameQuery.ID.Eq(id))
|
|
}
|
|
if name != "" {
|
|
count, err := w.Where(findNameQuery.Name.Eq(name)).Debug().Count()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if count > 0 {
|
|
panic("名称已存在")
|
|
}
|
|
}
|
|
if code != "" {
|
|
count, err := w.Where(findNameQuery.Code.Eq(code)).Debug().Count()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if count > 0 {
|
|
panic("编码已存在")
|
|
}
|
|
}
|
|
return nil
|
|
}
|