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

387 lines
15 KiB

  1. package zyos_go_condition_hdl
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db/model"
  5. zhios_condition_statistics_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils"
  6. zhios_condition_statistics_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils/logx"
  7. "fmt"
  8. "time"
  9. "xorm.io/xorm"
  10. )
  11. // 累计自购数
  12. func OwnBuyTotal(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  13. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Unix()
  14. etime := t.Unix()
  15. //联盟结算后的
  16. total, err := db.OrderListCountByUIDByOrderTypeByTime(engine, uid, 0, []string{"1", "2", "3", "5"}, stime, etime)
  17. if err != nil {
  18. zhios_condition_statistics_logx.Warn(err)
  19. return ""
  20. }
  21. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  22. mallEtime := t
  23. //确认收货的才算
  24. mallTotal, err := db.MallOrdByTimeWithCount(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  25. if err != nil {
  26. zhios_condition_statistics_logx.Warn(err)
  27. return zhios_condition_statistics_utils.Int64ToStr(total)
  28. }
  29. total += mallTotal
  30. return zhios_condition_statistics_utils.Int64ToStr(total)
  31. }
  32. // 累计已结算佣金
  33. func SettleOrderSum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  34. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Unix()
  35. etime := t.Unix()
  36. // 用户分佣的订单
  37. idAmountMap, err := db.OrderRelateListByTimeByState(engine, uid, []string{"1", "2", "3", "5"}, stime, etime)
  38. if err != nil {
  39. zhios_condition_statistics_logx.Warn(err)
  40. return ""
  41. }
  42. var sum float64
  43. for _, item := range *idAmountMap {
  44. sum += item
  45. }
  46. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  47. mallEtime := t
  48. _, mallSum, err := db.MallOrderRelateListByTimeByState(engine, uid, 3, mallStime, mallEtime)
  49. if err != nil {
  50. zhios_condition_statistics_logx.Warn(err)
  51. return zhios_condition_statistics_utils.Float64ToStr(sum)
  52. }
  53. sum += mallSum
  54. return zhios_condition_statistics_utils.Float64ToStr(sum)
  55. }
  56. func ExtendCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  57. // 累计直推人数
  58. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  59. etime := t.Format("2006-1-02 15:04:05")
  60. sqlTpl := `SELECT count(*) as count
  61. FROM user_profile as up
  62. LEFT JOIN user as u ON u.uid=up.uid
  63. WHERE %s;`
  64. condStr := fmt.Sprintf("up.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), stime, etime)
  65. sql := fmt.Sprintf(sqlTpl, condStr)
  66. results, err := db.QueryNativeString(engine, sql)
  67. if err != nil {
  68. zhios_condition_statistics_logx.Warn(err)
  69. return ""
  70. }
  71. count := ""
  72. if len(results) > 0 {
  73. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  74. }
  75. return count
  76. }
  77. func TeamCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  78. // 累计团队有效直推人数
  79. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  80. etime := t.Format("2006-1-02 15:04:05")
  81. sqlTpl := `SELECT count(*) as count
  82. FROM user_profile as up
  83. LEFT JOIN user as u ON u.uid=up.uid
  84. WHERE %s;`
  85. condStr := fmt.Sprintf("up.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s' and up.is_verify=%d", zhios_condition_statistics_utils.AnyToString(uid), stime, etime, 1)
  86. sql := fmt.Sprintf(sqlTpl, condStr)
  87. results, err := db.QueryNativeString(engine, sql)
  88. if err != nil {
  89. zhios_condition_statistics_logx.Warn(err)
  90. return ""
  91. }
  92. count := ""
  93. if len(results) > 0 {
  94. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  95. }
  96. return count
  97. }
  98. // 累计团队符合相应等级的人数
  99. func TeamByLvCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  100. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  101. etime := t.Format("2006-1-02 15:04:05")
  102. sqlTpl := `SELECT count(*) as count
  103. FROM user_relate as ur
  104. LEFT JOIN user as u ON u.uid=ur.uid
  105. WHERE %s;`
  106. condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), task["task_type_level_id"], stime, etime)
  107. sql := fmt.Sprintf(sqlTpl, condStr)
  108. results, err := db.QueryNativeString(engine, sql)
  109. if err != nil {
  110. zhios_condition_statistics_logx.Warn(err)
  111. return ""
  112. }
  113. count := ""
  114. if len(results) > 0 {
  115. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  116. }
  117. return count
  118. }
  119. // 累计直推符合相应等级的人数
  120. func ExtendByLvCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  121. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  122. etime := t.Format("2006-1-02 15:04:05")
  123. sqlTpl := `SELECT count(*) as count
  124. FROM user_relate as ur
  125. LEFT JOIN user as u ON u.uid=ur.uid
  126. WHERE %s;`
  127. condStr := fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND u.level='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), "1", task["TaskTypeLevelId"], stime, etime)
  128. sql := fmt.Sprintf(sqlTpl, condStr)
  129. results, err := db.QueryNativeString(engine, sql)
  130. if err != nil {
  131. zhios_condition_statistics_logx.Warn(err)
  132. return ""
  133. }
  134. count := ""
  135. if len(results) > 0 {
  136. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  137. }
  138. return count
  139. }
  140. // 累计自购消费金额
  141. func OwnbuySum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  142. var total float64 = 0
  143. var err error
  144. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "1"}) {
  145. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Unix()
  146. etime := t.Unix()
  147. total, err = db.OrderListPaidPriceByUIDByOrderTypeByTime(engine, uid, 0, []string{"1", "2", "3", "5"}, stime, etime)
  148. if err != nil {
  149. zhios_condition_statistics_logx.Warn(err)
  150. return ""
  151. }
  152. }
  153. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "2", "8", "9"}) {
  154. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  155. mallEtime := t
  156. orderType := 0
  157. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"8", "9"}) {
  158. orderType = zhios_condition_statistics_utils.StrToInt(task["task_type_pvd"])
  159. }
  160. //确认收货的才算
  161. mallTotal, err := db.MallOrdByTime(engine, uid, orderType, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"), "")
  162. if err != nil {
  163. zhios_condition_statistics_logx.Warn(err)
  164. return zhios_condition_statistics_utils.Float64ToStr(total)
  165. }
  166. total += mallTotal
  167. }
  168. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "3"}) {
  169. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  170. mallEtime := t
  171. //确认收货的才算
  172. o2oTotal, err := db.O2oOrdByTime(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  173. if err != nil {
  174. zhios_condition_statistics_logx.Warn(err)
  175. return zhios_condition_statistics_utils.Float64ToStr(total)
  176. }
  177. total += o2oTotal
  178. o2oTotal1, err := db.O2oOrdPayToMerchantByTime(engine, uid, 0, "1", mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  179. if err != nil {
  180. zhios_condition_statistics_logx.Warn(err)
  181. return zhios_condition_statistics_utils.Float64ToStr(total)
  182. }
  183. total += o2oTotal1
  184. }
  185. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "4"}) {
  186. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  187. mallEtime := t
  188. //确认收货的才算
  189. b2cTotal, err := db.B2cOrdByTime(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  190. if err != nil {
  191. zhios_condition_statistics_logx.Warn(err)
  192. return zhios_condition_statistics_utils.Float64ToStr(total)
  193. }
  194. total += b2cTotal
  195. }
  196. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2))
  197. }
  198. //累计团队消费金额
  199. func TeamBuySum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  200. var total float64 = 0
  201. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "2", "8", "9"}) {
  202. sqlTpl := `SELECT SUM(ol.cost_price) AS amount
  203. FROM mall_ord_list_relate olr
  204. JOIN mall_ord ol ON olr.oid = ol.ord_id
  205. WHERE olr.uid = ?
  206. AND ol.state =3 and olr.level>0 ?;
  207. `
  208. str := ""
  209. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"8", "9"}) {
  210. str += " and ol.order_type=" + task["task_type_pvd"]
  211. }
  212. result, err := db.QueryNativeString(engine, sqlTpl, uid, str)
  213. if err == nil {
  214. total += zhios_condition_statistics_utils.StrToFloat64(result[0]["amount"])
  215. }
  216. }
  217. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "3"}) {
  218. o2oSqlTpl := `SELECT SUM(ol.cost_price) AS amount
  219. FROM o2o_ord_list_relate olr
  220. JOIN o2o_ord ol ON olr.oid = ol.ord_id
  221. WHERE olr.uid = ?
  222. AND ol.state =3 and olr.level>0;
  223. `
  224. o2oResult, err := db.QueryNativeString(engine, o2oSqlTpl, uid)
  225. if err == nil {
  226. total += zhios_condition_statistics_utils.StrToFloat64(o2oResult[0]["amount"])
  227. }
  228. }
  229. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "4"}) {
  230. b2cSqlTpl := `SELECT SUM(ol.cost_price) AS amount
  231. FROM b2c_ord_list_relate olr
  232. JOIN b2c_ord ol ON olr.oid = ol.ord_id
  233. WHERE olr.uid = ?
  234. AND ol.state =3 and olr.level>0;
  235. `
  236. b2cResult, err := db.QueryNativeString(engine, b2cSqlTpl, uid)
  237. if err == nil {
  238. total += zhios_condition_statistics_utils.StrToFloat64(b2cResult[0]["amount"])
  239. }
  240. }
  241. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "1"}) {
  242. guideSqlTpl := `SELECT SUM(ol.paid_price) AS amount
  243. FROM ord_list_relate olr
  244. JOIN ord_list ol ON olr.oid = ol.ord_id
  245. WHERE olr.uid = ?
  246. AND ol.state in(1,2,3,5) and olr.level>0;
  247. `
  248. guideResult, err := db.QueryNativeString(engine, guideSqlTpl, uid)
  249. if err == nil {
  250. total += zhios_condition_statistics_utils.StrToFloat64(guideResult[0]["amount"])
  251. }
  252. }
  253. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2))
  254. }
  255. //小市场团队符合条件人数
  256. func SmallTeamSum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  257. //小市场团队符合条件人数
  258. smallUid := TotalSmallTeam(engine, zhios_condition_statistics_utils.AnyToString(uid))
  259. sqlTpl := `SELECT count(*) as count
  260. FROM user_relate as ur
  261. LEFT JOIN user as u ON u.uid=ur.uid
  262. WHERE %s;`
  263. condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' ", smallUid, task["task_type_level_id"])
  264. sql := fmt.Sprintf(sqlTpl, condStr)
  265. results, err := db.QueryNativeString(engine, sql)
  266. if err != nil {
  267. zhios_condition_statistics_logx.Warn(err)
  268. return ""
  269. }
  270. count := ""
  271. if len(results) > 0 {
  272. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  273. }
  274. return count
  275. }
  276. func ExtendStoreCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  277. if zhios_condition_statistics_utils.StrToInt(task["task_type_count"]) == 0 {
  278. return "0"
  279. }
  280. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"1"}) { //o2o
  281. sqlTpl := `SELECT os.id
  282. FROM user_relate ur
  283. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  284. LEFT JOIN o2o_store os on om.id=os.store_manager
  285. WHERE ur.parent_uid = ?
  286. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  287. `
  288. var ids = make([]string, 0)
  289. result, err := db.QueryNativeString(engine, sqlTpl, uid, task["task_type_count"])
  290. if err == nil {
  291. for _, v := range result {
  292. ids = append(ids, v["id"])
  293. }
  294. }
  295. if len(ids) > 0 {
  296. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"3", "4"}).Sum(&model.O2oOrd{}, "cost_price")
  297. sum1, _ := engine.In("belong_store_id", ids).Sum(&model.O2oPayToMerchant{}, "actual_pay_amount")
  298. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum+sum1, 4)
  299. }
  300. }
  301. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"2"}) { //b2c
  302. sqlTpl := `SELECT os.id
  303. FROM user_relate ur
  304. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  305. LEFT JOIN b2c_ord os on om.id=os.store_manager
  306. WHERE ur.parent_uid = ?
  307. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  308. `
  309. var ids = make([]string, 0)
  310. result, err := db.QueryNativeString(engine, sqlTpl, uid, task["task_type_count"])
  311. if err == nil {
  312. for _, v := range result {
  313. ids = append(ids, v["id"])
  314. }
  315. }
  316. if len(ids) > 0 {
  317. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"4"}).Sum(&model.B2cOrd{}, "cost_price")
  318. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum, 4)
  319. }
  320. }
  321. return "0"
  322. }
  323. func PayData(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time, hasPay bool, payOrdId int64) (string, bool, int64) {
  324. hasPay = true
  325. //TODO::根据 RegionalAgentSchemeTask 表中的 task_id 判断是否完成付费任务
  326. regionalAgentUserOrd, err := db.UserLevelOrderByTaskId(engine, zhios_condition_statistics_utils.AnyToString(uid), task["id"])
  327. if err != nil {
  328. _ = zhios_condition_statistics_logx.Warn(err)
  329. return "", hasPay, payOrdId
  330. }
  331. if regionalAgentUserOrd != nil {
  332. if regionalAgentUserOrd.ExpireAt.Unix() < t.Unix() && regionalAgentUserOrd.CreateAt.Format("2006-01-02 15:04:05") != regionalAgentUserOrd.ExpireAt.Format("2006-01-02 15:04:05") && regionalAgentUserOrd.DateType != 4 {
  333. return "0", hasPay, payOrdId
  334. }
  335. payOrdId = regionalAgentUserOrd.Id
  336. return "1", hasPay, payOrdId
  337. } else {
  338. return "0", hasPay, payOrdId
  339. }
  340. return "1", hasPay, payOrdId
  341. }
  342. func TotalSmallTeam(eg *xorm.Engine, uid string) string {
  343. sqlTpl := `SELECT sum(cstp.team_all_ord_price)+sum(cstp.my_ord_price) as amount,u.uid
  344. FROM user_profile u
  345. LEFT JOIN more_mall_outstanding_achievement cstp ON (cstp.uid =u.uid)
  346. WHERE u.parent_uid=%s GROUP BY u.uid ORDER BY amount desc,u.uid asc`
  347. sql := fmt.Sprintf(sqlTpl, uid)
  348. results, err := db.QueryNativeString(eg, sql)
  349. if err != nil {
  350. return "0"
  351. }
  352. if len(results) > 1 {
  353. return results[1]["uid"]
  354. }
  355. return "0"
  356. }