This commit is contained in:
laodaming
2023-06-26 18:22:59 +08:00
20 changed files with 759 additions and 11 deletions

View File

@@ -48,7 +48,7 @@ func QuotationDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
var req types.Request
var req types.RequestQuotationId
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{

View File

@@ -1,15 +1,20 @@
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 {
@@ -26,9 +31,193 @@ func NewQuotationDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Q
}
}
func (l *QuotationDetailLogic) QuotationDetail(req *types.Request, userinfo *auth.BackendUserInfo) (resp *basic.Response) {
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.ArrayColumn[int64](CanteenProduct, "product_id")
productList, err := l.svcCtx.AllModels.FsProduct.FindAllOnlyByIds(l.ctx, productIds)
collect.Array2MapByKeyTag[int64](productList, "id")
//获取size信息
var sizeIds = collect.ArrayColumn[int64](CanteenProduct, "SizeId")
sizes, err := l.svcCtx.AllModels.FsProductSize.GetAllSelectIdAndCapacityByIds(l.ctx, sizeIds)
//获取价格信息
productPrice, err := l.svcCtx.AllModels.FsProductPrice.GetAllSelectBySizeId(l.ctx, sizeIds)
// product := []map[string]interface{}{}
product := []map[string]interface{}{}
for _, parr := range list.Array() {
var priceList []map[string]int64
if !qFlag {
if price, ok := productPrice[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()
}
productList
$productList[$parr['product_id']]['title']
}
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,
})
// product = append(product, map[string]interface{}{
// "id": qFlag ? parr.ID : nil,
// "sId": parr.SID ? parr.SID : nil,
// "isGift": qFlag && parr.IsGift,
// "name": qFlag ? parr.Name : productList[parr.ProductID].Title,
// "size": qFlag ? parr.Size : sizes[parr.SizeId].Capacity,
// "cycle": qFlag ? parr.Cycle : productList[parr.ProductID].ProduceDays + productList[parr.ProductID].DeliveryDays,
// "img": qFlag ? parr.Img : "",
// "mark": qFlag && parr.Remark ? json.Unmarshal(parr.Remark) : nil,
// "priceList": qFlag ? json.Unmarshal(parr.PriceInfo) : priceList,
// "num": qFlag ? parr.Num : 1,
// })
}
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -0,0 +1,73 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/product-template/internal/logic"
"fusenapi/server/product-template/internal/svc"
"fusenapi/server/product-template/internal/types"
)
func AddBaseMapHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.BackendUserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil || claims == nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
// 从token中获取对应的用户信息
userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
var req types.AddBaseMapReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewAddBaseMapLogic(r.Context(), svcCtx)
resp := l.AddBaseMap(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,61 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/product-template/internal/logic"
"fusenapi/server/product-template/internal/svc"
)
func GetBaseMapListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.BackendUserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil || claims == nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
// 从token中获取对应的用户信息
userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
l := logic.NewGetBaseMapListLogic(r.Context(), svcCtx)
resp := l.GetBaseMapList(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,73 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/product-template/internal/logic"
"fusenapi/server/product-template/internal/svc"
"fusenapi/server/product-template/internal/types"
)
func GetTemplatevDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.BackendUserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil || claims == nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
// 从token中获取对应的用户信息
userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
var req types.GetTemplatevDetailReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetTemplatevDetailLogic(r.Context(), svcCtx)
resp := l.GetTemplatevDetail(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,42 @@
// Code generated by goctl. DO NOT EDIT.
package handler
import (
"net/http"
"fusenapi/server/product-template/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/product-template/detail",
Handler: GetTemplatevDetailHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/product-template/base-map-list",
Handler: GetBaseMapListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/product-template/base-map-update",
Handler: SaveBaseMapHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/product-template/base-map-add",
Handler: AddBaseMapHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/product-template/update-template",
Handler: UpdateTemplateHandler(serverCtx),
},
},
)
}

View File

@@ -0,0 +1,61 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/product-template/internal/logic"
"fusenapi/server/product-template/internal/svc"
)
func SaveBaseMapHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.BackendUserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil || claims == nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
// 从token中获取对应的用户信息
userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
l := logic.NewSaveBaseMapLogic(r.Context(), svcCtx)
resp := l.SaveBaseMap(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,73 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/product-template/internal/logic"
"fusenapi/server/product-template/internal/svc"
"fusenapi/server/product-template/internal/types"
)
func UpdateTemplateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.BackendUserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil || claims == nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
// 从token中获取对应的用户信息
userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
var req types.UpdateTemplateReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUpdateTemplateLogic(r.Context(), svcCtx)
resp := l.UpdateTemplate(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}