fusenapi/server/backend/internal/logic/quotationdetaillogic.go
2023-06-27 18:09:56 +08:00

432 lines
14 KiB
Go

package logic
import (
"bytes"
"encoding/json"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/collect"
"fusenapi/utils/format"
"log"
"strings"
"text/template"
"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
}
// GetDemoHtml 报价单
func (l *QuotationDetailLogic) GetDemoHtml(quot *gmodel.FsQuotation, quotationProduct []*gmodel.FsQuotationProduct) (htmlcontent string, err error) {
saler, err := l.svcCtx.AllModels.FsQuotationSaler.GetOne(l.ctx, quot.Id)
if err != nil {
return "", err
}
log.Println(saler)
// htmlContent := ""
priceHtml := ""
// productNum := len(quotationProduct)
// page := 3
// pageTotal := 3 + productNum
for _, quotProduct := range quotationProduct {
var priceInfo []map[string]interface{}
err := json.Unmarshal([]byte(*quotProduct.PriceInfo), &priceInfo)
if err != nil {
return "", err
}
priceHtmlTpl := `
{{range $i, $parr := .priceInfo}}
{{if lt $i 3}}
<div class="pr-card">
<p class="pr">${{$parr.price}}</p>
<p class="num">{{$parr.num}}<span> Units</span></p>
</div>
{{else}}
<div class="pr-card" style="margin-right: 8px;">
<p class="pr">${{$parr.price}}</p>
<p class="num">{{$parr.num}}<span> Units</span></p>
</div>
{{end}}
{{end}}`
tpl := template.Must(template.New("prCardTpl").Parse(priceHtmlTpl))
buf := &bytes.Buffer{}
err = tpl.Execute(buf, map[string]interface{}{
"priceInfo": priceInfo,
})
if err != nil {
log.Println(err)
return "", err
}
priceHtml = buf.String()
}
tplcontent := `
{{$product_num := len .products}}
{{$page := 3}}
{{$page_total := add $page $product_num}}
{{range $arr := .products}}
{{$price := json_decode $arr.price_info}}
{{$price_html := .priceHtml}}
{{$price_html}}
{{if gt $arr.is_gift 0 }}{{/* 赠品 */}}
<div class="box">
<div style="position: relative; margin-bottom: 30px;">
<p class="f-m" style="font-size: 39px; font-weight: 800; margin-bottom: 12px; line-height: 32px;">{{$arr.name}}</p>
<p class="f-b" style="font-size: 25px; font-weight: 800;line-height: 19px;">{{$arr.size}}</p>
<div class="logo">
<img src="https://fusenapi.kayue.cn:8010/storage/pdf/logo.svg" alt="logo">
</div>
</div>
<div class="img-box" style="height: 611px;margin-bottom: 32px; overflow: hidden;">
<div class="img" style="background-image: url({{$arr.img}});"></div>
</div>
<div>
<div class="pr-card big">
<p class="pr">FREE!</p>
<p class="num">{{$arr.num}}<span> Units</span></p>
</div>
<div>
<div class="card-box" style="display: inline-block;width: 183px;line-height: 36px;padding-left: 16px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">Lead Time</div>
<div class="card-box" style="display: inline-block;width: 183px;height: 52px; padding-left: 15px;margin-bottom: 8px;background-color: #E0E2EB; font-weight: 600;">
<span style="font-size: 35px;line-height: 52px;">{{$arr.cycle}}</span><span style="font-size: 26px;line-height: 52px;">Days</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Design</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Storage</span>
</div>
<div class="card-box more-card" style="margin-bottom: 0;">
<span class="free">FREE </span><span class="text">Shipping</span>
</div>
</div>
</div>
</div>
{{else}}{{/* 非赠品 */}}
{{if $arr.size}}
// 有尺寸的
<div class="box">
<div style="position: relative; margin-bottom: 30px;">
<p class="f-m" style="font-size: 39px; font-weight: 800; margin-bottom: 12px; line-height: 32px;">{{$arr.name}}</p>
<p class="f-b" style="font-size: 25px; font-weight: 800;line-height: 19px;">{{$arr.size}}</p>
<div class="logo">
<img src="https://fusenapi.kayue.cn:8010/storage/pdf/logo.svg" alt="logo">
</div>
</div>
<div class="img-box" style="height: 611px;margin-bottom: 32px; overflow: hidden;">
<div class="img"
style="background-image: url({{$arr.img}});"></div>
</div>
<div>
<div class="card-box"
style="float: left; width: 530px;line-height: 36px;padding-left: 19px;margin-right: 8px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">Unit Price (Tax included)</div>
<div class="card-box"
style="display: inline-block;width: 183px;line-height: 36px;padding-left: 16px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">
Lead Time</div>
{{$price_html}}
<div>
<div class="card-box"
style="display: inline-block;width: 183px;height: 52px; padding-left: 15px;margin-bottom: 8px;background-color: #E0E2EB; font-weight: 600;">
<span style="font-size: 35px;line-height: 52px;">{{$arr.cycle}}</span><span
style="font-size: 26px;line-height: 52px;">Days</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Design</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Storage</span>
</div>
<div class="card-box more-card" style="margin-bottom: 0;">
<span class="free">FREE </span><span class="text">Shipping</span>
</div>
</div>
</div>
</div>
{{else}}
<div class="box">
<div style="position: relative; margin-bottom: 32px;">
<p class="f-m" style="font-size: 39px; font-weight: 800; margin-bottom: 3px; line-height: 32px;">
{{$arr.name}}</p>
<div class="logo">
<img src="https://fusenapi.kayue.cn:8010/storage/pdf/logo.svg" alt="logo">
</div>
</div>
<div class="img-box" style="height: 641px;margin-bottom: 31px; overflow: hidden;">
<div class="img" style="background-image: url({{$arr.img}});">
</div>
</div>
<div>
<div class="card-box"
style="float: left;width: 530px;line-height: 36px;padding-left: 19px;margin-right: 8px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">
Unit Price (Tax included)</div>
<div class="card-box"
style="display: inline-block;width: 183px;line-height: 36px;padding-left: 16px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">
Lead Time</div>
{{$price_html}}
<div>
<div class="card-box"
style="display: inline-block;width: 183px;height: 52px; padding-left: 15px;margin-bottom: 8px;background-color: #E0E2EB; font-weight: 600;">
<span style="font-size: 35px;line-height: 52px;">{{$arr.cycle}}</span><span
style="font-size: 26px;line-height: 52px;">Days</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Design</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Storage</span>
</div>
<div class="card-box more-card" style="margin-bottom: 0;">
<span class="free">FREE </span><span class="text">Shipping</span>
</div>
</div>
</div>
</div>
{{end}}
{{end}}
<div class="page-box" style="background-color: rgba(125, 125, 125, 0.16);color: #000;text-align: center;height: 50px;line-height: 50px;margin-top: 30px;">{{$page}}/{{$page_total}}</div>
{{$page = add $page 1}}
{{end}} `
tpl := template.New("demo")
tpl = tpl.Funcs(format.TemplateFuncMap)
tpl = template.Must(tpl.Parse(tplcontent))
buf := bytes.NewBufferString("")
tpl.Execute(buf, map[string]interface{}{
"products": collect.StructSliceJson2Maps(quotationProduct),
"price_html": priceHtml,
})
htmlContent := buf.String()
log.Println(htmlContent)
return htmlContent, nil
}
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)
}
quotationProduct, 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 = quotationProduct
if len(quotationProduct) == 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(quotationProduct) > 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)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr)
}
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)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr)
}
sizesMap := collect.Array2MapByKeyTag[int64](sizes, "id")
//获取价格信息
productPrice, err := l.svcCtx.AllModels.FsProductPrice.GetAllSelectBySizeId(l.ctx, sizeIds)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr)
}
productPriceMap := collect.Array2MapByKeyTag[int64](productPrice, "size_id")
// product := []map[string]interface{}{}
products := []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
}
products = append(products, 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,
})
}
htmlContent, err := l.GetDemoHtml(&quot, quotationProduct)
if err != nil {
panic(err)
}
log.Println(htmlContent)
return resp.SetStatus(basic.CodeOK)
}