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

216 lines
4.8 KiB
Go

package orders
import (
"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"
"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,
OutTradeNo: item.OutTradeNo,
RechargeAccount: item.RechargeAccount,
AccountType: item.AccountType,
Number: item.Number,
NotifyUrl: item.NotifyUrl,
ExtendParameter: item.ExtendParameter,
Status: item.Status,
TransferStatus: item.TransferStatus,
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,
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,
OutTradeNo: item.OutTradeNo,
RechargeAccount: item.RechargeAccount,
AccountType: item.AccountType,
Number: item.Number,
NotifyUrl: item.NotifyUrl,
ExtendParameter: item.ExtendParameter,
Status: item.Status,
TransferStatus: item.TransferStatus,
CreatedAt: item.CreatedAt.Format(time.RFC3339),
})
}
}
total, err := orderServ.CountAll(
request.Id,
request.OrderNo,
request.MerchantId,
request.ProductId,
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,
}
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,
}
affected, err := orderServ.Update(orders)
if err != nil && affected > 0 {
common.Error500(c)
return
}
response := &orderEnt.UpdateResp{
Id: orders.Id,
}
common.Success(c, response)
}
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)
}