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

899 lines
35 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. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/md"
  6. zhios_condition_statistics_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles"
  8. "errors"
  9. "fmt"
  10. "time"
  11. "xorm.io/xorm"
  12. )
  13. func CommUpdate(params map[string]string, blockStarChain string, eg *xorm.Engine, dbName string, user *model.User, DataDetail md.DataDetail) (error, bool, int) {
  14. uid := DataDetail.Uid
  15. //判断有没有审核通过或审核中的记录
  16. has, err := db.UserLevelAuditingFindByUid(eg, zhios_condition_statistics_utils.StrToInt(uid))
  17. if err != nil {
  18. return err, false, 0
  19. }
  20. if has != nil {
  21. var msg = "您已审核通过,请勿重复申请"
  22. if has.State == 1 {
  23. msg = "正在审核中,请稍后再查看"
  24. }
  25. return errors.New(msg), false, 0
  26. }
  27. lv := user.Level
  28. result, AutoAudit := CommUplv(params, blockStarChain, eg, dbName, user, lv, DataDetail)
  29. if result == false {
  30. return errors.New("升级失败,请查看任务是否完成"), false, AutoAudit
  31. }
  32. return nil, result, AutoAudit
  33. }
  34. func CommUplv(params map[string]string, blockStarChain string, eg *xorm.Engine, dbName string, user *model.User, lv int, DataDetail md.DataDetail) (bool, int) {
  35. levelExpireAt := user.LevelExpireAt
  36. levelArriveAt := user.LevelArriveAt
  37. oldLevel := user.Level
  38. uid := zhios_condition_statistics_utils.IntToStr(user.Uid)
  39. result, _, payOrdId := IsCompleteSchemeTask(params, blockStarChain, eg, user, DataDetail.AgentType, DataDetail.IsChangeTime, dbName)
  40. AutoAudit := 0
  41. if result {
  42. level, err := db.UserLevelByID(eg, zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType))
  43. if err != nil || level == nil {
  44. return false, 0
  45. }
  46. date := time.Now()
  47. var auditData = model.UserLevelAudit{
  48. Uid: zhios_condition_statistics_utils.StrToInt(uid),
  49. CurrentLevelId: lv,
  50. NextLevelId: zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType),
  51. ConditionType: 2,
  52. AutoAudit: level.AutoAudit,
  53. DateType: 5,
  54. State: 1,
  55. RelateOid: payOrdId,
  56. CreateAt: date,
  57. UpdateAt: date,
  58. LevelDate: level.LevelDate,
  59. }
  60. has, err := db.UserLevelAuditInsertWithEngine(eg, &auditData)
  61. if has == 0 || err != nil {
  62. return false, 0
  63. }
  64. //自动审核
  65. if level.AutoAudit == 1 {
  66. //写入日志
  67. var log = &model.UserLevelChangeLog{
  68. Uid: user.Uid,
  69. BeforeLv: oldLevel,
  70. AfterLv: zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType),
  71. Time: int(time.Now().Unix()),
  72. BeforeLevelExpireAt: levelExpireAt,
  73. AfterLevelExpireAt: user.LevelExpireAt,
  74. BeforeLevelArriveAt: levelArriveAt,
  75. AfterLevelArriveAt: user.LevelArriveAt,
  76. Type: "level_up",
  77. }
  78. fmt.Println(log)
  79. eg.InsertOne(log)
  80. auditData.State = 2
  81. //审核记录修改
  82. if _, err := db.UserLevelAuditUpdateById(eg, auditData.Id, &auditData, "state"); err != nil {
  83. return false, 0
  84. }
  85. t := time.Now()
  86. var expire time.Time
  87. //订单时间修改
  88. if payOrdId > 0 {
  89. ord, err := db.UserLevelOrderById(eg, zhios_condition_statistics_utils.Int64ToStr(payOrdId))
  90. if err != nil {
  91. return false, 0
  92. }
  93. //过期时间在当前时间之前 并且 创建时间跟过期时间不相等 相等的话是永久会员
  94. if ord.ExpireAt.Unix() < t.Unix() && ord.CreateAt.Format("2006-01-02 15:04:05") != ord.ExpireAt.Format("2006-01-02 15:04:05") && ord.DateType != 4 {
  95. return false, 0
  96. }
  97. switch ord.DateType {
  98. case 1:
  99. // 月
  100. expire = t.AddDate(0, 1, 0)
  101. ord.ExpireAt = expire
  102. case 2:
  103. // 季
  104. expire = t.AddDate(0, 3, 0)
  105. ord.ExpireAt = expire
  106. case 3:
  107. // 年
  108. expire = t.AddDate(1, 0, 0)
  109. ord.ExpireAt = expire
  110. case 4:
  111. expire = t
  112. // 永久(设置为与创建时间相同)
  113. ord.ExpireAt = t
  114. }
  115. if _, err := db.UserLevelOrdUpdateOne(eg, ord); err != nil {
  116. return false, 0
  117. }
  118. }
  119. user.Level = zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType)
  120. user.LevelArriveAt = t
  121. if user.FirstArriveAt.IsZero() {
  122. user.FirstArriveAt = t
  123. }
  124. if payOrdId > 0 {
  125. user.LevelExpireAt = expire
  126. } else {
  127. if level.LevelDate == 0 { // 永久
  128. user.LevelExpireAt = t
  129. } else {
  130. user.LevelExpireAt = t.AddDate(0, level.LevelDate, 0)
  131. }
  132. }
  133. //用户等级修改
  134. // 更新user level 和 过期时间
  135. if _, err := db.UserUpdate(eg, user.Uid, user, "level,level_expire_at,level_arrive_at,first_arrive_at"); err != nil {
  136. return false, 0
  137. }
  138. eg.Where("uid=?", user.Uid).Cols("date").Update(&model.OneCirclesUserLvTime{Date: time.Now()})
  139. AutoAudit = 1
  140. var task model.UserLevelUpgradeTask
  141. eg.Where("scheme_id=? and task_type=22", DataDetail.AgentType).Get(&task)
  142. if task.Id > 0 {
  143. eg.Where("uid=?", user.Uid).Cols("is_not_down").Update(&model.User{IsNotDown: 0})
  144. } else {
  145. eg.Where("uid=?", user.Uid).Cols("is_not_down").Update(&model.User{IsNotDown: 1})
  146. }
  147. }
  148. }
  149. return result, AutoAudit
  150. }
  151. // IsCompleteSchemeTask 根据传入的 level 判断是否可以升级
  152. func IsCompleteSchemeTask(params map[string]string, blockStarChain string, engine *xorm.Engine, user *model.User, level, IsChangeTime, dbName string) (bool, bool, int64) {
  153. uid := zhios_condition_statistics_utils.IntToStr(user.Uid)
  154. userLevel, err := db.UserLevelByID(engine, user.Level)
  155. if err != nil {
  156. return false, false, 0
  157. }
  158. becomeLevel, err := db.UserLevelByID(engine, level)
  159. if err != nil {
  160. return false, false, 0
  161. }
  162. if userLevel == nil {
  163. if level == "" {
  164. level = "1"
  165. }
  166. } else {
  167. if becomeLevel.LevelWeight <= userLevel.LevelWeight && IsChangeTime != "1" {
  168. return false, false, 0
  169. }
  170. }
  171. //获取用户等级方案内容
  172. schemeTasks, err := db.BatchSelectUserLevelUpgradeTasks(engine, map[string]interface{}{
  173. "key": "scheme_id",
  174. "value": becomeLevel.Id,
  175. })
  176. if err != nil {
  177. return false, false, 0
  178. }
  179. return CheckSchemeTasksComplete(params, blockStarChain, engine, schemeTasks, uid, becomeLevel, level, "", dbName)
  180. }
  181. func CheckSchemeTasksComplete(params map[string]string, blockStarChain string, engine *xorm.Engine, tasks *[]model.UserLevelUpgradeTask, uid interface{}, scheme *model.UserLevel, level string, regionalId, dbName string) (bool, bool, int64) {
  182. //分离出 必做/选做 途径(任务组) && 分离出 必做/选做
  183. var tasksGroup []map[string]interface{}
  184. mustTasks := make(map[string][]interface{})
  185. selectTasks := make(map[string][]interface{})
  186. var (
  187. hasLevelPay = false
  188. LevelPayOrdId int64
  189. )
  190. allLvMap := db.UserLevelByAllMap(engine)
  191. for _, item := range *tasks {
  192. var temp = map[string]interface{}{}
  193. temp["id"] = item.Id
  194. temp["pid"] = item.Pid
  195. temp["schemeId"] = item.SchemeId
  196. temp["isMustTask"] = item.IsMustTask
  197. temp["taskType"] = item.TaskType
  198. temp["taskTypeLevelId"] = item.TaskTypeLevelId
  199. temp["withinDays"] = item.WithinDays
  200. temp["finishCount"] = item.FinishCount
  201. temp["payLevels"] = item.PayLevels
  202. temp["finish_task_num"] = item.FinishTaskNum
  203. if item.Pid == 0 && item.IsMustTask == 1 {
  204. temp["is_must"] = "1"
  205. tasksGroup = append(tasksGroup, temp)
  206. }
  207. if item.Pid == 0 && item.IsMustTask != 1 {
  208. temp["is_must"] = "0"
  209. tasksGroup = append(tasksGroup, temp)
  210. }
  211. name := GetTaskName(item.TaskType)
  212. finish, hasPay, payOrdId := GetFinishCount(params, blockStarChain, engine, uid, &item, level, regionalId, allLvMap, dbName)
  213. total := "" // 总共需完成的数
  214. if item.TaskType == 6 {
  215. // 当是付费条件是否,total的指标只能是1
  216. total = "1"
  217. hasLevelPay = hasPay
  218. LevelPayOrdId = payOrdId
  219. } else {
  220. total = item.FinishCount
  221. }
  222. if item.TaskType == 19 {
  223. total = zhios_condition_statistics_utils.IntToStr(zhios_condition_statistics_utils.StrToInt(total) + 1)
  224. }
  225. temp["name"] = name
  226. temp["total"] = total //需完成数
  227. temp["finish"] = finish //已完成数
  228. //fmt.Println("finish:::::::::::::", finish)
  229. //fmt.Println("total:::::::::::::", total)
  230. if zhios_condition_statistics_utils.AnyToFloat64(finish) >= zhios_condition_statistics_utils.AnyToFloat64(total) {
  231. temp["result"] = "1"
  232. } else {
  233. temp["result"] = "0"
  234. }
  235. if item.Pid != 0 && item.IsMustTask == 1 {
  236. mustTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)] = append(mustTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)], temp)
  237. }
  238. if item.Pid != 0 && item.IsMustTask == 0 {
  239. selectTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)] = append(selectTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)], temp)
  240. }
  241. }
  242. alreadyCompleteTasksGroup := 0 // 完成“选做途径”数量
  243. for _, v := range tasksGroup {
  244. fmt.Println(">>>>>>>>>>>>>>>>>>>>", v)
  245. if v["finish_task_num"] == nil {
  246. v["finishCount"] = 0
  247. }
  248. //处理必做途径
  249. if zhios_condition_statistics_utils.AnyToString(v["isMustTask"]) == "1" {
  250. if mustTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] != nil {
  251. needFinishTaskNum := v["finish_task_num"] // 需完成选做任务数
  252. alreadyFinishTaskNum := 0 // 已完成选做任务数
  253. for _, vv := range mustTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] {
  254. tmp := vv.(map[string]interface{})
  255. // 必做任务 且 未完成
  256. if tmp["isMustTask"] == 1 && tmp["result"] != "1" {
  257. return false, hasLevelPay, LevelPayOrdId
  258. }
  259. // 非必做任务 且 已完成
  260. if tmp["isMustTask"] != 1 && tmp["result"] == "1" {
  261. alreadyFinishTaskNum++
  262. }
  263. }
  264. // 需完成选做任务数 大于 已完成选做任务数
  265. if zhios_condition_statistics_utils.AnyToInt64(needFinishTaskNum) > zhios_condition_statistics_utils.AnyToInt64(alreadyFinishTaskNum) {
  266. return false, hasLevelPay, LevelPayOrdId
  267. }
  268. }
  269. }
  270. //处理选做途径
  271. if selectTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] != nil {
  272. needFinishTaskNum := v["finish_task_num"] // 需完成选做任务数
  273. alreadyFinishTaskNum := 0 // 已完成选做任务数
  274. alreadyFinishMustTask := true // 是否已完成必做任务
  275. for _, vv := range selectTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] {
  276. tmp := vv.(map[string]interface{})
  277. // 非必做任务 且 已完成
  278. if tmp["isMustTask"] != "1" && tmp["result"] == "1" {
  279. alreadyFinishTaskNum++
  280. }
  281. // 必做任务 且 未完成
  282. if tmp["isMustTask"] == "1" && tmp["result"] != "1" {
  283. alreadyFinishMustTask = false
  284. }
  285. }
  286. // 已完成必做任务 且 需完成选做任务数 小于等于 已完成选做任务数
  287. if alreadyFinishMustTask && zhios_condition_statistics_utils.AnyToInt64(needFinishTaskNum) <= zhios_condition_statistics_utils.AnyToInt64(alreadyFinishTaskNum) {
  288. alreadyCompleteTasksGroup++
  289. }
  290. }
  291. }
  292. //方案中设定的 需完成“选做途径”数 与 实际完成“选做途径”数 比较
  293. if scheme.ChoosableNum > alreadyCompleteTasksGroup {
  294. return false, hasLevelPay, LevelPayOrdId
  295. }
  296. return true, hasLevelPay, LevelPayOrdId
  297. }
  298. // GetTaskName is 获取任务名
  299. func GetTaskName(id int) string {
  300. switch id {
  301. case 1:
  302. return "累计自购订单总数"
  303. case 2:
  304. return "累计到账佣金"
  305. case 3:
  306. return "累计直推粉丝数量"
  307. case 4:
  308. return "累计团队有效直推人数"
  309. case 5:
  310. return "累计团队符合相应等级的人数"
  311. case 11:
  312. return "累计直推符合相应等级的人数"
  313. case 6:
  314. return "月付价格"
  315. case 7:
  316. return "季付价格"
  317. case 8:
  318. return "年付价格"
  319. case 9:
  320. return "永久价格"
  321. case 10:
  322. return "累计自购消费金额"
  323. case 12:
  324. return "累计团队消费金额"
  325. case 13:
  326. return "累计小市场团队符合等级人数"
  327. case 15:
  328. return "直推前N个店铺营业额"
  329. case 17:
  330. return "团队N条线中各有N个相应等级用户"
  331. case 18:
  332. return "小区累计自营消费金额"
  333. case 20:
  334. return "累计收益(结算)"
  335. case 22:
  336. return "连续活跃"
  337. default:
  338. return "无效名称"
  339. }
  340. }
  341. //
  342. // GetFinishCount is 计算规定时间内完成的指标的数量
  343. func GetFinishCount(params map[string]string, blockStarChain string, engine *xorm.Engine, uid interface{}, task *model.UserLevelUpgradeTask, level string, regionalId string, allLvMap map[int]*model.UserLevel, dbName string) (string, bool, int64) {
  344. t := time.Now()
  345. hasPay := false
  346. var payOrdId int64
  347. switch task.TaskType {
  348. case 20:
  349. var money float64 = 0
  350. shownewFinTotal, _ := db.SysCfgGetOne(engine, "shownew_fin_total")
  351. if shownewFinTotal != nil && shownewFinTotal.Val == "1" {
  352. var userStatistics model.UserStatistics
  353. engine.Where("uid=?", uid).Get(&userStatistics)
  354. money = zhios_condition_statistics_utils.StrToFloat64(userStatistics.ImportFinTotal) + zhios_condition_statistics_utils.StrToFloat64(userStatistics.FinTotal)
  355. } else {
  356. sum, _ := engine.Where("type=0 and uid=?", uid).Sum(&model.FinUserFlow{}, "amount")
  357. mallSum, _ := engine.Where("uid=? AND type=? and ord_title=?", uid, 1, "订单退款").Sum(&model.FinUserFlow{}, "amount")
  358. deSum2, _ := engine.Where("uid=? AND type=? and ord_type=?", uid, 0, "withdraw").Sum(&model.FinUserFlow{}, "amount")
  359. deSum3, _ := engine.Where("uid=? AND type=? and ord_title like ?", uid, 1, "%扣除%").Sum(&model.FinUserFlow{}, "amount")
  360. dsCheck, _ := db.SysCfgGetOne(engine, "ds_check")
  361. money = sum - deSum2 - deSum3 + mallSum
  362. if dsCheck != nil && dsCheck.Val == "1" {
  363. withdrawingSuccess, _ := engine.Where("uid=? ", uid).In("state", []string{"1", "2"}).Sum(&model.FinWithdrawApply{}, "amount")
  364. sql := `SELECT cast(SUM(LEFT(olr.amount,LENGTH(olr.amount)-2)) as decimal(50,4)) AS amount FROM ord_list_relate olr JOIN ord_list ol ON olr.oid = ol.ord_id WHERE olr.level=0 and olr.uid = " . $uid . " AND ol.state IN (0,1,2,3) and ol.is_transfer=1;`
  365. nativeString, _ := db.QueryNativeString(engine, sql)
  366. money = withdrawingSuccess
  367. for _, v := range nativeString {
  368. money += zhios_condition_statistics_utils.StrToFloat64(v["amount"])
  369. }
  370. }
  371. }
  372. return zhios_condition_statistics_utils.Float64ToStr(money), hasPay, payOrdId
  373. case 22:
  374. lvTime := db.GetOneCirclesUserLvTime(engine, uid)
  375. stime := time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()).Format("2006-01-02")
  376. if lvTime != nil && lvTime.Date.IsZero() == false {
  377. stime = lvTime.Date.Format("2006-01-02")
  378. }
  379. etime := t.Format("2006-01-02")
  380. if params["is_view"] == "1" {
  381. if lvTime == nil || (lvTime != nil && lvTime.Date.IsZero()) {
  382. stime = t.Format("2006-01-02")
  383. }
  384. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()).Format("2006-01-02")
  385. }
  386. _, days, activity := one_circles.CalcUserContinuousDailyActivityDays(engine, dbName, int(zhios_condition_statistics_utils.AnyToInt64(uid)), stime, etime)
  387. if activity == false {
  388. days = 0
  389. }
  390. return zhios_condition_statistics_utils.IntToStr(days), hasPay, payOrdId
  391. case 23:
  392. // 累计新人免单自购数
  393. total, err := db.OrderListCountByUIDByOrderTypeByTimeNew(engine, uid, "free", task)
  394. if err != nil {
  395. return "", hasPay, payOrdId
  396. }
  397. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  398. case 24:
  399. // 累计自购消费金额
  400. var total float64 = 0
  401. var err error
  402. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  403. total, err = db.OrderListSumByUIDByOrderTypeByTimeNew(engine, uid, "free", task)
  404. if err != nil {
  405. return "", hasPay, payOrdId
  406. }
  407. }
  408. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId
  409. case 1:
  410. // 累计自购数
  411. total, err := db.OrderListCountByUIDByOrderTypeByTimeNew(engine, uid, "", task)
  412. if err != nil {
  413. return "", hasPay, payOrdId
  414. }
  415. mallTotal, err := db.MallOrdWithCountNew(engine, uid, task)
  416. if err != nil {
  417. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  418. }
  419. total += mallTotal
  420. o2oTotal, err := db.O2oOrdWithCountNew(engine, uid, task)
  421. if err != nil {
  422. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  423. }
  424. total += o2oTotal
  425. b2cTotal, err := db.B2cOrdWithCountNew(engine, uid, task)
  426. if err != nil {
  427. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  428. }
  429. total += b2cTotal
  430. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  431. case 10:
  432. // 累计自购消费金额
  433. var total float64 = 0
  434. var err error
  435. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  436. total, err = db.OrderListSumByUIDByOrderTypeByTimeNew(engine, uid, "", task)
  437. if err != nil {
  438. return "", hasPay, payOrdId
  439. }
  440. }
  441. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) {
  442. orderType := 0
  443. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) {
  444. orderType = task.TaskTypePvd
  445. }
  446. secondTotal, err := db.MallOrdWithSumNew(engine, uid, orderType, task)
  447. if err != nil {
  448. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  449. }
  450. total += secondTotal
  451. }
  452. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) {
  453. secondTotal, err := db.O2oOrdWithSumNew(engine, uid, task)
  454. if err != nil {
  455. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  456. }
  457. total += secondTotal
  458. }
  459. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) {
  460. secondTotal, err := db.B2cOrdWithSumNew(engine, uid, task)
  461. if err != nil {
  462. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  463. }
  464. total += secondTotal
  465. }
  466. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId
  467. case 2, 25:
  468. // 累计自购佣金
  469. sum, err := db.OrderRelateListByTimeByStateNew(engine, uid, task)
  470. if err != nil {
  471. return "", hasPay, payOrdId
  472. }
  473. mallSum, err := db.MallOrderRelateListByTimeByStateNew(engine, uid, task)
  474. if err != nil {
  475. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  476. }
  477. sum += mallSum
  478. o2oSum, err := db.O2oOrderRelateListByTimeByStateNew(engine, uid, task)
  479. if err != nil {
  480. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  481. }
  482. sum += o2oSum
  483. b2cSum, err := db.B2cOrderRelateListByTimeByStateNew(engine, uid, task)
  484. if err != nil {
  485. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  486. }
  487. sum += b2cSum
  488. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  489. case 3:
  490. // 累计直推人数
  491. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  492. etime := t.Format("2006-1-02 15:04:05")
  493. sqlTpl := `SELECT count(*) as count
  494. FROM user_relate as up
  495. LEFT JOIN user as u ON u.uid=up.uid
  496. WHERE %s and up.level=1;`
  497. var condStr string
  498. if task.WithinDays <= 0 {
  499. condStr = fmt.Sprintf("up.parent_uid=%s ", zhios_condition_statistics_utils.AnyToString(uid))
  500. } else {
  501. 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)
  502. }
  503. sql := fmt.Sprintf(sqlTpl, condStr)
  504. results, err := db.QueryNativeString(engine, sql)
  505. if err != nil {
  506. return "", hasPay, payOrdId
  507. }
  508. count := ""
  509. if len(results) > 0 {
  510. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  511. }
  512. return count, hasPay, payOrdId
  513. case 4:
  514. // 累计团队有效直推人数
  515. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  516. etime := t.Format("2006-1-02 15:04:05")
  517. sqlTpl := `SELECT count(*) as count
  518. FROM user_relate as up
  519. LEFT JOIN user as u ON u.uid=up.uid
  520. LEFT JOIN user_profile as ups ON up.uid=ups.uid
  521. WHERE %s and up.level=1;`
  522. var condStr string
  523. if task.WithinDays <= 0 {
  524. condStr = fmt.Sprintf("up.parent_uid=%s and ups.is_verify=%d", zhios_condition_statistics_utils.AnyToString(uid), 1)
  525. } else {
  526. 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)
  527. }
  528. sql := fmt.Sprintf(sqlTpl, condStr)
  529. results, err := db.QueryNativeString(engine, sql)
  530. if err != nil {
  531. return "", hasPay, payOrdId
  532. }
  533. count := ""
  534. if len(results) > 0 {
  535. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  536. }
  537. return count, hasPay, payOrdId
  538. case 5:
  539. // 累计团队符合相应等级的人数
  540. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  541. etime := t.Format("2006-1-02 15:04:05")
  542. sqlTpl := `SELECT count(*) as count
  543. FROM user_relate as ur
  544. LEFT JOIN user as u ON u.uid=ur.uid
  545. LEFT JOIN user_level ul on u.level=ul.id
  546. WHERE %s;`
  547. levelList := allLvMap[task.TaskTypeLevelId]
  548. levelWeight := 0
  549. if levelList != nil {
  550. levelWeight = levelList.LevelWeight
  551. }
  552. var condStr string
  553. if task.WithinDays <= 0 {
  554. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight))
  555. if blockStarChain == "1" {
  556. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight))
  557. }
  558. } else {
  559. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime)
  560. if blockStarChain == "1" {
  561. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' AND u.level_arrive_at>'%s' AND u.level_arrive_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime)
  562. }
  563. }
  564. sql := fmt.Sprintf(sqlTpl, condStr)
  565. fmt.Println(sql)
  566. results, err := db.QueryNativeString(engine, sql)
  567. if err != nil {
  568. return "", hasPay, payOrdId
  569. }
  570. count := ""
  571. if len(results) > 0 {
  572. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  573. }
  574. return count, hasPay, payOrdId
  575. case 11:
  576. // 累计直推符合相应等级的人数
  577. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  578. etime := t.Format("2006-1-02 15:04:05")
  579. sqlTpl := `SELECT count(*) as count
  580. FROM user_relate as ur
  581. LEFT JOIN user as u ON u.uid=ur.uid
  582. LEFT JOIN user_level ul on u.level=ul.id
  583. WHERE %s;`
  584. levelList := allLvMap[task.TaskTypeLevelId]
  585. levelWeight := 0
  586. if levelList != nil {
  587. levelWeight = levelList.LevelWeight
  588. }
  589. var condStr string
  590. if task.WithinDays <= 0 {
  591. condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight))
  592. if blockStarChain == "1" {
  593. condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight))
  594. }
  595. } else {
  596. condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime)
  597. if blockStarChain == "1" {
  598. condStr = fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND ul.level_weight>='%s' AND u.level_arrive_at>'%s' AND u.level_arrive_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), "1", zhios_condition_statistics_utils.IntToStr(levelWeight), stime, etime)
  599. }
  600. }
  601. sql := fmt.Sprintf(sqlTpl, condStr)
  602. fmt.Println(sql)
  603. results, err := db.QueryNativeString(engine, sql)
  604. if err != nil {
  605. return "", hasPay, payOrdId
  606. }
  607. count := ""
  608. if len(results) > 0 {
  609. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  610. }
  611. return count, hasPay, payOrdId
  612. case 21:
  613. // 累计所有下级
  614. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  615. etime := t.Format("2006-1-02 15:04:05")
  616. sqlTpl := `SELECT count(*) as count
  617. FROM user_relate as ur
  618. LEFT JOIN user as u ON u.uid=ur.uid
  619. WHERE %s;`
  620. var condStr string
  621. if task.WithinDays <= 0 {
  622. condStr = fmt.Sprintf("ur.parent_uid=%s ", zhios_condition_statistics_utils.AnyToString(uid))
  623. } else {
  624. condStr = fmt.Sprintf("ur.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), stime, etime)
  625. }
  626. sql := fmt.Sprintf(sqlTpl, condStr)
  627. fmt.Println(sql)
  628. results, err := db.QueryNativeString(engine, sql)
  629. if err != nil {
  630. return "", hasPay, payOrdId
  631. }
  632. count := ""
  633. if len(results) > 0 {
  634. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  635. }
  636. return count, hasPay, payOrdId
  637. case 12: //累计团队消费金额
  638. var total float64 = 0
  639. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) {
  640. secondTotal, _ := db.MallOrderTeamRelateListByTimeByStateNew(engine, uid, task)
  641. total += secondTotal
  642. }
  643. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) {
  644. secondTotal, _ := db.O2oOrderTeamRelateListByTimeByStateNew(engine, uid, task)
  645. total += secondTotal
  646. }
  647. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) {
  648. secondTotal, _ := db.B2cOrderTeamRelateListByTimeByStateNew(engine, uid, task)
  649. total += secondTotal
  650. }
  651. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  652. secondTotal, _ := db.OrderTeamRelateListByTimeByStateNew(engine, uid, task)
  653. total += secondTotal
  654. }
  655. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId
  656. case 13:
  657. //小市场团队符合条件人数
  658. smallUid := TotalSmallTeam(engine, zhios_condition_statistics_utils.AnyToString(uid))
  659. sqlTpl := `SELECT count(*) as count
  660. FROM user_relate as ur
  661. LEFT JOIN user as u ON u.uid=ur.uid
  662. WHERE %s;`
  663. condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' ", smallUid, zhios_condition_statistics_utils.IntToStr(task.TaskTypeLevelId))
  664. sql := fmt.Sprintf(sqlTpl, condStr)
  665. results, err := db.QueryNativeString(engine, sql)
  666. if err != nil {
  667. return "", hasPay, payOrdId
  668. }
  669. count := ""
  670. if len(results) > 0 {
  671. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  672. }
  673. return count, hasPay, payOrdId
  674. case 15:
  675. if task.TaskTypeCount == 0 {
  676. return "0", hasPay, payOrdId
  677. }
  678. //stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-01-02 15:04:05")
  679. //etime := t.Format("2006-01-02 15:04:05")
  680. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"1"}) { //o2o
  681. sqlTpl := `SELECT os.id
  682. FROM user_relate ur
  683. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  684. LEFT JOIN o2o_store os on om.id=os.store_manager
  685. WHERE ur.parent_uid = ?
  686. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  687. `
  688. var ids = make([]string, 0)
  689. result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount)
  690. if err == nil {
  691. for _, v := range result {
  692. ids = append(ids, v["id"])
  693. }
  694. }
  695. if len(ids) > 0 {
  696. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"3", "4"}).Sum(&model.O2oOrd{}, "cost_price")
  697. sum1, _ := engine.In("belong_store_id", ids).Sum(&model.O2oPayToMerchant{}, "actual_pay_amount")
  698. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum+sum1, 4), hasPay, payOrdId
  699. }
  700. }
  701. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"2"}) { //b2c
  702. sqlTpl := `SELECT os.id
  703. FROM user_relate ur
  704. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  705. LEFT JOIN b2c_ord os on om.id=os.store_manager
  706. WHERE ur.parent_uid = ?
  707. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  708. `
  709. var ids = make([]string, 0)
  710. result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount)
  711. if err == nil {
  712. for _, v := range result {
  713. ids = append(ids, v["id"])
  714. }
  715. }
  716. if len(ids) > 0 {
  717. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"4"}).Sum(&model.B2cOrd{}, "cost_price")
  718. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum, 4), hasPay, payOrdId
  719. }
  720. }
  721. return "0", hasPay, payOrdId
  722. case 6:
  723. hasPay = true
  724. //TODO::根据 RegionalAgentSchemeTask 表中的 task_id 判断是否完成付费任务
  725. regionalAgentUserOrd, err := db.UserLevelOrderByTaskId(engine, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(task.Id))
  726. if err != nil {
  727. return "", hasPay, payOrdId
  728. }
  729. if regionalAgentUserOrd != nil {
  730. 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 {
  731. return "0", hasPay, payOrdId
  732. }
  733. payOrdId = regionalAgentUserOrd.Id
  734. return "1", hasPay, payOrdId
  735. } else {
  736. return "0", hasPay, payOrdId
  737. }
  738. return "1", hasPay, payOrdId
  739. case 17:
  740. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-01-02 15:04:05")
  741. etime := t.Format("2006-01-02 15:04:05")
  742. // 团队N条线中各有N个相应等级用户
  743. sqlTpl := `select ur.parent_uid,COUNT(*) as count FROM user_relate ur
  744. LEFT JOIN user u on u.uid=ur.uid
  745. LEFT JOIN user_level ul on u.level=ul.id
  746. where ur.parent_uid in(SELECT uid FROM user_relate WHERE parent_uid=%s and level=1 ) and ul.level_weight>=%s %s GROUP BY ur.parent_uid;`
  747. levelList := allLvMap[task.TaskTypeLevelId]
  748. levelWeight := 0
  749. if levelList != nil {
  750. levelWeight = levelList.LevelWeight
  751. }
  752. sql := ""
  753. if task.WithinDays <= 0 {
  754. sql = fmt.Sprintf(sqlTpl, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "")
  755. } else {
  756. sql = fmt.Sprintf(sqlTpl, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "and u.level_arrive_at>='"+stime+"' and u.level_arrive_at<='"+etime+"'")
  757. }
  758. results, err := db.QueryNativeString(engine, sql)
  759. if err != nil {
  760. return "", hasPay, payOrdId
  761. }
  762. sqlTpl1 := `select ur.uid FROM user_relate ur
  763. LEFT JOIN user u on u.uid=ur.uid
  764. LEFT JOIN user_level ul on u.level=ul.id
  765. where ur.parent_uid=%s and ur.level=1 and ul.level_weight>=%s %s ;`
  766. sql1 := ""
  767. if task.WithinDays <= 0 {
  768. sql1 = fmt.Sprintf(sqlTpl1, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "")
  769. } else {
  770. sql1 = fmt.Sprintf(sqlTpl1, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "and u.level_arrive_at>='"+stime+"' and u.level_arrive_at<='"+etime+"'")
  771. }
  772. results1, err1 := db.QueryNativeString(engine, sql1)
  773. if err1 != nil {
  774. return "", hasPay, payOrdId
  775. }
  776. var firstList = make(map[string]string)
  777. if len(results1) > 0 {
  778. for _, v := range results1 {
  779. firstList[v["uid"]] = "1"
  780. }
  781. }
  782. if len(results) > 0 {
  783. for _, v := range results {
  784. firstList[v["parent_uid"]] = zhios_condition_statistics_utils.IntToStr(zhios_condition_statistics_utils.StrToInt(v["count"]) + zhios_condition_statistics_utils.StrToInt(firstList[v["parent_uid"]]))
  785. }
  786. }
  787. count := 0
  788. if len(firstList) > 0 {
  789. for _, v := range firstList {
  790. if zhios_condition_statistics_utils.StrToInt(v) >= task.TaskTypeSecondCount && task.TaskTypeSecondCount > 0 {
  791. count++
  792. }
  793. }
  794. }
  795. return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId
  796. case 18:
  797. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Format("2006-01-02 15:04:05")
  798. etime := t.Format("2006-01-02 15:04:05")
  799. // 小区消费金额
  800. sqlTpl := `SELECT SUM(molr.goods_price) as price FROM mall_ord_list_relate molr
  801. LEFT JOIN mall_ord mo on mo.ord_id=molr.oid and mo.state in (%s)
  802. WHERE molr.uid in(SELECT uid FROM user_relate WHERE parent_uid=%s and level=1) %s GROUP BY molr.uid ;`
  803. state := "1,2,3"
  804. if task.TaskTypePvd == 1 {
  805. state = "2,3"
  806. }
  807. if task.TaskTypePvd == 2 {
  808. state = "3"
  809. }
  810. sql := ""
  811. if task.WithinDays <= 0 {
  812. sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "")
  813. } else {
  814. sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "and mo.create_time>='"+stime+"' and mo.create_time<='"+etime+"'")
  815. }
  816. results, err := db.QueryNativeString(engine, sql)
  817. if err != nil {
  818. return "", hasPay, payOrdId
  819. }
  820. var count float64 = 0
  821. if len(results) > 0 {
  822. var max float64 = 0
  823. for _, v := range results {
  824. if zhios_condition_statistics_utils.StrToFloat64(v["price"]) > max {
  825. max = zhios_condition_statistics_utils.StrToFloat64(v["price"])
  826. }
  827. count += zhios_condition_statistics_utils.StrToFloat64(v["price"])
  828. }
  829. count -= max
  830. }
  831. return zhios_condition_statistics_utils.Float64ToStrByPrec(count, 2), hasPay, payOrdId
  832. case 19:
  833. teamCountMinData, _ := db.SysCfgGetOne(engine, "team_count_min")
  834. teamCountMin := 15
  835. if teamCountMinData != nil {
  836. teamCountMin = zhios_condition_statistics_utils.StrToInt(teamCountMinData.Val)
  837. }
  838. teamtotal := db.GetUserTeamTotal(engine, zhios_condition_statistics_utils.AnyToString(uid))
  839. var count = 0
  840. if teamtotal != nil && teamtotal.FirstCount >= teamCountMin && teamtotal.SecondCount >= teamCountMin {
  841. count++
  842. }
  843. sql := `SELECT * FROM user_profile up
  844. LEFT JOIN user_team_total utt ON utt.uid=up.uid
  845. WHERE up.parent_uid=? and utt.first_count>=? and utt.second_count>=?
  846. `
  847. nativeString, _ := db.QueryNativeString(engine, sql, zhios_condition_statistics_utils.AnyToString(uid), teamCountMin, teamCountMin)
  848. if len(nativeString) > 0 {
  849. count += len(nativeString)
  850. }
  851. return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId
  852. default:
  853. return "", hasPay, payOrdId
  854. }
  855. }