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

307 lines
6.7 KiB
Go
Raw Normal View History

2024-07-12 18:11:21 +08:00
package orders
import (
2024-07-26 15:09:57 +08:00
"strconv"
2024-07-12 18:11:21 +08:00
"time"
"com.snow.auto_monitor/app/constants/errorcode"
common "com.snow.auto_monitor/app/http/controllers"
orderEnt "com.snow.auto_monitor/app/http/entities/orders"
orderMod "com.snow.auto_monitor/app/models/orders"
orderServ "com.snow.auto_monitor/app/services/orders"
2024-07-26 15:09:57 +08:00
transServ "com.snow.auto_monitor/app/services/transfersys"
2024-07-12 18:11:21 +08:00
"github.com/gin-gonic/gin"
)
func GetById(c *gin.Context) {
request := new(orderEnt.GetListByIdReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
item, err := orderServ.GetById(request.Id)
if err != nil {
common.Error500(c)
return
}
var response *orderEnt.GetListByIdResp = nil
if item != nil {
response = &orderEnt.GetListByIdResp{
Id: item.Id,
OrderNo: item.OrderNo,
MerchantId: item.MerchantId,
ProductId: item.ProductId,
2024-08-07 13:46:10 +08:00
DeviceNo: item.DeviceNo,
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),
}
}
common.Success(c, response)
}
func Search(c *gin.Context) {
request := new(orderEnt.SearchReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
start, end := "", ""
if len(request.CreatedAt) > 1 {
start = request.CreatedAt[0]
end = request.CreatedAt[1]
}
res, err := orderServ.Search(
request.Id,
request.OrderNo,
request.MerchantId,
request.ProductId,
2024-08-07 13:46:10 +08:00
request.DeviceNo,
2024-07-12 18:11:21 +08:00
request.OutTradeNo,
request.RechargeAccount,
request.AccountType,
request.Status,
request.TransferStatus,
start,
end,
request.PageSize,
request.PageNum,
)
if err != nil {
common.Error500(c)
return
}
var response []*orderEnt.SearchResp = nil
if len(res) > 0 {
for _, item := range res {
response = append(response, &orderEnt.SearchResp{
Id: item.Id,
OrderNo: item.OrderNo,
MerchantId: item.MerchantId,
ProductId: item.ProductId,
2024-08-07 13:46:10 +08:00
DeviceNo: item.DeviceNo,
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),
})
}
}
total, err := orderServ.CountAll(
request.Id,
request.OrderNo,
request.MerchantId,
request.ProductId,
2024-08-07 13:46:10 +08:00
request.DeviceNo,
2024-07-12 18:11:21 +08:00
request.OutTradeNo,
request.RechargeAccount,
request.AccountType,
request.Status,
request.TransferStatus,
start,
end,
)
if err != nil {
common.Error500(c)
return
}
common.SuccessWithList(c, response, total)
}
func Create(c *gin.Context) {
request := new(orderEnt.CreateReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
orders := &orderMod.Orders{
OrderNo: "12321",
MerchantId: request.MerchantId,
ProductId: request.ProductId,
OutTradeNo: request.OutTradeNo,
RechargeAccount: request.RechargeAccount,
AccountType: request.AccountType,
Number: request.Number,
NotifyUrl: request.NotifyUrl,
ExtendParameter: request.ExtendParameter,
Status: request.Status,
TransferStatus: request.TransferStatus,
2024-07-22 10:16:48 +08:00
FailReason: request.FailReason,
2024-07-12 18:11:21 +08:00
}
affected, err := orderServ.Create(orders)
if err != nil && affected > 0 {
common.Error500(c)
return
}
response := &orderEnt.CreateResp{
Id: orders.Id,
}
common.Success(c, response)
}
func Update(c *gin.Context) {
request := new(orderEnt.UpdateReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
orders := &orderMod.Orders{
Id: request.Id,
MerchantId: request.MerchantId,
ProductId: request.ProductId,
OutTradeNo: request.OutTradeNo,
RechargeAccount: request.RechargeAccount,
AccountType: request.AccountType,
Number: request.Number,
NotifyUrl: request.NotifyUrl,
ExtendParameter: request.ExtendParameter,
Status: request.Status,
TransferStatus: request.TransferStatus,
2024-07-22 10:16:48 +08:00
FailReason: request.FailReason,
2024-07-12 18:11:21 +08:00
}
affected, err := orderServ.Update(orders)
if err != nil && affected > 0 {
common.Error500(c)
return
}
response := &orderEnt.UpdateResp{
Id: orders.Id,
}
common.Success(c, response)
}
2024-07-26 15:09:57 +08:00
func Cancel(c *gin.Context) {
request := new(orderEnt.CancelReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
affected, err := orderServ.Cancel(request.Id)
if err != nil || affected == 0 {
common.Error(c, 400, err.Error())
return
}
2024-08-30 15:16:16 +08:00
err = transServ.AddCallback(strconv.Itoa(int(request.Id)))
if err != nil {
common.Error(c, 400, "添加回调失败")
return
}
response := &orderEnt.CancelResp{
Id: request.Id,
}
common.Success(c, response)
}
func DangerousConfirm(c *gin.Context) {
request := new(orderEnt.CancelReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
affected, err := orderServ.DangerousConfirm(request.Id)
if err != nil || affected == 0 {
common.Error(c, 400, err.Error())
return
}
2024-07-26 15:09:57 +08:00
err = transServ.AddCallback(strconv.Itoa(int(request.Id)))
if err != nil {
common.Error(c, 400, "添加回调失败")
return
}
response := &orderEnt.CancelResp{
Id: request.Id,
}
common.Success(c, response)
}
2024-08-21 19:50:25 +08:00
func DangerousCancel(c *gin.Context) {
request := new(orderEnt.CancelReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
affected, err := orderServ.DangerousCancel(request.Id)
if err != nil || affected == 0 {
common.Error(c, 400, err.Error())
return
}
err = transServ.AddCallback(strconv.Itoa(int(request.Id)))
if err != nil {
common.Error(c, 400, "添加回调失败")
return
}
response := &orderEnt.CancelResp{
Id: request.Id,
}
common.Success(c, response)
}
2024-07-12 18:11:21 +08:00
func Delete(c *gin.Context) {
request := new(orderEnt.DeleteReq)
err := common.GenRequest(c, request)
if err != nil {
common.Error(c, errorcode.ParamError)
return
}
affected, err := orderServ.Delete(request.Id)
if err != nil && affected > 0 {
common.Error500(c)
return
}
response := &orderEnt.DeleteResp{
Id: request.Id,
}
common.Success(c, response)
}