Browse Source

order no

master
黄梓健 5 years ago
parent
commit
e7dcaf1964
  1. 53
      controllers/client/good.go
  2. 4
      controllers/client/wx.go
  3. 3
      models/customer_order.go
  4. 10
      models/customer_order_sub.go
  5. 89
      services/pay/handle.go
  6. 14
      services/pay/order.go
  7. 46
      test/pay_test.go
  8. 1
      utils/define/define.go

53
controllers/client/good.go

@ -2,6 +2,7 @@ package client
import (
"encoding/json"
"fmt"
"github.com/ouxuanserver/osmanthuswine/src/core"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
@ -44,7 +45,6 @@ func (t *GoodCtl) ListGood() {
t.JSON([]interface{}{})
return
}
//t.Assert(exist, code.MSG_ORDER_NOT_EXIST, "订单活动不存在")
goods, err := models.GetGoodsByActivityId(activityId, areaId)
t.CheckErr(err)
@ -143,10 +143,6 @@ func (t *GoodCtl) Order() {
exist, err = option.GetByActivityId(param.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单活动不存在")
//if option.Status == 0 {
// t.ERROR("订单活动尚未开启", code.MSG_ORDER_RULE_NOT_EXIST)
// return
//}
// 注意库存 --> 开启事务
session := core.GetXormAuto().NewSession()
@ -177,14 +173,14 @@ func (t *GoodCtl) Order() {
}
price += int(good.Price*100) * g["num"]
subOrders = append(subOrders, &models.CustomerOrderSub{
IsDelete: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
OutTradeNo: "",
GoodsId: good.Id,
GoodsNum: g["num"],
GoodName: good.Name,
GoodPrice: good.Price,
IsDelete: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
OrderNo: "",
GoodsId: good.Id,
GoodsNum: g["num"],
GoodName: good.Name,
GoodPrice: good.Price,
})
}
@ -195,21 +191,23 @@ func (t *GoodCtl) Order() {
t.CheckErr(err)
}
count, err := session.Where("is_delete=0").Count(&models.CustomerOrder{})
if err != nil {
session.Rollback()
t.CheckErr(err)
}
order := models.CustomerOrder{
IsDelete: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
//UserPrizeId: , // 奖品
IsDelete: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
OrderNo: fmt.Sprint(define.DefaultOrderNo + int(count)),
ActivityId: activity.Id,
AreaId: area.Id,
AreaName: area.Name,
RehearsalId: activity.RehearsalId,
OutTradeNo: res["out_trade_no"].(string),
//OrderEntryPersonId: ,
BuyerId: user.Id,
//GoodsId: ,
//GoodsName: "",
//GoodsNum: 0,
BuyerId: user.Id,
TotalAmount: float64(price)/100 + option.PostFee,
PayAmount: float64(price)/100 + option.PostFee,
Status: 0,
@ -218,16 +216,9 @@ func (t *GoodCtl) Order() {
Phone: param.Phone,
IsDrawCash: 0,
Postage: option.PostFee,
//ExpressId: 0,
//ExpressName: "",
//ExpressNo: "",
CancelTime: define.DefaultTime,
AutoReceiveTime: define.DefaultTime,
//OrderTime: ,
//Good: nil,
//User: nil,
//OrderEntryPersonName: "",
//SubOrders: nil,
}
_, err = session.InsertOne(&order)
if err != nil {
@ -235,7 +226,7 @@ func (t *GoodCtl) Order() {
t.CheckErr(err)
}
for _, subOrder := range subOrders {
subOrder.OutTradeNo = order.OutTradeNo
subOrder.OrderNo = order.OrderNo
_, err = session.InsertOne(subOrder)
if err != nil {
session.Rollback()

4
controllers/client/wx.go

@ -61,8 +61,8 @@ func (t *WxCtl) Ticket() {
})
}
type WechatPay struct {
}
//type WechatPay struct {
//}
//func (t *WxCtl) Order() {
// userId := t.MustGetUID()

3
models/customer_order.go

@ -19,7 +19,8 @@ type CustomerOrder struct {
AreaId int64 `json:"area_id" xorm:"not null default 0 comment('地区id') INT(11)"`
AreaName string `json:"area_name" xorm:"not null default '' comment('地区名字') VARCHAR(255)"`
RehearsalId int64 `json:"rehearsal_id" xorm:"not null default 0 comment('彩排id/0正式') BIGINT(20)"`
OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('订单流水号') VARCHAR(255)"`
OrderNo string `json:"order_no" xorm:"not null default 0 comment('本机订单号') VARCHAR(255)"`
OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('微信订单流水号') VARCHAR(255)"`
OrderEntryPersonId int64 `json:"order_enter_person_id" xorm:"not null default 0 comment('订单录入人员id') BIGINT(20)"`
BuyerId int64 `json:"buyer_id" xorm:"not null default 0 comment('user表id') BIGINT(20)"`
GoodsId int64 `json:"goods_id,omitempty" xorm:"not null default 0 comment('customer_goods表id') BIGINT(20)"`

10
models/customer_order_sub.go

@ -14,11 +14,11 @@ type CustomerOrderSub struct {
CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
UpdatedAt time.Time `json:"updated_at" xorm:"not null updated comment('更新时间') DATETIME"`
OutTradeNo string `json:"out_trade_no" xorm:"not null default 0 comment('订单号') VARCHAR(128)"`
GoodsId int64 `json:"goods_id" xorm:"not null default 0 comment('商品id') INT(11)"`
GoodsNum int `json:"goods_num" xorm:"not null default 0 comment('商品数量') INT(11)"`
GoodName string `json:"good_name" xorm:"not null default '' comment('商品名字') VARCHAR(128)"`
GoodPrice float64 `json:"good_price" xorm:"not null default 0.00 comment('商品价格') DECIMAL(18)"`
OrderNo string `json:"order_no" xorm:"not null default 0 comment('订单号') VARCHAR(128)"`
GoodsId int64 `json:"goods_id" xorm:"not null default 0 comment('商品id') INT(11)"`
GoodsNum int `json:"goods_num" xorm:"not null default 0 comment('商品数量') INT(11)"`
GoodName string `json:"good_name" xorm:"not null default '' comment('商品名字') VARCHAR(128)"`
GoodPrice float64 `json:"good_price" xorm:"not null default 0.00 comment('商品价格') DECIMAL(18)"`
// 无关变量
GoodsPicUrl string `json:"goods_pic_url" xorm:"-"`

89
services/pay/handle.go

@ -17,51 +17,62 @@ func Handle(order *models.UserOrder) error {
}
if order.GoodType == 4 { // 直播商品
customerOrder := new(models.CustomerOrder)
exist, err := customerOrder.GetByOutTradeNO(order.OutTradeNo)
if err != nil || !exist {
return errors.New("直播商品信息异常")
}
//exist, err := customerOrder.GetByOutTradeNO(order.OutTradeNo)
//if err != nil || !exist {
// return errors.New("直播商品信息异常")
//}
activity := new(models.Activity)
exist, err = models.GetById(activity, order.ActivityId)
if err != nil || !exist {
return errors.New("互动信息异常")
if customerOrder.Status == 1 {
return errors.New("商品已经支付")
}
gift := new(models.OrderGift)
exist, err = gift.GetByActivityId(order.ActivityId)
_, err := customerOrder.UpdateStatusBy(order.OutTradeNo, 0, 1)
if err != nil {
return err
return errors.New("更新状态发送错误")
}
prize := new(models.UserPrize)
prize.UserId = order.UserId
prize.ActivityId = order.ActivityId
prize.RehearsalId = activity.RehearsalId
prize.ActivityName = activity.Name
prize.PrizeName = gift.GiftName
prize.PrizeImg = gift.GiftPicUrl
prize.PrizeType = 4
prize.IsDelete = false
prize.CreatedAt = time.Now()
prize.UpdatedAt = time.Now()
if exist {
if gift.Num < 0 {
} else if gift.Num > 0 {
count, err := new(models.CustomerOrder).Count(activity.Id, activity.RehearsalId, customerOrder.CreatedAt)
if err != nil {
return err
}
if gift.Num <= int(count) { // 人数足够
return nil
}
}
// gift.Num = 0 and
_, err = prize.Add()
if err != nil {
return err
}
}
//activity := new(models.Activity)
//exist, err = models.GetById(activity, order.ActivityId)
//if err != nil || !exist {
// return errors.New("互动信息异常")
//}
//
//gift := new(models.OrderGift)
//exist, err = gift.GetByActivityId(order.ActivityId)
//if err != nil {
// return err
//}
//
//prize := new(models.UserPrize)
//prize.UserId = order.UserId
//prize.ActivityId = order.ActivityId
//prize.RehearsalId = activity.RehearsalId
//prize.ActivityName = activity.Name
//prize.PrizeName = gift.GiftName
//prize.PrizeImg = gift.GiftPicUrl
//prize.PrizeType = 4
//prize.IsDelete = false
//prize.CreatedAt = time.Now()
//prize.UpdatedAt = time.Now()
//if exist {
// if gift.Num < 0 {
// } else if gift.Num > 0 {
// count, err := new(models.CustomerOrder).
// Count(activity.Id, activity.RehearsalId, customerOrder.CreatedAt)
// if err != nil {
// return err
// }
//
// if gift.Num <= int(count) { // 人数足够
// return nil
// }
// }
// // gift.Num = 0 and
// _, err = prize.Add()
// if err != nil {
// return err
// }
//}
} else if order.GoodType == 3 { // 直播红包
info := new(models.LiveRedEnvelopeRule)

14
services/pay/order.go

@ -83,11 +83,11 @@ func loopUnifiedOrder() {
panic("通道异常关闭")
}
if param.Expires <= time.Now().Unix() {
if param.Status == 0 {
order := new(models.UserOrder)
_, err = order.UpdateStatusByOutTradeNo(param.OutTradeNo, 7)
//Close(param.OutTradeNo) // 超时关闭订单
}
//if param.Status == 0 {
//order := new(models.UserOrder)
//_, err = order.UpdateStatusByOutTradeNo(param.OutTradeNo, 7)
//Close(param.OutTradeNo) // 超时关闭订单
//}
continue // 超时
}
@ -217,9 +217,7 @@ func ReOrder(outTradeNo string) (map[string]interface{}, error) {
//获取H5支付需要的paySign
pac := "prepay_id=" + userOrder.PrepayId
paySign := core2.JsapiSign(Appid, timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey)
if userOrder.TimeExpire <= core2.FormatTime(time.Now()) {
go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0)
}
go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0)
return map[string]interface{}{
"appid": Appid,
"timestamp": timestamp,

46
test/pay_test.go

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/chanxuehong/wechat/mch/core"
pay_service "hudongzhuanjia/services/pay"
"math"
"testing"
"time"
)
@ -46,3 +47,48 @@ func TestTimeExpire(t *testing.T) {
fmt.Println(t1, t2, core.FormatTime(t3))
fmt.Println(t3.Unix(), now.Unix())
}
func TestSendNotice(t *testing.T) {
//im_service.SendNoticeByActivityId(91)
}
// 地球半径,单位米
const R = 6367000
//const R = 6378.137
func rad(d float64) float64 {
return d * math.Pi / 180.0
}
// lonA, latA分别为A点的纬度和经度
// lonB, latB分别为B点的纬度和经度
// 返回的距离单位为米
func Sphere(lonA, latA, lonB, latB float64) float64 {
c := math.Sin(latA)*math.Sin(latB)*math.Cos(lonA-lonB) + math.Cos(latA)*math.Cos(latB)
return R * math.Acos(c) * math.Pi / 180
}
func TestSphere(t *testing.T) {
lonA, latA := 43.653887, 108.197424
lonB, latB := 42.901128, 107.562718
a := Sphere(lonA, latA, lonB, latB)
fmt.Print(a / 1000.0)
}
func TestMy(t *testing.T) {
a := `SELECT
shop_id ,
shop_name ,
lng ,
lat ,
POWER(lat - 40.05748 , 2) + POWER(lng - 116.30759 , 2) * POWER(COS((lat + 40.05748) / 2) , 2) AS distance
FROM
shop_list
HAVING
distance < 1000
ORDER BY
distance
LIMIT 100;
`
}

1
utils/define/define.go

@ -87,3 +87,4 @@ const (
)
var DefaultTime = time.Date(1970, 1, 1, 8, 0, 0, 0, time.UTC)
var DefaultOrderNo = 10000000000
Loading…
Cancel
Save