diff --git a/controllers/client/good.go b/controllers/client/good.go index 5b75547..b269524 100644 --- a/controllers/client/good.go +++ b/controllers/client/good.go @@ -1,6 +1,7 @@ package client import ( + "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" pay_service "hudongzhuanjia/services/pay" @@ -41,7 +42,8 @@ func (t *GoodCtl) ListOrder() { // 下订单 func (t *GoodCtl) Order() { - userId := t.MustGetUID() // + userId := t.MustGetUID() // + activityId := t.MustGetInt64("activity_id") areaId := t.MustGetInt64("area_id") // 地区id name := t.MustGet("name") // 收货人名字 phone := t.MustGet("phone") // 收货电话 @@ -56,13 +58,8 @@ func (t *GoodCtl) Order() { 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) + exist, err := models.GetById(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") @@ -71,15 +68,42 @@ func (t *GoodCtl) Order() { t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") - user := models.User{} + user := new(models.User) exist, err = models.GetById(user, userId) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") + // 注意库存 --> 开启事务 + session := core.GetXormAuto().NewSession() + defer session.Close() + session.Begin() + + good := new(models.CustomerGoods) + exist, err = session.Where("is_delete=0 and id=?", goodId).Get(good) + if err != nil || !exist { + session.Rollback() + t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) + } + if good.Stock == 0 { + session.Rollback() + t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST) + } + if good.Stock > 0 { + good.Stock-- + _, err = session.ID(good.Id).Cols("stock").Update(good) + if err != nil { + session.Rollback() + t.CheckErr(err) + } + } + res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, int64(int(good.Price*100)*num), 4, userId, activity.Id) - t.CheckErr(err) + if err != nil { + session.Rollback() + t.CheckErr(err) + } - order := models.CustomerOrder{} + order := new(models.CustomerOrder) order.AreaId = areaId order.AreaName = area.Name order.RehearsalId = activity.RehearsalId @@ -99,8 +123,12 @@ func (t *GoodCtl) Order() { order.IsDelete = false order.CreatedAt = time.Now() order.UpdatedAt = time.Now() - _, err = order.Add() - t.CheckErr(err) + _, err = session.InsertOne(order) + if err != nil { + session.Rollback() + t.CheckErr(err) + } + session.Commit() res["order_id"] = order.Id t.JSON(res) diff --git a/controllers/client/live.go b/controllers/client/live.go index 40969a6..71463bd 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -125,7 +125,7 @@ func (t *LiveCtl) SendLiveRedPack() { record.RedEnvelopeType = 1 record.LiveRedEnvelopeRuleId = rule.Id record.ActivityId = activityId - record.Name = activity.Name + record.Name = user.Nickname + "发红包" record.UserId = 0 record.Amount = utils.Float64CusDecimal(float64(v)/float64(100), 2) record.CreatedAt = time.Now()