This commit is contained in:
Hiven 2023-08-03 17:37:25 +08:00
parent a864335c0e
commit 6158779a95
14 changed files with 228 additions and 26 deletions

View File

@ -6,7 +6,7 @@ import (
"reflect"
"time"
"fusenapi/utils/handler"
"fusenapi/utils/handlers"
"gorm.io/gorm"
)
@ -60,7 +60,7 @@ func (o *FsOrderModel) FindPageListByPage(ctx context.Context, rowBuilder *gorm.
var resp []*FsOrderRel
// 过滤
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
// 排序
@ -70,11 +70,11 @@ func (o *FsOrderModel) FindPageListByPage(ctx context.Context, rowBuilder *gorm.
for i := 0; i < s.NumField(); i++ {
fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{}
}
rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap))
rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
}
// 分页
rowBuilder = rowBuilder.Scopes(handler.Paginate(page, pageSize))
rowBuilder = rowBuilder.Scopes(handlers.Paginate(page, pageSize))
// 结果
result := rowBuilder.Debug().WithContext(ctx).Find(&resp)
@ -125,7 +125,7 @@ func (m *FsOrderModel) FindCount(ctx context.Context, countBuilder *gorm.DB, fil
// 过滤
if filterMap != nil {
countBuilder = countBuilder.Scopes(handler.FilterData(filterMap))
countBuilder = countBuilder.Scopes(handlers.FilterData(filterMap))
}
result := countBuilder.WithContext(ctx).Limit(1).Count(&count)
@ -140,7 +140,7 @@ func (m *FsOrderModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB,
var resp FsOrderRel
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)

View File

@ -2,7 +2,7 @@ package gmodel
import (
"context"
"fusenapi/utils/handler"
"fusenapi/utils/handlers"
"reflect"
"gorm.io/gorm"
@ -55,7 +55,7 @@ func (m *FsPayModel) FindCount(ctx context.Context, countBuilder *gorm.DB, filte
// 过滤
if filterMap != nil {
countBuilder = countBuilder.Scopes(handler.FilterData(filterMap))
countBuilder = countBuilder.Scopes(handlers.FilterData(filterMap))
}
result := countBuilder.WithContext(ctx).Limit(1).Count(&count)
@ -70,7 +70,7 @@ func (m *FsPayModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB, fi
var resp FsPay
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)
@ -85,7 +85,7 @@ func (m *FsPayModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap
var resp []*FsPay
// 过滤
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
// 排序
@ -95,7 +95,7 @@ func (m *FsPayModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap
for i := 0; i < s.NumField(); i++ {
fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{}
}
rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap))
rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
}
result := rowBuilder.WithContext(ctx).Find(&resp)

View File

@ -2,7 +2,7 @@ package gmodel
import (
"context"
"fusenapi/utils/handler"
"fusenapi/utils/handlers"
"gorm.io/gorm"
)
@ -40,7 +40,7 @@ func (m *FsRefundReasonModel) FindOneByQuery(ctx context.Context, rowBuilder *go
var resp FsRefundReason
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)

View File

@ -3,7 +3,7 @@ package gmodel
import (
"context"
"errors"
"fusenapi/utils/handler"
"fusenapi/utils/handlers"
"gorm.io/gorm"
)
@ -36,7 +36,7 @@ func (m *FsResourceModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.D
var resp FsResource
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)

View File

@ -2,7 +2,7 @@ package gmodel
import (
"context"
"fusenapi/utils/handler"
"fusenapi/utils/handlers"
"gorm.io/gorm"
)
@ -23,7 +23,7 @@ func (m *FsResourcesModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.
var resp FsResources
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp)

View File

@ -2,7 +2,7 @@ package gmodel
import (
"context"
"fusenapi/utils/handler"
"fusenapi/utils/handlers"
"reflect"
"gorm.io/gorm"
@ -24,7 +24,7 @@ func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB,
var resp []*FsUserMaterial
// 过滤
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
// 排序
@ -34,7 +34,7 @@ func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB,
for i := 0; i < s.NumField(); i++ {
fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{}
}
rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap))
rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
}
result := rowBuilder.WithContext(ctx).Find(&resp)

View File

