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

870 lines
34 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. sum, _ := engine.Where("type=0 and uid=?", uid).Sum(&model.FinUserFlow{}, "amount")
  350. mallSum, _ := engine.Where("uid=? AND type=? and ord_title=?", uid, 1, "订单退款").Sum(&model.FinUserFlow{}, "amount")
  351. deSum2, _ := engine.Where("uid=? AND type=? and ord_type=?", uid, 0, "withdraw").Sum(&model.FinUserFlow{}, "amount")
  352. deSum3, _ := engine.Where("uid=? AND type=? and ord_title like ?", uid, 1, "%扣除%").Sum(&model.FinUserFlow{}, "amount")
  353. dsCheck, _ := db.SysCfgGetOne(engine, "ds_check")
  354. money := sum - deSum2 - deSum3 + mallSum
  355. if dsCheck != nil && dsCheck.Val == "1" {
  356. withdrawingSuccess, _ := engine.Where("uid=? ", uid).In("state", []string{"1", "2"}).Sum(&model.FinWithdrawApply{}, "amount")
  357. 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;`
  358. nativeString, _ := db.QueryNativeString(engine, sql)
  359. money = withdrawingSuccess
  360. for _, v := range nativeString {
  361. money += zhios_condition_statistics_utils.StrToFloat64(v["amount"])
  362. }
  363. }
  364. return zhios_condition_statistics_utils.Float64ToStr(money), hasPay, payOrdId
  365. case 22:
  366. lvTime := db.GetOneCirclesUserLvTime(engine, uid)
  367. stime := time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()).Format("2006-01-02")
  368. if lvTime != nil && lvTime.Date.IsZero() == false {
  369. stime = lvTime.Date.Format("2006-01-02")
  370. }
  371. etime := t.Format("2006-01-02")
  372. if params["is_view"] == "1" {
  373. if lvTime == nil || (lvTime != nil && lvTime.Date.IsZero()) {
  374. stime = t.Format("2006-01-02")
  375. }
  376. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()).Format("2006-01-02")
  377. }
  378. _, days, activity := one_circles.CalcUserContinuousDailyActivityDays(engine, dbName, int(zhios_condition_statistics_utils.AnyToInt64(uid)), stime, etime)
  379. if activity == false {
  380. days = 0
  381. }
  382. return zhios_condition_statistics_utils.IntToStr(days), hasPay, payOrdId
  383. case 1:
  384. // 累计自购数
  385. total, err := db.OrderListCountByUIDByOrderTypeByTimeNew(engine, uid, task)
  386. if err != nil {
  387. return "", hasPay, payOrdId
  388. }
  389. mallTotal, err := db.MallOrdWithCountNew(engine, uid, task)
  390. if err != nil {
  391. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  392. }
  393. total += mallTotal
  394. o2oTotal, err := db.O2oOrdWithCountNew(engine, uid, task)
  395. if err != nil {
  396. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  397. }
  398. total += o2oTotal
  399. b2cTotal, err := db.B2cOrdWithCountNew(engine, uid, task)
  400. if err != nil {
  401. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  402. }
  403. total += b2cTotal
  404. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  405. case 10:
  406. // 累计自购消费金额
  407. var total float64 = 0
  408. var err error
  409. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  410. total, err = db.OrderListSumByUIDByOrderTypeByTimeNew(engine, uid, task)
  411. if err != nil {
  412. return "", hasPay, payOrdId
  413. }
  414. }
  415. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) {
  416. orderType := 0
  417. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) {
  418. orderType = task.TaskTypePvd
  419. }
  420. secondTotal, err := db.MallOrdWithSumNew(engine, uid, orderType, task)
  421. if err != nil {
  422. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  423. }
  424. total += secondTotal
  425. }
  426. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) {
  427. secondTotal, err := db.O2oOrdWithSumNew(engine, uid, task)
  428. if err != nil {
  429. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  430. }
  431. total += secondTotal
  432. }
  433. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) {
  434. secondTotal, err := db.B2cOrdWithSumNew(engine, uid, task)
  435. if err != nil {
  436. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  437. }
  438. total += secondTotal
  439. }
  440. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId
  441. case 2:
  442. // 累计自购佣金
  443. sum, err := db.OrderRelateListByTimeByStateNew(engine, uid, task)
  444. if err != nil {
  445. return "", hasPay, payOrdId
  446. }
  447. mallSum, err := db.MallOrderRelateListByTimeByStateNew(engine, uid, task)
  448. if err != nil {
  449. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  450. }
  451. sum += mallSum
  452. o2oSum, err := db.O2oOrderRelateListByTimeByStateNew(engine, uid, task)
  453. if err != nil {
  454. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  455. }
  456. sum += o2oSum
  457. b2cSum, err := db.B2cOrderRelateListByTimeByStateNew(engine, uid, task)
  458. if err != nil {
  459. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  460. }
  461. sum += b2cSum
  462. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  463. case 3:
  464. // 累计直推人数
  465. 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")
  466. etime := t.Format("2006-1-02 15:04:05")
  467. sqlTpl := `SELECT count(*) as count
  468. FROM user_relate as up
  469. LEFT JOIN user as u ON u.uid=up.uid
  470. WHERE %s and up.level=1;`
  471. var condStr string
  472. if task.WithinDays <= 0 {
  473. condStr = fmt.Sprintf("up.parent_uid=%s ", zhios_condition_statistics_utils.AnyToString(uid))
  474. } else {
  475. 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)
  476. }
  477. sql := fmt.Sprintf(sqlTpl, condStr)
  478. results, err := db.QueryNativeString(engine, sql)
  479. if err != nil {
  480. return "", hasPay, payOrdId
  481. }
  482. count := ""
  483. if len(results) > 0 {
  484. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  485. }
  486. return count, hasPay, payOrdId
  487. case 4:
  488. // 累计团队有效直推人数
  489. 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")
  490. etime := t.Format("2006-1-02 15:04:05")
  491. sqlTpl := `SELECT count(*) as count
  492. FROM user_relate as up
  493. LEFT JOIN user as u ON u.uid=up.uid
  494. LEFT JOIN user_profile as ups ON up.uid=ups.uid
  495. WHERE %s and up.level=1;`
  496. var condStr string
  497. if task.WithinDays <= 0 {
  498. condStr = fmt.Sprintf("up.parent_uid=%s and ups.is_verify=%d", zhios_condition_statistics_utils.AnyToString(uid), 1)
  499. } else {
  500. 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)
  501. }
  502. sql := fmt.Sprintf(sqlTpl, condStr)
  503. results, err := db.QueryNativeString(engine, sql)
  504. if err != nil {
  505. return "", hasPay, payOrdId
  506. }
  507. count := ""
  508. if len(results) > 0 {
  509. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  510. }
  511. return count, hasPay, payOrdId
  512. case 5:
  513. // 累计团队符合相应等级的人数
  514. 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")
  515. etime := t.Format("2006-1-02 15:04:05")
  516. sqlTpl := `SELECT count(*) as count
  517. FROM user_relate as ur
  518. LEFT JOIN user as u ON u.uid=ur.uid
  519. LEFT JOIN user_level ul on u.level=ul.id
  520. WHERE %s;`
  521. levelList := allLvMap[task.TaskTypeLevelId]
  522. levelWeight := 0
  523. if levelList != nil {
  524. levelWeight = levelList.LevelWeight
  525. }
  526. var condStr string
  527. if task.WithinDays <= 0 {
  528. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight))
  529. if blockStarChain == "1" {
  530. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight))
  531. }
  532. } else {
  533. 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)
  534. if blockStarChain == "1" {
  535. 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)
  536. }
  537. }
  538. sql := fmt.Sprintf(sqlTpl, condStr)
  539. fmt.Println(sql)
  540. results, err := db.QueryNativeString(engine, sql)
  541. if err != nil {
  542. return "", hasPay, payOrdId
  543. }
  544. count := ""
  545. if len(results) > 0 {
  546. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  547. }
  548. return count, hasPay, payOrdId
  549. case 11:
  550. // 累计直推符合相应等级的人数
  551. 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")
  552. etime := t.Format("2006-1-02 15:04:05")
  553. sqlTpl := `SELECT count(*) as count
  554. FROM user_relate as ur
  555. LEFT JOIN user as u ON u.uid=ur.uid
  556. LEFT JOIN user_level ul on u.level=ul.id
  557. WHERE %s;`
  558. levelList := allLvMap[task.TaskTypeLevelId]
  559. levelWeight := 0
  560. if levelList != nil {
  561. levelWeight = levelList.LevelWeight
  562. }
  563. var condStr string
  564. if task.WithinDays <= 0 {
  565. 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))
  566. if blockStarChain == "1" {
  567. 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))
  568. }
  569. } else {
  570. 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)
  571. if blockStarChain == "1" {
  572. 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)
  573. }
  574. }
  575. sql := fmt.Sprintf(sqlTpl, condStr)
  576. fmt.Println(sql)
  577. results, err := db.QueryNativeString(engine, sql)
  578. if err != nil {
  579. return "", hasPay, payOrdId
  580. }
  581. count := ""
  582. if len(results) > 0 {
  583. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  584. }
  585. return count, hasPay, payOrdId
  586. case 21:
  587. // 累计所有下级
  588. 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")
  589. etime := t.Format("2006-1-02 15:04:05")
  590. sqlTpl := `SELECT count(*) as count
  591. FROM user_relate as ur
  592. LEFT JOIN user as u ON u.uid=ur.uid
  593. WHERE %s;`
  594. var condStr string
  595. if task.WithinDays <= 0 {
  596. condStr = fmt.Sprintf("ur.parent_uid=%s ", zhios_condition_statistics_utils.AnyToString(uid))
  597. } else {
  598. 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)
  599. }
  600. sql := fmt.Sprintf(sqlTpl, condStr)
  601. fmt.Println(sql)
  602. results, err := db.QueryNativeString(engine, sql)
  603. if err != nil {
  604. return "", hasPay, payOrdId
  605. }
  606. count := ""
  607. if len(results) > 0 {
  608. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  609. }
  610. return count, hasPay, payOrdId
  611. case 12: //累计团队消费金额
  612. var total float64 = 0
  613. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) {
  614. secondTotal, _ := db.MallOrderTeamRelateListByTimeByStateNew(engine, uid, task)
  615. total += secondTotal
  616. }
  617. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) {
  618. secondTotal, _ := db.O2oOrderTeamRelateListByTimeByStateNew(engine, uid, task)
  619. total += secondTotal
  620. }
  621. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) {
  622. secondTotal, _ := db.B2cOrderTeamRelateListByTimeByStateNew(engine, uid, task)
  623. total += secondTotal
  624. }
  625. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  626. secondTotal, _ := db.OrderTeamRelateListByTimeByStateNew(engine, uid, task)
  627. total += secondTotal
  628. }
  629. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId
  630. case 13:
  631. //小市场团队符合条件人数
  632. smallUid := TotalSmallTeam(engine, zhios_condition_statistics_utils.AnyToString(uid))
  633. sqlTpl := `SELECT count(*) as count
  634. FROM user_relate as ur
  635. LEFT JOIN user as u ON u.uid=ur.uid
  636. WHERE %s;`
  637. condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' ", smallUid, zhios_condition_statistics_utils.IntToStr(task.TaskTypeLevelId))
  638. sql := fmt.Sprintf(sqlTpl, condStr)
  639. results, err := db.QueryNativeString(engine, sql)
  640. if err != nil {
  641. return "", hasPay, payOrdId
  642. }
  643. count := ""
  644. if len(results) > 0 {
  645. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  646. }
  647. return count, hasPay, payOrdId
  648. case 15:
  649. if task.TaskTypeCount == 0 {
  650. return "0", hasPay, payOrdId
  651. }
  652. //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")
  653. //etime := t.Format("2006-01-02 15:04:05")
  654. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"1"}) { //o2o
  655. sqlTpl := `SELECT os.id
  656. FROM user_relate ur
  657. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  658. LEFT JOIN o2o_store os on om.id=os.store_manager
  659. WHERE ur.parent_uid = ?
  660. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  661. `
  662. var ids = make([]string, 0)
  663. result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount)
  664. if err == nil {
  665. for _, v := range result {
  666. ids = append(ids, v["id"])
  667. }
  668. }
  669. if len(ids) > 0 {
  670. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"3", "4"}).Sum(&model.O2oOrd{}, "cost_price")
  671. sum1, _ := engine.In("belong_store_id", ids).Sum(&model.O2oPayToMerchant{}, "actual_pay_amount")
  672. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum+sum1, 4), hasPay, payOrdId
  673. }
  674. }
  675. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"2"}) { //b2c
  676. sqlTpl := `SELECT os.id
  677. FROM user_relate ur
  678. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  679. LEFT JOIN b2c_ord os on om.id=os.store_manager
  680. WHERE ur.parent_uid = ?
  681. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  682. `
  683. var ids = make([]string, 0)
  684. result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount)
  685. if err == nil {
  686. for _, v := range result {
  687. ids = append(ids, v["id"])
  688. }
  689. }
  690. if len(ids) > 0 {
  691. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"4"}).Sum(&model.B2cOrd{}, "cost_price")
  692. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum, 4), hasPay, payOrdId
  693. }
  694. }
  695. return "0", hasPay, payOrdId
  696. case 6:
  697. hasPay = true
  698. //TODO::根据 RegionalAgentSchemeTask 表中的 task_id 判断是否完成付费任务
  699. regionalAgentUserOrd, err := db.UserLevelOrderByTaskId(engine, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(task.Id))
  700. if err != nil {
  701. return "", hasPay, payOrdId
  702. }
  703. if regionalAgentUserOrd != nil {
  704. 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 {
  705. return "0", hasPay, payOrdId
  706. }
  707. payOrdId = regionalAgentUserOrd.Id
  708. return "1", hasPay, payOrdId
  709. } else {
  710. return "0", hasPay, payOrdId
  711. }
  712. return "1", hasPay, payOrdId
  713. case 17:
  714. 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")
  715. etime := t.Format("2006-01-02 15:04:05")
  716. // 团队N条线中各有N个相应等级用户
  717. sqlTpl := `select ur.parent_uid,COUNT(*) as count FROM user_relate ur
  718. LEFT JOIN user u on u.uid=ur.uid
  719. LEFT JOIN user_level ul on u.level=ul.id
  720. 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;`
  721. levelList := allLvMap[task.TaskTypeLevelId]
  722. levelWeight := 0
  723. if levelList != nil {
  724. levelWeight = levelList.LevelWeight
  725. }
  726. sql := ""
  727. if task.WithinDays <= 0 {
  728. sql = fmt.Sprintf(sqlTpl, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "")
  729. } else {
  730. 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+"'")
  731. }
  732. results, err := db.QueryNativeString(engine, sql)
  733. if err != nil {
  734. return "", hasPay, payOrdId
  735. }
  736. sqlTpl1 := `select ur.uid FROM user_relate ur
  737. LEFT JOIN user u on u.uid=ur.uid
  738. LEFT JOIN user_level ul on u.level=ul.id
  739. where ur.parent_uid=%s and ur.level=1 and ul.level_weight>=%s %s ;`
  740. sql1 := ""
  741. if task.WithinDays <= 0 {
  742. sql1 = fmt.Sprintf(sqlTpl1, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "")
  743. } else {
  744. 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+"'")
  745. }
  746. results1, err1 := db.QueryNativeString(engine, sql1)
  747. if err1 != nil {
  748. return "", hasPay, payOrdId
  749. }
  750. var firstList = make(map[string]string)
  751. if len(results1) > 0 {
  752. for _, v := range results1 {
  753. firstList[v["uid"]] = "1"
  754. }
  755. }
  756. if len(results) > 0 {
  757. for _, v := range results {
  758. 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"]]))
  759. }
  760. }
  761. count := 0
  762. if len(firstList) > 0 {
  763. for _, v := range firstList {
  764. if zhios_condition_statistics_utils.StrToInt(v) >= task.TaskTypeSecondCount && task.TaskTypeSecondCount > 0 {
  765. count++
  766. }
  767. }
  768. }
  769. return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId
  770. case 18:
  771. 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")
  772. etime := t.Format("2006-01-02 15:04:05")
  773. // 小区消费金额
  774. sqlTpl := `SELECT SUM(molr.goods_price) as price FROM mall_ord_list_relate molr
  775. LEFT JOIN mall_ord mo on mo.ord_id=molr.oid and mo.state in (%s)
  776. WHERE molr.uid in(SELECT uid FROM user_relate WHERE parent_uid=%s and level=1) %s GROUP BY molr.uid ;`
  777. state := "1,2,3"
  778. if task.TaskTypePvd == 1 {
  779. state = "2,3"
  780. }
  781. if task.TaskTypePvd == 2 {
  782. state = "3"
  783. }
  784. sql := ""
  785. if task.WithinDays <= 0 {
  786. sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "")
  787. } else {
  788. sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "and mo.create_time>='"+stime+"' and mo.create_time<='"+etime+"'")
  789. }
  790. results, err := db.QueryNativeString(engine, sql)
  791. if err != nil {
  792. return "", hasPay, payOrdId
  793. }
  794. var count float64 = 0
  795. if len(results) > 0 {
  796. var max float64 = 0
  797. for _, v := range results {
  798. if zhios_condition_statistics_utils.StrToFloat64(v["price"]) > max {
  799. max = zhios_condition_statistics_utils.StrToFloat64(v["price"])
  800. }
  801. count += zhios_condition_statistics_utils.StrToFloat64(v["price"])
  802. }
  803. count -= max
  804. }
  805. return zhios_condition_statistics_utils.Float64ToStrByPrec(count, 2), hasPay, payOrdId
  806. case 19:
  807. teamCountMinData, _ := db.SysCfgGetOne(engine, "team_count_min")
  808. teamCountMin := 15
  809. if teamCountMinData != nil {
  810. teamCountMin = zhios_condition_statistics_utils.StrToInt(teamCountMinData.Val)
  811. }
  812. teamtotal := db.GetUserTeamTotal(engine, zhios_condition_statistics_utils.AnyToString(uid))
  813. var count = 0
  814. if teamtotal != nil && teamtotal.FirstCount >= teamCountMin && teamtotal.SecondCount >= teamCountMin {
  815. count++
  816. }
  817. sql := `SELECT * FROM user_profile up
  818. LEFT JOIN user_team_total utt ON utt.uid=up.uid
  819. WHERE up.parent_uid=? and utt.first_count>=? and utt.second_count>=?
  820. `
  821. nativeString, _ := db.QueryNativeString(engine, sql, zhios_condition_statistics_utils.AnyToString(uid), teamCountMin, teamCountMin)
  822. if len(nativeString) > 0 {
  823. count += len(nativeString)
  824. }
  825. return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId
  826. default:
  827. return "", hasPay, payOrdId
  828. }
  829. }