代码调整

This commit is contained in:
ziming 2025-05-28 19:38:51 +08:00
parent 91f7db4caa
commit 832827bee2
6 changed files with 229 additions and 218 deletions

View File

@ -1,159 +1 @@
package biz
import (
"context"
"fmt"
"time"
err2 "voucher/api/err"
v1 "voucher/api/v1"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
"voucher/internal/pkg/lock"
)
func (v *VoucherBiz) GetByOutBizNo(ctx context.Context, req *bo.OrderCreateReqBo) (*bo.OrderBo, error) {
order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo)
if err != nil && !err2.IsDbNotFound(err) {
return nil, err
}
return order, nil
}
func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (orderNo string, err error) {
order, err3 := v.GetByOutBizNo(ctx, req)
if err3 != nil {
return orderNo, err3
}
if order != nil {
if order.Status.IsFail() || order.Status.IsIng() {
if err4 := v.orderRetry(ctx, order); err4 != nil {
return orderNo, err4
}
}
return order.OrderNo, err
}
product, err3 := v.ProductRepo.GetByProductNo(ctx, req.ProductNo)
if err3 != nil {
return orderNo, err3
}
order, err3 = v.order(ctx, req, product)
if err3 != nil {
return orderNo, err3
}
return order.OrderNo, nil
}
func (v *VoucherBiz) CmbQuery(ctx context.Context, orderNo string) (resp *v1.CmbQueryReply, err error) {
c := vo.CmbQueryLockKey.BuildCache([]string{orderNo})
err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
order, err3 := v.OrderRepo.GetByOrderNo(ctx, orderNo)
if err3 != nil {
return err3
}
if err = v.Query(ctx, order); err != nil {
return err
}
status, err3 := order.Status.GetCmbStatusText()
if err3 != nil {
return err3
}
resp = &v1.CmbQueryReply{
Ticket: order.OrderNo,
Status: status.GetValue(),
TransDate: time.Now().Format("20060102150405"),
OrgNo: v.bc.Cmb.OrgNo,
Ext: "",
}
return nil
})
return
}
func (v *VoucherBiz) CmbProductQuery(ctx context.Context, productNo string) (reps *v1.CmbQueryProductReply, err error) {
c := vo.CmbProductQueryLockKey.BuildCache([]string{productNo})
err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
product, err3 := v.ProductRepo.GetByProductNo(ctx, productNo)
if err3 != nil {
return err3
}
if !product.Channel.IsWeChat() {
return fmt.Errorf("只支持微信")
}
wechatResp, err4 := v.WechatCpnRepo.QueryProduct(ctx, product.MchId, product.BatchNo)
if err4 != nil {
return err4
}
reps = &v1.CmbQueryProductReply{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
ActivityName: product.Name,
ActivityId: product.ProductNo,
Amount: "",
MinAmount: "",
AvailableType: "",
AvailableDays: "", // 动态有效期天数
StartTime: "",
EndTime: "",
AvailableStock: "",
Detail: *wechatResp.Description,
}
inputFormat := time.RFC3339
if wechatResp.AvailableBeginTime != nil {
availableBeginTime, _ := time.Parse(inputFormat, *wechatResp.AvailableBeginTime)
reps.StartTime = availableBeginTime.Format("2006-01-02 15:04:05.000")
reps.SaleStartTime = reps.StartTime
}
if wechatResp.AvailableEndTime != nil {
availableEndTime, _ := time.Parse(inputFormat, *wechatResp.AvailableEndTime)
reps.EndTime = availableEndTime.Format("2006-01-02 15:04:05.000")
reps.SaleEndTime = reps.EndTime
}
reps.Amount = fmt.Sprintf("%d", *wechatResp.StockUseRule.FixedNormalCoupon.CouponAmount)
reps.MinAmount = fmt.Sprintf("%d", *wechatResp.StockUseRule.FixedNormalCoupon.TransactionMinimum)
availableStock := *wechatResp.StockUseRule.MaxCoupons - *wechatResp.DistributedCoupons
reps.AvailableStock = fmt.Sprintf("%d", availableStock)
availableType, err3 := product.AvailableType.GetCmbAvailableType()
if err3 != nil {
return err3
}
reps.AvailableType = availableType.GetValue()
reps.AvailableDays = fmt.Sprintf("%d", product.AvailableDays)
return nil
})
return
}

View File

