package logic import ( "context" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/services/order/internal/dao" "lone-services/services/order/internal/model" "lone-services/services/order/validator" "strconv" order "lone-services/rpc/order/pb" "lone-services/services/order/internal/svc" "github.com/samber/lo" "github.com/zeromicro/go-zero/core/logx" ) type ItemsLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger BaseLogic } func NewItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ItemsLogic { return &ItemsLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *ItemsLogic) Items(in *order.OrderItemsRequest) (*order.Response, error) { var v validator.OrderItemsValidator if fail := l.checkParams(in, &v); fail != nil { return fail, nil } adminInfo := utils.GetUserFromCtx(l.ctx) if adminInfo.ID < utils.NumberOne { return l.fail(utils.ErrorNoLoginInfo) } var info []dao.OrderInfo w := modelbase.Params{ Page: int(in.Page), Size: int(in.Size), Order: "id desc", } w.Like = make(map[string]string) w.Eq = make(map[string]string) w.Other = make(map[string]string) if in.ProductId > utils.NumberZero { w.Other["FIND_IN_SET(?, product_ids)"] = strconv.FormatInt(in.ProductId, utils.NumberTen) } //Time string if len(in.Mobile) > utils.NumberZero { phone := utils.GetSearchPhone(in.Mobile) if len(phone) > utils.NumberZero { w.Like["mobile LIKE ? "] = "%%" + phone + "%%" } } if len(in.OrderSn) > utils.NumberZero { w.Like["order_sn LIKE ? "] = "%%" + in.OrderSn + "%%" } if in.StoreId > utils.NumberZero { w.Eq["store_id"] = strconv.FormatInt(in.StoreId, utils.NumberTen) } if in.SaleId > utils.NumberZero { w.Eq["sale_id"] = strconv.FormatInt(in.SaleId, utils.NumberTen) } if in.Status > utils.NumberZero { w.Eq["status"] = string(in.Status) } if len(in.Time) > utils.NumberZero { w.Other["create_time > ?"] = strconv.Itoa(int(in.Time[utils.NumberZero])) w.Other["create_time < ?"] = strconv.Itoa(int(in.Time[utils.NumberOne])) } items, err := model.OrderModel{}.Init().Page(w, &info) if err != nil { l.Logger.Error(err) return l.fail(utils.ErrorNotFund) } var productItems []dao.OrderProducts err = model.OrderProductModel{}.Init().Items(w, &productItems) if err != nil { utils.Logger.Error("product err:", err) return l.fail(utils.ErrorNotFund) } productMap := lo.GroupBy(productItems, func(item dao.OrderProducts) string { return item.OrderSn }) for key, v := range info { mobile, aErr := utils.DecryptPhone(v.Mobile) if aErr == nil { if _, ok := productMap[v.OrderSn]; ok { info[key].ProductItems = productMap[v.OrderSn] } info[key].AesMobile = v.Mobile info[key].Mobile = utils.DecryptPhoneReplace(mobile) } } items.Items = info return l.ok(items) }