migu_server/app/admin/apis/migumanage/migu_notice.go

355 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package migumanage
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/logger"
"go-admin/app/admin/models"
"go-admin/tools"
"go-admin/tools/app"
"gorm.io/gorm"
"net/http"
"time"
)
// 以下接口是回调接口
// SubscribeNotice 订购成功通知接口
// @Summary 订购成功通知接口
// @Tags 2024-咪咕-回调
// @Produce json
// @Accept json
// @Param request body models.NewSendDataReq true "订购成功通知接口"
// @Success 200 {object} models.OppoSendDataResp
// @Router /api/v1/notice/subscribe [get]
func (e MiGuDeployService) SubscribeNotice(c *gin.Context) {
fmt.Println("SubscribeNotice-start")
logger.Info("订购通知-start")
fmt.Println("URL:", c.Request.URL.String())
orderId := c.Query("orderId") // 对应linkId
extData := c.Query("extData") // 对应outTradeNo
status := c.Query("status") // status: 1成功 -1: 失败
rsCode := c.Query("rsCode") // 失败编码
rsMsg := c.Query("rsMsg") // 失败原因
fmt.Println("orderId:", orderId)
fmt.Println("extData:", extData)
fmt.Println("status:", status)
logger.Info(fmt.Sprintf("orderId is:%s, extData is:%s, status is:%s", orderId, extData, status))
if orderId == "" { // 没有返回linkId
logger.Error("orderId is null")
app.MiGuNoticeOK(c, "failed")
return
}
if !(status == "1" || status == "-1") {
logger.Error("status is", status)
app.MiGuNoticeOK(c, fmt.Sprintf("failed,status is:%s", status))
return
}
err := e.MakeContext(c).MakeOrm().Errors
if err != nil {
fmt.Println("MakeContext err:", err)
e.Logger.Error(err)
app.MiGuNoticeOK(c, "failed")
return
}
if status == "1" { // 订购成功
fmt.Println("订购成功通知status is 1")
logger.Info("订购成功通知status is 1")
var orderInfo models.MgOrder
for i := 0; i < 3; i++ {
// 查询订单表是否有记录
err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).First(&orderInfo).Error
if err != nil && err.Error() != "record not found" {
logger.Errorf("SubscribeNotice query mg_order err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
if errors.Is(err, gorm.ErrRecordNotFound) { // 没有查询到记录循环查询3次
time.Sleep(800 * time.Millisecond)
} else if err == nil { // 查询到记录,则跳出循环
break
}
}
var checkPhoneNum string
if errors.Is(err, gorm.ErrRecordNotFound) { // 订单表没有记录
// 查询交易流水表
var logInfo models.MgTransactionLog
err = e.Orm.Table("mg_transaction_log").
Where("link_id = ?", orderId).First(&logInfo).Error
if err != nil {
logger.Errorf("SubscribeNotice query mg_transaction_log err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
checkPhoneNum = logInfo.PhoneNumber
// 插入订单表
var inOrder models.MgOrder
inOrder.CreatedAt = logInfo.CreatedAt
inOrder.UpdatedAt = logInfo.UpdatedAt
inOrder.ProductID = logInfo.ProductID
inOrder.ChannelCode = logInfo.ChannelCode
inOrder.OrderSerial = logInfo.OutTradeNo
strTime := time.Now()
inOrder.SubscribeTime = &strTime
inOrder.PhoneNumber = logInfo.PhoneNumber
inOrder.SM4PhoneNumber, _ = tools.SM4Encrypt(models.SM4KEy, logInfo.PhoneNumber)
inOrder.ExternalOrderID = logInfo.LinkId
inOrder.ChannelTradeNo = logInfo.ChannelTradeNo
inOrder.State = models.SubscribeOK
err = e.Orm.Create(&inOrder).Error
if err != nil {
logger.Info("Create MgOrder err:", err)
app.MiGuError(c, http.StatusBadRequest, err, err.Error())
return
}
orderInfo.ChannelCode = inOrder.ChannelCode
orderInfo.OrderSerial = inOrder.OrderSerial
orderInfo.ChannelTradeNo = inOrder.ChannelTradeNo
} else { // 订单表有记录
checkPhoneNum = orderInfo.PhoneNumber
if orderInfo.State != 1 {
err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).Updates(map[string]interface{}{
"state": models.SubscribeOK,
"subscribe_time": time.Now(),
"updated_at": time.Now(),
}).Error
if err != nil {
logger.Errorf("SubscribeNotice update mg_order err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
}
}
// 判断是否为子渠道订购,是的话需要通知子渠道
if models.IsValidChannelEx(orderInfo.ChannelCode, e.Orm) {
channelInfo, err := models.GetChannelInfoByChannelCode(orderInfo.ChannelCode, e.Orm)
if err != nil {
fmt.Println("SubscribeNotice GetChannelInfoByChannelCode err:", err)
fmt.Println("SubscribeNotice sub_channel_code is:", orderInfo.ChannelCode)
logger.Errorf("SubscribeNotice GetChannelInfoByChannelCode err, sub_channel_code is:", orderInfo.ChannelCode)
}
if channelInfo.SubscribeURL != "" {
for i := 0; i < 3; i++ {
resp, err := models.NoticeSubChannel(channelInfo.SubscribeURL, orderInfo.OrderSerial,
orderInfo.ChannelTradeNo, "1", channelInfo.MainChannelCode, channelInfo.Remarks,
"", "")
if err != nil {
fmt.Println("NoticeSubChannel err:", err)
fmt.Println("i is:", i)
continue
}
if resp != "success" {
continue
} else {
break
}
}
} else {
fmt.Println("SubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode)
logger.Error("SubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode)
}
}
var req models.QueryRightsInfoReq
productChannelCode := models.GetProductChannelCode(orderInfo.ChannelCode)
req.AppChannelList = append(req.AppChannelList, productChannelCode)
req.Mobile = checkPhoneNum
resp, err := models.MiGuQueryRightsInfo(&req)
if err != nil {
fmt.Println("CheckOrderState MiGuQueryRightsInfo err:", err.Error())
logger.Errorf("CheckOrderState MiGuQueryRightsInfo err:", err.Error())
}
fmt.Println("CheckOrderState MiGuQueryRightsInfo resp:", resp)
fmt.Println("订购成功通知-end")
logger.Info("订购成功通知-end")
} else if status == "-1" { // 订购失败
fmt.Println("订购失败通知status is -1")
logger.Info("订购失败通知status is -1")
// 查询交易流水表
var logInfo models.MgTransactionLog
err = e.Orm.Table("mg_transaction_log").
Where("link_id = ?", orderId).First(&logInfo).Error
if err != nil {
logger.Errorf("SubscribeNotice query mg_transaction_log err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
// 解析错误信息
msg, _ := tools.DecodeURL(rsMsg)
// 更新交易流水表
err = e.Orm.Table("mg_transaction_log").Where("link_id = ?", orderId).Updates(map[string]interface{}{
"result": rsCode,
"reason": msg,
"updated_at": time.Now(),
}).Error
if err != nil {
logger.Errorf("SubscribeNotice update mg_transaction_log err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
// 判断是否为子渠道订购,是的话需要通知子渠道
if models.IsValidChannelEx(logInfo.ChannelCode, e.Orm) {
channelInfo, err := models.GetChannelInfoByChannelCode(logInfo.ChannelCode, e.Orm)
if err != nil {
fmt.Println("SubscribeNotice GetChannelInfoByChannelCode err:", err)
fmt.Println("SubscribeNotice sub_channel_code is:", logInfo.ChannelCode)
logger.Errorf("SubscribeNotice GetChannelInfoByChannelCode err, sub_channel_code is:", logInfo.ChannelCode)
}
if channelInfo.SubscribeURL != "" {
for i := 0; i < 3; i++ {
resp, err := models.NoticeSubChannel(channelInfo.SubscribeURL, logInfo.OutTradeNo,
logInfo.ChannelTradeNo, "-1", channelInfo.MainChannelCode, channelInfo.Remarks,
rsCode, rsMsg)
if err != nil {
fmt.Println("NoticeSubChannel err:", err)
fmt.Println("i is:", i)
continue
}
if resp != "success" {
continue
} else {
break
}
}
} else {
fmt.Println("SubscribeNotice SubscribeURL is null, sub_channel_code is:", logInfo.ChannelCode)
logger.Error("SubscribeNotice SubscribeURL is null, sub_channel_code is:", logInfo.ChannelCode)
}
}
fmt.Println("订购失败通知-end")
logger.Info("订购失败通知-end")
}
fmt.Println("SubscribeNotice-end")
logger.Info("订购通知-end")
app.MiGuNoticeOK(c, "success")
return
}
// UnsubscribeNotice 退订通知接口
// @Summary 退订通知接口
// @Tags 2024-咪咕-回调
// @Produce json
// @Accept json
// @Param request body models.NewSendDataReq true "退订通知接口"
// @Success 200 {object} models.OppoSendDataResp
// @Router /api/v1/notice/unsubscribe [get]
func (e MiGuDeployService) UnsubscribeNotice(c *gin.Context) {
fmt.Println("UnsubscribeNotice-start")
logger.Info("退订通知-start")
fmt.Println("URL:", c.Request.URL.String())
orderId := c.Query("orderId") // 对应linkId
extData := c.Query("extData") // 对应outTradeNo
status := c.Query("status") // status: 1成功 不存在status也认为是订购成功通知
fmt.Println("orderId:", orderId)
fmt.Println("extData:", extData)
fmt.Println("status:", status)
logger.Info(fmt.Sprintf("orderId is:%s, extData is:%s, status is:%s", orderId, extData, status))
if orderId == "" { // 没有返回linkId
logger.Error("orderId is null")
app.MiGuNoticeOK(c, "failed")
return
}
if !(status == "2") {
logger.Error("status is", status)
app.MiGuNoticeOK(c, fmt.Sprintf("failed,status is:%s", status))
return
}
err := e.MakeContext(c).MakeOrm().Errors
if err != nil {
fmt.Println("MakeContext err:", err)
e.Logger.Error(err)
app.MiGuNoticeOK(c, "failed")
return
}
// 查询是否有记录
var orderInfo models.MgOrder
err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).First(&orderInfo).Error
if err != nil {
logger.Errorf("UnsubscribeNotice query mg_order err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
if orderInfo.State != 2 { // 订单不是退订状态才更新
err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).Updates(map[string]interface{}{
"state": models.UnsubscribeOK,
"unsubscribe_time": time.Now(),
"updated_at": time.Now(),
}).Error
if err != nil {
logger.Errorf("UnsubscribeNotice update mg_order err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
}
// 判断是否为子渠道订购,是的话需要通知子渠道
if models.IsValidChannelEx(orderInfo.ChannelCode, e.Orm) {
channelInfo, err := models.GetChannelInfoByChannelCode(orderInfo.ChannelCode, e.Orm)
if err != nil {
fmt.Println("UnsubscribeNotice GetChannelInfoByChannelCode err:", err)
fmt.Println("UnsubscribeNotice sub_channel_code is:", orderInfo.ChannelCode)
logger.Errorf("UnsubscribeNotice GetChannelInfoByChannelCode err, sub_channel_code is:", orderInfo.ChannelCode)
}
if channelInfo.SubscribeURL != "" {
for i := 0; i < 3; i++ {
resp, err := models.NoticeSubChannel(channelInfo.SubscribeURL, orderInfo.OrderSerial,
orderInfo.ChannelTradeNo, "2", channelInfo.MainChannelCode, channelInfo.Remarks,
"", "")
if err != nil {
continue
}
if resp != "success" {
continue
} else {
break
}
}
} else {
fmt.Println("UnsubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode)
logger.Error("UnsubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode)
}
}
fmt.Println("UnsubscribeNotice-end")
logger.Info("退订通知-end")
app.MiGuNoticeOK(c, "success")
return
}