voucher/internal/biz/order.go

215 lines
4.9 KiB
Go

package biz
import (
"context"
"encoding/json"
"fmt"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
err2 "voucher/api/err"
v1 "voucher/api/v1"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
)
func (v *VoucherBiz) CmbOrder(ctx context.Context, request *v1.CmbRequest) (*v1.CmbReply, error) {
order, err := v.cmbOrder(ctx, request)
if err != nil {
return v.OrderFail(ctx, err)
}
reply, err := v.OrderSuccess(ctx, order.OrderNo)
if err != nil {
return nil, err
}
if err = v.bbToWx(ctx, order, reply); err != nil {
log.Errorf("处理请求kg报错:%s", err.Error())
}
return reply, nil
}
func (c *VoucherBiz) cmbOrder(ctx context.Context, request *v1.CmbRequest) (*bo.OrderBo, error) {
bizContent, err := c.CmbMixRepo.OrderVerify(ctx, request)
if err != nil {
return nil, err
}
boReq := &bo.OrderCreateReqBo{
OutBizNo: bizContent.TransactionId,
ProductNo: bizContent.ActivityId,
Account: bizContent.CmbUid,
AppID: bizContent.AppId,
Attach: bizContent.Attach,
AccountType: vo.OrderAccountTypeOpenId,
Type: vo.OrderTypeCmb,
NotifyUrl: c.bc.Cmb.NotifyUrl,
}
order, err := c.Order(ctx, boReq, bizContent)
if err != nil {
return nil, err
}
return order, nil
}
func (v *VoucherBiz) Order(ctx context.Context, req *bo.OrderCreateReqBo, cmbReq *v1.CmbOrderRequest) (order *bo.OrderBo, err error) {
product, err3 := v.ProductRepo.GetByProductNo(ctx, req.ProductNo)
if err3 != nil {
return order, err3
}
order, err = v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo)
if err != nil && !err2.IsDbNotFound(err) {
return order, err
}
if order != nil {
order.MiniMum = product.MiniMum
if order.Status.IsFail() {
if err4 := v.orderRetry(ctx, order); err4 != nil {
return order, err4
}
}
return order, err
}
order, err = v.order(ctx, req, product, cmbReq)
if err != nil {
return nil, err
}
order.MiniMum = product.MiniMum
return order, nil
}
func (v *VoucherBiz) order(ctx context.Context, req *bo.OrderCreateReqBo, product *bo.ProductBo, cmbReq *v1.CmbOrderRequest) (*bo.OrderBo, error) {
order, err := v.create(ctx, req, product)
if err != nil {
return nil, err
}
_ = v.cmbToBB(ctx, cmbReq)
if err = v.registerNotifyTag(ctx, order.MerchantNo, order.BatchNo); err != nil {
return nil, err
}
voucherNo, err := v.WechatCpnRepo.Order(ctx, order)
if err != nil {
if err3 := v.fail(ctx, order, err); err3 != nil {
return nil, err3
}
return nil, err
}
if err = v.success(ctx, order, voucherNo); err != nil {
return order, err
}
return order, nil
}
func (v *VoucherBiz) orderRetry(ctx context.Context, order *bo.OrderBo) error {
voucherNo, err := v.WechatCpnRepo.Order(ctx, order)
if err != nil {
if err3 := v.fail(ctx, order, err); err3 != nil {
return err3
}
return err
}
return v.success(ctx, order, voucherNo)
}
func (v *VoucherBiz) create(ctx context.Context, req *bo.OrderCreateReqBo, product *bo.ProductBo) (*bo.OrderBo, error) {
o := &bo.OrderBo{
OrderNo: v.GenerateMixRepo.GeneratorString(ctx, fmt.Sprintf("%d%s", req.Type, req.OutBizNo)),
OutBizNo: req.OutBizNo,
ProductNo: req.ProductNo,
Account: req.Account,
AppID: req.AppID,
MerchantNo: product.MchId,
Channel: product.Channel,
BatchNo: product.BatchNo,
NotifyUrl: req.NotifyUrl,
AccountType: vo.OrderAccountTypeOpenId,
Type: req.Type,
Status: vo.OrderStatusIng, // 同步发放,状态至为发放中
Attach: req.Attach,
}
return v.OrderRepo.Create(ctx, o)
}
func (v *VoucherBiz) ing(ctx context.Context, id uint64) error {
return v.OrderRepo.Ing(ctx, id)
}
func (v *VoucherBiz) success(ctx context.Context, order *bo.OrderBo, voucherNo string) error {
order.VoucherNo = voucherNo
return v.OrderRepo.Success(ctx, order.ID, voucherNo)
}
func (v *VoucherBiz) fail(ctx context.Context, order *bo.OrderBo, errReq error) error {
if err := v.OrderRepo.Fail(ctx, order.ID, errReq.Error()); err != nil {
return err
}
if err2.IsWechatAccountFail(errReq) || err2.IsWechatUserMaxCoupons(errReq) {
return nil // 过滤调该类型错误通知
}
return v.alarm(ctx, order, errReq.Error())
}
func (c *VoucherBiz) 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 *VoucherBiz) 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)
}