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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 查询草稿列表
|
2024-02-23 09:22:07 +08:00
|
|
|
func PageCategoryQuery(query *dto.PageCategoryReqDto) *dto.PageDto[*model.Category] {
|
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 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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
2024-02-23 09:22:07 +08:00
|
|
|
return &dto.PageDto[*model.Category]{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询草稿列表
|
2023-10-20 18:08:06 +08:00
|
|
|
func ListCategoryQuery(query *dto.CategoryReqDto) []*model.Category {
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
return records
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建草稿
|
2023-10-20 18:08:06 +08:00
|
|
|
func CreateCategory(cd *dto.CategoryDto) *model.Category {
|
2023-08-30 18:05:11 +08:00
|
|
|
if err := checkCategoryInfo(cd.Name, cd.Code, 0); err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
|
|
|
d := model.Category{
|
2023-08-30 18:05:11 +08:00
|
|
|
Name: cd.Name,
|
|
|
|
Code: cd.Code,
|
|
|
|
Config: cd.Config,
|
2023-08-03 17:01:28 +08:00
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdateAt: time.Now(),
|
|
|
|
}
|
|
|
|
err := dbquery.Category.Save(&d)
|
|
|
|
if err != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
2023-10-20 18:08:06 +08:00
|
|
|
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
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2023-08-30 18:05:11 +08:00
|
|
|
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()})
|
2023-08-10 17:22:42 +08:00
|
|
|
}
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
2023-08-31 16:16:18 +08:00
|
|
|
oldD.Code = cd.Code
|
|
|
|
oldD.Config = cd.Config
|
|
|
|
oldD.Name = cd.Name
|
2023-08-03 17:01:28 +08:00
|
|
|
oldD.UpdateAt = time.Now()
|
|
|
|
_, error := dbquery.Category.Updates(oldD)
|
|
|
|
if error != nil {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-03 17:01:28 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-08-25 17:49:09 +08:00
|
|
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-10 17:22:42 +08:00
|
|
|
}
|
|
|
|
if count > 0 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "名称已存在"})
|
2023-08-10 17:22:42 +08:00
|
|
|
}
|
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 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.QueryDBError, Message: err.Error()})
|
2023-08-10 17:22:42 +08:00
|
|
|
}
|
|
|
|
if count > 0 {
|
2023-08-30 18:05:11 +08:00
|
|
|
panic(dto.ErrorDto{Code: dto.DataAlreadyExist, Message: "编码已存在"})
|
2023-08-10 17:22:42 +08:00
|
|
|
}
|
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
|
|
|
}
|