package analysisadminlogic import ( "context" "strconv" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/pkg/validate" analysis "lone-services/rpc/analysis/pb" "lone-services/services/analysis/internal/dao" "lone-services/services/analysis/internal/logic/common" "lone-services/services/analysis/internal/model" "lone-services/services/analysis/internal/svc" "lone-services/services/analysis/validator" jsoniter "github.com/json-iterator/go" "github.com/zeromicro/go-zero/core/logx" ) type GraphListLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewGraphListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GraphListLogic { return &GraphListLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *GraphListLogic) GraphList(in *analysis.AdminGraphListReq) (*analysis.Response, error) { var req validator.AdminGraphListValidator if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty { return outParams(msg), nil } if _, fail := common.RequireAdmin(l.ctx); fail != nil { return fail, nil } params := modelbase.Params{ Eq: map[string]string{ "status": strconv.Itoa(utils.StatusOk), }, Order: "id desc", Page: int(req.Page), Size: int(req.Size), } if req.Type > utils.NumberZero { params.Eq["type"] = strconv.Itoa(int(req.Type)) } if req.Name != utils.StringEmpty { params.Like = map[string]string{ "name LIKE ?": "%" + req.Name + "%", } } var rows []dao.UserGraph page, err := model.GraphModel{}.Init().Page(params, &rows) if err != nil { l.Errorf("admin graph list: %v", err) return common.FailResponse(utils.Fail), nil } if len(rows) == utils.NumberZero { page.Items = []dao.GraphResource{} return common.OkResponse(page), nil } var types []dao.UserGraphType err = model.GraphTypeModel{}.Init().Items(modelbase.Params{ Eq: map[string]string{ "status": strconv.Itoa(utils.StatusOk), }, }, &types) if err != nil { l.Errorf("admin graph list types: %v", err) return common.FailResponse(utils.Fail), nil } typeMap := make(map[int]string, len(types)) for _, t := range types { typeMap[int(t.Id)] = t.Name } list := make([]dao.GraphResource, 0, len(rows)) for _, row := range rows { var title []dao.GraphTitleItem var images []dao.GraphImageItem _ = jsoniter.UnmarshalFromString(row.Title, &title) _ = jsoniter.UnmarshalFromString(row.Images, &images) list = append(list, dao.GraphResource{ Id: row.Id, Type: row.Type, TypeName: typeMap[row.Type], Name: row.Name, Title: title, Images: images, CreateAt: row.CreateAt, }) } page.Items = list return common.OkResponse(page), nil }