add cancel

This commit is contained in:
duyu 2024-07-26 15:09:57 +08:00
parent 347c44ce01
commit 06ca99cf73
8 changed files with 119 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package orders
import (
"strconv"
"time"
"com.snow.auto_monitor/app/constants/errorcode"
@ -8,6 +9,7 @@ import (
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"
transServ "com.snow.auto_monitor/app/services/transfersys"
"github.com/gin-gonic/gin"
)
@ -197,6 +199,33 @@ func Update(c *gin.Context) {
common.Success(c, response)
}
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
}
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 Delete(c *gin.Context) {
request := new(orderEnt.DeleteReq)
err := common.GenRequest(c, request)

View File

@ -18,6 +18,11 @@ func GetOrder(c *gin.Context) {
common.Error(c, 400, err.Error())
return
}
err = orderMod.GetInstance().OrderOutQueue(item.Id)
if err != nil {
common.Error(c, 400, err.Error())
return
}
var response *transEnt.GetOrderResp = nil
@ -80,7 +85,7 @@ func FinishOrder(c *gin.Context) {
affected, err := transServ.FinishOrder(orders)
if err != nil || affected == 0 {
common.Error(c, 400, "无法修改已完成订单")
common.Error(c, 400, "完成订单失败")
return
}

View File

@ -90,6 +90,14 @@ type UpdateResp struct {
Id int64 `json:"id" `
}
type CancelReq struct {
Id int64 `json:"id" validate:"required"`
}
type CancelResp struct {
Id int64 `json:"id" `
}
type DeleteReq struct {
Id int64 `json:"id" validate:"required"`
}

View File

@ -73,6 +73,7 @@ func RegisterRoute(router *gin.Engine) {
{
orders.POST("/get_by_id", ordersCon.GetById)
orders.POST("/search", ordersCon.Search)
orders.POST("/cancel", ordersCon.Cancel)
// orders.POST("/create", ordersCon.Create)
// orders.POST("/update", ordersCon.Update)
// orders.POST("/delete", ordersCon.Delete)

View File

@ -2,6 +2,7 @@ package orders
import (
"encoding/json"
"errors"
"sync"
"time"
@ -242,7 +243,7 @@ func (m *ordersModel) GetIdleOrder() (orders *Orders, has bool, err error) {
orders = nil
return
}
orders.TransferStatus = 2 // 1.成功 2.充值中 3. 等待充值 4.充值失败 5.异常需要人工处理
orders.TransferStatus = 7 // 1.成功 2.充值中 3. 等待充值 4.充值失败 5.异常需要人工处理 6.取消订单 7.订单入队
affected, err := session.ID(orders.Id).Where("transfer_status = 3").Update(orders)
if err != nil || affected == 0 {
orders = nil
@ -252,6 +253,70 @@ func (m *ordersModel) GetIdleOrder() (orders *Orders, has bool, err error) {
return
}
func (m *ordersModel) OrderOutQueue(orderId int64) (err error) {
orders := &Orders{}
session := m.GetDb().NewSession()
defer session.Close()
// add Begin() before any action
if err = session.Begin(); err != nil {
orders = nil
return
}
orders = &Orders{}
has, err := session.ID(orderId).Get(orders)
if err != nil || !has {
orders = nil
return
}
if orders.TransferStatus != 7 {
err = errors.New("订单已取消或者未在队列中")
return
}
orders.TransferStatus = 2 // 1.成功 2.充值中 3. 等待充值 4.充值失败 5.异常需要人工处理 6.取消订单 7.订单入队
affected, err := session.ID(orders.Id).Where("transfer_status = 7").Update(orders)
if err != nil || affected == 0 {
orders = nil
return
}
err = session.Commit()
return
}
func (m *ordersModel) CancelOrder(orderId int64) (affected int64, err error) {
orders := &Orders{}
session := m.GetDb().NewSession()
defer session.Close()
// add Begin() before any action
if err = session.Begin(); err != nil {
orders = nil
return
}
orders = &Orders{}
has, err := session.ID(orderId).Get(orders)
if err != nil || !has {
orders = nil
return
}
orders.Status = 5
orders.TransferStatus = 6 // 1.成功 2.充值中 3. 等待充值 4.充值失败 5.异常需要人工处理 6.取消订单 7.订单入队
orders.FailReason = "订单取消"
affected, err = session.ID(orders.Id).Where("transfer_status not in (1,2,4,5,6)").Update(orders)
if err != nil || affected == 0 {
err = errors.New("订单正在充值或者已经完成")
orders = nil
return
}
err = session.Commit()
return
}
func (m *ordersModel) SetDealingOrder(orders *Orders) (affected int64, err error) {
session := m.GetDb().NewSession()

View File

@ -70,6 +70,11 @@ func Update(orders *models.Orders) (affected int64, err error) {
return
}
func Cancel(id int64) (affected int64, err error) {
affected, err = models.GetInstance().CancelOrder(id)
return
}
func Delete(id int64) (affected int64, err error) {
affected, err = models.GetInstance().Delete(id)
return

View File

@ -35,6 +35,8 @@ func ReadGroup() (order *models.Orders, err error) {
}
jsonstr := res[key].(string)
json.Unmarshal([]byte(jsonstr), &order)
return order, nil
}

View File

@ -18,8 +18,8 @@ create table orders (
notify_url varchar(255),
extend_parameter varchar(1024),
status tinyint,-- 1.成功 2.充值中 3.充值失败 4.异常需要人工处理
transfer_status tinyint,-- 1.成功 2.充值中 3. 等待充值 4.充值失败 5.异常需要人工处理
status tinyint,-- 1.成功 2.充值中 3.充值失败 4.异常需要人工处理 5.取消订单
transfer_status tinyint,-- 1.成功 2.充值中(出队) 3. 等待充值(等待入队) 4.充值失败 5.异常需要人工处理 6.取消订单 7.订单入队
fail_reason varchar(255),
created_at timestamp default current_timestamp
);