互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.5 KiB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/xormplus/xorm"
  6. "git.ouxuan.net/tommy/osmanthuswine/src/core"
  7. )
  8. const CustomerOrderSubTN = TableNamePrefix + "customer_order_sub"
  9. type CustomerOrderSub struct {
  10. Id int `json:"id" xorm:"not null pk autoincr comment('主键') INT(11)"`
  11. IsDelete bool `json:"is_delete" xorm:"not null default 0 comment('软删除') TINYINT(1)"`
  12. CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
  13. UpdatedAt time.Time `json:"updated_at" xorm:"not null updated comment('更新时间') DATETIME"`
  14. OrderId int `json:"order_id" xorm:"not null default 0 comment('订单id') VARCHAR(128)"`
  15. GoodsId int `json:"goods_id" xorm:"not null default 0 comment('商品id') INT(11)"`
  16. GoodsNum int `json:"goods_num" xorm:"not null default 0 comment('商品数量') INT(11)"`
  17. GoodName string `json:"good_name" xorm:"not null default '' comment('商品名字') VARCHAR(128)"`
  18. GoodPrice float64 `json:"good_price" xorm:"not null default 0.00 comment('商品价格') DECIMAL(18,2)"`
  19. // 无关变量
  20. GoodsPicUrl interface{} `json:"goods_pic_url" xorm:"extends"`
  21. }
  22. func (t *CustomerOrderSub) TableName() string {
  23. return CustomerOrderSubTN
  24. }
  25. func (t *CustomerOrderSub) Alias(n string) string {
  26. return fmt.Sprintf("%s as %s", t.TableName(), n)
  27. }
  28. type SubOrderResult struct {
  29. OrderId int `json:"order_id"`
  30. GoodsId int `json:"goods_id"`
  31. GoodsNum int `json:"goods_num"`
  32. GoodName string `json:"good_name"`
  33. GoodPrice float64 `json:"good_price"`
  34. GoodsPicUrl []interface{} `json:"goods_pic_url"`
  35. }
  36. func GetCustomerOrderSubsByOrderIds(orderIds interface{}) (subs []*SubOrderResult, err error) {
  37. err = core.GetXormAuto().Table(new(CustomerOrderSub)).Alias("s").
  38. Select("s.order_id, s.goods_id, s.goods_num, s.good_name, s.good_price, g.goods_pic_url").
  39. Join("left", new(CustomerGoods).Alias("g"), "g.id=s.goods_id").
  40. Where("s.is_delete=0").In("s.order_id", orderIds).Desc("s.created_at").Find(&subs)
  41. return
  42. }
  43. func GetCustomerOrderSubsByOrderId(orderId interface{}) (subs []map[string]string, err error) {
  44. err = core.GetXormAuto().Table(new(CustomerOrderSub)).Alias("s").
  45. Select("s.order_id, s.goods_id, s.goods_num, s.good_name, s.good_price, g.goods_pic_url").
  46. Join("left", new(CustomerGoods).Alias("g"), "g.id=s.goods_id").
  47. Where("s.is_delete=0 and s.order_id", orderId).Desc("s.created_at").Find(&subs)
  48. return
  49. }
  50. func GetSubOrdersByOrderId(orderId interface{}) ([]*CustomerOrderSub, error) {
  51. subs := make([]*CustomerOrderSub, 0)
  52. err := core.GetXormAuto().Where("is_delete=0 and order_id=?", orderId).Find(&subs)
  53. return subs, err
  54. }
  55. type OrderGoodNum struct {
  56. GoodsId int `json:"goods_id"`
  57. GoodsNum int `json:"goods_num"`
  58. }
  59. func GetSubOrderGoodNum(goodIds, rehearsalId, archId interface{}) (res []*OrderGoodNum, err error) {
  60. return GetSubOrderGoodNumBySession(core.GetXormAuto().NewSession(), goodIds, rehearsalId, archId)
  61. }
  62. func GetSubOrderGoodNumBySession(s *xorm.Session, goodIds, rehearsalId, archId interface{}) (res []*OrderGoodNum, err error) {
  63. err = s.Table(&CustomerOrderSub{}).Alias("s").Select("s.goods_id, SUM(s.goods_num) as goods_num").
  64. Join("left", (&CustomerOrder{}).TableName()+" as o", "o.id=s.order_id").
  65. Where("o.is_delete=0 and s.is_delete=0 and s.rehearsal_id=? and arch_id=?", rehearsalId, archId).
  66. In("s.goods_id", goodIds).NotIn("o.status", "0", "6", "8", "9").GroupBy("s.goods_id").Find(&res)
  67. return
  68. }