2021-06-30 02:12:05 +00:00
package controller
import (
"encoding/json"
"errors"
"fmt"
"github.com/codinl/go-logger"
"github.com/gin-gonic/gin"
2024-08-26 10:14:41 +00:00
"github.com/jinzhu/gorm"
2021-06-30 02:12:05 +00:00
"golang.org/x/sync/errgroup"
2021-11-01 03:32:23 +00:00
"io/ioutil"
2021-06-30 02:12:05 +00:00
"mh-server/kuaidi"
"mh-server/lib/auth"
"mh-server/lib/status"
"mh-server/lib/wxpay"
"mh-server/model"
2021-11-01 03:32:23 +00:00
"sync"
2021-06-30 02:12:05 +00:00
"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("验证失败")
// }
//}
2022-01-26 08:02:21 +00:00
func RentCardOrderList ( c * gin . Context ) {
fmt . Println ( "RentCardOrderList" )
req := model . OrderListReq { }
2021-06-30 02:12:05 +00:00
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
}
2022-01-26 08:02:21 +00:00
req . Uid = uc . Uid
2022-04-21 09:03:33 +00:00
orderCardList , totalPage , err := req . GetOrderCardList ( )
2021-06-30 02:12:05 +00:00
if err != nil {
logger . Error ( "err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
ret := map [ string ] interface { } {
2022-01-26 08:02:21 +00:00
"order_card_list" : orderCardList ,
"cur_page" : req . Page ,
"total_page" : totalPage ,
2021-06-30 02:12:05 +00:00
}
RespOK ( c , ret )
return
}
func OrderInfo ( c * gin . Context ) {
req := struct {
2022-01-27 14:24:29 +00:00
//OrderId uint32 `json:"order_id"`
//GameCardId uint64 `json:"game_card_id"` // 游戏id
//GameCardGoodsId uint64 `json:"game_card_goods_id" ` // 游戏卡id
2022-01-29 02:21:38 +00:00
OrderCardId uint32 ` json:"order_card_id" `
2021-06-30 02:12:05 +00:00
} { }
if c . ShouldBindJSON ( & req ) != nil {
logger . Error ( "ShouldBindJSON err" )
RespJson ( c , status . BadRequest , nil )
return
}
order := model . Order { }
2022-01-27 14:24:29 +00:00
//order.ID = req.OrderId
//order.GameCardId = req.GameCardId
info , err := order . Info ( req . OrderCardId )
2021-06-30 02:12:05 +00:00
if err != nil {
logger . Error ( "err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-01-26 08:02:21 +00:00
var orderCard model . OrderCard
if len ( info ) > 0 {
orderCard = info [ 0 ]
}
RespOK ( c , orderCard )
2021-06-30 02:12:05 +00:00
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)
}
2021-11-01 03:32:23 +00:00
var orderCreateLocker sync . Mutex
2021-06-30 02:12:05 +00:00
// 创建订单
2022-01-26 08:02:21 +00:00
func RentCardOrderCreate ( c * gin . Context ) {
2021-06-30 02:12:05 +00:00
req := struct {
2022-01-16 08:56:20 +00:00
GameCardList [ ] model . CardInfo ` json:"game_card_list" `
2022-01-26 08:02:21 +00:00
StoreId uint32 ` json:"store_id" `
UserAddressId uint32 ` json:"user_address_id" `
2022-01-16 08:56:20 +00:00
Price uint32 ` json:"price" `
DeliveryType uint8 ` json:"delivery_type" ` // 取货方式
ExpressFee uint32 ` json:"express_fee" `
2022-09-29 07:49:16 +00:00
UserCouponId uint32 ` json:"user_coupon_id" `
2021-06-30 02:12:05 +00:00
} { }
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
}
fmt . Println ( "商品下单 req:" , req )
uc := auth . GetCurrentUser ( c )
if uc == nil {
logger . Error ( "uc is nil" )
RespJson ( c , status . BadRequest , nil )
return
}
2022-09-29 07:49:16 +00:00
2021-06-30 02:12:05 +00:00
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
}
2022-01-26 08:02:21 +00:00
2022-10-13 06:51:34 +00:00
exist , err := model . QueryRecordExist ( fmt . Sprintf ( "SELECT * FROM deposit_refund_record WHERE uid=%d AND `status`= %d" , uc . Uid , model . DepositRefundStatusUnconfirmed ) )
if err != nil {
logger . Error ( "exist user deposit refund record err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
if exist {
logger . Error ( errors . New ( "user deposit refund apply" ) )
RespJson ( c , status . UserApplyDepositRefund , nil )
return
}
2023-07-14 09:29:05 +00:00
memberConfig , err := model . GetMemberConfig ( user . MemberLevel , user . MemberGenre )
2022-01-16 08:56:20 +00:00
if err != nil {
logger . Error ( "GetMemberConfig err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-09-29 07:49:16 +00:00
2022-01-26 08:02:21 +00:00
if user . Deposit != memberConfig . MemberDeposit {
logger . Error ( errors . New ( "user deposit 0" ) )
RespJson ( c , status . OrderUnpaidDeposit , nil )
return
}
2022-01-16 08:56:20 +00:00
cardCount := uint32 ( 0 )
for _ , v := range req . GameCardList {
cardCount += v . Count
}
2022-09-29 07:49:16 +00:00
2024-08-26 10:14:41 +00:00
var rentCard * model . UserRentCard
var tx * gorm . DB
if req . Price == 0 {
fmt . Println ( "*********** req.Price == 0 ***********" )
tx = model . TransactionBegin ( )
rentCard = model . GetUserRentCard ( tx , uc . Uid )
fmt . Println ( "*********** rentCard is:" , rentCard )
if rentCard == nil {
//logger.Error(errors.New("GetUserByUid err"))
//RespJson(c, status.InternalServerError, nil)
//return
rentCard = & model . UserRentCard { LevelRentCount : memberConfig . CardMax , CanRentCount : memberConfig . CardMax }
}
} else {
fmt . Println ( "*********** req.Price != 0 ***********" )
rentCard = model . GetUserRentCard ( nil , uc . Uid )
fmt . Println ( "*********** rentCard is:" , rentCard )
if rentCard == nil {
//logger.Error(errors.New("GetUserByUid err"))
//RespJson(c, status.InternalServerError, nil)
//return
rentCard = & model . UserRentCard { LevelRentCount : memberConfig . CardMax , CanRentCount : memberConfig . CardMax }
}
2022-01-26 08:02:21 +00:00
}
2022-09-29 07:49:16 +00:00
2022-12-13 06:11:54 +00:00
//if uc.Uid == 45935373 {
// rentCard.CanRentCount -= 1
//}
2022-01-26 08:02:21 +00:00
if cardCount > rentCard . CanRentCount {
2024-08-26 10:14:41 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
logger . Error ( "err:" , "会员超过可借卡数" )
2022-01-26 08:02:21 +00:00
RespJson ( c , status . OrderOutRentCount , nil )
2022-01-16 08:56:20 +00:00
return
}
2022-04-25 05:59:56 +00:00
2022-01-26 08:02:21 +00:00
// 订单成功后 扣减库存
//model.UnPayOrderSetCancel(user.Uid)
2021-11-01 03:32:23 +00:00
orderCreateLocker . Lock ( )
defer orderCreateLocker . Unlock ( )
2022-01-26 08:02:21 +00:00
isRentCount , err := rentCard . IsHaveUnreturnedOrders ( cardCount )
2022-04-21 09:03:33 +00:00
//unreturnedOrders, err := model.IsHaveUnreturnedOrders(uc.Uid) //
2021-06-30 02:12:05 +00:00
if err != nil {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2021-06-30 02:12:05 +00:00
logger . Error ( "err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-01-26 08:02:21 +00:00
if isRentCount {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2021-06-30 02:12:05 +00:00
logger . Error ( "unreturnedOrders" )
RespJson ( c , status . HaveUnreturnedOrders , nil )
return
}
2022-01-16 08:56:20 +00:00
online , err := model . IsGameCardListOnline ( req . GameCardList )
2021-06-30 02:12:05 +00:00
if err != nil {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2021-06-30 02:12:05 +00:00
logger . Error ( "err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
if ! online {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2021-06-30 02:12:05 +00:00
logger . Error ( "unreturnedOrders" )
RespJson ( c , status . GoodsSoldOut , nil )
return
}
2022-05-13 09:35:10 +00:00
store := & model . Store { }
store . ID = req . StoreId
err = store . Info ( )
if err != nil {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2022-05-13 09:35:10 +00:00
logger . Error ( "store err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
2021-11-01 03:32:23 +00:00
2022-04-21 09:03:33 +00:00
// 邮费
2021-11-01 03:32:23 +00:00
orderSn := model . GetOrderSn ( )
2021-06-30 02:12:05 +00:00
order := model . Order {
2022-05-13 09:35:10 +00:00
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 ,
2022-09-29 07:49:16 +00:00
PostageType : 1 ,
}
var userCoupon model . UserCoupon
//err = model.NewUserCouponQuerySet(model.DB).ActivityTypeEq(3).UidEq(uc.Uid).StateEq(1).
// OrderAscByID().Limit(1).One(&userCoupon)
err = model . NewUserCouponQuerySet ( model . DB ) . IDEq ( req . UserCouponId ) . One ( & userCoupon )
if err != nil && err != model . RecordNotFound {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2022-09-29 07:49:16 +00:00
logger . Error ( "user coupon err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
if err == nil && userCoupon . Uid == uc . Uid &&
userCoupon . State == 1 && userCoupon . ActivityType == 3 {
req . Price = 0
order . PostageType = 2
2021-06-30 02:12:05 +00:00
}
2021-11-01 03:32:23 +00:00
2022-04-21 09:03:33 +00:00
// 以支付成功作为订单下单成功的依据,不是订单创建 已修改
2022-01-16 08:56:20 +00:00
//var gameCardGoodsStock model.GameCardGoodsStock
//err = model.NewGameCardGoodsStockQuerySet(model.DB).StoreIdEq(req.StoreId).GameCardIdEq(req.GameCardId).One(&gameCardGoodsStock)
2021-11-01 03:32:23 +00:00
//if err != nil {
// logger.Error("err:", err)
// RespJson(c, status.InternalServerError, nil)
// return
//}
2022-01-16 08:56:20 +00:00
//if gameCardGoodsStock.RentStock <= 0 {
// logger.Error("order stock out ")
// RespJson(c, status.OrderStockOut, nil)
// return
//}
2022-01-26 08:02:21 +00:00
stockEnough , err := model . IsCardGoodsStockEnough ( req . GameCardList , req . StoreId )
if err != nil {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2022-01-26 08:02:21 +00:00
logger . Error ( "err:" , err )
2022-01-27 14:24:29 +00:00
RespJson ( c , status . OrderStockOut , nil )
2022-01-26 08:02:21 +00:00
return
}
if stockEnough {
2024-09-16 17:19:57 +00:00
if req . Price == 0 {
tx . Rollback ( )
}
2022-01-26 08:02:21 +00:00
logger . Error ( "order stock out " )
RespJson ( c , status . OrderStockOut , nil )
return
}
2022-06-14 06:07:15 +00:00
//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)
// }
// }
// }()
//}
2022-01-26 08:02:21 +00:00
2022-04-23 07:47:13 +00:00
//fmt.Println("PayPrice:", order.PayPrice)
2022-01-26 08:02:21 +00:00
if req . Price == 0 {
2024-09-04 11:10:28 +00:00
if tx == nil {
tx = model . TransactionBegin ( )
}
2022-01-26 08:02:21 +00:00
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
//}
2024-09-16 17:19:57 +00:00
err = model . GameCardGoodsInventoryReduction ( req . GameCardList , req . StoreId , tx )
2022-01-26 08:02:21 +00:00
if err != nil {
tx . Rollback ( )
2022-05-28 07:24:17 +00:00
logger . Error ( "inventory reduction err:" , err . Error ( ) )
2022-01-26 08:02:21 +00:00
RespJson ( c , status . InternalServerError , nil )
return
}
err = order . CreatOrderCard ( req . GameCardList , req . StoreId , tx )
if err != nil {
tx . Rollback ( )
2022-05-28 07:24:17 +00:00
logger . Error ( "inventory reduction err:" , err . Error ( ) )
2022-01-26 08:02:21 +00:00
RespJson ( c , status . InternalServerError , nil )
return
}
2022-09-29 07:49:16 +00:00
if userCoupon . ID != 0 {
err = model . NewUserCouponQuerySet ( tx ) . IDEq ( userCoupon . ID ) . GetUpdater ( ) . SetState ( 2 ) . Update ( )
if err != nil {
tx . Rollback ( )
logger . Error ( "update user coupon state err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
}
2022-01-26 08:02:21 +00:00
err = tx . Commit ( ) . Error
if err != nil {
tx . Rollback ( )
logger . Error ( "commit err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-01-27 14:24:29 +00:00
go model . OrderCardUserRentCard ( uc . Uid , cardCount , memberConfig , nil )
2022-01-26 08:02:21 +00:00
ret := map [ string ] interface { } {
"web_pay" : "" ,
"order_id" : order . ID ,
}
2022-02-26 06:29:39 +00:00
go model . ShoppingCartCreateOrder ( uc . Uid , req . GameCardList )
2022-01-26 08:02:21 +00:00
RespOK ( c , ret )
return
}
//err = order.OrderCreate()
2022-01-16 08:56:20 +00:00
//if err != nil {
// logger.Error("err:", err)
// RespJson(c, status.InternalServerError, nil)
// return
//}
2022-01-26 08:02:21 +00:00
begin := model . DB . Begin ( )
err = order . Create ( begin )
if err != nil {
begin . Rollback ( )
logger . Error ( "err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
// 减库存
2022-01-16 08:56:20 +00:00
//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
//}
2022-04-21 09:03:33 +00:00
// 订单回调成功减库存
2022-01-26 08:02:21 +00:00
//err = model.GameCardGoodsInventoryReduction(req.GameCardList, req.StoreId, begin)
2022-01-16 08:56:20 +00:00
//if err != nil {
// begin.Rollback()
2022-01-26 08:02:21 +00:00
// logger.Error("inventory reduction err:%s", err.Error())
2022-01-16 08:56:20 +00:00
// RespJson(c, status.InternalServerError, nil)
// return
//}
2022-01-26 08:02:21 +00:00
err = order . CreatOrderCard ( req . GameCardList , req . StoreId , begin )
if err != nil {
begin . Rollback ( )
2022-05-28 07:24:17 +00:00
logger . Error ( "inventory reduction err:" , err . Error ( ) )
2022-01-26 08:02:21 +00:00
RespJson ( c , status . InternalServerError , nil )
return
}
err = begin . Commit ( ) . Error
if err != nil {
begin . Rollback ( )
2022-05-28 07:24:17 +00:00
logger . Errorf ( "err:%#v" , err )
2022-01-26 08:02:21 +00:00
RespJson ( c , status . InternalServerError , nil )
return
}
2022-01-16 10:47:48 +00:00
//orderId := fmt.Sprintf("%d", order.ID)
2022-09-29 07:49:16 +00:00
//orderId = "100000"
2022-01-16 10:47:48 +00:00
//fmt.Println("orderId", orderId)
//fmt.Println("Price", req.Price)
//fmt.Println("WxOpenID", user.WxOpenID)
//fmt.Println("WxPayRentCard", wxpay.WxPayRentCard)
2022-04-25 05:59:56 +00:00
configInfo , err := model . PayConfigInfo ( )
if err != nil {
logger . Error ( err )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-11-26 08:52:05 +00:00
err = model . UserOpenMemberRecord { Uid : uc . Uid , OpenNo : orderSn , OrderId : order . ID , OrderType : 1 , Attach : wxpay . WxPayRentCard } . Insert ( )
2021-11-01 03:32:23 +00:00
if err != nil {
logger . Error ( errors . New ( "WebPay err" ) )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-11-26 08:52:05 +00:00
//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
//}
webPay , err := wxpay . HmJsPayUnifiedOrder ( order . OrderSn , req . Price , user . WxOpenID , configInfo . NotifyUrl )
if err != nil {
logger . Error ( errors . New ( "WebPay err" ) )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-11-22 07:42:48 +00:00
//webPay, err := wxpay.HmJsPayUnifiedOrder(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
//}
2021-11-01 03:32:23 +00:00
//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
//}
ret := map [ string ] interface { } {
"web_pay" : webPay ,
"order_id" : order . ID ,
}
RespOK ( c , ret )
2022-06-14 06:07:15 +00:00
return
2021-11-01 03:32:23 +00:00
}
2021-12-25 08:38:30 +00:00
//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)
// }
//
//}
2021-11-01 03:32:23 +00:00
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 ( ) ) {
2021-12-25 08:38:30 +00:00
//if order.PayStatus != PayStatusUnPay || order.CardStatus != OrderCardStatusUnPick || order.CreatedAt.Add(3*time.Minute).Before(time.Now()) {
2021-11-01 03:32:23 +00:00
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
}
2022-04-25 05:59:56 +00:00
configInfo , err := model . PayConfigInfo ( )
if err != nil {
logger . Error ( err )
RespJson ( c , status . InternalServerError , nil )
return
}
2022-11-26 08:52:05 +00:00
//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
//}
orderSn := model . GetOrderSn ( )
err = model . UserOpenMemberRecord { Uid : uint32 ( order . Uid ) , OpenNo : orderSn , OrderId : order . ID , OrderType : 1 , Attach : wxpay . WxPayRentCard } . Insert ( )
if err != nil {
logger . Error ( errors . New ( "WebPay err" ) )
RespJson ( c , status . InternalServerError , nil )
return
}
webPay , err := wxpay . HmJsPayUnifiedOrder ( order . OrderSn , order . PayPrice , user . WxOpenID , configInfo . NotifyUrl )
2021-06-30 02:12:05 +00:00
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 {
2021-11-01 03:32:23 +00:00
StoreId uint32 ` json:"store_id" binding:"required" `
2021-06-30 02:12:05 +00:00
UserAddressId uint32 ` json:"user_address_id" binding:"required" `
Price uint32 ` json:"price" `
2022-09-29 07:49:16 +00:00
UserCouponId uint32 ` json:"user_coupon_id" `
2021-06-30 02:12:05 +00:00
} { }
if err := c . ShouldBindJSON ( & req ) ; err != nil {
logger . Error ( "ShouldBindJSON err:" , err )
RespJson ( c , status . BadRequest , nil )
return
}
2022-09-29 07:49:16 +00:00
uc := auth . GetCurrentUser ( c )
if uc == nil {
logger . Error ( "uc is nil" )
RespJson ( c , status . Unauthorized , nil )
return
}
2021-06-30 02:12:05 +00:00
var (
eg errgroup . Group
userAddress = & model . UserAddress { }
2021-11-01 03:32:23 +00:00
store = & model . Store { }
2021-06-30 02:12:05 +00:00
)
2021-11-01 03:32:23 +00:00
eg . Go ( func ( ) error {
store . ID = req . StoreId
err := store . Info ( )
if err != nil {
logger . Error ( "err:" , err )
}
return nil
} )
2021-06-30 02:12:05 +00:00
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
}
2022-09-29 07:49:16 +00:00
//model.UserCoupon{}
2021-06-30 02:12:05 +00:00
expressFee := model . ExpressFeeProvince
2021-11-01 03:32:23 +00:00
//if userAddress.Province != "广东省" {
if userAddress . Province != store . Province {
2021-06-30 02:12:05 +00:00
expressFee = model . ExpressFeeOutsideProvince
}
2022-09-29 07:49:16 +00:00
hasPostagePackage := 0
if req . UserCouponId != 0 {
var userCoupon model . UserCoupon
err := model . NewUserCouponQuerySet ( model . DB ) . IDEq ( req . UserCouponId ) . One ( & userCoupon )
if err != nil {
logger . Error ( "user coupon err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
if userCoupon . State == 1 && userCoupon . ActivityType == 3 && userCoupon . Uid == uc . Uid {
expressFee = 0
}
} else {
exist , err := model . QueryRecordExist ( fmt . Sprintf ( "SELECT * FROM user_coupon WHERE uid=%d AND activity_type=3 AND state=1" , uc . Uid ) )
if err != nil {
logger . Error ( "user coupon err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
if ! exist {
err := model . DB . Exec ( "UPDATE statistic SET count=count+1 WHERE event_type=1" ) . Error
if err != nil {
logger . Error ( "update statistic err:" , err )
}
hasPostagePackage = 1
}
}
hasPostagePackage = 1
2021-06-30 02:12:05 +00:00
ret := map [ string ] interface { } {
2022-09-29 07:49:16 +00:00
"total_amount" : req . Price + uint32 ( expressFee ) ,
"express_fee" : expressFee ,
"has_postage_package" : hasPostagePackage ,
2021-06-30 02:12:05 +00:00
}
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
2022-04-21 09:03:33 +00:00
// // 支付金额 地址id
2021-06-30 02:12:05 +00:00
// 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
2022-01-26 08:02:21 +00:00
err := order . Revert ( )
2021-06-30 02:12:05 +00:00
if err != nil {
logger . Error ( "err:" , err )
2021-11-01 03:32:23 +00:00
RespJson ( c , status . OrderCompleted , nil )
2021-06-30 02:12:05 +00:00
return
}
2022-01-26 08:02:21 +00:00
RespOK ( c , nil )
2021-06-30 02:12:05 +00:00
return
}
2021-11-01 03:32:23 +00:00
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
2022-01-26 08:02:21 +00:00
_ , err := order . RevertCancel ( )
2021-11-01 03:32:23 +00:00
if err != nil {
logger . Error ( "err:" , err )
2022-01-26 08:02:21 +00:00
RespJson ( c , status . OrderStatusNotReturning , nil )
2021-11-01 03:32:23 +00:00
return
}
2022-01-26 08:02:21 +00:00
//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 )
2021-11-01 03:32:23 +00:00
return
}
func OrderCancel ( c * gin . Context ) {
2022-01-26 08:02:21 +00:00
order := model . Order { }
if c . ShouldBindJSON ( & order ) != nil {
2021-11-01 03:32:23 +00:00
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
//}
2022-01-26 08:02:21 +00:00
fmt . Println ( "OrderId:" , order . ID )
//order := &model.Order{}
//order.ID = req.OrderId
2022-01-29 02:21:38 +00:00
isRecede , orderInfo , err := order . Cancel ( )
2021-11-01 03:32:23 +00:00
if err != nil {
logger . Error ( "err:" , err )
RespJson ( c , status . OrderDelivered , nil )
return
}
2022-01-29 02:21:38 +00:00
order = orderInfo
2021-12-25 08:38:30 +00:00
fmt . Println ( "order:" , order )
2022-01-26 08:02:21 +00:00
if isRecede {
2022-09-29 07:49:16 +00:00
if order . PostageType == 1 {
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
}
2021-12-25 08:38:30 +00:00
}
2022-09-29 07:49:16 +00:00
if order . PostageType == 2 {
var userCoupon model . UserCoupon
err = model . NewUserCouponQuerySet ( model . DB ) . ActivityTypeEq ( 3 ) . UidEq ( uint32 ( order . Uid ) ) . StateEq ( 2 ) .
OrderAscByID ( ) . Limit ( 1 ) . One ( & userCoupon )
if err != nil && err != model . RecordNotFound {
logger . Error ( "user coupon err:" , err )
RespJson ( c , status . InternalServerError , nil )
return
}
err = model . NewUserCouponQuerySet ( model . DB ) . IDEq ( userCoupon . ID ) . GetUpdater ( ) . SetState ( 1 ) . Update ( )
if err != nil {
logger . Error ( "user coupon err:" , err )
}
}
2022-04-25 05:59:56 +00:00
//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))
2021-12-25 08:38:30 +00:00
//err = wxpay.WxPayOrderRefund(orderRefund)
2022-04-25 05:59:56 +00:00
2021-12-25 08:38:30 +00:00
}
2021-11-01 03:32:23 +00:00
RespOK ( c , nil )
return
}