100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"36wisdom/biz/consts"
|
|
"36wisdom/biz/dao"
|
|
"36wisdom/biz/model/dto"
|
|
"36wisdom/common"
|
|
)
|
|
|
|
// ---------- 后台管理 ----------
|
|
|
|
type adminBadge struct{}
|
|
|
|
var AdminBadge = &adminBadge{}
|
|
|
|
// List 全部徽章(含下架),按 id 排序。
|
|
func (s *adminBadge) List(ctx context.Context, req *dto.AdminBadgeListReq) (*dto.AdminBadgeListRes, error) {
|
|
recs, err := dao.Badge.ListAll(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]*dto.AdminBadgeItem, 0, len(recs))
|
|
for _, r := range recs {
|
|
items = append(items, &dto.AdminBadgeItem{
|
|
Id: r.Id, Name: r.Name, Icon: r.Icon,
|
|
CondType: r.CondType, CondValue: r.CondValue,
|
|
Status: r.Status,
|
|
})
|
|
}
|
|
return &dto.AdminBadgeListRes{List: items}, nil
|
|
}
|
|
|
|
// Create 新增徽章,清内容缓存。
|
|
func (s *adminBadge) Create(ctx context.Context, req *dto.AdminBadgeCreateReq) (*dto.AdminBadgeCreateRes, error) {
|
|
id, err := dao.Badge.InsertAndReturnId(ctx, g.Map{
|
|
"name": req.Name, "icon": req.Icon, "cond_type": req.CondType, "cond_value": req.CondValue,
|
|
"status": consts.StatusEnabled,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableBadge)
|
|
return &dto.AdminBadgeCreateRes{Id: id}, nil
|
|
}
|
|
|
|
// Update 编辑徽章,清内容缓存。
|
|
func (s *adminBadge) Update(ctx context.Context, req *dto.AdminBadgeUpdateReq) (*dto.AdminBadgeUpdateRes, error) {
|
|
rec, err := dao.Badge.GetByPk(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rec == nil {
|
|
return nil, gerror.New("徽章不存在")
|
|
}
|
|
if err = dao.Badge.UpdateByPk(ctx, req.Id, g.Map{
|
|
"name": req.Name, "icon": req.Icon, "cond_type": req.CondType, "cond_value": req.CondValue,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableBadge)
|
|
return &dto.AdminBadgeUpdateRes{}, nil
|
|
}
|
|
|
|
// setStatus 上下架(软删除)。
|
|
func (s *adminBadge) setStatus(ctx context.Context, id int64, status int) error {
|
|
rec, err := dao.Badge.GetByPk(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if rec == nil {
|
|
return gerror.New("徽章不存在")
|
|
}
|
|
if err = dao.Badge.UpdateByPk(ctx, id, g.Map{"status": status}); err != nil {
|
|
return err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableBadge)
|
|
return nil
|
|
}
|
|
|
|
// Disable 下架徽章。
|
|
func (s *adminBadge) Disable(ctx context.Context, req *dto.AdminBadgeDisableReq) (*dto.AdminBadgeDisableRes, error) {
|
|
if err := s.setStatus(ctx, req.Id, consts.StatusDisabled); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.AdminBadgeDisableRes{}, nil
|
|
}
|
|
|
|
// Enable 上架徽章。
|
|
func (s *adminBadge) Enable(ctx context.Context, req *dto.AdminBadgeEnableReq) (*dto.AdminBadgeEnableRes, error) {
|
|
if err := s.setStatus(ctx, req.Id, consts.StatusEnabled); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.AdminBadgeEnableRes{}, nil
|
|
}
|