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

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