com.snow.auto_monitor/app/http/controllers/transfersys/transfersys.go

267 lines
6.2 KiB
Go
Raw Permalink Normal View History

2024-07-12 18:11:21 +08:00
package orders
import (
2024-07-26 10:52:53 +08:00
"strconv"
2024-07-12 18:11:21 +08:00
"time"
common "com.snow.auto_monitor/app/http/controllers"
transEnt "com.snow.auto_monitor/app/http/entities/transfersys"
orderMod "com.snow.auto_monitor/app/models/orders"
2024-07-17 17:04:25 +08:00
proServ "com.snow.auto_monitor/app/services/product"
transServ "com.snow.auto_monitor/app/services/transfersys"
2024-07-12 18:11:21 +08:00
"github.com/gin-gonic/gin"
)
func GetOrder(c *gin.Context) {
2024-09-10 14:26:07 +08:00
//队列里取出一个订单
item, err := transServ.ReadGroup(0)
2024-07-12 18:11:21 +08:00
if err != nil {
common.Error(c, 400, err.Error())
return
}
2024-09-10 14:26:07 +08:00
//标记订单已经去除
2024-07-26 15:09:57 +08:00
err = orderMod.GetInstance().OrderOutQueue(item.Id)
if err != nil {
common.Error(c, 400, err.Error())
return
}
2024-07-12 18:11:21 +08:00
var response *transEnt.GetOrderResp = nil
2024-09-10 14:26:07 +08:00
//获取产品信息
2024-07-17 17:04:25 +08:00
product, err := proServ.GetById(item.ProductId)
if err != nil {
common.Error(c, 400, "产品不存在")
return
}
2024-09-10 14:26:07 +08:00
if item != nil {
response = &transEnt.GetOrderResp{
Id: item.Id,
OrderNo: item.OrderNo,
MerchantId: item.MerchantId,
ProductId: item.ProductId,
ProductUrl: product.ProductUrl,
QueueNo: item.QueueNo,
OutTradeNo: item.OutTradeNo,
RechargeAccount: item.RechargeAccount,
AccountType: item.AccountType,
Number: item.Number,
NotifyUrl: item.NotifyUrl,
ExtendParameter: item.ExtendParameter,
Status: item.Status,
TransferStatus: item.TransferStatus,
FailReason: item.FailReason,
CreatedAt: item.CreatedAt.Format(time.RFC3339),
}
} else {
c.JSON(500, gin.H{
"code": 500,
"message": "no data",
})
return
}
common.Success(c, response)
}
2024-07-17 17:04:25 +08:00
2024-09-10 14:26:07 +08:00
func GetOrderFromQueue(c *gin.Context) {
request := new(transEnt.GetOrderReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, 400, err.Error())
return
}
var queue_no int64 = 0
if request.QueueNo != 0 {
queue_no = request.QueueNo
}
//队列里取出一个订单
item, err := transServ.ReadGroup(queue_no)
if err != nil {
common.Error(c, 400, err.Error())
return
}
//标记订单已经去除
err = orderMod.GetInstance().OrderOutQueue(item.Id)
if err != nil {
common.Error(c, 400, err.Error())
return
}
2024-09-29 12:04:36 +08:00
var response *transEnt.GetOrderResp = nil
//获取产品信息
product, err := proServ.GetById(item.ProductId)
if err != nil {
common.Error(c, 400, "产品不存在")
return
}
if item != nil {
response = &transEnt.GetOrderResp{
Id: item.Id,
OrderNo: item.OrderNo,
MerchantId: item.MerchantId,
ProductId: item.ProductId,
ProductUrl: product.ProductUrl,
QueueNo: item.QueueNo,
OutTradeNo: item.OutTradeNo,
RechargeAccount: item.RechargeAccount,
AccountType: item.AccountType,
Number: item.Number,
NotifyUrl: item.NotifyUrl,
ExtendParameter: item.ExtendParameter,
Status: item.Status,
TransferStatus: item.TransferStatus,
FailReason: item.FailReason,
CreatedAt: item.CreatedAt.Format(time.RFC3339),
}
} else {
c.JSON(500, gin.H{
"code": 500,
"message": "no data",
})
return
}
common.Success(c, response)
}
func GetOrderFromQueues(c *gin.Context) {
request := new(transEnt.GetOrderFromQueuesReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, 400, err.Error())
return
}
var queue_nos []int64
if len(request.QueueNos) > 0 {
queue_nos = request.QueueNos
} else {
common.Error(c, 400, "队列号不能为空")
return
}
var item *orderMod.Orders
for _, ord := range queue_nos {
//队列里取出一个订单
item, err = transServ.ReadGroup(ord)
if err != nil {
continue
}
//标记订单已经去除
err = orderMod.GetInstance().OrderOutQueue(item.Id)
if err != nil {
common.Error(c, 400, err.Error())
return
}
break
}
if err != nil {
common.Error(c, 400, err.Error())
return
}
2024-09-10 14:26:07 +08:00
var response *transEnt.GetOrderResp = nil
//获取产品信息
product, err := proServ.GetById(item.ProductId)
if err != nil {
common.Error(c, 400, "产品不存在")
return
}
2024-07-12 18:11:21 +08:00
if item != nil {
response = &transEnt.GetOrderResp{
Id: item.Id,
OrderNo: item.OrderNo,
MerchantId: item.MerchantId,
ProductId: item.ProductId,
2024-07-17 17:04:25 +08:00
ProductUrl: product.ProductUrl,
2024-09-10 14:26:07 +08:00
QueueNo: item.QueueNo,
2024-07-12 18:11:21 +08:00
OutTradeNo: item.OutTradeNo,
RechargeAccount: item.RechargeAccount,
AccountType: item.AccountType,
Number: item.Number,
NotifyUrl: item.NotifyUrl,
ExtendParameter: item.ExtendParameter,
Status: item.Status,
TransferStatus: item.TransferStatus,
2024-07-22 10:16:48 +08:00
FailReason: item.FailReason,
2024-07-12 18:11:21 +08:00
CreatedAt: item.CreatedAt.Format(time.RFC3339),
}
} else {
c.JSON(500, gin.H{
"code": 500,
"message": "no data",
})
return
}
common.Success(c, response)
}
func FinishOrder(c *gin.Context) {
request := new(transEnt.FinishOrderReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, 400, err.Error())
return
}
status := 5
if request.Result == "success" {
status = 1
} else if request.Result == "fail" {
status = 4
}
orders := &orderMod.Orders{
Id: request.Id,
ProductId: request.ProductId,
MerchantId: request.MerchantId,
2024-08-07 13:46:10 +08:00
DeviceNo: request.DeviceNo,
2024-07-12 18:11:21 +08:00
TransferStatus: int64(status),
2024-07-22 10:16:48 +08:00
FailReason: request.FailReason,
2024-07-12 18:11:21 +08:00
}
2024-07-17 17:04:25 +08:00
affected, err := transServ.FinishOrder(orders)
2024-07-12 18:11:21 +08:00
if err != nil || affected == 0 {
2024-07-26 15:09:57 +08:00
common.Error(c, 400, "完成订单失败")
2024-07-12 18:11:21 +08:00
return
}
2024-07-26 10:52:53 +08:00
err = transServ.AddCallback(strconv.Itoa(int(orders.Id)))
if err != nil {
common.Error(c, 400, "添加回调失败")
return
}
2024-07-12 18:11:21 +08:00
response := &transEnt.FinishOrderResp{
Id: orders.Id,
}
common.Success(c, response)
}
2024-08-15 09:13:51 +08:00
func SetOrderMobile(c *gin.Context) {
request := new(transEnt.SetOrderMobileReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, 400, err.Error())
return
}
orders := &orderMod.Orders{
Id: request.Id,
ProductId: request.ProductId,
MerchantId: request.MerchantId,
DeviceNo: request.DeviceNo,
}
affected, err := transServ.SetOrderMobile(orders)
if err != nil || affected == 0 {
common.Error(c, 400, "设置订单失败:"+err.Error())
return
}
response := &transEnt.SetOrderMobileResp{
Id: orders.Id,
}
common.Success(c, response)
}