234 lines
5.4 KiB
Go
234 lines
5.4 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
"time"
|
|
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
|
|
}
|
|
|
|
_ = v.bbToWx(ctx, order, reply)
|
|
|
|
return reply, nil
|
|
}
|
|
|
|
func (v *VoucherBiz) cmbOrder(ctx context.Context, request *v1.CmbRequest) (*bo.OrderBo, error) {
|
|
|
|
bizContent, err := v.CmbMixRepo.OrderVerify(ctx, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
product, err3 := v.ProductRepo.GetByProductNo(ctx, bizContent.ActivityId)
|
|
if err3 != nil {
|
|
return nil, err
|
|
}
|
|
|
|
order, err := v.Order(ctx, product, bizContent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
order.MiniMum = product.MiniMum
|
|
order.CouponValue = product.Amount
|
|
|
|
return order, nil
|
|
}
|
|
|
|
func (v *VoucherBiz) Order(ctx context.Context, product *bo.ProductBo, bizContent *v1.CmbOrderRequest) (order *bo.OrderBo, err error) {
|
|
|
|
order, err = v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, bizContent.TransactionId)
|
|
if err != nil && !err2.IsDbNotFound(err) {
|
|
return order, err
|
|
}
|
|
|
|
if order != nil {
|
|
|
|
if order.Status.IsFail() || order.Status.IsIng() {
|
|
|
|
if err4 := v.orderRetry(ctx, order); err4 != nil {
|
|
return order, err4
|
|
}
|
|
}
|
|
|
|
return order, err
|
|
}
|
|
|
|
order, err = v.order(ctx, product, bizContent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return order, nil
|
|
}
|
|
|
|
func (v *VoucherBiz) order(ctx context.Context, product *bo.ProductBo, bizContent *v1.CmbOrderRequest) (*bo.OrderBo, error) {
|
|
|
|
req := &bo.OrderCreateReqBo{
|
|
OutBizNo: bizContent.TransactionId,
|
|
ProductNo: bizContent.ActivityId,
|
|
Account: bizContent.CmbUid,
|
|
AppID: bizContent.AppId,
|
|
Attach: bizContent.Attach,
|
|
AccountType: vo.OrderAccountTypeOpenId,
|
|
Type: vo.OrderTypeCmb,
|
|
NotifyUrl: v.bc.Cmb.NotifyUrl,
|
|
}
|
|
|
|
order, err := v.create(ctx, req, product)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_ = v.cmbToBB(ctx, bizContent)
|
|
|
|
//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
|
|
//}
|
|
|
|
//休眠100微妙
|
|
time.Sleep(time.Millisecond * 100)
|
|
//mock发券成功,测试使用
|
|
voucherNo := order.OrderNo
|
|
|
|
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 {
|
|
|
|
// mock发券成功,测试使用
|
|
voucherNo := order.OrderNo
|
|
|
|
//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 (v *VoucherBiz) UpdateOrderStatus(ctx context.Context, orderId uint64, status vo.OrderStatus) error {
|
|
|
|
if status.IsSuccess() {
|
|
|
|
return v.OrderRepo.Available(ctx, orderId)
|
|
|
|
} else if status.IsUse() {
|
|
|
|
return v.OrderRepo.Used(ctx, orderId)
|
|
|
|
} else if status.IsExpired() {
|
|
|
|
return v.OrderRepo.Expired(ctx, orderId)
|
|
}
|
|
|
|
return fmt.Errorf("notice 未知券状态,orderId:%d,statuText:%s", orderId, status.GetText())
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
bizReply := &v1.CmbOrderReply{
|
|
RespCode: vo.CmbResponseStatusFail.GetValue(),
|
|
RespMsg: se.Message,
|
|
CodeNo: "",
|
|
ThirdErrCode: se.Reason,
|
|
}
|
|
|
|
replyBizContent, _ := json.Marshal(bizReply)
|
|
|
|
return c.GetResponse(ctx, replyBizContent)
|
|
}
|