fusenapi/server/backend/internal/logic/quotationdetaillogic.go
2023-06-26 19:51:59 +08:00

215 lines
5.4 KiB
Go

package logic
import (
"encoding/json"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/collect"
"strings"
"context"
"fusenapi/server/backend/internal/svc"
"fusenapi/server/backend/internal/types"
"github.com/tidwall/gjson"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
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,
}
}
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) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
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)
var productIds = collect.ArrayColumnTag[int64](CanteenProduct, "product_id")
productList, err := l.svcCtx.AllModels.FsProduct.FindAllOnlyByIds(l.ctx, productIds)
productListMap := collect.Array2MapByKeyTag[int64](productList, "id")
//获取size信息
var sizeIds = collect.ArrayColumnTag[int64](CanteenProduct, "size_id")
sizes, err := l.svcCtx.AllModels.FsProductSize.GetAllSelectIdAndCapacityByIds(l.ctx, sizeIds)
sizesMap := collect.Array2MapByKeyTag[int64](sizes, "id")
//获取价格信息
productPrice, err := l.svcCtx.AllModels.FsProductPrice.GetAllSelectBySizeId(l.ctx, sizeIds)
productPriceMap := collect.Array2MapByKeyTag[int64](productPrice, "size_id")
// product := []map[string]interface{}{}
product := []map[string]interface{}{}
for _, parr := range list.Array() {
var priceList []map[string]int64
if !qFlag {
if price, ok := productPriceMap[parr.Get("size_id").Int()]; !ok {
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()
}
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
}
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,
})
}
return resp.SetStatus(basic.CodeOK)
}