@ -8,41 +8,36 @@ import (
"voucher/internal/biz/vo"
)
func (v *VoucherBiz) OrderRetry(ctx context.Context, outBizNos []string) error {
func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (orderNo string, err error) {
if len(outBizNos) > 0 {
for _, outBizNo := range outBizNos {
order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, outBizNo)
if err != nil {
return fmt.Errorf(fmt.Sprintf("获取订单%s异常:%v", outBizNo, err))
order, err3 := v.GetByOutBizNo(ctx, req)
if err3 != nil {
return orderNo, err3
}
if !order.Status.IsIng() {
return fmt.Errorf(fmt.Sprintf("订单%s状态异常:%s", order.OrderNo, order.Status))
}
if order != nil {
if order.Status.IsFail() || order.Status.IsIng() {
if err4 := v.orderRetry(ctx, order); err4 != nil {
return err4
return orderNo, err4
}
}
return nil
return order.OrderNo, err
}
return v.OrderRepo.FindIngInBatches(ctx, func(ctx context.Context, rows []*bo.OrderBo) error {
for _, order := range rows {
if err4 := v.orderRetry(ctx, order); err4 != nil {
return err4
}
product, err3 := v.ProductRepo.GetByProductNo(ctx, req.ProductNo)
if err3 != nil {
return orderNo, err3
}
return nil
})
order, err3 = v.order(ctx, req, product)
if err3 != nil {
return orderNo, err3
}
return order.OrderNo, nil
}
func (v *VoucherBiz) order(ctx context.Context, req *bo.OrderCreateReqBo, product *bo.ProductBo) (*bo.OrderBo, error) {
@ -126,6 +121,17 @@ func (v *VoucherBiz) fail(ctx context.Context, order *bo.OrderBo, errReq error)
return v.alarm(ctx, order, errReq.Error())
}
func (v *VoucherBiz) GetByOutBizNo(ctx context.Context, req *bo.OrderCreateReqBo) (*bo.OrderBo, error) {
order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo)
if err != nil && !err2.IsDbNotFound(err) {
return nil, err
}
return order, nil
}
func (v *VoucherBiz) UpdateOrderStatus(ctx context.Context, orderId uint64, status vo.OrderStatus) error {
if status.IsSuccess() {

80
internal/biz/product.go Normal file
View File

@ -0,0 +1,80 @@
package biz
import (
"context"
"fmt"
"time"
v1 "voucher/api/v1"
"voucher/internal/biz/vo"
"voucher/internal/pkg/lock"
)
func (v *VoucherBiz) CmbProductQuery(ctx context.Context, productNo string) (reps *v1.CmbQueryProductReply, err error) {
c := vo.CmbProductQueryLockKey.BuildCache([]string{productNo})
err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
product, err3 := v.ProductRepo.GetByProductNo(ctx, productNo)
if err3 != nil {
return err3
}
if !product.Channel.IsWeChat() {
return fmt.Errorf("只支持微信")
}
wechatResp, err4 := v.WechatCpnRepo.QueryProduct(ctx, product.MchId, product.BatchNo)
if err4 != nil {
return err4
}
reps = &v1.CmbQueryProductReply{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
ActivityName: product.Name,
ActivityId: product.ProductNo,
Amount: "",
MinAmount: "",
AvailableType: "",
AvailableDays: "", // 动态有效期天数
StartTime: "",
EndTime: "",
AvailableStock: "",
Detail: *wechatResp.Description,
}
inputFormat := time.RFC3339
if wechatResp.AvailableBeginTime != nil {
availableBeginTime, _ := time.Parse(inputFormat, *wechatResp.AvailableBeginTime)
reps.StartTime = availableBeginTime.Format("2006-01-02 15:04:05.000")
reps.SaleStartTime = reps.StartTime
}
if wechatResp.AvailableEndTime != nil {
availableEndTime, _ := time.Parse(inputFormat, *wechatResp.AvailableEndTime)
reps.EndTime = availableEndTime.Format("2006-01-02 15:04:05.000")
reps.SaleEndTime = reps.EndTime
}
reps.Amount = fmt.Sprintf("%d", *wechatResp.StockUseRule.FixedNormalCoupon.CouponAmount)
reps.MinAmount = fmt.Sprintf("%d", *wechatResp.StockUseRule.FixedNormalCoupon.TransactionMinimum)
availableStock := *wechatResp.StockUseRule.MaxCoupons - *wechatResp.DistributedCoupons
reps.AvailableStock = fmt.Sprintf("%d", availableStock)
availableType, err3 := product.AvailableType.GetCmbAvailableType()
if err3 != nil {
return err3
}
reps.AvailableType = availableType.GetValue()
reps.AvailableDays = fmt.Sprintf("%d", product.AvailableDays)
return nil
})
return
}

View File

@ -4,9 +4,47 @@ import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"time"
v1 "voucher/api/v1"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
"voucher/internal/pkg/lock"
)
func (v *VoucherBiz) CmbQuery(ctx context.Context, orderNo string) (resp *v1.CmbQueryReply, err error) {
c := vo.CmbQueryLockKey.BuildCache([]string{orderNo})
err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
order, err3 := v.OrderRepo.GetByOrderNo(ctx, orderNo)
if err3 != nil {
return err3
}
if err = v.Query(ctx, order); err != nil {
return err
}
status, err3 := order.Status.GetCmbStatusText()
if err3 != nil {
return err3
}
resp = &v1.CmbQueryReply{
Ticket: order.OrderNo,
Status: status.GetValue(),
TransDate: time.Now().Format("20060102150405"),
OrgNo: v.bc.Cmb.OrgNo,
Ext: "",
}
return nil
})
return
}
func (v *VoucherBiz) Query(ctx context.Context, order *bo.OrderBo) error {
status, err := v.WechatCpnRepo.Query(ctx, order)

45
internal/biz/retry.go Normal file
View File

@ -0,0 +1,45 @@
package biz
import (
"context"
"fmt"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
)
func (v *VoucherBiz) OrderRetry(ctx context.Context, outBizNos []string) error {
if len(outBizNos) > 0 {
for _, outBizNo := range outBizNos {
order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, outBizNo)
if err != nil {
return fmt.Errorf(fmt.Sprintf("获取订单%s异常:%v", outBizNo, err))
}
if !order.Status.IsIng() {
return fmt.Errorf(fmt.Sprintf("订单%s状态异常:%s", order.OrderNo, order.Status))
}
if err4 := v.orderRetry(ctx, order); err4 != nil {
return err4
}
}
return nil
}
return v.OrderRepo.FindIngInBatches(ctx, func(ctx context.Context, rows []*bo.OrderBo) error {
for _, order := range rows {
if err4 := v.orderRetry(ctx, order); err4 != nil {
return err4
}
}
return nil
})
}

