package logic import ( "context" "encoding/json" "strconv" "product/internal/dao" "product/internal/model" "product/internal/svc" "product/product" "product/validator" "pkg.local/log" "pkg.local/modelbase" "pkg.local/utils" validateService "pkg.local/validate" "github.com/zeromicro/go-zero/core/logx" ) type VerifyLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewVerifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerifyLogic { return &VerifyLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *VerifyLogic) Verify(in *product.VerifyReq) (*product.Response, error) { var req validator.ProductVerifyItemsValidator if msg := validateService.ValidateFromProto(in, &req); msg != utils.StringEmpty { return outResponse(utils.ErrorParams, msg), nil } verifyStatus := dao.VerifyStatusChecking if req.Status == uint32(dao.VerifyListSecond) { verifyStatus = dao.VerifyStatusSecond } page := int(req.Page) size := int(req.PageSize) if page < modelbase.DefaultPage { page = modelbase.DefaultPage } if size < modelbase.DefaultPage { size = modelbase.DefaultSize } w := modelbase.Params{ Eq: map[string]string{"verify_status": strconv.Itoa(int(verifyStatus))}, Page: page, Size: size, Order: "id desc", } var rows []dao.ProductVerifyListRow result, err := model.ProductModel{}.Init().Page(w, &rows) if err != nil { log.Errorf("product verify list: %v", err) return failResponse(utils.Fail), nil } items := make([]*product.VerifyItem, 0) if len(rows) == 0 { return okResponse(utils.StructToJson(&product.VerifyItemsData{ Count: result.Count, Items: items, })), nil } ids := make([]string, 0, len(rows)) for _, row := range rows { ids = append(ids, strconv.Itoa(row.Id)) } ww := modelbase.Params{ Eq: map[string]string{ "verify_status": strconv.Itoa(int(verifyStatus)), "type": strconv.Itoa(int(dao.VerifyTypeProduct)), }, In: map[string][]string{"verify_id": ids}, } var verifies []dao.VerifyInfo err = model.VerifyModel{}.Init().Items(ww, &verifies) if err != nil { log.Errorf("verify info items: %v", err) return failResponse(utils.Fail), nil } verifyByProduct := make(map[string]dao.VerifyInfo, len(verifies)) for _, v := range verifies { verifyByProduct[v.VerifyId] = v } for _, row := range rows { v, ok := verifyByProduct[strconv.Itoa(row.Id)] if !ok || len(v.Content) <= utils.NumberTen { continue } var content dao.ProductVerify err = json.Unmarshal([]byte(v.Content), &content) if err != nil { log.Errorf("product verify content json: %v", err) return failResponse(utils.ErrorFormatDataError), nil } items = append(items, &product.VerifyItem{ Name: row.Name, SalesModel: uint32(row.SalesModel), New: &product.VerifyParams{ Id: int64(row.Id), ModelCode: content.ModelCode, Price: float64(content.Price), StorePrice: float64(content.StorePrice), SalePrice: float64(content.SalePrice), SharePrice: float64(content.SharePrice), AgentPrice: float64(content.AgentPrice), SaleReward: content.SaleReward, Type: uint32(content.Type), NormsNumber: uint32(content.NormsNumber), BoxNumber: content.BoxNumber, VerifyId: int64(v.Id), }, Old: &product.VerifyParams{ Id: int64(row.Id), ModelCode: row.ModelCode, Price: float64(row.Price), StorePrice: float64(row.StorePrice), SalePrice: float64(row.SalePrice), SharePrice: float64(row.SharePrice), AgentPrice: float64(row.AgentPrice), SaleReward: row.SaleReward, Type: uint32(row.Type), NormsNumber: uint32(row.NormsNumber), BoxNumber: row.BoxNumber, VerifyId: int64(v.Id), }, }) } return okResponse(utils.StructToJson(&product.VerifyItemsData{ Count: result.Count, Items: items, })), nil }