package models import ( "fmt" "time" "github.com/xormplus/xorm" "git.ouxuan.net/tommy/osmanthuswine/src/core" ) const CustomerOrderSubTN = TableNamePrefix + "customer_order_sub" type CustomerOrderSub struct { Id int `json:"id" xorm:"not null pk autoincr comment('主键') INT(11)"` IsDelete bool `json:"is_delete" xorm:"not null default 0 comment('软删除') TINYINT(1)"` CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` UpdatedAt time.Time `json:"updated_at" xorm:"not null updated comment('更新时间') DATETIME"` OrderId int `json:"order_id" xorm:"not null default 0 comment('订单id') VARCHAR(128)"` GoodsId int `json:"goods_id" xorm:"not null default 0 comment('商品id') INT(11)"` 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,2)"` // 无关变量 GoodsPicUrl interface{} `json:"goods_pic_url" xorm:"extends"` } func (t *CustomerOrderSub) TableName() string { return CustomerOrderSubTN } func (t *CustomerOrderSub) Alias(n string) string { return fmt.Sprintf("%s as %s", t.TableName(), n) } type SubOrderResult struct { OrderId int `json:"order_id"` GoodsId int `json:"goods_id"` GoodsNum int `json:"goods_num"` GoodName string `json:"good_name"` GoodPrice float64 `json:"good_price"` GoodsPicUrl string `json:"goods_pic_url"` } func GetCustomerOrderSubsByOrderIds(orderIds interface{}) (subs []*SubOrderResult, 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").In("s.order_id", orderIds).Desc("s.created_at").Find(&subs) 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) return subs, err } type OrderGoodNum struct { GoodsId int `json:"goods_id"` GoodsNum int `json:"goods_num"` } func GetSubOrderGoodNum(goodIds, rehearsalId, archId interface{}) (res []*OrderGoodNum, err error) { return GetSubOrderGoodNumBySession(core.GetXormAuto().NewSession(), goodIds, rehearsalId, archId) } func GetSubOrderGoodNumBySession(s *xorm.Session, goodIds, rehearsalId, archId interface{}) (res []*OrderGoodNum, err error) { err = s.Table(&CustomerOrderSub{}).Alias("s").Select("s.goods_id, SUM(s.goods_num) as goods_num"). Join("left", (&CustomerOrder{}).TableName()+" as o", "o.id=s.order_id"). Where("o.is_delete=0 and s.is_delete=0 and o.rehearsal_id=? and arch_id=?", rehearsalId, archId). In("s.goods_id", goodIds).NotIn("o.status", "0", "6", "8", "9").GroupBy("s.goods_id").Find(&res) return }