121 lines
3.5 KiB
Go
121 lines
3.5 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 {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
|
}
|
|
|
|
// 查询草稿列表
|
|
func ListCategoryQuery(query *dto.CategoryReqDto) []*model.Category {
|
|
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(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return records
|
|
}
|
|
|
|
// 创建草稿
|
|
func CreateCategory(cd *dto.CategoryDto) *model.Category {
|
|
if err := checkCategoryInfo(cd.Name, cd.Code, 0); err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
d := model.Category{
|
|
Name: cd.Name,
|
|
Code: cd.Code,
|
|
Config: cd.Config,
|
|
CreatedAt: time.Now(),
|
|
UpdateAt: time.Now(),
|
|
}
|
|
err := dbquery.Category.Save(&d)
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
data, err := dbquery.Category.Where(dbquery.Category.Name.Eq(cd.Name)).Order(dbquery.Category.CreatedAt).Debug().First()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
func QueryCategory(id int32) *model.Category {
|
|
data, err := dbquery.Category.Where(dbquery.Category.ID.Eq(id)).Debug().First()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
return data
|
|
}
|
|
|
|
func UpdateCategory(id int32, cd *dto.CategoryDto) bool {
|
|
if err := checkCategoryInfo(cd.Name, cd.Code, id); err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
findOldQuery := dbquery.Category
|
|
oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(id)).Debug().First()
|
|
if oldD == nil || err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
oldD.Code = cd.Code
|
|
oldD.Config = cd.Config
|
|
oldD.Name = cd.Name
|
|
oldD.UpdateAt = time.Now()
|
|
_, error := dbquery.Category.Updates(oldD)
|
|
if error != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.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.NotIn(id))
|
|
}
|
|
if name != "" {
|
|
count, err := w.Where(findNameQuery.Name.Eq(name)).Debug().Count()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
if count > 0 {
|
|
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "名称已存在"})
|
|
}
|
|
}
|
|
if code != "" {
|
|
count, err := w.Where(findNameQuery.Code.Eq(code)).Debug().Count()
|
|
if err != nil {
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
|
}
|
|
if count > 0 {
|
|
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "编码已存在"})
|
|
}
|
|
}
|
|
return nil
|
|
}
|