相关条件的统计
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

208 строки
9.3 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. "strings"
  8. "time"
  9. "xorm.io/xorm"
  10. )
  11. // OrderListCountByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单数
  12. func OrderListCountByUIDByOrderTypeByTime(Db *xorm.Engine, uid, buyType, state, stime, etime interface{}) (int64, error) {
  13. 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",
  14. zhios_condition_statistics_utils.AnyToString(uid), buyType, state, stime, etime)
  15. zhios_condition_statistics_utils.FilePutContents("OrderListCountByUIDByOrderTypeByTime", zhios_condition_statistics_utils.SerializeStr(map[string]interface{}{
  16. "uid": uid,
  17. "buyType": buyType,
  18. "state": state,
  19. "stime": stime,
  20. "etime": etime,
  21. }))
  22. fmt.Println("============test订单数=================")
  23. fmt.Println(str)
  24. total, _ := Db.Where("uid = ? AND confirm_at > ? AND confirm_at < ? and settle_at=0", uid, stime, etime).In("state", state).Count(&model.OrdList{})
  25. total1, _ := Db.Where("uid = ? AND confirm_at > ? AND confirm_at < ? and settle_at>0", uid, stime, etime).Count(&model.OrdList{})
  26. total3, _ := Db.Where("uid = ? AND settle_at > ? AND settle_at < ? and confirm_at=0", uid, stime, etime).Count(&model.OrdList{})
  27. total2, _ := Db.Where("uid = ? AND created_at > ? AND created_at < ? and state=?", uid, stime, etime, 1).Count(&model.PrivilegeCardOrd{})
  28. 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{})
  29. fmt.Println(total)
  30. return total + total1 + total2 + total3 + total4, nil
  31. }
  32. // 查询用户某时间段内某状态的佣金(ord_id => amount)
  33. func OrderRelateListByTimeByState(Db *xorm.Engine, uid, state, startTime, endTime interface{}) (*map[int64]float64, error) {
  34. var o []*model.OrdList
  35. var or []*model.OrdListRelate
  36. var idAmountMap = make(map[int64]float64)
  37. // 分佣明细数据
  38. err := Db.Where("`uid` = ?", uid).Find(&or)
  39. if err != nil {
  40. zhios_condition_statistics_logx.Error(err)
  41. return nil, err
  42. }
  43. if len(or) == 0 {
  44. return &idAmountMap, nil
  45. }
  46. var ids []int64
  47. for _, item := range or {
  48. ids = append(ids, item.Oid)
  49. }
  50. str := fmt.Sprintf("state in (%s) AND create_at >%s AND create_at < %s", state, startTime, endTime)
  51. fmt.Println("============test佣金数=================")
  52. fmt.Println(str)
  53. // 已结算的订单数据
  54. err = Db.In("ord_id", ids).In("state", state).Where(" confirm_at > ? AND confirm_at < ?", startTime, endTime).Find(&o)
  55. if err != nil {
  56. zhios_condition_statistics_logx.Error(err)
  57. return nil, err
  58. }
  59. if len(o) == 0 {
  60. return &idAmountMap, nil
  61. }
  62. // id => amount
  63. for _, item := range o {
  64. for _, i := range or {
  65. if item.OrdId == i.Oid {
  66. idAmountMap[i.Oid] = i.Amount
  67. }
  68. }
  69. }
  70. return &idAmountMap, nil
  71. }
  72. // OrderListPaidPriceByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单消费金额
  73. func OrderListPaidPriceByUIDByOrderTypeByTime(Db *xorm.Engine, uid, buyType, state, stime, etime interface{}, isSettle int) (float64, error) {
  74. 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",
  75. zhios_condition_statistics_utils.AnyToString(uid), buyType, state, stime, etime)
  76. fmt.Println("============test订单数=================")
  77. fmt.Println(str)
  78. var total float64 = 0
  79. if isSettle == 1 {
  80. total1, _ := Db.Where("uid = ? AND settle_at > ? AND settle_at < ? and settle_at>0", uid, stime, etime).Sum(&model.OrdList{}, "paid_price")
  81. total2, _ := Db.Where("uid = ? AND created_at > ? AND created_at < ? and state=?", uid, stime, etime, 1).Sum(&model.PrivilegeCardOrd{}, "paid_price")
  82. 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")
  83. total = total + total1 + total2 + total4
  84. } else {
  85. types := "confirm_at"
  86. serializeStr := zhios_condition_statistics_utils.SerializeStr(state)
  87. if strings.Contains(serializeStr, "3") {
  88. types = "pvd_settle_at"
  89. }
  90. if strings.Contains(serializeStr, "1") || strings.Contains(serializeStr, "2") {
  91. types = "confirm_at"
  92. }
  93. if strings.Contains(serializeStr, "0") {
  94. types = "create_at"
  95. }
  96. sqlStr := fmt.Sprintf(" %s >'%s' AND %s < '%s'", types, stime, types, etime)
  97. total, _ = Db.Where("uid = ? AND ? and settle_at=0", uid, sqlStr).In("state", state).Sum(&model.OrdList{}, "paid_price")
  98. total1, _ := Db.Where("uid = ? AND ? and settle_at>0", uid, sqlStr).Sum(&model.OrdList{}, "paid_price")
  99. total2, _ := Db.Where("uid = ? AND created_at > ? AND created_at < ? and state=?", uid, stime, etime, 1).Sum(&model.PrivilegeCardOrd{}, "paid_price")
  100. 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")
  101. total = total + total1 + total2 + total4
  102. }
  103. fmt.Println(total)
  104. return total, nil
  105. }
  106. // OrderListCountByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单数
  107. func OrderListCountByUIDByOrderType(Db *xorm.Engine, uid, buyType, state interface{}) (int64, error) {
  108. str := fmt.Sprintf("uid = %s AND order_type = %s AND ( state in (%s) or settle_at>0) ",
  109. zhios_condition_statistics_utils.AnyToString(uid), buyType, state)
  110. zhios_condition_statistics_utils.FilePutContents("OrderListCountByUIDByOrderTypeByTime", zhios_condition_statistics_utils.SerializeStr(map[string]interface{}{
  111. "uid": uid,
  112. "buyType": buyType,
  113. "state": state,
  114. }))
  115. fmt.Println("============test订单数=================")
  116. fmt.Println(str)
  117. total, _ := Db.Where("uid = ? and settle_at=0", uid).In("state", state).Count(&model.OrdList{})
  118. total1, _ := Db.Where("uid = ? and settle_at>0", uid).Count(&model.OrdList{})
  119. total3, _ := Db.Where("uid = ? and confirm_at=0", uid).Count(&model.OrdList{})
  120. total2, _ := Db.Where("uid = ? and state=?", uid, 1).Count(&model.PrivilegeCardOrd{})
  121. total4, _ := Db.Where("uid = ? and status=?", uid, "已付款").Count(&model.RechargeOrder{})
  122. fmt.Println(total)
  123. return total + total1 + total2 + total3 + total4, nil
  124. }
  125. // 查询用户某时间段内某状态的佣金(ord_id => amount)
  126. func OrderRelateListByState(Db *xorm.Engine, uid, state interface{}) (*map[int64]float64, error) {
  127. var o []*model.OrdList
  128. var or []*model.OrdListRelate
  129. var idAmountMap = make(map[int64]float64)
  130. // 分佣明细数据
  131. err := Db.Where("`uid` = ?", uid).Find(&or)
  132. if err != nil {
  133. zhios_condition_statistics_logx.Error(err)
  134. return nil, err
  135. }
  136. if len(or) == 0 {
  137. return &idAmountMap, nil
  138. }
  139. var ids []int64
  140. for _, item := range or {
  141. ids = append(ids, item.Oid)
  142. }
  143. str := fmt.Sprintf("state in (%s) ", state)
  144. fmt.Println("============test佣金数=================")
  145. fmt.Println(str)
  146. // 已结算的订单数据
  147. err = Db.In("ord_id", ids).In("state", state).Find(&o)
  148. if err != nil {
  149. zhios_condition_statistics_logx.Error(err)
  150. return nil, err
  151. }
  152. if len(o) == 0 {
  153. return &idAmountMap, nil
  154. }
  155. // id => amount
  156. for _, item := range o {
  157. for _, i := range or {
  158. if item.OrdId == i.Oid {
  159. idAmountMap[i.Oid] = i.Amount
  160. }
  161. }
  162. }
  163. return &idAmountMap, nil
  164. }
  165. // OrderListPaidPriceByUIDByOrderTypeByTime is 查询订单 by uid by time ,获取规定时间内的订单类型的订单消费金额
  166. func OrderListPaidPriceByUIDByOrderType(Db *xorm.Engine, uid, buyType, state interface{}, isSettle int) (float64, error) {
  167. str := fmt.Sprintf("uid = %s AND order_type = %s AND ( state in (%s) or settle_at>0) ",
  168. zhios_condition_statistics_utils.AnyToString(uid), buyType, state)
  169. fmt.Println("============test订单数=================")
  170. fmt.Println(str)
  171. var total float64 = 0
  172. if isSettle == 1 {
  173. total, _ = Db.Where("uid = ? and settle_at>0", uid).Sum(&model.OrdList{}, "paid_price")
  174. total2, _ := Db.Where("uid = ? and state=?", uid, 1).Sum(&model.PrivilegeCardOrd{}, "paid_price")
  175. total4, _ := Db.Where("uid = ? and status=?", uid, "已付款").Sum(&model.RechargeOrder{}, "amount")
  176. total = total + total2 + total4
  177. } else {
  178. total, _ = Db.Where("uid = ? and settle_at=0", uid).In("state", state).Sum(&model.OrdList{}, "paid_price")
  179. total1, _ := Db.Where("uid = ? and settle_at>0", uid).Sum(&model.OrdList{}, "paid_price")
  180. total2, _ := Db.Where("uid = ? and state=?", uid, 1).Sum(&model.PrivilegeCardOrd{}, "paid_price")
  181. total4, _ := Db.Where("uid = ? and status=?", uid, "已付款").Sum(&model.RechargeOrder{}, "amount")
  182. total = total + total1 + total2 + total4
  183. }
  184. fmt.Println(total)
  185. return total, nil
  186. }