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

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