964 lines
25 KiB
Go
964 lines
25 KiB
Go
package controller
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/codinl/go-logger"
|
|
"github.com/gin-gonic/gin"
|
|
"golang.org/x/sync/errgroup"
|
|
"io/ioutil"
|
|
"mh-server/kuaidi"
|
|
"mh-server/lib/auth"
|
|
"mh-server/lib/status"
|
|
"mh-server/lib/wxpay"
|
|
"mh-server/model"
|
|
"sync"
|
|
|
|
"time"
|
|
)
|
|
|
|
//func checkout(response http.ResponseWriter, request *http.Request) {
|
|
//func WxMsg(c *gin.Context) {
|
|
// // 获取参数
|
|
// signature := c.PostForm("signature")
|
|
// timestamp := c.PostForm("timestamp")
|
|
// nonce := c.PostForm("nonce")
|
|
// echostr := c.PostForm("echostr")
|
|
//
|
|
// //timestamp := request.FormValue("echostr")
|
|
// //nonce := request.FormValue("nonce")
|
|
// //echostr := request.FormValue("echostr")
|
|
//
|
|
// //将token、timestamp、nonce三个参数进行字典序排序
|
|
// var tempArray = []string{TOKEN, timestamp, nonce}
|
|
// sort.Strings(tempArray)
|
|
//
|
|
// //将三个参数字符串拼接成一个字符串进行sha1加密
|
|
// var sha1String string = ""
|
|
// for _, v := range tempArray {
|
|
// sha1String += v
|
|
// }
|
|
//
|
|
// h := sha1.New()
|
|
// h.Write([]byte(sha1String))
|
|
// sha1String = hex.EncodeToString(h.Sum([]byte("")))
|
|
//
|
|
// //获得加密后的字符串可与signature对比
|
|
// if sha1String == signature {
|
|
// _, err := c.Writer.Write([]byte(echostr))
|
|
// if err != nil {
|
|
// fmt.Println("响应失败。。。")
|
|
// }
|
|
// } else {
|
|
// fmt.Println("验证失败")
|
|
// }
|
|
//}
|
|
|
|
func RentCardOrderList(c *gin.Context) {
|
|
fmt.Println("RentCardOrderList")
|
|
req := model.OrderListReq{}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
logger.Error("ShouldBindJSON err")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
logger.Error("uc is nil")
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
req.Uid = uc.Uid
|
|
|
|
orderCardList, totalPage, err := req.GetOrderCardList()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
ret := map[string]interface{}{
|
|
"order_card_list": orderCardList,
|
|
"cur_page": req.Page,
|
|
"total_page": totalPage,
|
|
}
|
|
RespOK(c, ret)
|
|
return
|
|
}
|
|
|
|
func OrderInfo(c *gin.Context) {
|
|
req := struct {
|
|
//OrderId uint32 `json:"order_id"`
|
|
//GameCardId uint64 `json:"game_card_id"` // 游戏id
|
|
//GameCardGoodsId uint64 `json:"game_card_goods_id" ` // 游戏卡id
|
|
OrderCardId uint32 `json:"order_card_id"`
|
|
}{}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
logger.Error("ShouldBindJSON err")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
order := model.Order{}
|
|
//order.ID = req.OrderId
|
|
//order.GameCardId = req.GameCardId
|
|
info, err := order.Info(req.OrderCardId)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
var orderCard model.OrderCard
|
|
if len(info) > 0 {
|
|
orderCard = info[0]
|
|
}
|
|
RespOK(c, orderCard)
|
|
return
|
|
}
|
|
|
|
func OrderExpress(c *gin.Context) {
|
|
req := struct {
|
|
ExpressCompanyNo string `json:"express_company_no"`
|
|
ExpressNo string `json:"express_no"`
|
|
}{}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
logger.Error("ShouldBindJSON err")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
fmt.Println("CompanyNo:", req.ExpressCompanyNo)
|
|
fmt.Println("Num:", req.ExpressNo)
|
|
|
|
expressInfo, err := kuaidi.ExpressInfo(req.ExpressCompanyNo, req.ExpressNo)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, expressInfo)
|
|
return
|
|
}
|
|
|
|
func WXPaySuccess(c *gin.Context) {
|
|
//fbConfig := model.GetFBConfig()
|
|
//if fbConfig.AssistanceValidityTerm == 0 {
|
|
// logger.Error(errors.New("get assistance_validity_term err"))
|
|
// RespJson(c, status.DBOperateError, nil)
|
|
// return
|
|
//}
|
|
|
|
//assistanceValidityTerm := time.Unix(time.Now().Unix()+int64(fbConfig.AssistanceValidityTerm), 0)
|
|
//ret := map[string]interface{}{
|
|
// "assistance_validity_term": assistanceValidityTerm,
|
|
// "postage": fbConfig.Postage,
|
|
//}
|
|
//RespOK(c, ret)
|
|
}
|
|
|
|
var orderCreateLocker sync.Mutex
|
|
|
|
// 创建订单
|
|
func RentCardOrderCreate(c *gin.Context) {
|
|
//type Goods struct {
|
|
// Price uint32 `json:"price"`
|
|
//}
|
|
|
|
req := struct {
|
|
GameCardList []model.CardInfo `json:"game_card_list"`
|
|
StoreId uint32 `json:"store_id"`
|
|
UserAddressId uint32 `json:"user_address_id"`
|
|
Price uint32 `json:"price"`
|
|
DeliveryType uint8 `json:"delivery_type"` // 取货方式
|
|
ExpressFee uint32 `json:"express_fee"`
|
|
}{}
|
|
|
|
bodyString, err := ioutil.ReadAll(c.Request.Body)
|
|
if err != nil {
|
|
logger.Error(errors.New("OrderCreate ReadAll req err"))
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
//bodyString := strings.Replace(string(body), "\"[", "[", -1)
|
|
//bodyString = strings.Replace(string(bodyString), "]\"", "]", -1)
|
|
//bodyString = strings.Replace(string(bodyString), "\\", "", -1)
|
|
logger.Info("bodyString:", string(bodyString))
|
|
|
|
err = json.Unmarshal([]byte(bodyString), &req)
|
|
if err != nil {
|
|
logger.Error(errors.New("OrderCreate Unmarshal req err"))
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
//if err := c.ShouldBindJSON(&req);err != nil {
|
|
// logger.Error(err)
|
|
// RespJson(c,status.BadRequest,nil)
|
|
// return
|
|
//}
|
|
|
|
fmt.Println("商品下单 req:", req)
|
|
|
|
//uc := &struct {
|
|
// Uid uint32 `json:"uid"`
|
|
//}{}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
logger.Error("uc is nil")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
//uc.Uid = 62389201
|
|
user := model.GetUserByUid(uc.Uid)
|
|
if user == nil {
|
|
logger.Error(errors.New("GetUserByUid err"))
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
if user.MemberLevel == 1 {
|
|
logger.Error(errors.New("user level 1"))
|
|
RespJson(c, status.NotMember, nil)
|
|
return
|
|
}
|
|
|
|
memberConfig, err := model.GetMemberConfig(user.MemberLevel)
|
|
if err != nil {
|
|
logger.Error("GetMemberConfig err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
fmt.Println("Deposit:", user.Deposit)
|
|
fmt.Println("MemberDeposit:", memberConfig.MemberDeposit)
|
|
if user.Deposit != memberConfig.MemberDeposit {
|
|
logger.Error(errors.New("user deposit 0"))
|
|
RespJson(c, status.OrderUnpaidDeposit, nil)
|
|
return
|
|
}
|
|
|
|
cardCount := uint32(0)
|
|
for _, v := range req.GameCardList {
|
|
cardCount += v.Count
|
|
}
|
|
//fmt.Println("cardCount:", cardCount)
|
|
rentCard := model.GetUserRentCard(uc.Uid)
|
|
if rentCard == nil {
|
|
//logger.Error(errors.New("GetUserByUid err"))
|
|
//RespJson(c, status.InternalServerError, nil)
|
|
//return
|
|
//fmt.Println("CardMax", memberConfig.CardMax)
|
|
rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax}
|
|
}
|
|
//fmt.Println("rentCard", rentCard.CanRentCount)
|
|
if uc.Uid == 15304136 || uc.Uid == 45935373 {
|
|
rentCard.CanRentCount -= 1
|
|
}
|
|
if cardCount > rentCard.CanRentCount {
|
|
logger.Error("GetMemberConfig err:", err)
|
|
RespJson(c, status.OrderOutRentCount, nil)
|
|
return
|
|
}
|
|
|
|
// 订单成功后 扣减库存
|
|
//model.UnPayOrderSetCancel(user.Uid)
|
|
|
|
orderCreateLocker.Lock()
|
|
defer orderCreateLocker.Unlock()
|
|
|
|
isRentCount, err := rentCard.IsHaveUnreturnedOrders(cardCount)
|
|
//unreturnedOrders, err := model.IsHaveUnreturnedOrders(uc.Uid) //
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
if isRentCount {
|
|
logger.Error("unreturnedOrders")
|
|
RespJson(c, status.HaveUnreturnedOrders, nil)
|
|
return
|
|
}
|
|
|
|
online, err := model.IsGameCardListOnline(req.GameCardList)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
if !online {
|
|
logger.Error("unreturnedOrders")
|
|
RespJson(c, status.GoodsSoldOut, nil)
|
|
return
|
|
}
|
|
store := &model.Store{}
|
|
store.ID = req.StoreId
|
|
err = store.Info()
|
|
if err != nil {
|
|
logger.Error("store err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
// 邮费
|
|
orderSn := model.GetOrderSn()
|
|
|
|
order := model.Order{
|
|
Uid: uint64(uc.Uid),
|
|
GameCardId: 0,
|
|
StoreId: uint64(req.StoreId),
|
|
UserAddressId: uint64(req.UserAddressId),
|
|
DeliveryType: req.DeliveryType,
|
|
Count: 1,
|
|
PickupCode: model.GetPickupCode(), // 取货码
|
|
CardStatus: model.OrderCardStatusUnPick,
|
|
PayStatus: model.PayStatusUnPay,
|
|
PayTime: time.Now(),
|
|
OrderSn: orderSn,
|
|
PayPrice: req.Price,
|
|
Postage: req.ExpressFee,
|
|
CooperativeBusinessId: store.CooperativeBusinessId,
|
|
CooperativeName: store.CooperativeName,
|
|
}
|
|
|
|
// 以支付成功作为订单下单成功的依据,不是订单创建 已修改
|
|
//var gameCardGoodsStock model.GameCardGoodsStock
|
|
//err = model.NewGameCardGoodsStockQuerySet(model.DB).StoreIdEq(req.StoreId).GameCardIdEq(req.GameCardId).One(&gameCardGoodsStock)
|
|
//if err != nil {
|
|
// logger.Error("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
//if gameCardGoodsStock.RentStock <= 0 {
|
|
// logger.Error("order stock out ")
|
|
// RespJson(c, status.OrderStockOut, nil)
|
|
// return
|
|
//}
|
|
stockEnough, err := model.IsCardGoodsStockEnough(req.GameCardList, req.StoreId)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.OrderStockOut, nil)
|
|
return
|
|
}
|
|
if stockEnough {
|
|
logger.Error("order stock out ")
|
|
RespJson(c, status.OrderStockOut, nil)
|
|
return
|
|
}
|
|
if user.OpenMemberChannel == model.OpenMemberChannelStorePromotion {
|
|
go func() {
|
|
exist, err := model.QueryRecordExist(fmt.Sprintf("SELECT * FROM `order` WHERE uid = %d", uc.Uid))
|
|
if err != nil {
|
|
logger.Error("order exist err:", err)
|
|
return
|
|
}
|
|
|
|
if !exist && user.OpenMemberTime.Format(model.DateTimeFormat) == time.Now().Format(model.DateTimeFormat) {
|
|
_, err = model.NewUserQuerySet(model.DB).UidEq(uc.Uid).GetUpdater().SetStoreId(uint64(req.StoreId)).UpdateNum()
|
|
if err != nil {
|
|
logger.Error("update user err:", err)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
//fmt.Println("PayPrice:", order.PayPrice)
|
|
if req.Price == 0 {
|
|
tx := model.TransactionBegin()
|
|
order.PayStatus = model.PayStatusPaid
|
|
fmt.Println("orderId:", order.PayStatus)
|
|
err = order.OrderCreate(tx)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
// 减库存
|
|
//sql := fmt.Sprintf("UPDATE game_card_goods_stock SET rent_stock= rent_stock-1 WHERE store_id=%d AND game_card_id=%d;", req.StoreId, req.GameCardId)
|
|
//fmt.Println("sql:", sql)
|
|
//err = model.DB.Exec(sql).Error
|
|
//if err != nil {
|
|
// logger.Errorf("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
err := model.GameCardGoodsInventoryReduction(req.GameCardList, req.StoreId, tx)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
logger.Error("inventory reduction err:", err.Error())
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
err = order.CreatOrderCard(req.GameCardList, req.StoreId, tx)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
logger.Error("inventory reduction err:", err.Error())
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
err = tx.Commit().Error
|
|
if err != nil {
|
|
tx.Rollback()
|
|
logger.Error("commit err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
go model.OrderCardUserRentCard(uc.Uid, cardCount, memberConfig, nil)
|
|
ret := map[string]interface{}{
|
|
"web_pay": "",
|
|
"order_id": order.ID,
|
|
}
|
|
|
|
go model.ShoppingCartCreateOrder(uc.Uid, req.GameCardList)
|
|
RespOK(c, ret)
|
|
return
|
|
}
|
|
|
|
//err = order.OrderCreate()
|
|
//if err != nil {
|
|
// logger.Error("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
begin := model.DB.Begin()
|
|
err = order.Create(begin)
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
// 减库存
|
|
//sql := fmt.Sprintf("UPDATE game_card_goods_stock SET rent_stock= rent_stock-1 WHERE store_id=%d AND game_card_id=%d;", req.StoreId, req.GameCardId)
|
|
//fmt.Println("sql:", sql)
|
|
//err = begin.Exec(sql).Error
|
|
//if err != nil {
|
|
// begin.Rollback()
|
|
// logger.Errorf("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
// 订单回调成功减库存
|
|
//err = model.GameCardGoodsInventoryReduction(req.GameCardList, req.StoreId, begin)
|
|
//if err != nil {
|
|
// begin.Rollback()
|
|
// logger.Error("inventory reduction err:%s", err.Error())
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
err = order.CreatOrderCard(req.GameCardList, req.StoreId, begin)
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("inventory reduction err:", err.Error())
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
err = begin.Commit().Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Errorf("err:%#v", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
//orderId := fmt.Sprintf("%d", order.ID)
|
|
////orderId = "100000"
|
|
//fmt.Println("orderId", orderId)
|
|
//fmt.Println("Price", req.Price)
|
|
//fmt.Println("WxOpenID", user.WxOpenID)
|
|
//fmt.Println("WxPayRentCard", wxpay.WxPayRentCard)
|
|
configInfo, err := model.PayConfigInfo()
|
|
if err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
webPay, err := wxpay.WebPay(order.OrderSn, req.Price, user.WxOpenID, "N", wxpay.WxPayRentCard, configInfo.NotifyUrl)
|
|
if err != nil {
|
|
logger.Error(errors.New("WebPay err"))
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
//num, err := model.NewOrderQuerySet(model.DB).IDEq(order.ID).GetUpdater().
|
|
// SetOutTradeNo(webPay.NonceStr).SetMchid(config.AppConfig.WxMchID).UpdateNum()
|
|
//if err != nil {
|
|
// logger.Error("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
//if num == 0 {
|
|
// logger.Error("UpdateNum is 0")
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
|
|
err = model.UserOpenMemberRecord{Uid: uc.Uid, OpenNo: orderSn, OrderId: order.ID, OrderType: 1}.Insert()
|
|
if err != nil {
|
|
logger.Error(errors.New("WebPay err"))
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
ret := map[string]interface{}{
|
|
"web_pay": webPay,
|
|
"order_id": order.ID,
|
|
}
|
|
|
|
RespOK(c, ret)
|
|
}
|
|
|
|
//func ExpressFeeRefund(c *gin.Context) {
|
|
// req := struct {
|
|
// OrderId uint32 `json:"order_id"`
|
|
// }{}
|
|
// if c.ShouldBindJSON(&req) != nil {
|
|
// logger.Error("ShouldBindJSON err")
|
|
// RespJson(c, status.BadRequest, nil)
|
|
// return
|
|
// }
|
|
// order := model.Order{}
|
|
// order.ID = req.OrderId
|
|
// order.Info()
|
|
// outTradeNo, err := model.GetWxPayExpressFeeRefundRecord(req.OrderId)
|
|
// if err != nil {
|
|
// logger.Error("err:", err)
|
|
// return
|
|
// }
|
|
//
|
|
// orderSn := model.GetOrderSn()
|
|
// err = model.UserOpenMemberRecord{Uid: uint32(order.Uid), OpenNo: orderSn, OrderId: order.ID, OrderType: 2}.Insert()
|
|
// if err != nil {
|
|
// logger.Error(errors.New("WebPay err"))
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
// }
|
|
//
|
|
// orderRefund := wxpay.OrderRefund{
|
|
// OutTradeNo: outTradeNo,
|
|
// OutRefundNo: orderSn,
|
|
// NotifyUrl: "",
|
|
// Amount: wxpay.OrderRefundAmount{
|
|
// Refund: order.Postage,
|
|
// Total: order.Postage,
|
|
// Currency: "CNY",
|
|
// },
|
|
// }
|
|
// err = wxpay.WxPayOrderRefund(orderRefund)
|
|
// if err != nil {
|
|
// logger.Error("err:", err)
|
|
// }
|
|
//
|
|
//}
|
|
|
|
//func ExpressFeeRefund(c *gin.Context) {
|
|
// req := struct {
|
|
// OrderId uint32 `json:"order_id"`
|
|
// }{}
|
|
// if c.ShouldBindJSON(&req) != nil {
|
|
// logger.Error("ShouldBindJSON err")
|
|
// RespJson(c, status.BadRequest, nil)
|
|
// return
|
|
// }
|
|
// order := model.Order{}
|
|
// order.ID = req.OrderId
|
|
// order.Info()
|
|
// outTradeNo, err := model.GetWxPayExpressFeeRefundRecord(req.OrderId)
|
|
// if err != nil {
|
|
// logger.Error("err:", err)
|
|
// return
|
|
// }
|
|
//
|
|
// orderSn := model.GetOrderSn()
|
|
// err = model.UserOpenMemberRecord{Uid: uint32(order.Uid), OpenNo: orderSn, OrderId: order.ID, OrderType: 2}.Insert()
|
|
// if err != nil {
|
|
// logger.Error(errors.New("WebPay err"))
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
// }
|
|
//
|
|
// orderRefund := wxpay.OrderRefund{
|
|
// OutTradeNo: outTradeNo,
|
|
// OutRefundNo: orderSn,
|
|
// NotifyUrl: "",
|
|
// Amount: wxpay.OrderRefundAmount{
|
|
// Refund: order.Postage,
|
|
// Total: order.Postage,
|
|
// Currency: "CNY",
|
|
// },
|
|
// }
|
|
// err = wxpay.WxPayOrderRefund(orderRefund)
|
|
// if err != nil {
|
|
// logger.Error("err:", err)
|
|
// }
|
|
//
|
|
//}
|
|
|
|
const (
|
|
PayStatusUnPay = 1 // 未支付
|
|
PayStatusPaid = 2 // 已支付
|
|
|
|
OrderCardStatusUnPick = 1 // 待取货中
|
|
OrderCardStatusPlaying = 2 // 游玩中
|
|
OrderCardStatusReturning = 3 // 归还中
|
|
OrderCardStatusCompleted = 4 // 已完成
|
|
OrderCardStatusCancel = 5 // 已取消
|
|
OrderCardStatusRefund = 6 // 已退款
|
|
)
|
|
|
|
// 创建订单
|
|
func OrderPay(c *gin.Context) {
|
|
req := struct {
|
|
OrderId uint32 `json:"order_id"`
|
|
//StoreId uint64 `json:"store_id"`
|
|
//UserAddressId uint64 `json:"user_address_id"`
|
|
//Price uint32 `json:"price"`
|
|
//DeliveryType uint8 `json:"delivery_type"` // 取货方式
|
|
//ExpressFee uint32 `json:"express_fee"`
|
|
//
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error("ShouldBindJSON err:", err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
var order model.Order
|
|
err := model.NewOrderQuerySet(model.DB).IDEq(req.OrderId).One(&order)
|
|
if err != nil {
|
|
logger.Error("Order err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
if order.PayStatus != PayStatusUnPay || order.CardStatus != OrderCardStatusUnPick || order.CreatedAt.Add(30*time.Minute).Before(time.Now()) {
|
|
//if order.PayStatus != PayStatusUnPay || order.CardStatus != OrderCardStatusUnPick || order.CreatedAt.Add(3*time.Minute).Before(time.Now()) {
|
|
logger.Error("Order err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
//orderId := fmt.Sprintf("%d", order.ID)
|
|
var user model.User
|
|
err = model.NewUserQuerySet(model.DB).UidEq(uint32(order.Uid)).One(&user)
|
|
if err != nil {
|
|
logger.Error("Order err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
configInfo, err := model.PayConfigInfo()
|
|
if err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
webPay, err := wxpay.WebPay(order.OrderSn, order.PayPrice, user.WxOpenID, "N", wxpay.WxPayRentCard, configInfo.NotifyUrl)
|
|
if err != nil {
|
|
logger.Error(errors.New("WebPay err"))
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
ret := map[string]interface{}{
|
|
"web_pay": webPay,
|
|
"order_id": order.ID,
|
|
}
|
|
RespOK(c, ret)
|
|
}
|
|
|
|
func OrderAmount(c *gin.Context) {
|
|
req := struct {
|
|
StoreId uint32 `json:"store_id" binding:"required"`
|
|
UserAddressId uint32 `json:"user_address_id" binding:"required"`
|
|
Price uint32 `json:"price" `
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error("ShouldBindJSON err:", err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
var (
|
|
eg errgroup.Group
|
|
userAddress = &model.UserAddress{}
|
|
store = &model.Store{}
|
|
)
|
|
|
|
eg.Go(func() error {
|
|
store.ID = req.StoreId
|
|
err := store.Info()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
eg.Go(func() error {
|
|
userAddress.ID = req.UserAddressId
|
|
err := userAddress.Info()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
logger.Error("err:", err)
|
|
//fmt.Println("Successfully fetched all URLs.")
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
expressFee := model.ExpressFeeProvince
|
|
//if userAddress.Province != "广东省" {
|
|
if userAddress.Province != store.Province {
|
|
expressFee = model.ExpressFeeOutsideProvince
|
|
}
|
|
|
|
ret := map[string]interface{}{
|
|
"total_amount": req.Price + uint32(expressFee),
|
|
"express_fee": expressFee,
|
|
}
|
|
RespOK(c, ret)
|
|
return
|
|
}
|
|
|
|
//func WXPay(c *gin.Context) {
|
|
// req := struct {
|
|
// //TotalFee uint32 `json:"total_fee"`
|
|
// AddressId uint32 `json:"address_id"`
|
|
// OrderSn string `json:"order_sn"`
|
|
// }{}
|
|
// if err := c.ShouldBindJSON(&req); err != nil {
|
|
// logger.Error(err)
|
|
// RespJson(c, status.BadRequest, nil)
|
|
// return
|
|
// }
|
|
//
|
|
// uc := auth.GetCurrentUser(c)
|
|
// if uc == nil {
|
|
// RespJson(c, status.Unauthorized, nil)
|
|
// return
|
|
// }
|
|
//
|
|
// //user := model.GetUserByUid(uc.Uid)
|
|
//
|
|
// var order model.Order
|
|
// // 支付金额 地址id
|
|
// err := model.NewOrderQuerySet(model.DB).OrderSnEq(req.OrderSn).One(&order)
|
|
// if err != nil {
|
|
// logger.Error(err)
|
|
// RespJson(c, status.DBOperateError, nil)
|
|
// return
|
|
// }
|
|
//
|
|
// //if err := model.NewFBOrderQuerySet(model.DB).OrderSnEq(req.OrderSn).GetUpdater().
|
|
// // SetAddressId(req.AddressId).Update(); err != nil {
|
|
// // logger.Error(err)
|
|
// // RespJson(c, status.DBOperateError, nil)
|
|
// // return
|
|
// //}
|
|
// //
|
|
// //var sextuple *wxpay.Sextuple
|
|
// //sextuple, err = wxpay.FBXiaoChengXuPay(order.PayPrice, req.OrderSn, model.FB_WX_PAY_TYPE, user.WxOpenID)
|
|
// //if err != nil {
|
|
// // logger.Error(err)
|
|
// // RespJson(c, status.InternalServerError, nil)
|
|
// // return
|
|
// //}
|
|
//
|
|
// //RespOK(c, sextuple)
|
|
//}
|
|
|
|
func ConfirmReceipt(c *gin.Context) {
|
|
req := struct {
|
|
OrderId uint32 `json:"order_id"`
|
|
GameCardId uint32 `json:"game_card_id"`
|
|
}{}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
logger.Error("uc is nil")
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
order := model.Order{Uid: uint64(uc.Uid)}
|
|
order.ID = req.OrderId
|
|
order.GameCardId = uint64(req.GameCardId)
|
|
order.CardStatus = model.OrderCardStatusPlaying
|
|
info, err := order.Modify()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, info)
|
|
return
|
|
}
|
|
|
|
func ExpressCompanyList(c *gin.Context) {
|
|
company := &model.ExpressCompany{}
|
|
list, err := company.List()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, list)
|
|
return
|
|
}
|
|
|
|
func OrderRevert(c *gin.Context) {
|
|
order := &model.Order{}
|
|
if c.ShouldBindJSON(order) != nil {
|
|
logger.Error("ShouldBindJSON err")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
//order := model.Order{}
|
|
//order.ID = req.OrderId
|
|
err := order.Revert()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.OrderCompleted, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, nil)
|
|
return
|
|
}
|
|
|
|
func OrderRevertCancel(c *gin.Context) {
|
|
order := &model.Order{}
|
|
if c.ShouldBindJSON(order) != nil {
|
|
logger.Error("ShouldBindJSON err")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
//order := model.Order{}
|
|
//order.ID = req.OrderId
|
|
_, err := order.RevertCancel()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.OrderStatusNotReturning, nil)
|
|
return
|
|
}
|
|
//if isRecede {
|
|
// outTradeNo, err := model.GetWxPayExpressFeeRefundRecord(order.ID)
|
|
// if err != nil {
|
|
// logger.Error("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
// }
|
|
//
|
|
// orderSn := model.GetOrderSn()
|
|
// err = model.UserOpenMemberRecord{Uid: uint32(order.Uid), OpenNo: orderSn, OrderId: order.ID, OrderType: 2}.Insert()
|
|
// if err != nil {
|
|
// logger.Error(errors.New("WebPay err"))
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
// }
|
|
//
|
|
// orderRefund := wxpay.OrderRefund{
|
|
// OutTradeNo: outTradeNo,
|
|
// OutRefundNo: orderSn,
|
|
// NotifyUrl: "",
|
|
// Amount: wxpay.OrderRefundAmount{
|
|
// Refund: order.PayPrice,
|
|
// Total: order.PayPrice,
|
|
// Currency: "CNY",
|
|
// },
|
|
// }
|
|
// //bytes, _ := json.Marshal(orderRefund)
|
|
// //fmt.Println("订单取消:", string(bytes))
|
|
// //err = wxpay.WxPayOrderRefund(orderRefund)
|
|
// err = wxpay.TransactionOrderRefund(orderRefund)
|
|
// if err != nil {
|
|
// logger.Error("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
// }
|
|
//}
|
|
RespOK(c, nil)
|
|
return
|
|
}
|
|
|
|
func OrderCancel(c *gin.Context) {
|
|
order := model.Order{}
|
|
if c.ShouldBindJSON(&order) != nil {
|
|
logger.Error("ShouldBindJSON err")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
//uc := auth.GetCurrentUser(c)
|
|
//if uc == nil {
|
|
// logger.Error("uc is nil")
|
|
// RespJson(c, status.Unauthorized, nil)
|
|
// return
|
|
//}
|
|
fmt.Println("OrderId:", order.ID)
|
|
//order := &model.Order{}
|
|
//order.ID = req.OrderId
|
|
isRecede, orderInfo, err := order.Cancel()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.OrderDelivered, nil)
|
|
return
|
|
}
|
|
order = orderInfo
|
|
fmt.Println("order:", order)
|
|
if isRecede {
|
|
outTradeNo, err := model.GetWxPayExpressFeeRefundRecord(order.ID)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
//m.OpenNo = model.GetOrderSn()
|
|
memberRecord := &model.UserOpenMemberRecord{OpenNo: model.GetOrderSn(), OrderType: 2, Order: &order}
|
|
err = memberRecord.OrderRefund(outTradeNo)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
//orderSn := model.GetOrderSn()
|
|
//err = model.UserOpenMemberRecord{Uid: uint32(order.Uid), OpenNo: orderSn, OrderId: order.ID, OrderType: 2}.Insert()
|
|
//if err != nil {
|
|
// logger.Error(errors.New("WebPay err"))
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
|
|
//orderRefund := wxpay.OrderRefund{
|
|
// OutTradeNo: outTradeNo,
|
|
// OutRefundNo: orderSn,
|
|
// NotifyUrl: "",
|
|
// Amount: wxpay.OrderRefundAmount{
|
|
// Refund: order.PayPrice,
|
|
// Total: order.PayPrice,
|
|
// Currency: "CNY",
|
|
// },
|
|
//}
|
|
//err = wxpay.TransactionOrderRefund(orderRefund)
|
|
//if err != nil {
|
|
// logger.Error("err:", err)
|
|
// RespJson(c, status.InternalServerError, nil)
|
|
// return
|
|
//}
|
|
|
|
//bytes, _ := json.Marshal(order)
|
|
//fmt.Println("订单取消:", string(bytes))
|
|
//orderRefundJson, _ := json.Marshal(&orderRefund)
|
|
//fmt.Println("订单取消 orderRefundJson:", string(orderRefundJson))
|
|
//err = wxpay.WxPayOrderRefund(orderRefund)
|
|
|
|
}
|
|
|
|
RespOK(c, nil)
|
|
return
|
|
}
|