相关条件的统计
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.

177 lines
7.8 KiB

  1. package db
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db/model"
  4. zhios_condition_statistics_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils"
  5. zhios_condition_statistics_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils/logx"
  6. "fmt"
  7. "time"
  8. "xorm.io/xorm"
  9. )
  10. // OrderListCountByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单数
  11. func OrderListCountByUIDByOrderTypeByTime(Db *xorm.Engine, uid, buyType, state, stime, etime interface{}) (int64, error) {
  12. str := fmt.Sprintf("uid = %s AND order_type = %s AND ( state in (%s) or settle_at>0) AND create_at > %s AND create_at < %s",
  13. zhios_condition_statistics_utils.AnyToString(uid), buyType, state, stime, etime)
  14. zhios_condition_statistics_utils.FilePutContents("OrderListCountByUIDByOrderTypeByTime", zhios_condition_statistics_utils.SerializeStr(map[string]interface{}{
  15. "uid": uid,
  16. "buyType": buyType,
  17. "state": state,
  18. "stime": stime,
  19. "etime": etime,
  20. }))
  21. fmt.Println("============test订单数=================")
  22. fmt.Println(str)
  23. total, _ := Db.Where("uid = ? AND confirm_at > ? AND confirm_at < ? and settle_at=0", uid, stime, etime).In("state", state).Count(&model.OrdList{})
  24. total1, _ := Db.Where("uid = ? AND confirm_at > ? AND confirm_at < ? and settle_at>0", uid, stime, etime).Count(&model.OrdList{})
  25. total3, _ := Db.Where("uid = ? AND settle_at > ? AND settle_at < ? and confirm_at=0", uid, stime, etime).Count(&model.OrdList{})
  26. total2, _ := Db.Where("uid = ? AND created_at > ? AND created_at < ? and state=?", uid, stime, etime, 1).Count(&model.PrivilegeCardOrd{})
  27. total4, _ := Db.Where("uid = ? AND create_time > ? AND create_time < ? and status=?", uid, time.Unix(zhios_condition_statistics_utils.AnyToInt64(stime), 0).Format("2006-01-02 15:04:05"), time.Unix(zhios_condition_statistics_utils.AnyToInt64(etime), 0).Format("2006-01-02 15:04:05"), "已付款").Count(&model.RechargeOrder{})
  28. fmt.Println(total)
  29. return total + total1 + total2 + total3 + total4, nil
  30. }
  31. // 查询用户某时间段内某状态的佣金(ord_id => amount)
  32. func OrderRelateListByTimeByState(Db *xorm.Engine, uid, state, startTime, endTime interface{}) (*map[int64]float64, error) {
  33. var o []*model.OrdList
  34. var or []*model.OrdListRelate
  35. var idAmountMap = make(map[int64]float64)
  36. // 分佣明细数据
  37. err := Db.Where("`uid` = ?", uid).Find(&or)
  38. if err != nil {
  39. zhios_condition_statistics_logx.Error(err)
  40. return nil, err
  41. }
  42. if len(or) == 0 {
  43. return &idAmountMap, nil
  44. }
  45. var ids []int64
  46. for _, item := range or {
  47. ids = append(ids, item.Oid)
  48. }
  49. str := fmt.Sprintf("state in (%s) AND create_at >%s AND create_at < %s", state, startTime, endTime)
  50. fmt.Println("============test佣金数=================")
  51. fmt.Println(str)
  52. // 已结算的订单数据
  53. err = Db.In("ord_id", ids).In("state", state).Where(" confirm_at > ? AND confirm_at < ?", startTime, endTime).Find(&o)
  54. if err != nil {
  55. zhios_condition_statistics_logx.Error(err)
  56. return nil, err
  57. }
  58. if len(o) == 0 {
  59. return &idAmountMap, nil
  60. }
  61. // id => amount
  62. for _, item := range o {
  63. for _, i := range or {
  64. if item.OrdId == i.Oid {
  65. idAmountMap[i.Oid] = i.Amount
  66. }
  67. }
  68. }
  69. return &idAmountMap, nil
  70. }
  71. // OrderListPaidPriceByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单消费金额
  72. func OrderListPaidPriceByUIDByOrderTypeByTime(Db *xorm.Engine, uid, buyType, state, stime, etime interface{}) (float64, error) {
  73. str := fmt.Sprintf("uid = %s AND order_type = %s AND ( state in (%s) or settle_at>0) AND create_at > %s AND create_at < %s",
  74. zhios_condition_statistics_utils.AnyToString(uid), buyType, state, stime, etime)
  75. fmt.Println("============test订单数=================")
  76. fmt.Println(str)
  77. total, _ := Db.Where("uid = ? AND confirm_at > ? AND confirm_at < ? and settle_at=0", uid, stime, etime).In("state", state).Sum(&model.OrdList{}, "paid_price")
  78. total1, _ := Db.Where("uid = ? AND confirm_at > ? AND confirm_at < ? and settle_at>0", uid, stime, etime).Sum(&model.OrdList{}, "paid_price")
  79. total2, _ := Db.Where("uid = ? AND created_at > ? AND created_at < ? and state=?", uid, stime, etime, 1).Sum(&model.PrivilegeCardOrd{}, "paid_price")
  80. total4, _ := Db.Where("uid = ? AND create_time > ? AND create_time < ? and status=?", uid, time.Unix(zhios_condition_statistics_utils.AnyToInt64(stime), 0).Format("2006-01-02 15:04:05"), time.Unix(zhios_condition_statistics_utils.AnyToInt64(etime), 0).Format("2006-01-02 15:04:05"), "已付款").Sum(&model.RechargeOrder{}, "amount")
  81. fmt.Println(total)
  82. return total + total1 + total2 + total4, nil
  83. }
  84. // OrderListCountByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单数
  85. func OrderListCountByUIDByOrderType(Db *xorm.Engine, uid, buyType, state interface{}) (int64, error) {
  86. str := fmt.Sprintf("uid = %s AND order_type = %s AND ( state in (%s) or settle_at>0) ",
  87. zhios_condition_statistics_utils.AnyToString(uid), buyType, state)
  88. zhios_condition_statistics_utils.FilePutContents("OrderListCountByUIDByOrderTypeByTime", zhios_condition_statistics_utils.SerializeStr(map[string]interface{}{
  89. "uid": uid,
  90. "buyType": buyType,
  91. "state": state,
  92. }))
  93. fmt.Println("============test订单数=================")
  94. fmt.Println(str)
  95. total, _ := Db.Where("uid = ? and settle_at=0", uid).In("state", state).Count(&model.OrdList{})
  96. total1, _ := Db.Where("uid = ? and settle_at>0", uid).Count(&model.OrdList{})
  97. total3, _ := Db.Where("uid = ? and confirm_at=0", uid).Count(&model.OrdList{})
  98. total2, _ := Db.Where("uid = ? and state=?", uid, 1).Count(&model.PrivilegeCardOrd{})
  99. total4, _ := Db.Where("uid = ? and status=?", uid, "已付款").Count(&model.RechargeOrder{})
  100. fmt.Println(total)
  101. return total + total1 + total2 + total3 + total4, nil
  102. }
  103. // 查询用户某时间段内某状态的佣金(ord_id => amount)
  104. func OrderRelateListByState(Db *xorm.Engine, uid, state interface{}) (*map[int64]float64, error) {
  105. var o []*model.OrdList
  106. var or []*model.OrdListRelate
  107. var idAmountMap = make(map[int64]float64)
  108. // 分佣明细数据
  109. err := Db.Where("`uid` = ?", uid).Find(&or)
  110. if err != nil {
  111. zhios_condition_statistics_logx.Error(err)
  112. return nil, err
  113. }
  114. if len(or) == 0 {
  115. return &idAmountMap, nil
  116. }
  117. var ids []int64
  118. for _, item := range or {
  119. ids = append(ids, item.Oid)
  120. }
  121. str := fmt.Sprintf("state in (%s) ", state)
  122. fmt.Println("============test佣金数=================")
  123. fmt.Println(str)
  124. // 已结算的订单数据
  125. err = Db.In("ord_id", ids).In("state", state).Find(&o)
  126. if err != nil {
  127. zhios_condition_statistics_logx.Error(err)
  128. return nil, err
  129. }
  130. if len(o) == 0 {
  131. return &idAmountMap, nil
  132. }
  133. // id => amount
  134. for _, item := range o {
  135. for _, i := range or {
  136. if item.OrdId == i.Oid {
  137. idAmountMap[i.Oid] = i.Amount
  138. }
  139. }
  140. }
  141. return &idAmountMap, nil
  142. }
  143. // OrderListPaidPriceByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单消费金额
  144. func OrderListPaidPriceByUIDByOrderType(Db *xorm.Engine, uid, buyType, state interface{}) (float64, error) {
  145. str := fmt.Sprintf("uid = %s AND order_type = %s AND ( state in (%s) or settle_at>0) ",
  146. zhios_condition_statistics_utils.AnyToString(uid), buyType, state)
  147. fmt.Println("============test订单数=================")
  148. fmt.Println(str)
  149. total, _ := Db.Where("uid = ? and settle_at=0", uid).In("state", state).Sum(&model.OrdList{}, "paid_price")
  150. total1, _ := Db.Where("uid = ? and settle_at>0", uid).Sum(&model.OrdList{}, "paid_price")
  151. total2, _ := Db.Where("uid = ? and state=?", uid, 1).Sum(&model.PrivilegeCardOrd{}, "paid_price")
  152. total4, _ := Db.Where("uid = ? and status=?", uid, "已付款").Sum(&model.RechargeOrder{}, "amount")
  153. fmt.Println(total)
  154. return total + total1 + total2 + total4, nil
  155. }