116 lines
3.5 KiB
Go
116 lines
3.5 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 adminLifeTask struct{}
|
|
|
|
var AdminLifeTask = &adminLifeTask{}
|
|
|
|
// List 全部生活任务(含下架),按 id 排序。
|
|
func (s *adminLifeTask) List(ctx context.Context, req *dto.AdminLifeTaskListReq) (*dto.AdminLifeTaskListRes, error) {
|
|
recs, err := dao.LifeTask.ListAll(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
strategyIds := make([]int64, 0, len(recs))
|
|
for _, r := range recs {
|
|
strategyIds = append(strategyIds, r.StrategyId)
|
|
}
|
|
names, err := strategyNames(ctx, strategyIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]*dto.AdminLifeTaskItem, 0, len(recs))
|
|
for _, r := range recs {
|
|
items = append(items, &dto.AdminLifeTaskItem{
|
|
Id: r.Id, StrategyId: r.StrategyId, StrategyName: names[r.StrategyId],
|
|
Title: r.Title, Description: r.Description,
|
|
Guide: r.Guide, RewardPoints: r.RewardPoints,
|
|
Status: r.Status,
|
|
})
|
|
}
|
|
return &dto.AdminLifeTaskListRes{List: items}, nil
|
|
}
|
|
|
|
// Create 新增生活任务:计策存在校验,清内容缓存。
|
|
func (s *adminLifeTask) Create(ctx context.Context, req *dto.AdminLifeTaskCreateReq) (*dto.AdminLifeTaskCreateRes, error) {
|
|
if err := checkStrategy(ctx, req.StrategyId); err != nil {
|
|
return nil, err
|
|
}
|
|
id, err := dao.LifeTask.InsertAndReturnId(ctx, g.Map{
|
|
"strategy_id": req.StrategyId, "title": req.Title, "description": req.Description,
|
|
"guide": req.Guide, "reward_points": req.RewardPoints, "status": consts.StatusEnabled,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableLifeTask)
|
|
return &dto.AdminLifeTaskCreateRes{Id: id}, nil
|
|
}
|
|
|
|
// Update 编辑生活任务,清内容缓存。
|
|
func (s *adminLifeTask) Update(ctx context.Context, req *dto.AdminLifeTaskUpdateReq) (*dto.AdminLifeTaskUpdateRes, error) {
|
|
rec, err := dao.LifeTask.GetByPk(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rec == nil {
|
|
return nil, gerror.New("生活任务不存在")
|
|
}
|
|
if err = checkStrategy(ctx, req.StrategyId); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = dao.LifeTask.UpdateByPk(ctx, req.Id, g.Map{
|
|
"strategy_id": req.StrategyId, "title": req.Title, "description": req.Description,
|
|
"guide": req.Guide, "reward_points": req.RewardPoints,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableLifeTask)
|
|
return &dto.AdminLifeTaskUpdateRes{}, nil
|
|
}
|
|
|
|
// setStatus 上下架(软删除)。
|
|
func (s *adminLifeTask) setStatus(ctx context.Context, id int64, status int) error {
|
|
rec, err := dao.LifeTask.GetByPk(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if rec == nil {
|
|
return gerror.New("生活任务不存在")
|
|
}
|
|
if err = dao.LifeTask.UpdateByPk(ctx, id, g.Map{"status": status}); err != nil {
|
|
return err
|
|
}
|
|
common.InvalidateContentCache(ctx, consts.TableLifeTask)
|
|
return nil
|
|
}
|
|
|
|
// Disable 下架生活任务。
|
|
func (s *adminLifeTask) Disable(ctx context.Context, req *dto.AdminLifeTaskDisableReq) (*dto.AdminLifeTaskDisableRes, error) {
|
|
if err := s.setStatus(ctx, req.Id, consts.StatusDisabled); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.AdminLifeTaskDisableRes{}, nil
|
|
}
|
|
|
|
// Enable 上架生活任务。
|
|
func (s *adminLifeTask) Enable(ctx context.Context, req *dto.AdminLifeTaskEnableReq) (*dto.AdminLifeTaskEnableRes, error) {
|
|
if err := s.setStatus(ctx, req.Id, consts.StatusEnabled); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.AdminLifeTaskEnableRes{}, nil
|
|
}
|