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) 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 } 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.GetById(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.GetCustomerOrderSubByOrderNos(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.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, "订单活动不存在") // 注意库存 --> 开启事务 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(), OrderNo: "", 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) } count, err := session.Where("is_delete=0").Count(&models.CustomerOrder{}) if err != nil { session.Rollback() t.CheckErr(err) } order := models.CustomerOrder{ IsDelete: false, CreatedAt: time.Now(), UpdatedAt: time.Now(), 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, Receiver: param.Name, Address: param.Address, Phone: param.Phone, IsDrawCash: 0, Postage: option.PostFee, } _, err = session.InsertOne(&order) if err != nil { session.Rollback() t.CheckErr(err) } for _, subOrder := range subOrders { subOrder.OrderNo = order.OrderNo _, 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) } session := core.GetXormAuto().NewSession() defer session.Close() err = session.Begin() t.CheckErr(err) order.Status = 9 order.CancelTime = time.Now() _, err = session.Where("is_delete=0 and status=? and out_trade_no=?", 0, outTradeNo).Cols("status, cancel_time").Update(order) if err != nil { session.Rollback() t.CheckErr(err) } subs := make([]*models.CustomerOrderSub, 0) err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs) if err != nil { session.Rollback() t.CheckErr(err) } for _, sub := range subs { _, err = session.Where("id=?", sub.GoodsId). Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{}) if err != nil { session.Rollback() t.CheckErr(err) } } err = session.Commit() t.CheckErr(err) 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("成功确认已收货") }