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

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