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

282 lines
7.6 KiB

package client
import (
"encoding/json"
"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) GoodOption() {
activityId := t.MustGetInt64("activity_id")
//areaId := t.MustGetInt64("area_id")
option := new(models.CustomerOrderOption)
exist, err := option.GetByActivityId(activityId)
t.CheckErr(err)
if !exist {
t.JSON([]interface{}{})
return
}
//t.Assert(exist, code.MSG_ORDER_NOT_EXIST, "订单活动不存在")
t.JSON(option)
}
// 商品列表
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)
if !exist {
t.JSON([]interface{}{})
return
}
//t.Assert(exist, code.MSG_ORDER_NOT_EXIST, "订单活动不存在")
goods, err := models.GetGoodsByActivityId(activityId, areaId)
t.CheckErr(err)
//
//for index := range goods {
// goods[index].GoodType = option.PostFeeType
// goods[index].Postage = option.PostFee
//}
t.JSON(goods)
return
}
func (t *GoodCtl) ListOrder() {
activityId := t.MustGetInt64("activity_id")
status := t.MustGetInt("status")
areaId := t.MustGetInt64("area_id")
uid := t.MustGetUID()
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(uid, activity.Id, activity.RehearsalId,
areaId, status, t.Page, t.PageSize)
t.CheckErr(err)
outTradeNos := make([]string, 0)
for _, order := range orders {
outTradeNos = append(outTradeNos, order.OutTradeNo)
}
subs, err := models.GetCustomerOrderSubByOutTradeNos(outTradeNos)
t.CheckErr(err)
for index, order := range orders {
for _, sub := range subs {
if order.OutTradeNo == sub["out_trade_no"] {
orders[index].SubOrders = append(orders[index].SubOrders, sub)
}
}
}
t.JSON(orders)
}
type OrderParam struct {
ActivityId int64 `json:"activity_id"`
AreaId int64 `json:"area_id"`
Name string `json:"name"`
Phone string `json:"phone"`
Address string `json:"address"`
Goods []map[string]int `json:"goods"`
}
// 下订单
func (t *GoodCtl) Order() {
userId := t.MustGetUID() //
param := OrderParam{}
if t.Request.OriginRequest.Method == "POST" {
t.CheckErr(t.Bind(&param))
} else if t.Request.OriginRequest.Method == "GET" {
param.ActivityId = t.MustGetInt64("activity_id")
param.AreaId = t.MustGetInt64("area_id")
param.Name = t.MustGet("name")
param.Phone = t.MustGet("phone")
param.Address = t.MustGet("address")
goods := t.MustGet("goods")
err := json.Unmarshal([]byte(goods), &param.Goods)
t.CheckErr(err)
}
activity := new(models.Activity)
exist, err := models.GetById(activity, param.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
area := new(models.AreaStore)
exist, err = models.GetById(area, param.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(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()
defer session.Close()
session.Begin()
subOrders := make([]*models.CustomerOrderSub, 0)
var price = 0
for _, g := range param.Goods {
good := new(models.CustomerGoods)
exist, err = session.Where("is_delete=0 and id=?", g["id"]).Get(good)
if err != nil || !exist {
session.Rollback()
t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST)
}
if good.Stock-g["num"] < 0 {
session.Rollback()
t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST)
}
if good.Stock > 0 {
_, err = session.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
}
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,
})
}
res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid,
int64(price+int(option.PostFee*100)), 4, userId, activity.Id)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
order := &models.CustomerOrder{
IsDelete: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
//UserPrizeId: , // 奖品
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,
TotalAmount: float64(price)/100 + option.PostFee,
PayAmount: float64(price)/100 + option.PostFee,
Status: 0,
Receiver: param.Name,
Address: param.Address,
Phone: param.Phone,
IsDrawCash: 0,
Postage: option.PostFee,
//ExpressId: 0,
//ExpressName: "",
//ExpressNo: "",
//CancelTime: time.Time{},
AutoReceiveTime: time.Now().AddDate(0, 0, 15),
//OrderTime: time.Now(),
//Good: nil,
//User: nil,
//OrderEntryPersonName: "",
//SubOrders: nil,
}
_, err = session.InsertOne(order)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
for _, subOrder := range subOrders {
subOrder.OutTradeNo = order.OutTradeNo
_, err = session.InsertOne(subOrder)
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)
}
// 订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]
// 申请退款
func (t *GoodCtl) RefundOrder() {
outTradeNo := t.MustGet("out_trade_no")
order := new(models.CustomerOrder)
exist, err := order.GetByOutTradeNO(outTradeNo)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
if order.Status == 1 { // 退款
_, err = order.UpdateStatusBy(outTradeNo, 1, 5)
} else if order.Status == 3 {
_, err = order.UpdateStatusBy(outTradeNo, 3, 7)
} else {
t.ERROR("订单完成状态不能申请退款", code.MSG_CUSTOMER_ORDER_ERROR)
return
}
t.CheckErr(err)
t.SUCCESS("成功申请退款")
}
func (t *GoodCtl) CancelOrder() {
outTradeNo := t.MustGet("out_trade_no")
order := new(models.CustomerOrder)
exist, err := order.GetByOutTradeNO(outTradeNo)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
if order.Status != 0 {
t.ERROR("非待支付状态不能取消", code.MSG_CUSTOMER_ORDER_ERROR)
}
_, err = order.UpdateStatusBy(outTradeNo, 0, 9)
t.CheckErr(err)
t.SUCCESS("成功取消订单")
}