YouChuKoffee/app/http/controllers/front/OrderController.go

104 lines
3.2 KiB
Go
Raw Normal View History

2024-06-19 18:32:34 +08:00
package front
import (
"github.com/ahmetb/go-linq/v3"
"github.com/gin-gonic/gin"
2024-06-24 11:44:38 +08:00
"net/http"
2024-06-19 18:32:34 +08:00
"qteam/app/constants/common"
"qteam/app/constants/errorcode"
"qteam/app/http/controllers"
"qteam/app/http/entities/front"
"qteam/app/models/ordersmodel"
"qteam/app/services"
"qteam/app/utils"
2024-07-03 18:30:53 +08:00
"qteam/config"
2024-06-19 18:32:34 +08:00
"strconv"
2024-07-03 18:30:53 +08:00
"time"
2024-06-19 18:32:34 +08:00
)
func CreateOrder(c *gin.Context) {
var request = controllers.GetRequest(c).(*front.OrderCreateRequest)
2024-06-25 13:51:22 +08:00
userId := controllers.GetUserId(c)
2024-06-25 18:31:16 +08:00
if userId == 0 {
controllers.HandCodeRes(c, nil, errorcode.NotLogin)
return
}
2024-06-19 18:32:34 +08:00
code, data := services.CreateOrderService(userId, request.ProductId)
controllers.HandCodeRes(c, data, code)
}
func OrderList(c *gin.Context) {
var request = controllers.GetRequest(c).(*front.OrderListRequest)
2024-06-25 13:51:22 +08:00
userId := controllers.GetUserId(c)
2024-06-19 18:32:34 +08:00
code, orderList, count := services.OrderQueryService(userId, request)
2024-06-24 11:44:38 +08:00
var rsp []front.OrderListResponse
2024-06-19 18:32:34 +08:00
if count > 0 {
2024-06-24 11:44:38 +08:00
linq.From(orderList).SelectT(func(in ordersmodel.OrdersProductsList) (out front.OrderListResponse) {
2024-06-19 18:32:34 +08:00
out.ResponseFromDb(in)
return out
}).ToSlice(&rsp)
}
controllers.HandCodeRes(c, gin.H{"data": rsp, "count": count}, code)
}
func OrderQuery(c *gin.Context) {
var request = controllers.GetRequest(c).(*front.OrderQueryRequest)
orderId, _ := strconv.Atoi(request.OrderId)
2024-06-24 11:44:38 +08:00
order := ordersmodel.Orders{}
order.Id = orderId
2024-06-27 11:01:49 +08:00
order.OrderNo = request.OrderNo
2024-07-16 14:13:30 +08:00
order.UserId = controllers.GetUserId(c)
2024-06-19 18:32:34 +08:00
var OrderQueryResponse front.OrderQueryResponse
has, err := services.OrderDetailService(&order)
if err != nil {
controllers.Error(c, 500, "订单查询失败")
return
}
if has {
2024-06-25 18:31:16 +08:00
if order.State < common.ORDER_STATUS_PAY && order.VoucherLink == "" {
2024-06-19 18:32:34 +08:00
_, rsp := services.YouChuOrderQuery(order)
utils.Log(nil, "三方订单查询", rsp)
if rsp.OrderSta == "03" {
2024-06-27 15:56:47 +08:00
err := services.OrdersUpdateService(front.OrdersUpdateRequest{Id: order.Id, Status: common.ORDER_STATUS_PAY, OrgTxnSeq: rsp.OrderNo})
2024-06-19 18:32:34 +08:00
if err != nil {
2024-06-27 15:02:47 +08:00
controllers.Error(c, 500, "订单更新失败")
2024-06-19 18:32:34 +08:00
return
}
2024-06-27 15:02:47 +08:00
order.State = common.ORDER_STATUS_PAY
2024-06-19 18:32:34 +08:00
}
}
OrderQueryResponse.ResponseFromDb(order)
2024-06-27 18:36:01 +08:00
OrderQueryResponse.MainImage = services.GetProductImage(order.ProductId)
2024-06-19 18:32:34 +08:00
controllers.Success(c, OrderQueryResponse, "请求成功")
} else {
controllers.HandCodeRes(c, nil, errorcode.NotFound)
return
}
}
func OrderRefund(c *gin.Context) {
var request = controllers.GetRequest(c).(*front.OrderQueryRequest)
2024-06-24 11:44:38 +08:00
userId := controllers.GetUserId(c)
//userId := 1
2024-06-19 18:32:34 +08:00
OrderId, _ := strconv.Atoi(request.OrderId)
code := services.OrderRefundService(userId, OrderId)
controllers.HandCodeRes(c, nil, code)
}
2024-07-31 09:22:18 +08:00
func OrderDelete(c *gin.Context) {
var request = controllers.GetRequest(c).(*front.OrderQueryRequest)
err := services.OrderDelete(request.OrderId)
controllers.HandRes(c, nil, err)
}
2024-06-19 18:32:34 +08:00
func OrderNotify(c *gin.Context) {
2024-07-03 18:30:53 +08:00
c.Header("method", "unionpay.b2cNotify")
c.Header("merchantId", config.GetConf().YouChu.MerchantId)
c.Header("appID", config.GetConf().YouChu.AppID)
c.Header("partnerTxSriNo", time.Now().Format("20060102150405")+utils.RandomNumber(10))
c.Header("reqTime", time.Now().Format("20060102150405"))
2024-06-24 11:44:38 +08:00
var request = controllers.GetRequest(c).(*front.YouChuRequest)
response := services.OrderNotify(*request)
c.JSON(http.StatusOK, response)
2024-06-19 18:32:34 +08:00
}