互动
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.

144 lines
3.7 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")
goods, err := models.GetGoodsByActivityId(activityId, areaId)
t.CheckErr(err)
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() //
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") // 商品数量
_type := t.MustGetInt("delivery") // 配送: 1 快递免邮 2 快递 xxx 3 自提 4 到付
fee, exist := t.GetDouble("fee") // 快递费用
if _type == 2 && !exist || fee == 0 {
t.ERROR("fee参数错误", code.MSG_ERR_Param)
return
}
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, "用户不存在")
// 注意库存 --> 开启事务
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)
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
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 = fee
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)
}