|
|
package client
import ( "encoding/json" "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" pay_service "hudongzhuanjia/services/pay" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "strings" "time" )
type GoodCtl struct { controllers.AuthorCtl }
func (t *GoodCtl) GoodDetail() { goodsId := t.MustGetInt64("customer_goods_id") good := new(models.CustomerGoods) exist, err := models.Get(good, goodsId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_GOOD_NOT_EXIST, "商品详情不存在") t.JSON(good) }
func (t *GoodCtl) GoodOption() { activityId := t.MustGetInt64("activity_id")
option := new(models.CustomerOrderOption) exist, err := option.GetByActivityId(activityId) t.CheckErr(err) if !exist { t.JSON([]interface{}{}) return }
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 }
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.MustGet("status") areaId := t.MustGetInt64("area_id") uid := t.MustGetUID()
statusList := strings.Split(status, ",")
activity := new(models.Activity) exist, err := models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
option := new(models.CustomerOrderOption) exist, err = option.GetByActivityId(activityId) t.CheckErr(err) if !exist { t.JSON([]interface{}{}) return }
orders, err := models.GetCustomerOrdersByActivityId(uid, activity.Id, activity.RehearsalId, areaId, statusList, t.Page, t.PageSize) t.CheckErr(err) orderNos := make([]string, 0) for _, order := range orders { orderNos = append(orderNos, order.OrderNo) }
subs, err := models.GetCustomerOrderSubsByOrderNos(orderNos...) t.CheckErr(err) for index, order := range orders { order.ServicePhone = option.MainServicePhone for _, sub := range subs { if order.OrderNo == sub["order_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(¶m)) } 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), ¶m.Goods) t.CheckErr(err) }
activity := new(models.Activity) exist, err := models.Get(activity, param.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
area := new(models.AreaStore) exist, err = models.Get(area, param.AreaId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
user := new(models.User) exist, err = models.Get(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, "订单活动不存在")
// 注意库存 --> 开启事务
session := core.GetXormAuto().NewSession() defer session.Close() session.Begin()
goodIds := make([]interface{}, 0) for _, g := range param.Goods { goodIds = append(goodIds, g["id"]) } goods := make([]*models.CustomerGoods, 0) err = session.Where("is_delete=0").In("id", goodIds).Find(&goods) if err != nil || len(goods) != len(goodIds) || len(goods) <= 0 { session.Rollback() t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) }
subOrders := make([]*models.CustomerOrderSub, 0) var price = 0 // 检测库存
for _, good := range goods { for _, g := range param.Goods { if good.Id == int64(g["id"]) { if good.Stock-g["num"] < 0 { session.Rollback() t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST) } else { _, 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(), OrderNo: "", GoodsId: good.Id, GoodsNum: g["num"], GoodName: good.Name, GoodPrice: good.Price, }) break } } } }
var expireAt = time.Now().Add(30 * time.Second).Unix() res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, int64(price+int(option.PostFee*100)), 4, userId, activity.Id, expireAt) if err != nil { session.Rollback() t.CheckErr(err) }
count, err := session.Where("is_delete=0").Count(&models.CustomerOrder{}) if err != nil { session.Rollback() t.CheckErr(err) }
order := models.CustomerOrder{ OrderNo: fmt.Sprint(define.DefaultOrderNo + int(count)), ActivityId: activity.Id, AreaId: area.Id, AreaName: area.Name, RehearsalId: activity.RehearsalId, OutTradeNo: res["out_trade_no"].(string), BuyerId: user.Id, TotalAmount: float64(price)/100 + option.PostFee, PayAmount: float64(price)/100 + option.PostFee, Status: 0, Type: 1, // 直播订单
Receiver: param.Name, Address: param.Address, Phone: param.Phone, IsDrawCash: 0, ExpireTime: expireAt, Postage: option.PostFee, } _, err = session.InsertOne(&order) if err != nil { session.Rollback() t.CheckErr(err) }
// 批量插入
beans := make([]interface{}, 0) for _, subOrder := range subOrders { subOrder.OrderNo = order.OrderNo beans = append(beans, subOrder) } session.Insert(beans...) if err != nil { session.Rollback() t.CheckErr(err) }
err = session.Commit() t.CheckErr(err) 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") pay_service.HandleCancelOrder(outTradeNo) pay_service.Close(outTradeNo) t.SUCCESS("成功取消订单") }
func (t *GoodCtl) VerifyOrder() { 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 != 3 && order.Status != 1 { t.ERROR("非已支付或已发货状态不能确认", code.MSG_CUSTOMER_ORDER_ERROR) } _, err = order.UpdateStatusBy(outTradeNo, 3, 4) t.CheckErr(err) t.SUCCESS("成功确认已收货") }
|