diff --git a/app/http/controllers/front/OrderController.go b/app/http/controllers/front/OrderController.go index 086d625..a71bad7 100644 --- a/app/http/controllers/front/OrderController.go +++ b/app/http/controllers/front/OrderController.go @@ -4,10 +4,14 @@ import ( "github.com/ahmetb/go-linq/v3" "github.com/gin-gonic/gin" "qteam/app/constants/common" + "qteam/app/constants/errorcode" "qteam/app/http/controllers" "qteam/app/http/entities/front" "qteam/app/models/ordermodel" "qteam/app/services" + "qteam/app/third/market" + "qteam/app/utils" + "qteam/config" "strconv" ) @@ -37,21 +41,36 @@ func OrderQuery(c *gin.Context) { order := ordermodel.Order{OrderNo: request.OrderNo} has, err := services.OrderDetailService(&order) if err != nil { - controllers.Error500(c) + controllers.Error(c, 500, "订单查询失败") return } if has { var state string - if order.State != common.ORDER_STATUS_PAY { + if order.State < common.ORDER_STATUS_PAY { _, rsp := services.OrderXyQuery(request.OrderNo) + utils.Log(nil, "三方订单查询", rsp) if rsp.OrderInfo.TxnStatus == "00" { - state = strconv.Itoa(common.ORDER_STATUS_FINISH) + client := market.NewMarketClient(config.GetConf().OpenApiMarketConfig) + data, err := client.MarketSend(order.OrderNo, strconv.Itoa(order.VoucherId), "", "1") + if err != nil { + controllers.Error(c, 500, "三方订单查询失败") + return + } + if data.ErrCode == "00" { + err := services.OrdersUpdateService(front.OrdersUpdateRequest{Id: order.Id, Status: common.ORDER_STATUS_PAY, VoucherLink: data.Data.ShortUrl}) + if err != nil { + controllers.Error(c, 500, "订单更新失败") + utils.Log(nil, "营销系统下单失败", data) + return + } + } + state = strconv.Itoa(common.ORDER_STATUS_PAY) } - } else { - state = strconv.Itoa(order.State) } + state = strconv.Itoa(order.State) controllers.Success(c, gin.H{"state": state}, "请求成功") } else { - controllers.Error404(c) + controllers.HandCodeRes(c, nil, errorcode.NotFound) + return } } diff --git a/app/http/entities/front/order.go b/app/http/entities/front/order.go index c7d4c5e..d5d07ea 100644 --- a/app/http/entities/front/order.go +++ b/app/http/entities/front/order.go @@ -39,6 +39,7 @@ func (p *OrderQueryResponse) ResponseFromDb(l ordermodel.Order) { } type OrdersUpdateRequest struct { - Id int `json:"id" validate:"required" form:"id" validate:"required" example:"1"` - Status int `json:"status" form:"status" validate:"oneof=1 2 3 4" example:"1"` // '状态(1/待支付,2/已支付,3/已完成4/取消5作废)' + Id int `json:"id" validate:"required" form:"id" validate:"required" example:"1"` + Status int `json:"status" form:"status" validate:"oneof=1 2 3 4" example:"1"` // '状态(1/待支付,2/已支付,3/已完成4/取消5作废)' + VoucherLink string `json:"voucher_link"` } diff --git a/app/services/OrderService.go b/app/services/OrderService.go index 71f100c..52fa636 100644 --- a/app/services/OrderService.go +++ b/app/services/OrderService.go @@ -91,16 +91,26 @@ func OrderDetailService(order *ordermodel.Order) (has bool, err error) { repo := ordermodel.GetInstance().GetDb() conn := builder.NewCond() if order.Id != 0 { - conn = conn.And(builder.Eq{"Id": order.Id}) + conn = conn.And(builder.Eq{"id": order.Id}) } if order.OrderNo != "" { - conn = conn.And(builder.Eq{"OrderNo": order.OrderNo}) + conn = conn.And(builder.Eq{"order_no": order.OrderNo}) } return repo.Where(conn).Get(order) } func OrdersUpdateService(req front.OrdersUpdateRequest) (err error) { repo := ordermodel.GetInstance().GetDb() - _, err = repo.Where("Id = ?", req.Id).Update(&ordermodel.Order{State: req.Status}) + var order ordermodel.Order + if req.Id != 0 { + order.Id = req.Id + } + if req.Status != 0 { + order.State = req.Status + } + if req.VoucherLink != "" { + order.VoucherLink = req.VoucherLink + } + _, err = repo.Where("Id = ?", req.Id).Update(&order) return }