38e040af07
CI / changes (push) Successful in 37s
CI / ad (push) Successful in 29s
CI / admin (push) Successful in 17s
CI / bff (push) Successful in 13s
CI / chore (push) Successful in 15s
CI / equipment (push) Successful in 14s
CI / express (push) Successful in 14s
CI / product (push) Successful in 13s
CI / record (push) Successful in 13s
CI / sale (push) Successful in 13s
CI / task (push) Successful in 13s
CI / user (push) Successful in 14s
CI / wecom (push) Successful in 13s
207 lines
6.0 KiB
Go
207 lines
6.0 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"
|
|
"time"
|
|
|
|
"lone-services/rpc/order/pb"
|
|
"lone-services/services/order/internal/svc"
|
|
|
|
"github.com/samber/lo"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type OrderDownLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewOrderDownLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderDownLogic {
|
|
return &OrderDownLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *OrderDownLogic) OrderDown(in *order.OrderItemsRequest) (*order.ExcelExportResponse, error) {
|
|
res := &order.ExcelExportResponse{}
|
|
var v validator.OrderItemsValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
res.Msg = utils.Fail.Msg
|
|
return res, nil
|
|
}
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
res.Msg = utils.ErrorNotFund.Msg
|
|
return res, nil
|
|
}
|
|
var info []dao.OrderInfo
|
|
w := modelbase.Params{
|
|
Order: "id desc",
|
|
}
|
|
|
|
w.Like = make(map[string]string)
|
|
w.Other = make(map[string]string)
|
|
w.Eq = 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]))
|
|
}
|
|
|
|
//如果是公司销售
|
|
svc.SetGroupWhere(&w, adminInfo, false)
|
|
|
|
err := model.OrderModel{}.Init().Items(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
res.Msg = utils.ErrorNotFund.Msg
|
|
return res, nil
|
|
}
|
|
logx.Error("info:", info)
|
|
items, _ := l.Down(info)
|
|
logx.Error("items:", items)
|
|
// 2. 配置导出列:表头 ↔ 结构体字段
|
|
columns := []utils.ExcelColumn{
|
|
{Header: "申请日期", Field: "CreateDate", Width: 12},
|
|
{Header: "门店名", Field: "StoreName", Width: 15},
|
|
{Header: "门店联系人", Field: "Addressee"},
|
|
{Header: "电话", Field: "Mobile", Width: 15},
|
|
{Header: "地址", Field: "Address", Width: 30},
|
|
{Header: "产品名称", Field: "ProductName", Width: 30},
|
|
{Header: "数量", Field: "ProductNumber"},
|
|
{Header: "营业执照", Field: "StoreBusinessLicense", IsImage: true},
|
|
{Header: "准确时间", Field: "CreateTime", Width: 22},
|
|
{Header: "订单号", Field: "OrderSn", Width: 40},
|
|
}
|
|
res.Name = "订单列表"
|
|
if len(in.Time) > utils.NumberZero {
|
|
res.Name = strconv.Itoa(int(in.Time[utils.NumberZero])) + "-" +
|
|
strconv.Itoa(int(in.Time[utils.NumberOne])) + "-订单列表"
|
|
}
|
|
// 3. 一行导出
|
|
data, err := utils.ExcelExport(columns, items)
|
|
if err != nil {
|
|
res.Msg = err.Error()
|
|
return res, nil
|
|
}
|
|
res.Data = data
|
|
return res, nil
|
|
}
|
|
|
|
func (l *OrderDownLogic) Down(items []dao.OrderInfo) ([]dao.OrderDownInfo, utils.Status) {
|
|
var sns []string
|
|
var storeIds []string
|
|
for _, item := range items {
|
|
sns = append(sns, item.OrderSn)
|
|
storeIds = append(storeIds, strconv.Itoa(item.StoreId))
|
|
}
|
|
|
|
var retData []dao.OrderDownInfo
|
|
|
|
if len(sns) < utils.NumberOne {
|
|
return retData, utils.Ok
|
|
}
|
|
|
|
//TODO 获取门店信息
|
|
//var storeMap map[int]dao.StoreOrderInfo
|
|
//w := models.Params{In: map[string][]string{"id": storeIds}}
|
|
//var storeItems []dao.StoreOrderInfo
|
|
//gErr := models.StoreModel{}.Init().Items(w, &storeItems)
|
|
//if gErr != nil {
|
|
// utils.Logger.Error("store err:", gErr)
|
|
// return retData, nil
|
|
//}
|
|
//
|
|
//storeMap = lo.SliceToMap(storeItems, func(item dao.StoreOrderInfo) (int, dao.StoreOrderInfo) {
|
|
// return item.Id, item
|
|
//})
|
|
productMap := l.getProductItems(sns)
|
|
|
|
for _, item := range items {
|
|
var storeBusinessLicense string
|
|
//TODO 获取门店营业执照
|
|
//if _, ok := storeMap[item.StoreId]; ok {
|
|
// storeBusinessLicense, _ = utils.Crypto{}.AESDecryptECB(storeMap[item.StoreId].BusinessLicense)
|
|
//}
|
|
|
|
if _, ok := productMap[item.OrderSn]; ok {
|
|
for _, value := range productMap[item.OrderSn] {
|
|
mobile, _ := utils.DecryptPhone(item.Mobile)
|
|
retData = append(retData, dao.OrderDownInfo{
|
|
Id: item.Id,
|
|
OrderSn: item.OrderSn,
|
|
StoreId: item.StoreId,
|
|
StoreName: item.StoreName,
|
|
SaleName: item.SaleName,
|
|
UserId: item.UserId,
|
|
UserName: item.UserName,
|
|
SaleId: item.SaleId,
|
|
Mobile: utils.DecryptPhoneReplace(mobile),
|
|
Address: item.Address,
|
|
Addressee: item.Addressee,
|
|
DistrictIds: item.DistrictIds,
|
|
Status: item.Status,
|
|
ProductIds: item.ProductIds,
|
|
CreateTime: item.CreateTime,
|
|
CreateDate: item.CreateTime.Format(time.DateOnly),
|
|
ProductName: value.ProductName,
|
|
ProductNumber: value.Number,
|
|
StoreBusinessLicense: storeBusinessLicense,
|
|
})
|
|
}
|
|
}
|
|
|
|
}
|
|
return retData, utils.Ok
|
|
}
|
|
|
|
func (l *OrderDownLogic) getProductItems(sns []string) map[string][]dao.OrderProducts {
|
|
w := modelbase.Params{In: map[string][]string{"order_sn": sns}}
|
|
var productItems []dao.OrderProducts
|
|
err := model.OrderProductModel{}.Init().Items(w, &productItems)
|
|
var productMap map[string][]dao.OrderProducts
|
|
if err != nil {
|
|
utils.Logger.Error("product err:", err)
|
|
}
|
|
|
|
productMap = lo.GroupBy(productItems, func(item dao.OrderProducts) string {
|
|
return item.OrderSn
|
|
})
|
|
return productMap
|
|
}
|