package client import ( "encoding/json" "fmt" "hudongzhuanjia/controllers" "hudongzhuanjia/models" pay_service "hudongzhuanjia/services/pay" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "strings" "time" "github.com/ouxuanserver/osmanthuswine/src/core" ) type GoodCtl struct { controllers.AuthorCtl } func (t *GoodCtl) GoodDetail() { goodsId := t.MustGetInt("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.MustGetInt("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.MustGetInt("activity_id") areaId := t.MustGetInt("area_id") area := &models.AreaStore{} exist, err := models.Get(area, areaId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") if area.IsMainArea != 1 && area.AreaGoodsRuleSwitch != 1 { exist, err = area.GetMainAreaById(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") } option := new(models.CustomerOrderOption) exist, err = option.GetByActivityId(activityId) t.CheckErr(err) if !exist { t.JSON([]interface{}{}) return } // 库存 goods, err := models.GetGoodsByActivityId(activityId, area.Id) t.CheckErr(err) goodIds := make([]int, 0) for _, g := range goods { goodIds = append(goodIds, g.Id) } res, err := models.GetSubOrderGoodNum(goodIds) t.CheckErr(err) for index := range goods { goods[index].GoodType = option.PostFeeType goods[index].Postage = option.PostFee for _, v := range res { if goods[index].Id == v.GoodsId { goods[index].Stock -= v.GoodsNum } } } t.JSON(goods) return } func (t *GoodCtl) ListOrder() { activityId := t.MustGetInt("activity_id") status := t.MustGet("status") areaId := t.MustGetInt("area_id") uid := t.GetAccountId() 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, activity.ArchId) t.CheckErr(err) orderIds := make([]int, 0) for _, order := range orders { orderIds = append(orderIds, order.Id) } subs, err := models.GetCustomerOrderSubsByOrderIds(orderIds) t.CheckErr(err) for index, order := range orders { order.ServicePhone = option.MainServicePhone for _, sub := range subs { if order.Id == sub.OrderId { orders[index].SubOrders = append(orders[index].SubOrders, sub) } } } t.JSON(orders) } type OrderParam struct { ActivityId int `json:"activity_id"` AreaId int `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.GetAccountId() // param := OrderParam{} if t.Request.OriginRequest.Method == "POST" { t.CheckErr(t.Bind(¶m)) } else if t.Request.OriginRequest.Method == "GET" { param.ActivityId = t.MustGetInt("activity_id") param.AreaId = t.MustGetInt("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, "订单活动不存在") // 注意库存 --> 开启事务 s := core.GetXormAuto().NewSession() defer s.Close() s.Begin() goodIds := make([]interface{}, 0) for _, g := range param.Goods { goodIds = append(goodIds, g["id"]) } goods := make([]*models.CustomerGoods, 0) err = s.Where("is_delete=0").In("id", goodIds).Find(&goods) if err != nil || len(goods) != len(goodIds) || len(goods) <= 0 { s.Rollback() t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) } orderGoodNum, err := models.GetSubOrderGoodNumBySession(s, goodIds) if err != nil { s.Rollback() t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) return } subOrders := make([]*models.CustomerOrderSub, 0) var price = 0 // 检测库存 for _, good := range goods { for _, g := range param.Goods { if good.Id == g["id"] { for _, v := range orderGoodNum { // 库存计算 if v.GoodsId == good.Id { good.Stock -= v.GoodsNum } } if good.Stock-g["num"] < 0 { s.Rollback() t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST) } else { // 需要减掉库存 //_, err = s.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good) //if err != nil { // s.Rollback() // t.CheckErr(err) //} price += int(good.Price*100) * g["num"] subOrders = append(subOrders, &models.CustomerOrderSub{ IsDelete: false, CreatedAt: time.Now(), UpdatedAt: time.Now(), GoodsId: good.Id, GoodsNum: g["num"], GoodName: good.Name, GoodPrice: good.Price, }) break } } } } var expireAt = time.Now().Add(30 * time.Second).Unix() count, err := s.Where("is_delete=0").Count(&models.CustomerOrder{}) if err != nil { s.Rollback() t.CheckErr(err) } order := models.CustomerOrder{ OrderNo: fmt.Sprint(define.DefaultOrderNo + int(count)), ActivityId: activity.Id, ArchId: activity.ArchId, AreaId: area.Id, AreaName: area.Name, RehearsalId: activity.RehearsalId, BuyerId: user.Id, TotalAmount: float64(price)/100 + option.PostFee, PayAmount: float64(price)/100 + option.PostFee, Status: 1, Type: 1, // 直播订单 Receiver: param.Name, Address: param.Address, Phone: param.Phone, GoodsId: subOrders[0].GoodsId, GoodsName: subOrders[0].GoodName, IsDrawCash: 0, ExpireTime: expireAt, Postage: option.PostFee, } amount := price + int(option.PostFee*100) res := make(map[string]interface{}) if amount > 0.00 { res, err = pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, amount, 4, userId, activity.Id, expireAt) if err != nil { s.Rollback() t.CheckErr(err) } order.OutTradeNo = res["out_trade_no"].(string) order.Status = 0 } _, err = s.InsertOne(&order) if err != nil { s.Rollback() t.CheckErr(err) } // 批量插入 beans := make([]interface{}, 0) for _, subOrder := range subOrders { subOrder.OrderId = order.Id beans = append(beans, subOrder) } s.Insert(beans...) if err != nil { s.Rollback() t.CheckErr(err) } err = s.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("成功确认已收货") }