fusenapi/server/backend/internal/logic/quotationdetaillogic.go

215 lines
5.4 KiB
Go
Raw Normal View History

2023-06-25 10:30:39 +00:00
package logic
import (
2023-06-26 10:19:51 +00:00
"encoding/json"
2023-06-25 10:30:39 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-06-26 10:19:51 +00:00
"fusenapi/utils/collect"
"strings"
2023-06-25 10:30:39 +00:00
"context"
"fusenapi/server/backend/internal/svc"
"fusenapi/server/backend/internal/types"
2023-06-26 10:19:51 +00:00
"github.com/tidwall/gjson"
2023-06-25 10:30:39 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-06-26 10:19:51 +00:00
"gorm.io/gorm"
2023-06-25 10:30:39 +00:00
)
type QuotationDetailLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewQuotationDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuotationDetailLogic {
return &QuotationDetailLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
2023-06-26 10:19:51 +00:00
func GetPrice(num int64, stepNum []int64, stepPrice []int64) int64 {
//阶梯价计算
if num > stepNum[len(stepNum)-1] {
return stepPrice[len(stepPrice)-1]
}
for k, v := range stepNum {
if num <= v {
return stepPrice[k]
}
}
return 0
}
func (l *QuotationDetailLogic) QuotationDetail(req *types.RequestQuotationId, userinfo *auth.BackendUserInfo) (resp *basic.Response) {
2023-06-25 10:30:39 +00:00
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
2023-06-26 10:19:51 +00:00
quot, err := l.svcCtx.AllModels.FsQuotation.FindOneOnlyById(l.ctx, req.QuotationId)
if err != nil {
if err == gorm.ErrRecordNotFound {
return resp.SetStatus(basic.CodeDbRecordNotFoundErr)
}
return resp.SetStatus(basic.CodeDbSqlErr)
}
CanteenProduct, err := l.svcCtx.AllModels.FsCanteenProduct.GetAllByCanteenTypeIdOrderAsc(l.ctx, *quot.CanteenType)
if err != nil {
if err == gorm.ErrRecordNotFound {
return resp.SetStatus(basic.CodeDbRecordNotFoundErr)
}
return resp.SetStatus(basic.CodeDbSqlErr)
}
quotation, err := l.svcCtx.AllModels.FsQuotationProduct.GetAllByQuotationIdOrderAsc(l.ctx, req.QuotationId)
if err != nil {
if err != gorm.ErrRecordNotFound {
return resp.SetStatus(basic.CodeDbSqlErr)
}
}
var target any = quotation
if len(quotation) == 0 {
target = CanteenProduct
}
jdata, err := json.Marshal(target)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeJsonErr)
}
list := gjson.ParseBytes(jdata)
qFlag := len(quotation) > 0
//获取备注模板
markList, err := l.svcCtx.AllModels.FsQuotationRemarkTemplate.GetAllSelectContent(l.ctx)
2023-06-26 11:51:59 +00:00
var productIds = collect.ArrayColumnTag[int64](CanteenProduct, "product_id")
2023-06-26 10:19:51 +00:00
productList, err := l.svcCtx.AllModels.FsProduct.FindAllOnlyByIds(l.ctx, productIds)
2023-06-26 11:51:59 +00:00
productListMap := collect.Array2MapByKeyTag[int64](productList, "id")
2023-06-26 10:19:51 +00:00
//获取size信息
2023-06-26 11:51:59 +00:00
var sizeIds = collect.ArrayColumnTag[int64](CanteenProduct, "size_id")
2023-06-26 10:19:51 +00:00
sizes, err := l.svcCtx.AllModels.FsProductSize.GetAllSelectIdAndCapacityByIds(l.ctx, sizeIds)
2023-06-26 11:51:59 +00:00
sizesMap := collect.Array2MapByKeyTag[int64](sizes, "id")
2023-06-26 10:19:51 +00:00
//获取价格信息
productPrice, err := l.svcCtx.AllModels.FsProductPrice.GetAllSelectBySizeId(l.ctx, sizeIds)
2023-06-26 11:51:59 +00:00
productPriceMap := collect.Array2MapByKeyTag[int64](productPrice, "size_id")
2023-06-26 10:19:51 +00:00
// product := []map[string]interface{}{}
product := []map[string]interface{}{}
for _, parr := range list.Array() {
var priceList []map[string]int64
if !qFlag {
2023-06-26 11:51:59 +00:00
if price, ok := productPriceMap[parr.Get("size_id").Int()]; !ok {
2023-06-26 10:19:51 +00:00
priceList = []map[string]int64{
{"num": 1, "price": 0},
{"num": 1, "price": 0},
{"num": 1, "price": 0},
}
} else {
i := 0
price.StepNum = collect.ArrayString2Int(strings.Split(price.StepNum.(string), ","))
price.StepPrice = collect.ArrayString2Int(strings.Split(price.StepPrice.(string), ","))
for price.MinBuyNum < int64(len(price.StepNum.([]string))+5) && i < 3 {
priceList = append(priceList, map[string]int64{
"num": int64(price.MinBuyNum * price.EachBoxNum),
"price": GetPrice(price.MinBuyNum, price.StepNum.([]int64), price.StepPrice.([]int64)) / 100,
})
price.MinBuyNum++
i++
}
if len(priceList) < 3 {
for j := 0; j < 3-len(priceList); j++ {
priceList = append(priceList, map[string]int64{"num": 1, "price": 0})
}
}
}
}
var (
Id any
priceInfo any
mark any
sid any
isGift bool
name any
size any
cycle any
img any
num any
)
if qFlag {
Id = parr.Get("id")
err = json.Unmarshal([]byte(parr.Get("price_info").String()), &priceInfo)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeJsonErr)
}
if parr.Get("sid").Exists() {
sid = parr.Get("sid").Int()
}
name = parr.Get("name").String()
size = parr.Get("size").Int()
cycle = parr.Get("cycle").Int()
img = parr.Get("img").String()
if parr.Get("remark").Exists() {
err = json.Unmarshal([]byte(parr.Get("remark").String()), &mark)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeJsonErr)
}
}
num = parr.Get("num").Int()
} else {
if parr.Get("sid").Exists() {
sid = parr.Get("sid").Int()
}
2023-06-26 11:51:59 +00:00
product := productListMap[parr.Get("product_id").Int()]
name = product.Title
size = sizesMap[parr.Get("size_id").Int()].Capacity
cycle = *product.ProduceDays + *product.DeliveryDays
img = ""
mark = []string{}
priceInfo = priceList
num = 1
2023-06-26 10:19:51 +00:00
}
product = append(product, map[string]interface{}{
"id": Id,
"s_id": sid,
"is_gift": isGift,
"name": name,
"size": size,
"cycle": cycle,
"img": img,
"mark": mark,
"price_list": priceInfo,
"num": num,
})
}
2023-06-25 10:30:39 +00:00
return resp.SetStatus(basic.CodeOK)
}