diff --git a/controllers/client/order_entry.go b/controllers/client/order_entry.go index 5cba9f0..7e75cc5 100644 --- a/controllers/client/order_entry.go +++ b/controllers/client/order_entry.go @@ -59,11 +59,6 @@ func (t *OrderEntryCtl) Order() { t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") - good := models.CustomerGoods{} - exist, err = models.Get(&good, goodId) - t.CheckErr(err) - t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在") - s := core.GetXormAuto().NewSession() defer s.Close() err = s.Begin() @@ -71,7 +66,32 @@ func (t *OrderEntryCtl) Order() { s.Rollback() t.CheckErr(err) } + + good := models.CustomerGoods{} + exist, err = s.Where("is_delete=0 and id=?", goodId).Get(&good) + if err != nil || !exist { + s.Rollback() + t.ERROR("商品信息异常", code.MSG_ERR_Param) + return + } + // 找出商品库存 + ms := make([]map[string]int, 0) + err = s.Table(&models.CustomerOrderSub{}).Select("goods_id, sum(goods_num) as goods_num"). + Where("goods_id=?", good.Id).GroupBy("goods_id").Find(&ms) + if err != nil { + s.Rollback() + t.CheckErr(err) + } + for _, m := range ms { + if m["goods_id"] == int(good.Id) && m["goods_num"] >= good.Stock { + s.Rollback() + t.ERROR(good.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH) + return + } + } + order := models.CustomerOrder{} + // 查询库存 total, err := s.Where("is_delete=0").Count(&order) // 订单总数 if err != nil { s.Rollback() @@ -86,6 +106,7 @@ func (t *OrderEntryCtl) Order() { order.ActivityId = activity.Id order.RehearsalId = activity.RehearsalId order.OrderEntryPersonId = entryPerson.Id + order.OrderEntryPersonName = entryPerson.Name order.TotalAmount = good.Price order.OutTradeNo = utils.RandomStr(32) order.OrderNo = fmt.Sprint(define.DefaultOrderNo + int(total)) @@ -201,20 +222,9 @@ func (t *OrderEntryCtl) ManualOrder() { t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") - subs := make([]*models.CustomerOrderSub, 0) - totalAmount := 0.00 + goodIds := make([]int, 0) for _, g := range param.Goods { - good := models.CustomerGoods{} - exist, err = models.Get(&good, g.GoodId) - t.CheckErr(err) - t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在") - subs = append(subs, &models.CustomerOrderSub{ - GoodsId: good.Id, - GoodsNum: g.GoodNum, - GoodName: good.Name, - GoodPrice: good.Price, - }) - totalAmount += good.Price * float64(g.GoodNum) + goodIds = append(goodIds, g.GoodId) } s := core.GetXormAuto().NewSession() @@ -229,12 +239,58 @@ func (t *OrderEntryCtl) ManualOrder() { Nickname: param.Name, Password: utils.RandomStr(6), } - _, err = models.Add(&user) + _, err = s.InsertOne(&user) + if err != nil { + s.Rollback() + t.CheckErr(err) + } + // 校验库存 + goods := make([]*models.CustomerGoods, 0) + err = s.Where("is_delete=0").In("id", goodIds).Find(&goods) if err != nil { s.Rollback() t.CheckErr(err) } + if len(goods) != len(param.Goods) { + s.Rollback() + t.ERROR("商品信息异常", code.MSG_ERR_Param) + return + } + subs := make([]*models.CustomerOrderSub, 0) + totalAmount := 0.00 + for _, g := range param.Goods { + for _, good := range goods { + if g.GoodId == int(good.Id) { + subs = append(subs, &models.CustomerOrderSub{ + GoodsId: good.Id, + GoodsNum: g.GoodNum, + GoodName: good.Name, + GoodPrice: good.Price, + }) + totalAmount += good.Price * float64(g.GoodNum) + } + } + } + ms := make([]map[string]int, 0) + err = s.Table(&models.CustomerOrderSub{}).Select("goods_id, sum(goods_num) as goods_num"). + In("goods_id", goodIds).GroupBy("goods_id").Find(&ms) + if err != nil { + s.Rollback() + t.CheckErr(err) + } + for _, m := range ms { + for _, g := range goods { + if g.Stock == -1 { // 无上限 + break + } + if m["goods_id"] == int(g.Id) && m["goods_num"] >= g.Stock { + s.Rollback() + t.ERROR(g.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH) + return + } + } + } order := models.CustomerOrder{} count, err := s.Where("is_delete=0").Count(&order) // 查看订单所在 if err != nil { @@ -249,6 +305,7 @@ func (t *OrderEntryCtl) ManualOrder() { order.ActivityId = activity.Id order.RehearsalId = activity.RehearsalId order.OrderEntryPersonId = entryPerson.Id + order.OrderEntryPersonName = entryPerson.Name order.TotalAmount = totalAmount order.OutTradeNo = utils.RandomStr(32) order.OrderNo = fmt.Sprint(define.DefaultOrderNo + int(count)) @@ -322,6 +379,7 @@ func (t *OrderEntryCtl) ManualOrder() { } t.SUCCESS("成功录入订单") } + func (t *OrderEntryCtl) DeleteOrder() { orderId := t.MustGetInt64("order_id") order := new(models.CustomerOrder) diff --git a/models/customer_order.go b/models/customer_order.go index c57c9e6..f90abf5 100644 --- a/models/customer_order.go +++ b/models/customer_order.go @@ -14,22 +14,23 @@ type CustomerOrder struct { CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` UpdatedAt time.Time `json:"updated_at" xorm:"not null updated comment('更新时间') DATETIME"` - UserPrizeId int64 `json:"user_prize_id" xorm:"not null default 0 comment('用户奖品id') INT(11)"` - ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('主活动id') INT(11)"` - ArchId int `json:"arch_id" xorm:"not null default 0 comment('归档id') INT(11)"` - AreaId int64 `json:"area_id" xorm:"not null default 0 comment('地区id') INT(11)"` - AreaName string `json:"area_name" xorm:"not null default '' comment('地区名字') VARCHAR(255)"` - RehearsalId int64 `json:"rehearsal_id" xorm:"not null default 0 comment('彩排id/0正式') BIGINT(20)"` - OrderNo string `json:"order_no" xorm:"not null default 0 comment('本机订单号') VARCHAR(255)"` - OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('微信订单流水号') VARCHAR(255)"` - OrderEntryPersonId int64 `json:"order_enter_person_id" xorm:"not null default 0 comment('订单录入人员id') BIGINT(20)"` - BuyerId int64 `json:"buyer_id" xorm:"not null default 0 comment('user表id') BIGINT(20)"` - TotalAmount float64 `json:"total_amount" xorm:"not null default 0.00 comment('订单总额')"` - PayAmount float64 `json:"pay_amount" xorm:"not null default 0.00 comment('支付金额')"` - Postage float64 `json:"postage" xorm:"not null default 0.00 comment('邮费[0免邮]')"` - Status int `json:"status" xorm:"not null default 0 comment('订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]')"` - ExpireTime int64 `json:"expire_time" xorm:"not null default 0 comment('订单过时') INT(11)"` - Type int `json:"type" xorm:"not null default 0 comment('订单类型、0录入订单1直播订单')"` + UserPrizeId int64 `json:"user_prize_id" xorm:"not null default 0 comment('用户奖品id') INT(11)"` + ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('主活动id') INT(11)"` + ArchId int `json:"arch_id" xorm:"not null default 0 comment('归档id') INT(11)"` + AreaId int64 `json:"area_id" xorm:"not null default 0 comment('地区id') INT(11)"` + AreaName string `json:"area_name" xorm:"not null default '' comment('地区名字') VARCHAR(255)"` + RehearsalId int64 `json:"rehearsal_id" xorm:"not null default 0 comment('彩排id/0正式') BIGINT(20)"` + OrderNo string `json:"order_no" xorm:"not null default 0 comment('本机订单号') VARCHAR(255)"` + OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('微信订单流水号') VARCHAR(255)"` + OrderEntryPersonId int64 `json:"order_enter_person_id" xorm:"not null default 0 comment('订单录入人员id') INT(11)"` + OrderEntryPersonName string `json:"order_entry_person_name" xorm:"not null default '' comment('订单录入人员名字') INT(11)"` + BuyerId int64 `json:"buyer_id" xorm:"not null default 0 comment('user表id') BIGINT(20)"` + TotalAmount float64 `json:"total_amount" xorm:"not null default 0.00 comment('订单总额')"` + PayAmount float64 `json:"pay_amount" xorm:"not null default 0.00 comment('支付金额')"` + Postage float64 `json:"postage" xorm:"not null default 0.00 comment('邮费[0免邮]')"` + Status int `json:"status" xorm:"not null default 0 comment('订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]')"` + ExpireTime int64 `json:"expire_time" xorm:"not null default 0 comment('订单过时') INT(11)"` + Type int `json:"type" xorm:"not null default 0 comment('订单类型、0录入订单1直播订单')"` // 快递信息 Receiver string `json:"receiver" xorm:"not null default '' comment('收件人') VARCHAR(128)"` @@ -46,12 +47,11 @@ type CustomerOrder struct { //GoodsId int64 `json:"goods_id,omitempty" xorm:"not null default 0 comment('customer_goods表id') BIGINT(20)"` //GoodsName string `json:"goods_name,omitempty" xorm:"not null default '' comment('商品名字') VARCHAR(255)"` //GoodsNum int `json:"goods_num,omitempty" xorm:"not null default 0 comment('商品数量') INT(11)"` - OrderTime string `json:"order_time,omitempty" xorm:"-"` - Good *CustomerGoods `json:"good,omitempty" xorm:"-"` - User *User `json:"user,omitempty" xorm:"-"` - OrderEntryPersonName string `json:"order_entry_person_name,omitempty" xorm:"-"` - ServicePhone string `json:"service_phone" xorm:"-"` - SubOrders []map[string]string `json:"sub_orders,omitempty" xorm:"-"` + OrderTime string `json:"order_time,omitempty" xorm:"-"` + Good *CustomerGoods `json:"good,omitempty" xorm:"-"` + User *User `json:"user,omitempty" xorm:"-"` + ServicePhone string `json:"service_phone" xorm:"-"` + SubOrders []map[string]string `json:"sub_orders,omitempty" xorm:"-"` } func (t *CustomerOrder) TableName() string { diff --git a/models/customer_order_sub.go b/models/customer_order_sub.go index 7216629..bd5bdc6 100644 --- a/models/customer_order_sub.go +++ b/models/customer_order_sub.go @@ -40,6 +40,14 @@ func GetCustomerOrderSubsByOrderIds(orderIds interface{}) (subs []map[string]str return } +func GetCustomerOrderSubsByOrderId(orderId interface{}) (subs []map[string]string, err error) { + err = core.GetXormAuto().Table(new(CustomerOrderSub)).Alias("s"). + Select("s.order_id, s.goods_id, s.goods_num, s.good_name, s.good_price, g.goods_pic_url"). + Join("left", new(CustomerGoods).Alias("g"), "g.id=s.goods_id"). + Where("s.is_delete=0 and s.order_id", orderId).Desc("s.created_at").Find(&subs) + return +} + func GetSubOrdersByOrderId(orderId interface{}) ([]*CustomerOrderSub, error) { subs := make([]*CustomerOrderSub, 0) err := core.GetXormAuto().Where("is_delete=0 and order_id=?", orderId).Find(&subs) diff --git a/services/pay/handle.go b/services/pay/handle.go index 761db37..bfc4cfd 100644 --- a/services/pay/handle.go +++ b/services/pay/handle.go @@ -45,11 +45,12 @@ func HandleSuccess(order *models.UserOrder) error { return errors.New("订单状态异常") } - subs := make([]map[string]string, 0) - err = core.GetXormAuto().Table(new(models.CustomerOrderSub)).Alias("s"). - Select("s.order_no, s.goods_id, s.goods_num, s.good_name, s.good_price, g.goods_pic_url"). - Join("left", new(models.CustomerGoods).Alias("g"), "g.id=s.goods_id"). - Where("s.is_delete=0 and order_no=?", cOrder.OrderNo).Find(&subs) + //subs := make([]map[string]string, 0) + subs, err := models.GetCustomerOrderSubsByOrderId(cOrder.Id) + //err = core.GetXormAuto().Table(new(models.CustomerOrderSub)).Alias("s"). + // Select("s.order_no, s.goods_id, s.goods_num, s.good_name, s.good_price, g.goods_pic_url"). + // Join("left", new(models.CustomerGoods).Alias("g"), "g.id=s.goods_id"). + // Where("s.is_delete=0 and s.order_=?", cOrder.Id).Find(&subs) if err != nil { s.Rollback() return err