110 lines
3.4 KiB
Go
110 lines
3.4 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 element struct{}
|
|
|
|
var Element = &element{}
|
|
|
|
// ---------- 后台管理 ----------
|
|
|
|
type adminElement struct{}
|
|
|
|
var AdminElement = &adminElement{}
|
|
|
|
// List 元素列表(e_type=0 全部),按类型 + 序号排序。
|
|
func (s *adminElement) List(ctx context.Context, req *dto.AdminElementListReq) (*dto.AdminElementListRes, error) {
|
|
recs, err := dao.Element.ListAll(ctx, req.EType)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]*dto.AdminElementItem, 0, len(recs))
|
|
for _, r := range recs {
|
|
items = append(items, &dto.AdminElementItem{
|
|
Id: r.Id, EType: r.EType, Name: r.Name,
|
|
Image: r.Image, Audio: r.Audio,
|
|
Description: r.Description,
|
|
Status: r.Status, SortOrder: r.SortOrder,
|
|
})
|
|
}
|
|
return &dto.AdminElementListRes{List: items}, nil
|
|
}
|
|
|
|
// Create 新增元素:拼音一次性标注后入库,清内容缓存。
|
|
func (s *adminElement) Create(ctx context.Context, req *dto.AdminElementCreateReq) (*dto.AdminElementCreateRes, error) {
|
|
id, err := dao.Element.InsertAndReturnId(ctx, g.Map{
|
|
"e_type": req.EType, "name": req.Name, "name_pinyin": common.AnnotatePinyin(req.Name),
|
|
"image": req.Image, "audio": req.Audio,
|
|
"description": req.Description, "description_pinyin": common.AnnotatePinyin(req.Description),
|
|
"sort_order": req.SortOrder, "status": consts.StatusEnabled,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableElement)
|
|
return &dto.AdminElementCreateRes{Id: id}, nil
|
|
}
|
|
|
|
// Update 编辑元素:全字段覆盖,拼音重标,清内容缓存。
|
|
func (s *adminElement) Update(ctx context.Context, req *dto.AdminElementUpdateReq) (*dto.AdminElementUpdateRes, error) {
|
|
rec, err := dao.Element.GetByPk(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rec == nil {
|
|
return nil, gerror.New("元素不存在")
|
|
}
|
|
if err = dao.Element.UpdateByPk(ctx, req.Id, g.Map{
|
|
"e_type": req.EType, "name": req.Name, "name_pinyin": common.AnnotatePinyin(req.Name),
|
|
"image": req.Image, "audio": req.Audio,
|
|
"description": req.Description, "description_pinyin": common.AnnotatePinyin(req.Description),
|
|
"sort_order": req.SortOrder,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableElement)
|
|
return &dto.AdminElementUpdateRes{}, nil
|
|
}
|
|
|
|
// Disable 下架元素(软删除,引用它的内容前台自动隐藏)。
|
|
func (s *adminElement) Disable(ctx context.Context, req *dto.AdminElementDisableReq) (*dto.AdminElementDisableRes, error) {
|
|
if err := s.setStatus(ctx, req.Id, consts.StatusDisabled); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.AdminElementDisableRes{}, nil
|
|
}
|
|
|
|
// Enable 上架元素。
|
|
func (s *adminElement) Enable(ctx context.Context, req *dto.AdminElementEnableReq) (*dto.AdminElementEnableRes, error) {
|
|
if err := s.setStatus(ctx, req.Id, consts.StatusEnabled); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.AdminElementEnableRes{}, nil
|
|
}
|
|
|
|
// setStatus 上下架(软删除)。
|
|
func (s *adminElement) setStatus(ctx context.Context, id int64, status int) error {
|
|
rec, err := dao.Element.GetByPk(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if rec == nil {
|
|
return gerror.New("元素不存在")
|
|
}
|
|
if err = dao.Element.UpdateByPk(ctx, id, g.Map{"status": status}); err != nil {
|
|
return err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableElement)
|
|
return nil
|
|
}
|