From 851767ad0d6b6f39c983df673c66ea312ea39086 Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Fri, 24 Apr 2020 10:31:51 +0800 Subject: [PATCH] order --- controllers/base.go | 10 +++++-- controllers/client/good.go | 70 ++++++++++++++++++++++++++++---------------- log/hdzj.log | 1 + models/customer_order.go | 28 +++++++++++------- models/customer_order_sub.go | 12 ++++++-- 5 files changed, 80 insertions(+), 41 deletions(-) diff --git a/controllers/base.go b/controllers/base.go index 166e08e..95e34f8 100644 --- a/controllers/base.go +++ b/controllers/base.go @@ -17,7 +17,6 @@ type BaseCtl struct { core.Controller Page int PageSize int - Limit int // page * pagesize } func (t *BaseCtl) Prepare() { @@ -33,8 +32,15 @@ func (t *BaseCtl) Prepare() { return } t.Page, _ = t.GetInt("page") + if t.Page <= 0 { + t.Page = 0 + } else { + t.Page = t.Page - 1 + } t.PageSize, _ = t.GetInt("page_size") - t.Limit = t.Page * t.PageSize + if t.PageSize == 0 { + t.PageSize = 10 + } } type M map[string]interface{} diff --git a/controllers/client/good.go b/controllers/client/good.go index db8013e..d6507aa 100644 --- a/controllers/client/good.go +++ b/controllers/client/good.go @@ -66,7 +66,8 @@ func (t *GoodCtl) ListOrder() { t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") - orders, err := models.GetCustomerOrdersByActivityId(uid, activity.Id, activity.RehearsalId, areaId, status) + 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 { @@ -76,7 +77,7 @@ func (t *GoodCtl) ListOrder() { t.CheckErr(err) for index, order := range orders { for _, sub := range subs { - if order.OutTradeNo == sub.OutTradeNo { + if order.OutTradeNo == sub["out_trade_no"] { orders[index].SubOrders = append(orders[index].SubOrders, sub) } } @@ -85,12 +86,12 @@ func (t *GoodCtl) ListOrder() { } 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 interface{} `json:"goods"` + 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"` } // 下订单 @@ -141,7 +142,7 @@ func (t *GoodCtl) Order() { subOrders := make([]*models.CustomerOrderSub, 0) var price = 0 - for _, g := range goods { + 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 { @@ -181,23 +182,40 @@ func (t *GoodCtl) Order() { t.CheckErr(err) } - order := new(models.CustomerOrder) - order.AreaId = param.AreaId - order.AreaName = area.Name - order.RehearsalId = activity.RehearsalId - order.BuyerId = userId - order.ActivityId = activity.Id - order.TotalAmount = float64(price)/100 + option.PostFee - order.PayAmount = order.TotalAmount - order.OutTradeNo = res["out_trade_no"].(string) - order.Address = param.Address - order.Receiver = param.Name - order.Phone = param.Phone - order.Postage = option.PostFee - order.Status = 0 - order.IsDelete = false - order.CreatedAt = time.Now() - order.UpdatedAt = time.Now() + 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() diff --git a/log/hdzj.log b/log/hdzj.log index 4a42c08..43cb3cf 100644 --- a/log/hdzj.log +++ b/log/hdzj.log @@ -24,3 +24,4 @@ 2020-04-23 16:34:10.928 ERROR logger/logger.go:92 check err {"error": "FAILinvalid total_fee"} 2020-04-23 16:37:49.184 ERROR logger/logger.go:92 check err {"error": "FAILinvalid total_fee"} 2020-04-23 19:00:16.688 ERROR logger/logger.go:92 check err {"error": "json: cannot unmarshal string into Go struct field OrderParam.activity_id of type int64"} +2020-04-23 19:33:15.206 ERROR logger/logger.go:92 check err {"error": "Error 1052: Column 'is_delete' in where clause is ambiguous"} diff --git a/models/customer_order.go b/models/customer_order.go index 849dc7a..409a267 100644 --- a/models/customer_order.go +++ b/models/customer_order.go @@ -30,16 +30,24 @@ type CustomerOrder struct { Postage float64 `json:"postage" xorm:"not null default 0.00 comment('邮费[0免邮]') DECIMAL(18)"` Status int `json:"status" xorm:"not null default 0 comment('订单状态[0未支付1已支付2待发货3已发货4确认收货5申请退款6已退款]')"` // 快递信息 - Receiver string `json:"receiver" xorm:"not null default '' comment('收件人') VARCHAR(128)"` - Address string `json:"address" xorm:"not null default '' comment('收件人地址') VARCHAR(255)"` - Phone string `json:"phone" xorm:"not null default '' comment('收件人电话') VARCHAR(128)"` + Receiver string `json:"receiver" xorm:"not null default '' comment('收件人') VARCHAR(128)"` + Address string `json:"address" xorm:"not null default '' comment('收件人地址') VARCHAR(255)"` + Phone string `json:"phone" xorm:"not null default '' comment('收件人电话') VARCHAR(128)"` + IsDrawCash int `json:"is_draw_cash" xorm:"not null default 0 comment('订单是否提取贷款0否1是') TINYINT(1)"` + ExpressId int `json:"express_id" xorm:"not null default '0' comment('快递公司名字') INT(11)"` + ExpressName string `json:"express_name" xorm:"not null default '' comment('快递名字') VARCHAR(128)"` + ExpressNo string `json:"express_no" xorm:"not null default '' comment('快递单号') VARCHAR(128)"` + CancelTime time.Time `json:"cancel_time" xorm:"not null default '1970-01-01 08:00:00' comment('取消时间') TIMESTAMP"` + AutoReceiveTime time.Time `json:"auto_receive_time" xorm:"not null default '1970-01-01 08:00:00' comment('自动收货时间') TIMESTAMP"` // 无关变量 - 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:"-"` - SubOrders []*CustomerOrderSub `json:"sub_orders,omitempty" xorm:"-"` + 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:"-"` + + // 无关变量 + SubOrders []map[string]string `json:"sub_orders,omitempty" xorm:"-"` } func (t *CustomerOrder) TableName() string { @@ -64,11 +72,11 @@ func (t *CustomerOrder) Count(activityId, rehearsalId int64, limitTime time.Time limitTime, activityId, rehearsalId).Count(t) } -func GetCustomerOrdersByActivityId(userId, activityId, rehearsalId, areaId int64, status int) ([]*CustomerOrder, error) { +func GetCustomerOrdersByActivityId(userId, activityId, rehearsalId, areaId int64, status, page, pageSize int) ([]*CustomerOrder, error) { orders := make([]*CustomerOrder, 0) err := core.GetXormAuto().Where("is_delete=0 and buyer_id=? and activity_id=? and "+ "rehearsal_id=? and area_id=? and status=?", userId, activityId, rehearsalId, areaId, status). - Asc("created_at").Find(&orders) + Asc("created_at").Limit(pageSize, page*pageSize).Find(&orders) return orders, err } diff --git a/models/customer_order_sub.go b/models/customer_order_sub.go index 8194c8b..5bcebbd 100644 --- a/models/customer_order_sub.go +++ b/models/customer_order_sub.go @@ -19,6 +19,9 @@ type CustomerOrderSub struct { GoodsNum int `json:"goods_num" xorm:"not null default 0 comment('商品数量') INT(11)"` GoodName string `json:"good_name" xorm:"not null default '' comment('商品名字') VARCHAR(128)"` GoodPrice float64 `json:"good_price" xorm:"not null default 0.00 comment('商品价格') DECIMAL(18)"` + + // 无关变量 + GoodsPicUrl string `json:"goods_pic_url" xorm:"-"` } func (t *CustomerOrderSub) TableName() string { @@ -29,8 +32,11 @@ func (t *CustomerOrderSub) Alias(n string) string { return fmt.Sprintf("%s as %s", t.TableName(), n) } -func GetCustomerOrderSubByOutTradeNos(outTradeNos []string) (subs []*CustomerOrderSub, err error) { - err = core.GetXormAuto().Where("is_delete=0").In("out_trade_no", outTradeNos). - Asc("created_at").Find(&subs) +func GetCustomerOrderSubByOutTradeNos(outTradeNos []string) (subs []map[string]string, err error) { + err = core.GetXormAuto().Table(new(CustomerOrderSub)).Alias("s"). + Select("s.out_trade_no, 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").In("s.out_trade_no", outTradeNos). + Asc("s.created_at").Find(&subs) return }