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

218 lines
6.4 KiB
Go
Raw Normal View History

2024-10-18 15:46:54 +00:00
package migumanage
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/logger"
"go-admin/app/admin/models"
"go-admin/tools/app"
"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成功 不存在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 == "1" || status == "") {
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("SubscribeNotice query mg_order err:", err.Error())
app.MiGuNoticeOK(c, "failed")
return
}
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")
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)
}
}
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")
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
}