互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.2 KiB

package client
import (
"github.com/ouxuanserver/osmanthuswine/src/core"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
pay_service "hudongzhuanjia/services/pay"
"hudongzhuanjia/utils/code"
"time"
)
type GoodCtl struct {
controllers.AuthorCtl
}
// 商品列表
func (t *GoodCtl) ListGood() {
activityId := t.MustGetInt64("activity_id")
areaId := t.MustGetInt64("area_id")
option := new(models.CustomerOrderOption)
exist, err := option.GetByActivityId(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
//} else {
goods, err := models.GetGoodsByActivityId(activityId, areaId)
for index := range goods {
goods[index].GoodType = option.PostFeeType
goods[index].Postage = option.PostFee
}
t.CheckErr(err)
t.JSON(goods)
//}
return
}
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() //
activityId := t.MustGetInt64("activity_id")
areaId := t.MustGetInt64("area_id") // 地区id
name := t.MustGet("name") // 收货人名字
phone := t.MustGet("phone") // 收货电话
address := t.MustGet("address") // 收货地址
goodId := t.MustGetInt64("good_id") // 商品id
num := t.MustGetInt("num") // 商品数量
activity := new(models.Activity)
exist, err := models.GetById(activity, 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 := new(models.User)
exist, err = models.GetById(user, userId)
t.CheckErr(err)
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
option := new(models.CustomerOrderOption)
exist, err = option.GetByActivityId(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()
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+int(option.PostFee*100)), 4, userId, activity.Id)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
order := new(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 + option.PostFee
order.PayAmount = order.TotalAmount
order.OutTradeNo = res["out_trade_no"].(string)
order.GoodsNum = num
order.Address = address
order.Receiver = name
order.Phone = phone
order.Postage = option.PostFee
order.Status = 0
order.IsDelete = false
order.CreatedAt = time.Now()
order.UpdatedAt = time.Now()
_, err = session.InsertOne(order)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
session.Commit()
res["order_id"] = order.Id
t.JSON(res)
}
// 为支付的重新支付
func (t *GoodCtl) Reorder() {
outTradeNo := t.MustGet("out_trade_no")
res, err := pay_service.ReOrder(outTradeNo)
t.CheckErr(err)
t.JSON(res)
}