Files
lone-services/services/order/internal/logic/supplyItemsLogic.go
T
2026-09-21 15:03:52 +08:00

146 lines
3.9 KiB
Go

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"
"strings"
"lone-services/rpc/order/pb"
"lone-services/services/order/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type SupplyItemsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
BaseLogic
}
func NewSupplyItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SupplyItemsLogic {
return &SupplyItemsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SupplyItemsLogic) SupplyItems(in *order.SupplyItemsRequest) (*order.Response, error) {
var v validator.SupplyItemsValidator
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)
}
w := modelbase.Params{Eq: map[string]string{
"sale_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
"status": string(in.Status),
},
Order: "id desc",
Page: int(in.Page),
Size: int(in.Size),
}
if in.Status > utils.NumberZero {
w.Eq["status"] = string(in.Status)
}
w.Like = make(map[string]string)
if len(in.OrderSn) > utils.NumberZero {
w.Like["order_sn"] = in.OrderSn
}
w.Other = make(map[string]string)
if len(in.Time) > utils.NumberZero {
w.Other["create_time > ?"] = in.Time[utils.NumberZero]
w.Other["create_time < ?"] = in.Time[utils.NumberOne]
}
var info []dao.AdminSupplyInfo
items, err := model.SupplyModel{}.Init().Page(w, &info)
if err != nil {
return l.fail(utils.Fail)
}
l.getData(&info)
items.Items = info
return l.ok(items)
}
func (l *SupplyItemsLogic) getData(info *[]dao.AdminSupplyInfo) utils.Status {
var orderSns []string
for _, item := range *info {
orderSns = append(orderSns, item.OrderSn)
}
w := modelbase.Params{In: map[string][]string{"order_sn": orderSns}}
var orders []dao.OrderInfo
err := model.OrderModel{}.Init().Items(w, &orders)
if err != nil {
return utils.Fail
}
var orderProducts []dao.OrderProductInfo
err = model.OrderProductModel{}.Init().Items(w, &orderProducts)
if err != nil {
return utils.Fail
}
productInfos := make(map[string][]dao.SupplyOrderProduct)
for _, item := range orderProducts {
productInfos[item.OrderSn] = append(productInfos[item.OrderSn], dao.SupplyOrderProduct{
Id: item.ProductId,
Name: item.ProductName,
Price: utils.RoundFloat(item.Price),
Number: item.Number,
Images: strings.Split(item.Images, utils.DecollatorComma),
Content: item.Content,
})
}
l.Logger.Error(orders)
l.Logger.Error(orderProducts)
productIds := make(map[string]dao.SupplyOrderInfo)
for _, item := range orders {
tmp := dao.SupplyOrderInfo{
SaleName: item.SaleName,
StoreId: int64(item.StoreId),
StoreName: item.StoreName,
SaleMobile: item.SaleMobile,
Mobile: item.Mobile,
Address: item.Address,
Addressee: item.Addressee,
OrderStatus: int32(item.Status),
}
if _, ok := productInfos[item.OrderSn]; ok {
tmp.ProductInfo = productInfos[item.OrderSn]
}
productIds[item.OrderSn] = tmp
}
for key, item := range *info {
if _, ok := productIds[item.OrderSn]; !ok {
continue
}
(*info)[key].SaleName = productIds[item.OrderSn].SaleName
(*info)[key].StoreId = productIds[item.OrderSn].StoreId
(*info)[key].StoreName = productIds[item.OrderSn].StoreName
(*info)[key].SaleMobile = productIds[item.OrderSn].SaleMobile
(*info)[key].Mobile = productIds[item.OrderSn].Mobile
(*info)[key].Address = productIds[item.OrderSn].Address
(*info)[key].Addressee = productIds[item.OrderSn].Addressee
(*info)[key].OrderStatus = productIds[item.OrderSn].OrderStatus
(*info)[key].ProductInfo = productIds[item.OrderSn].ProductInfo
}
return utils.Ok
}