View File

@ -11,41 +11,6 @@ import (
"voucher/internal/biz/vo"
)
func (c *CmbService) OrderSuccess(ctx context.Context, orderNo string) (*v1.CmbReply, error) {
bizReply := &v1.CmbOrderReply{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
CodeNo: orderNo,
}
replyBizContent, _ := json.Marshal(bizReply)
return c.GetResponse(ctx, replyBizContent)
}
func (c *CmbService) OrderFail(ctx context.Context, err error) (*v1.CmbReply, error) {
se := errors.FromError(err)
if len(se.Reason) == 0 {
se.Reason = err2.CmbErr_CMB_UNKNOWN.String()
}
log.Errorf("order fail: %v", se)
bizReply := &v1.CmbOrderReply{
RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: se.Message,
CodeNo: "",
ThirdErrCode: se.Reason,
}
replyBizContent, _ := json.Marshal(bizReply)
return c.GetResponse(ctx, replyBizContent)
}
func (c *CmbService) Order(ctx context.Context, request *v1.CmbRequest) (*v1.CmbReply, error) {
orderNo, err := c.order(ctx, request)
@ -84,3 +49,38 @@ func (c *CmbService) order(ctx context.Context, request *v1.CmbRequest) (string,
return orderNo, nil
}
func (c *CmbService) OrderSuccess(ctx context.Context, orderNo string) (*v1.CmbReply, error) {
bizReply := &v1.CmbOrderReply{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
CodeNo: orderNo,
}
replyBizContent, _ := json.Marshal(bizReply)
return c.GetResponse(ctx, replyBizContent)
}
func (c *CmbService) OrderFail(ctx context.Context, err error) (*v1.CmbReply, error) {
se := errors.FromError(err)
if len(se.Reason) == 0 {
se.Reason = err2.CmbErr_CMB_UNKNOWN.String()
}
log.Errorf("order fail: %v", se)
bizReply := &v1.CmbOrderReply{
RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: se.Message,
CodeNo: "",
ThirdErrCode: se.Reason,
}
replyBizContent, _ := json.Marshal(bizReply)
return c.GetResponse(ctx, replyBizContent)
}