86 lines
2.7 KiB
Go
86 lines
2.7 KiB
Go
package category
|
|
|
|
import (
|
|
"context"
|
|
"git.echol.cn/loser/lckt/global"
|
|
"git.echol.cn/loser/lckt/model/category"
|
|
categoryReq "git.echol.cn/loser/lckt/model/category/request"
|
|
)
|
|
|
|
type CategoryService struct{}
|
|
|
|
// CreateCategory 创建类别记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (catService *CategoryService) CreateCategory(ctx context.Context, cat *category.Category) (err error) {
|
|
err = global.GVA_DB.Create(cat).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteCategory 删除类别记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (catService *CategoryService) DeleteCategory(ctx context.Context, ID string) (err error) {
|
|
err = global.GVA_DB.Delete(&category.Category{}, "id = ?", ID).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteCategoryByIds 批量删除类别记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (catService *CategoryService) DeleteCategoryByIds(ctx context.Context, IDs []string) (err error) {
|
|
err = global.GVA_DB.Delete(&[]category.Category{}, "id in ?", IDs).Error
|
|
return err
|
|
}
|
|
|
|
// UpdateCategory 更新类别记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (catService *CategoryService) UpdateCategory(ctx context.Context, cat category.Category) (err error) {
|
|
err = global.GVA_DB.Model(&category.Category{}).Where("id = ?", cat.ID).Updates(&cat).Error
|
|
return err
|
|
}
|
|
|
|
// GetCategory 根据ID获取类别记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (catService *CategoryService) GetCategory(ctx context.Context, ID string) (cat category.Category, err error) {
|
|
err = global.GVA_DB.Where("id = ?", ID).First(&cat).Error
|
|
return
|
|
}
|
|
|
|
// GetCategoryInfoList 分页获取类别记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (catService *CategoryService) GetCategoryInfoList(ctx context.Context, info categoryReq.CategorySearch) (list []category.Category, total int64, err error) {
|
|
limit := info.PageSize
|
|
offset := info.PageSize * (info.Page - 1)
|
|
// 创建db
|
|
db := global.GVA_DB.Model(&category.Category{})
|
|
var cats []category.Category
|
|
// 如果有条件搜索 下方会自动创建搜索语句
|
|
if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
|
|
db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
|
|
}
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
var OrderStr string
|
|
orderMap := make(map[string]bool)
|
|
orderMap["name"] = true
|
|
orderMap["order"] = true
|
|
if orderMap[info.Sort] {
|
|
OrderStr = info.Sort
|
|
if info.Order == "descending" {
|
|
OrderStr = OrderStr + " desc"
|
|
}
|
|
db = db.Order(OrderStr)
|
|
}
|
|
|
|
if limit != 0 {
|
|
db = db.Limit(limit).Offset(offset)
|
|
}
|
|
|
|
err = db.Find(&cats).Error
|
|
return cats, total, err
|
|
}
|
|
func (catService *CategoryService) GetCategoryPublic(ctx context.Context) {
|
|
// 此方法为获取数据源定义的数据
|
|
// 请自行实现
|
|
}
|