@ -97,6 +97,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/user/logo-list",
Handler: UserLogoListHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/user/one-more-order",
Handler: UserAgainOrderHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserAgainOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserAgainOrderReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewUserAgainOrderLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.UserAgainOrder(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@ -0,0 +1,144 @@
package logic
import (
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type UserAgainOrderLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserAgainOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAgainOrderLogic {
return &UserAgainOrderLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserAgainOrderLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserAgainOrderLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *UserAgainOrderLogic) UserAgainOrder(req *types.UserAgainOrderReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if userinfo == nil || userinfo.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
// 查询订单数据
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
fsOrderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
fsProductDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
rsbOrder := orderModel.RowSelectBuilder(nil)
rsbOrder = rsbOrder.Where("sn =?", req.Sn).Preload("FsOrderDetails")
rsbOrder = rsbOrder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(fsOrderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(orderDetailTemplateModel.TableName()).Preload("FsProductDesignInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(fsProductDesignModel.TableName())
})
})
})
fsOrderRelInfo, err := orderModel.FindOneByQuery(l.ctx, rsbOrder, nil)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
}
if len(fsOrderRelInfo.FsOrderDetails) > 0 {
for _, fsOrderDetail := range fsOrderRelInfo.FsOrderDetails {
var isCheck int64 = 1
productDesignInfo := fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo
if productDesignInfo.Id != 0 {
// 查找是否有此材质、产品、大小id的阶梯价格
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
priceStatus := int64(1)
priceReq := gmodel.FindOneProductPriceByParamsReq{
ProductId: productDesignInfo.ProductId,
MaterialId: productDesignInfo.MaterialId,
SizeId: productDesignInfo.SizeId,
Status: &priceStatus,
}
productPriceInfo, err := productPriceModel.FindOneProductPriceByParams(l.ctx, priceReq)
if err == nil && productPriceInfo.Id != 0 && *productPriceInfo.EachBoxNum > 0 {
// 买的数量和每箱数量取余为0 且 份数大于等于最小购买数量才算满足条件
if *fsOrderDetail.BuyNum%*productPriceInfo.EachBoxNum == 0 && int64(float64(*fsOrderDetail.BuyNum)/float64(*productPriceInfo.EachBoxNum)) >= *productPriceInfo.MinBuyNum {
// 查询购物车
cartModel := gmodel.NewFsCartModel(l.svcCtx.MysqlConn)
cartStatus := int64(1)
cartReq := gmodel.FindOneCartByParamsReq{
UserId: &userinfo.UserId,
ProductId: productDesignInfo.ProductId,
TemplateId: productDesignInfo.TemplateId,
PriceId: &productPriceInfo.Id,
DesignId: &productDesignInfo.Id,
MaterialId: productDesignInfo.MaterialId,
Status: &cartStatus,
}
cartInfo, err := cartModel.FindOneCartByParams(l.ctx, cartReq)
if err == nil && (err != nil && errors.Is(err, gorm.ErrRecordNotFound)) {
now := time.Now().Unix()
nowTime := time.Now()
data := gmodel.FsCart{
UserId: &userinfo.UserId,
ProductId: productPriceInfo.ProductId,
TemplateId: productDesignInfo.TemplateId,
PriceId: &productPriceInfo.Id,
MaterialId: productDesignInfo.MaterialId,
SizeId: productDesignInfo.SizeId,
BuyNum: fsOrderDetail.BuyNum,
Cover: productDesignInfo.Cover,
DesignId: &productDesignInfo.Id,
Ctime: &now,
Status: &cartStatus,
OptionalId: productDesignInfo.OptionalId,
IsCheck: &isCheck,
TsTime: &nowTime,
}
if cartInfo == nil {
err = cartModel.Create(l.ctx, data)
} else {
err = cartModel.Update(l.ctx, cartInfo.Id, data)
}
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to add to cart")
}
}
}
}
}
}
}
return resp.SetStatus(basic.CodeOK)
}

View File

@ -6,13 +6,13 @@ import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/handlers"
"time"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
handlerUtils "fusenapi/utils/handler"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
@ -74,7 +74,7 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us
orderInfo.RefundReason = &req.RefundReason
var nowTime = time.Now().Unix()
var payList []handlerUtils.PayInfo
var payList []handlers.PayInfo
// 事务处理
err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) {
// 修改订单信息
@ -106,7 +106,7 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us
if *payInfo.PaymentMethod == int64(constants.PAYMETHOD_STRIPE) {
key = l.svcCtx.Config.PayConfig.Stripe.Key
}
payList = append(payList, handlerUtils.PayInfo{
payList = append(payList, handlers.PayInfo{
TradeNo: *payInfo.TradeNo,
PaymentMethod: *payInfo.PaymentMethod,
Key: key,
@ -115,7 +115,7 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us
return nil
})
// 退款申请--调取第三方接口发起退款
handlerUtils.PayRefundHandler(&handlerUtils.PayRefundHandlerReq{
handlers.PayRefundHandler(&handlers.PayRefundHandlerReq{
PayInfoList: payList,
})
if err != nil {

View File

@ -5,6 +5,13 @@ import (
"fusenapi/utils/basic"
)
type UserAgainOrderReq struct {
Sn string `form:"sn"` // 订单编号
}
type UserAgainOrderRes struct {
}
type UserLogoListReq struct {
}

View File

@ -71,8 +71,19 @@ service home-user-auth {
// 用户logo列表
@handler UserLogoListHandler
get /api/user/logo-list (UserLogoListReq) returns (response);
// 再来一单
@handler UserAgainOrderHandler
get /api/user/one-more-order (UserAgainOrderReq) returns (response);
}
type (
UserAgainOrderReq {
Sn string `form:"sn"` // 订单编号
}
UserAgainOrderRes struct{}
)
type (
UserLogoListReq {
}

View File

@ -1,4 +1,4 @@
package handler
package handlers
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package handler
package handlers
import (
"fusenapi/constants"