From 3dd286f31cf684a1ee6ddfec3f25f3ef5d9f9444 Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Mon, 20 Apr 2020 15:13:03 +0800 Subject: [PATCH] order --- controllers/client/order.go | 68 ++++++++++++++++++++++++++++++++++++++- controllers/client/order_entry.go | 30 ++++++++--------- models/customer_order.go | 20 ++++++++++++ models/user_order.go | 2 +- models/user_prize.go | 6 +++- services/pay/handle.go | 50 +++++++++++++++++++++++++++- 6 files changed, 157 insertions(+), 19 deletions(-) diff --git a/controllers/client/order.go b/controllers/client/order.go index e22bee8..25d702b 100644 --- a/controllers/client/order.go +++ b/controllers/client/order.go @@ -3,7 +3,9 @@ package client import ( "hudongzhuanjia/controllers" "hudongzhuanjia/models" + pay_service "hudongzhuanjia/services/pay" "hudongzhuanjia/utils/code" + "time" ) type GoodCtl struct { @@ -11,7 +13,7 @@ type GoodCtl struct { } // 商品列表 -func (t *GoodCtl) List() { +func (t *GoodCtl) ListGood() { activityId := t.MustGetInt64("activity_id") areaId := t.MustGetInt64("area_id") @@ -21,8 +23,26 @@ func (t *GoodCtl) List() { t.JSON(goods) } +func (t *GoodCtl) ListOrder() { + activityId := t.MustGetInt64("activity_id") + status := t.MustGetInt("status") + areaId := t.MustGetInt64("area_id") + + activity := new(models.Activity) + exist, err := models.GetById(activity, activityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") + + orders, err := models.GetCustomerOrdersByActivityId(activity.Id, activity.RehearsalId, areaId, status) + t.CheckErr(err) + + t.JSON(orders) +} + // 下订单 func (t *GoodCtl) Order() { + userId := t.MustGetUID() // + areaId := t.MustGetInt64("area_id") // 地区id name := t.MustGet("name") // 收货人名字 phone := t.MustGet("phone") // 收货电话 address := t.MustGet("address") // 收货地址 @@ -35,6 +55,52 @@ func (t *GoodCtl) Order() { t.ERROR("fee参数错误", code.MSG_ERR_Param) return } + + good := new(models.CustomerGoods) + exist, err := models.GetById(good, goodId) + t.CheckErr(err) + t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在") + + activity := new(models.Activity) + exist, err = models.GetById(activity, good.ActivityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") + + area := new(models.AreaStore) + exist, err = models.GetById(area, areaId) + t.CheckErr(err) + t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") + + user := models.User{} + exist, err = models.GetById(user, userId) + t.CheckErr(err) + t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") + + res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, int64(int(good.Price*100)*num), 4, userId, activity.Id) + t.CheckErr(err) + order := models.CustomerOrder{} + order.AreaId = areaId + order.AreaName = area.Name + order.RehearsalId = activity.RehearsalId + order.BuyerId = userId + order.GoodsId = goodId + order.GoodsName = good.Name + order.ActivityId = activity.Id + order.TotalAmount = float64(num) * good.Price + order.OutTradeNo = res["out_trade_no"].(string) + order.GoodsNum = num + order.Address = address + order.Receiver = name + order.Phone = phone + order.Postage = fee + order.Status = 0 + order.IsDelete = false + order.CreatedAt = time.Now() + order.UpdatedAt = time.Now() + _, err = order.Add() + t.CheckErr(err) + res["order_id"] = order.Id + t.JSON(res) } diff --git a/controllers/client/order_entry.go b/controllers/client/order_entry.go index 4fff3ac..cb6bdf3 100644 --- a/controllers/client/order_entry.go +++ b/controllers/client/order_entry.go @@ -72,24 +72,24 @@ func (t *OrderEntryCtl) Order() { exist, err = orderGift.GetByActivityId(activityId) t.CheckErr(err) - userPrize := new(models.UserPrize) - userPrize.UserId = userId - userPrize.ActivityId = activityId - userPrize.RehearsalId = activity.RehearsalId - userPrize.ActivityName = activity.Name - userPrize.PrizeName = orderGift.GiftName - userPrize.PrizeImg = orderGift.GiftPicUrl - userPrize.PrizeType = 3 - userPrize.IsDelete = false - userPrize.CreatedAt = time.Now() - userPrize.UpdatedAt = time.Now() + prize := new(models.UserPrize) + prize.UserId = userId + prize.ActivityId = activityId + prize.RehearsalId = activity.RehearsalId + prize.ActivityName = activity.Name + prize.PrizeName = orderGift.GiftName + prize.PrizeImg = orderGift.GiftPicUrl + prize.PrizeType = 3 + prize.IsDelete = false + prize.CreatedAt = time.Now() + prize.UpdatedAt = time.Now() if exist && orderGift.Num == 0 { // 存在无限制 - _, err := core.GetXormAuto().Insert(userPrize) + _, err = prize.Add() t.CheckErr(err) } order := new(models.CustomerOrder) - order.UserPrizeId = userPrize.Id + order.UserPrizeId = prize.Id order.AreaId = entryPerson.AreaId order.AreaName = area.Name order.RehearsalId = activity.RehearsalId @@ -113,9 +113,9 @@ func (t *OrderEntryCtl) Order() { order.CreatedAt, activityId, activity.RehearsalId).Count(new(models.CustomerOrder)) t.CheckErr(err) if orderGift.Num >= int(count) { - _, err = core.GetXormAuto().InsertOne(userPrize) + _, err = core.GetXormAuto().InsertOne(prize) t.CheckErr(err) - order.UserPrizeId = userPrize.Id + order.UserPrizeId = prize.Id _, err := core.GetXormAuto().Where("id=?", order.Id).Cols("user_prize_id").Update(order) t.CheckErr(err) } diff --git a/models/customer_order.go b/models/customer_order.go index 861aa6e..637aee3 100644 --- a/models/customer_order.go +++ b/models/customer_order.go @@ -48,3 +48,23 @@ func (t *CustomerOrder) SoftDeleteById(id int64) (int64, error) { t.IsDelete = true return core.GetXormAuto().Where("id=?", id).Cols("is_delete").Update(t) } + +func (t *CustomerOrder) Add() (int64, error) { + return core.GetXormAuto().InsertOne(t) +} + +func (t *CustomerOrder) GetByOutTradeNO(outTradeNo string) (bool, error) { + return core.GetXormAuto().Where("is_delete=0 and out_trade_no=?", outTradeNo).Get(t) +} + +func (t *CustomerOrder) Count(activityId, rehearsalId int64, limitTime time.Time) (int64, error) { + return core.GetXormAuto().Where("created_at >= ? and activity_id=? and rehearsal_id=? and is_delete=0", + limitTime, activityId, rehearsalId).Count(t) +} + +func GetCustomerOrdersByActivityId(activityId, rehearsalId, areaId int64, status int) ([]*CustomerOrder, error) { + orders := make([]*CustomerOrder, 0) + err := core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=? and status=? and area_id=?", + status, activityId, rehearsalId, areaId).Asc("created_at").Find(&orders) + return orders, err +} diff --git a/models/user_order.go b/models/user_order.go index 5b102fa..c58847c 100644 --- a/models/user_order.go +++ b/models/user_order.go @@ -15,7 +15,7 @@ type UserOrder struct { // 订单信息 DeviceInfo string `json:"device_info" xorm:"not null default('') comment('设备号') VARCHAR(32)"` - GoodType int64 `json:"good_type" xorm:"not null default(0) comment('1霸屏2打赏3直播红包')"` + GoodType int64 `json:"good_type" xorm:"not null default(0) comment('1霸屏2打赏3直播红包4商品订单')"` Body string `json:"body" xorm:"not null default('') comment('') VARCHAR(128)"` OutTradeNo string `json:"out_trade_no" xorm:"not null default('') comment('商户订单号') VARCHAR(32)"` FeeType string `json:"fee_type" xorm:"not null default('CNY') comment('货币种类') VARCHAR(16)"` diff --git a/models/user_prize.go b/models/user_prize.go index abc2a40..efa9d0d 100644 --- a/models/user_prize.go +++ b/models/user_prize.go @@ -15,7 +15,7 @@ type UserPrize struct { ActivityName string `json:"activity_name" xorm:"not null default('') comment('互动名字') VARCHAR(128)"` PrizeName string `json:"prize_name" xorm:"not null default('') comment('奖品名字') VARCHAR(128)"` PrizeImg string `json:"prize_img" xorm:"not null default('') comment('奖品图片') VARCHAR(255)"` - PrizeType int `json:"prize_type" xorm:"not null default(0) comment('订单来源1普通抽奖2订单抽奖3订单送礼') INT(11)"` + PrizeType int `json:"prize_type" xorm:"not null default(0) comment('订单来源1普通抽奖2订单抽奖3订单送礼4直播下单') INT(11)"` IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('软删除') TINYINT(1)"` CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` UpdatedAt time.Time `json:"updated_at" xorm:"not null updated comment('更新时间') DATETIME"` @@ -29,3 +29,7 @@ func (t *UserPrize) SoftDeleteById(id int64) (int64, error) { t.IsDelete = true return core.GetXormAuto().Where("id=?", id).Cols("is_delete").Update(t) } + +func (t *UserPrize) Add() (int64, error) { + return core.GetXormAuto().InsertOne(t) +} diff --git a/services/pay/handle.go b/services/pay/handle.go index 31b4200..78488e7 100644 --- a/services/pay/handle.go +++ b/services/pay/handle.go @@ -15,7 +15,55 @@ func Handle(order *models.UserOrder) error { if order == nil { return errors.New("订单信息不存在") } - if order.GoodType == 3 { // 直播红包 + if order.GoodType == 4 { // 直播商品 + customerOrder := new(models.CustomerOrder) + 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("互动信息异常") + } + + 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) exist, err := info.GetByOutTradeNo(order.OutTradeNo) if err != nil || !exist {