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

967 lines
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. "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. // 永久(设置为与创建时间相同)
  111. ord.ExpireAt = t
  112. }
  113. if _, err := db.UserLevelOrdUpdateOne(eg, ord); err != nil {
  114. return false, 0
  115. }
  116. }
  117. user.Level = zhios_condition_statistics_utils.StrToInt(DataDetail.AgentType)
  118. user.LevelArriveAt = t
  119. if user.FirstArriveAt.IsZero() {
  120. user.FirstArriveAt = t
  121. }
  122. if payOrdId > 0 {
  123. user.LevelExpireAt = expire
  124. } else {
  125. if level.LevelDate == 0 { // 永久
  126. user.LevelExpireAt = t
  127. } else {
  128. user.LevelExpireAt = t.AddDate(0, level.LevelDate, 0)
  129. }
  130. }
  131. //用户等级修改
  132. // 更新user level 和 过期时间
  133. if _, err := db.UserUpdate(eg, user.Uid, user, "level,level_expire_at,level_arrive_at,first_arrive_at"); err != nil {
  134. return false, 0
  135. }
  136. AutoAudit = 1
  137. }
  138. }
  139. return result, AutoAudit
  140. }
  141. // IsCompleteSchemeTask 根据传入的 level 判断是否可以升级
  142. func IsCompleteSchemeTask(blockStarChain string, engine *xorm.Engine, user *model.User, level, IsChangeTime string) (bool, bool, int64) {
  143. uid := zhios_condition_statistics_utils.IntToStr(user.Uid)
  144. userLevel, err := db.UserLevelByID(engine, user.Level)
  145. if err != nil {
  146. return false, false, 0
  147. }
  148. becomeLevel, err := db.UserLevelByID(engine, level)
  149. if err != nil {
  150. return false, false, 0
  151. }
  152. if userLevel == nil {
  153. if level == "" {
  154. level = "1"
  155. }
  156. } else {
  157. if becomeLevel.LevelWeight <= userLevel.LevelWeight && IsChangeTime != "1" {
  158. return false, false, 0
  159. }
  160. }
  161. //获取用户等级方案内容
  162. schemeTasks, err := db.BatchSelectUserLevelUpgradeTasks(engine, map[string]interface{}{
  163. "key": "scheme_id",
  164. "value": becomeLevel.Id,
  165. })
  166. if err != nil {
  167. return false, false, 0
  168. }
  169. return CheckSchemeTasksComplete(blockStarChain, engine, schemeTasks, uid, becomeLevel, level, "")
  170. }
  171. func CheckSchemeTasksComplete(blockStarChain string, engine *xorm.Engine, tasks *[]model.UserLevelUpgradeTask, uid interface{}, scheme *model.UserLevel, level string, regionalId string) (bool, bool, int64) {
  172. //分离出 必做/选做 途径(任务组) && 分离出 必做/选做
  173. var tasksGroup []map[string]interface{}
  174. mustTasks := make(map[string][]interface{})
  175. selectTasks := make(map[string][]interface{})
  176. var (
  177. hasLevelPay = false
  178. LevelPayOrdId int64
  179. )
  180. allLvMap := db.UserLevelByAllMap(engine)
  181. for _, item := range *tasks {
  182. var temp = map[string]interface{}{}
  183. temp["id"] = item.Id
  184. temp["pid"] = item.Pid
  185. temp["schemeId"] = item.SchemeId
  186. temp["isMustTask"] = item.IsMustTask
  187. temp["taskType"] = item.TaskType
  188. temp["taskTypeLevelId"] = item.TaskTypeLevelId
  189. temp["withinDays"] = item.WithinDays
  190. temp["finishCount"] = item.FinishCount
  191. temp["payLevels"] = item.PayLevels
  192. temp["finish_task_num"] = item.FinishTaskNum
  193. if item.Pid == 0 && item.IsMustTask == 1 {
  194. temp["is_must"] = "1"
  195. tasksGroup = append(tasksGroup, temp)
  196. }
  197. if item.Pid == 0 && item.IsMustTask != 1 {
  198. temp["is_must"] = "0"
  199. tasksGroup = append(tasksGroup, temp)
  200. }
  201. name := GetTaskName(item.TaskType)
  202. finish, hasPay, payOrdId := GetFinishCount(blockStarChain, engine, uid, &item, level, regionalId, allLvMap)
  203. total := "" // 总共需完成的数
  204. if item.TaskType == 6 {
  205. // 当是付费条件是否,total的指标只能是1
  206. total = "1"
  207. hasLevelPay = hasPay
  208. LevelPayOrdId = payOrdId
  209. } else {
  210. total = item.FinishCount
  211. }
  212. if item.TaskType == 19 {
  213. total = zhios_condition_statistics_utils.IntToStr(zhios_condition_statistics_utils.StrToInt(total) + 1)
  214. }
  215. temp["name"] = name
  216. temp["total"] = total //需完成数
  217. temp["finish"] = finish //已完成数
  218. //fmt.Println("finish:::::::::::::", finish)
  219. //fmt.Println("total:::::::::::::", total)
  220. if zhios_condition_statistics_utils.AnyToFloat64(finish) >= zhios_condition_statistics_utils.AnyToFloat64(total) {
  221. temp["result"] = "1"
  222. } else {
  223. temp["result"] = "0"
  224. }
  225. if item.Pid != 0 && item.IsMustTask == 1 {
  226. mustTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)] = append(mustTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)], temp)
  227. }
  228. if item.Pid != 0 && item.IsMustTask == 0 {
  229. selectTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)] = append(selectTasks[zhios_condition_statistics_utils.AnyToString(item.Pid)], temp)
  230. }
  231. }
  232. alreadyCompleteTasksGroup := 0 // 完成“选做途径”数量
  233. for _, v := range tasksGroup {
  234. fmt.Println(">>>>>>>>>>>>>>>>>>>>", v)
  235. if v["finish_task_num"] == nil {
  236. v["finishCount"] = 0
  237. }
  238. //处理必做途径
  239. if zhios_condition_statistics_utils.AnyToString(v["isMustTask"]) == "1" {
  240. if mustTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] != nil {
  241. needFinishTaskNum := v["finish_task_num"] // 需完成选做任务数
  242. alreadyFinishTaskNum := 0 // 已完成选做任务数
  243. for _, vv := range mustTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] {
  244. tmp := vv.(map[string]interface{})
  245. // 必做任务 且 未完成
  246. if tmp["isMustTask"] == 1 && tmp["result"] != "1" {
  247. return false, hasLevelPay, LevelPayOrdId
  248. }
  249. // 非必做任务 且 已完成
  250. if tmp["isMustTask"] != 1 && tmp["result"] == "1" {
  251. alreadyFinishTaskNum++
  252. }
  253. }
  254. // 需完成选做任务数 大于 已完成选做任务数
  255. if zhios_condition_statistics_utils.AnyToInt64(needFinishTaskNum) > zhios_condition_statistics_utils.AnyToInt64(alreadyFinishTaskNum) {
  256. return false, hasLevelPay, LevelPayOrdId
  257. }
  258. }
  259. }
  260. //处理选做途径
  261. if selectTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] != nil {
  262. needFinishTaskNum := v["finish_task_num"] // 需完成选做任务数
  263. alreadyFinishTaskNum := 0 // 已完成选做任务数
  264. alreadyFinishMustTask := true // 是否已完成必做任务
  265. for _, vv := range selectTasks[zhios_condition_statistics_utils.AnyToString(v["id"])] {
  266. tmp := vv.(map[string]interface{})
  267. // 非必做任务 且 已完成
  268. if tmp["isMustTask"] != "1" && tmp["result"] == "1" {
  269. alreadyFinishTaskNum++
  270. }
  271. // 必做任务 且 未完成
  272. if tmp["isMustTask"] == "1" && tmp["result"] != "1" {
  273. alreadyFinishMustTask = false
  274. }
  275. }
  276. // 已完成必做任务 且 需完成选做任务数 小于等于 已完成选做任务数
  277. if alreadyFinishMustTask && zhios_condition_statistics_utils.AnyToInt64(needFinishTaskNum) <= zhios_condition_statistics_utils.AnyToInt64(alreadyFinishTaskNum) {
  278. alreadyCompleteTasksGroup++
  279. }
  280. }
  281. }
  282. //方案中设定的 需完成“选做途径”数 与 实际完成“选做途径”数 比较
  283. if scheme.ChoosableNum > alreadyCompleteTasksGroup {
  284. return false, hasLevelPay, LevelPayOrdId
  285. }
  286. return true, hasLevelPay, LevelPayOrdId
  287. }
  288. // GetTaskName is 获取任务名
  289. func GetTaskName(id int) string {
  290. switch id {
  291. case 1:
  292. return "累计自购订单总数"
  293. case 2:
  294. return "累计到账佣金"
  295. case 3:
  296. return "累计直推粉丝数量"
  297. case 4:
  298. return "累计团队有效直推人数"
  299. case 5:
  300. return "累计团队符合相应等级的人数"
  301. case 11:
  302. return "累计直推符合相应等级的人数"
  303. case 6:
  304. return "月付价格"
  305. case 7:
  306. return "季付价格"
  307. case 8:
  308. return "年付价格"
  309. case 9:
  310. return "永久价格"
  311. case 10:
  312. return "累计自购消费金额"
  313. case 12:
  314. return "累计团队消费金额"
  315. case 13:
  316. return "累计小市场团队符合等级人数"
  317. case 15:
  318. return "直推前N个店铺营业额"
  319. case 17:
  320. return "团队N条线中各有N个相应等级用户"
  321. case 18:
  322. return "小区累计自营消费金额"
  323. default:
  324. return "无效名称"
  325. }
  326. }
  327. //
  328. // GetFinishCount is 计算规定时间内完成的指标的数量
  329. func GetFinishCount(blockStarChain string, engine *xorm.Engine, uid interface{}, task *model.UserLevelUpgradeTask, level string, regionalId string, allLvMap map[int]*model.UserLevel) (string, bool, int64) {
  330. t := time.Now()
  331. hasPay := false
  332. var payOrdId int64
  333. switch task.TaskType {
  334. case 1:
  335. // 累计自购数
  336. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Unix()
  337. etime := t.Unix()
  338. mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location())
  339. mallEtime := t
  340. var total int64
  341. var err error
  342. //联盟结算后的
  343. if task.WithinDays <= 0 {
  344. total, err = db.OrderListCountByUIDByOrderType(engine, uid, 0, []string{"3", "5"})
  345. if err != nil {
  346. return "", hasPay, payOrdId
  347. }
  348. //确认收货的才算
  349. mallTotal, err := db.MallOrdWithCount(engine, uid, 0, []string{"3"})
  350. if err != nil {
  351. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  352. }
  353. total += mallTotal
  354. } else {
  355. total, err = db.OrderListCountByUIDByOrderTypeByTime(engine, uid, 0, []string{"3", "5"}, stime, etime)
  356. if err != nil {
  357. return "", hasPay, payOrdId
  358. }
  359. //确认收货的才算
  360. mallTotal, err := db.MallOrdByTimeWithCount(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  361. if err != nil {
  362. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  363. }
  364. total += mallTotal
  365. }
  366. return zhios_condition_statistics_utils.Int64ToStr(total), hasPay, payOrdId
  367. case 10:
  368. // 累计自购消费金额
  369. var total float64 = 0
  370. var err error
  371. if task.WithinDays <= 0 {
  372. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  373. state := []string{"3", "5"}
  374. if task.OrdState == 1 { //已付款
  375. state = []string{"0", "1", "2", "3", "5"}
  376. }
  377. if task.OrdState == 2 { //已完成
  378. state = []string{"1", "2", "3", "5"}
  379. }
  380. total, err = db.OrderListPaidPriceByUIDByOrderType(engine, uid, 0, state)
  381. if err != nil {
  382. return "", hasPay, payOrdId
  383. }
  384. }
  385. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) {
  386. orderType := 0
  387. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) {
  388. orderType = task.TaskTypePvd
  389. }
  390. state := []string{"3"}
  391. if task.OrdState == 1 { //已付款
  392. state = []string{"1", "2", "3"}
  393. }
  394. if task.OrdState == 2 { //已完成
  395. state = []string{"3"}
  396. }
  397. //确认收货的才算
  398. mallTotal, err := db.MallOrd(engine, uid, orderType, state)
  399. if err != nil {
  400. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  401. }
  402. total += mallTotal
  403. }
  404. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) {
  405. state := []string{"3"}
  406. if task.OrdState == 1 { //已付款
  407. state = []string{"1", "2", "3"}
  408. }
  409. if task.OrdState == 2 { //已完成
  410. state = []string{"3"}
  411. }
  412. //确认收货的才算
  413. o2oTotal, err := db.O2oOrd(engine, uid, 0, state)
  414. if err != nil {
  415. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  416. }
  417. total += o2oTotal
  418. o2oTotal1, err := db.O2oOrdPayToMerchant(engine, uid, 0, "1,4")
  419. if err != nil {
  420. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  421. }
  422. total += o2oTotal1
  423. }
  424. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) {
  425. state := []string{"3"}
  426. if task.OrdState == 1 { //已付款
  427. state = []string{"1", "2", "3"}
  428. }
  429. if task.OrdState == 2 { //已完成
  430. state = []string{"3"}
  431. }
  432. //确认收货的才算
  433. b2cTotal, err := db.B2cOrd(engine, uid, 0, state)
  434. if err != nil {
  435. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  436. }
  437. total += b2cTotal
  438. }
  439. } else {
  440. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  441. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Unix()
  442. etime := t.Unix()
  443. state := []string{"3", "5"}
  444. if task.OrdState == 1 { //已付款
  445. state = []string{"0", "1", "2", "3", "5"}
  446. }
  447. if task.OrdState == 2 { //已完成
  448. state = []string{"1", "2", "3", "5"}
  449. }
  450. total, err = db.OrderListPaidPriceByUIDByOrderTypeByTime(engine, uid, 0, state, stime, etime)
  451. if err != nil {
  452. return "", hasPay, payOrdId
  453. }
  454. }
  455. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) {
  456. mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location())
  457. mallEtime := t
  458. orderType := 0
  459. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) {
  460. orderType = task.TaskTypePvd
  461. }
  462. state := []string{"3"}
  463. if task.OrdState == 1 { //已付款
  464. state = []string{"1", "2", "3"}
  465. }
  466. if task.OrdState == 2 { //已完成
  467. state = []string{"3"}
  468. }
  469. //确认收货的才算
  470. mallTotal, err := db.MallOrdByTime(engine, uid, orderType, state, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"), zhios_condition_statistics_utils.IntToStr(task.OrdState))
  471. if err != nil {
  472. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  473. }
  474. total += mallTotal
  475. }
  476. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) {
  477. mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location())
  478. mallEtime := t
  479. state := []string{"3"}
  480. if task.OrdState == 1 { //已付款
  481. state = []string{"1", "2", "3"}
  482. }
  483. if task.OrdState == 2 { //已完成
  484. state = []string{"3"}
  485. }
  486. //确认收货的才算
  487. o2oTotal, err := db.O2oOrdByTime(engine, uid, 0, state, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  488. if err != nil {
  489. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  490. }
  491. total += o2oTotal
  492. o2oTotal1, err := db.O2oOrdPayToMerchantByTime(engine, uid, 0, "1,4", mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  493. if err != nil {
  494. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  495. }
  496. total += o2oTotal1
  497. }
  498. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) {
  499. mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location())
  500. mallEtime := t
  501. state := []string{"3"}
  502. if task.OrdState == 1 { //已付款
  503. state = []string{"1", "2", "3"}
  504. }
  505. if task.OrdState == 2 { //已完成
  506. state = []string{"3"}
  507. }
  508. //确认收货的才算
  509. b2cTotal, err := db.B2cOrdByTime(engine, uid, 0, state, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  510. if err != nil {
  511. return zhios_condition_statistics_utils.Float64ToStr(total), hasPay, payOrdId
  512. }
  513. total += b2cTotal
  514. }
  515. }
  516. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId
  517. case 2:
  518. // 累计已结算佣金
  519. stime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location()).Unix()
  520. etime := t.Unix()
  521. mallStime := time.Date(t.Year(), t.Month(), t.Day()-task.WithinDays, t.Hour(), 0, 0, 0, t.Location())
  522. mallEtime := t
  523. var sum float64
  524. if task.WithinDays <= 0 {
  525. // 用户分佣的订单
  526. idAmountMap, err := db.OrderRelateListByState(engine, uid, "3,5")
  527. if err != nil {
  528. return "", hasPay, payOrdId
  529. }
  530. for _, item := range *idAmountMap {
  531. sum += item
  532. }
  533. _, mallSum, err := db.MallOrderRelateListByState(engine, uid, 3)
  534. if err != nil {
  535. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  536. }
  537. sum += mallSum
  538. } else {
  539. // 用户分佣的订单
  540. idAmountMap, err := db.OrderRelateListByTimeByState(engine, uid, "3,5", stime, etime)
  541. if err != nil {
  542. return "", hasPay, payOrdId
  543. }
  544. for _, item := range *idAmountMap {
  545. sum += item
  546. }
  547. _, mallSum, err := db.MallOrderRelateListByTimeByState(engine, uid, 3, mallStime, mallEtime)
  548. if err != nil {
  549. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  550. }
  551. sum += mallSum
  552. }
  553. return zhios_condition_statistics_utils.Float64ToStr(sum), hasPay, payOrdId
  554. case 3:
  555. // 累计直推人数
  556. 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")
  557. etime := t.Format("2006-1-02 15:04:05")
  558. sqlTpl := `SELECT count(*) as count
  559. FROM user_profile as up
  560. LEFT JOIN user as u ON u.uid=up.uid
  561. WHERE %s;`
  562. var condStr string
  563. if task.WithinDays <= 0 {
  564. condStr = fmt.Sprintf("up.parent_uid=%s ", zhios_condition_statistics_utils.AnyToString(uid))
  565. } else {
  566. 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)
  567. }
  568. sql := fmt.Sprintf(sqlTpl, condStr)
  569. results, err := db.QueryNativeString(engine, sql)
  570. if err != nil {
  571. return "", hasPay, payOrdId
  572. }
  573. count := ""
  574. if len(results) > 0 {
  575. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  576. }
  577. return count, hasPay, payOrdId
  578. case 4:
  579. // 累计团队有效直推人数
  580. 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")
  581. etime := t.Format("2006-1-02 15:04:05")
  582. sqlTpl := `SELECT count(*) as count
  583. FROM user_profile as up
  584. LEFT JOIN user as u ON u.uid=up.uid
  585. WHERE %s;`
  586. var condStr string
  587. if task.WithinDays <= 0 {
  588. condStr = fmt.Sprintf("up.parent_uid=%s and up.is_verify=%d", zhios_condition_statistics_utils.AnyToString(uid), 1)
  589. } else {
  590. 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)
  591. }
  592. sql := fmt.Sprintf(sqlTpl, condStr)
  593. results, err := db.QueryNativeString(engine, sql)
  594. if err != nil {
  595. return "", hasPay, payOrdId
  596. }
  597. count := ""
  598. if len(results) > 0 {
  599. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  600. }
  601. return count, hasPay, payOrdId
  602. case 5:
  603. // 累计团队符合相应等级的人数
  604. 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")
  605. etime := t.Format("2006-1-02 15:04:05")
  606. sqlTpl := `SELECT count(*) as count
  607. FROM user_relate as ur
  608. LEFT JOIN user as u ON u.uid=ur.uid
  609. LEFT JOIN user_level ul on u.level=ul.id
  610. WHERE %s;`
  611. levelList := allLvMap[task.TaskTypeLevelId]
  612. levelWeight := 0
  613. if levelList != nil {
  614. levelWeight = levelList.LevelWeight
  615. }
  616. var condStr string
  617. if task.WithinDays <= 0 {
  618. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight))
  619. if blockStarChain == "1" {
  620. condStr = fmt.Sprintf("ur.parent_uid=%s AND ul.level_weight>='%s' ", zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight))
  621. }
  622. } else {
  623. 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)
  624. if blockStarChain == "1" {
  625. 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)
  626. }
  627. }
  628. sql := fmt.Sprintf(sqlTpl, condStr)
  629. fmt.Println(sql)
  630. results, err := db.QueryNativeString(engine, sql)
  631. if err != nil {
  632. return "", hasPay, payOrdId
  633. }
  634. count := ""
  635. if len(results) > 0 {
  636. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  637. }
  638. return count, hasPay, payOrdId
  639. case 11:
  640. // 累计直推符合相应等级的人数
  641. 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")
  642. etime := t.Format("2006-1-02 15:04:05")
  643. sqlTpl := `SELECT count(*) as count
  644. FROM user_relate as ur
  645. LEFT JOIN user as u ON u.uid=ur.uid
  646. LEFT JOIN user_level ul on u.level=ul.id
  647. WHERE %s;`
  648. levelList := allLvMap[task.TaskTypeLevelId]
  649. levelWeight := 0
  650. if levelList != nil {
  651. levelWeight = levelList.LevelWeight
  652. }
  653. var condStr string
  654. if task.WithinDays <= 0 {
  655. 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))
  656. if blockStarChain == "1" {
  657. 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))
  658. }
  659. } else {
  660. 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)
  661. if blockStarChain == "1" {
  662. 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)
  663. }
  664. }
  665. sql := fmt.Sprintf(sqlTpl, condStr)
  666. fmt.Println(sql)
  667. results, err := db.QueryNativeString(engine, sql)
  668. if err != nil {
  669. return "", hasPay, payOrdId
  670. }
  671. count := ""
  672. if len(results) > 0 {
  673. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  674. }
  675. return count, hasPay, payOrdId
  676. case 12: //累计团队消费金额
  677. var total float64 = 0
  678. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "2", "8", "9"}) {
  679. sqlTpl := `SELECT SUM(ol.cost_price) AS amount
  680. FROM mall_ord_list_relate olr
  681. JOIN mall_ord ol ON olr.oid = ol.ord_id
  682. WHERE olr.uid = ?
  683. AND ol.state =3 and olr.level>0 %s;
  684. `
  685. str := ""
  686. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"8", "9"}) {
  687. str += " and ol.order_type=" + zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd)
  688. }
  689. sqlTpl = fmt.Sprintf(sqlTpl, str)
  690. result, err := db.QueryNativeString(engine, sqlTpl, uid, str)
  691. if err == nil {
  692. total += zhios_condition_statistics_utils.StrToFloat64(result[0]["amount"])
  693. }
  694. }
  695. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "3"}) {
  696. o2oSqlTpl := `SELECT SUM(ol.cost_price) AS amount
  697. FROM o2o_ord_list_relate olr
  698. JOIN o2o_ord ol ON olr.oid = ol.ord_id
  699. WHERE olr.uid = ?
  700. AND ol.state =3 and olr.level>0;
  701. `
  702. o2oResult, err := db.QueryNativeString(engine, o2oSqlTpl, uid)
  703. if err == nil {
  704. total += zhios_condition_statistics_utils.StrToFloat64(o2oResult[0]["amount"])
  705. }
  706. }
  707. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "4"}) {
  708. b2cSqlTpl := `SELECT SUM(ol.cost_price) AS amount
  709. FROM b2c_ord_list_relate olr
  710. JOIN b2c_ord ol ON olr.oid = ol.ord_id
  711. WHERE olr.uid = ?
  712. AND ol.state =3 and olr.level>0;
  713. `
  714. b2cResult, err := db.QueryNativeString(engine, b2cSqlTpl, uid)
  715. if err == nil {
  716. total += zhios_condition_statistics_utils.StrToFloat64(b2cResult[0]["amount"])
  717. }
  718. }
  719. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"0", "1"}) {
  720. guideSqlTpl := `SELECT SUM(ol.paid_price) AS amount
  721. FROM ord_list_relate olr
  722. JOIN ord_list ol ON olr.oid = ol.ord_id
  723. WHERE olr.uid = ?
  724. AND ol.state in(3,5) and olr.level>0;
  725. `
  726. guideResult, err := db.QueryNativeString(engine, guideSqlTpl, uid)
  727. if err == nil {
  728. total += zhios_condition_statistics_utils.StrToFloat64(guideResult[0]["amount"])
  729. }
  730. }
  731. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2)), hasPay, payOrdId
  732. case 13:
  733. //小市场团队符合条件人数
  734. smallUid := TotalSmallTeam(engine, zhios_condition_statistics_utils.AnyToString(uid))
  735. sqlTpl := `SELECT count(*) as count
  736. FROM user_relate as ur
  737. LEFT JOIN user as u ON u.uid=ur.uid
  738. WHERE %s;`
  739. condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' ", smallUid, zhios_condition_statistics_utils.IntToStr(task.TaskTypeLevelId))
  740. sql := fmt.Sprintf(sqlTpl, condStr)
  741. results, err := db.QueryNativeString(engine, sql)
  742. if err != nil {
  743. return "", hasPay, payOrdId
  744. }
  745. count := ""
  746. if len(results) > 0 {
  747. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  748. }
  749. return count, hasPay, payOrdId
  750. case 15:
  751. if task.TaskTypeCount == 0 {
  752. return "0", hasPay, payOrdId
  753. }
  754. //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")
  755. //etime := t.Format("2006-01-02 15:04:05")
  756. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"1"}) { //o2o
  757. sqlTpl := `SELECT os.id
  758. FROM user_relate ur
  759. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  760. LEFT JOIN o2o_store os on om.id=os.store_manager
  761. WHERE ur.parent_uid = ?
  762. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  763. `
  764. var ids = make([]string, 0)
  765. result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount)
  766. if err == nil {
  767. for _, v := range result {
  768. ids = append(ids, v["id"])
  769. }
  770. }
  771. if len(ids) > 0 {
  772. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"3", "4"}).Sum(&model.O2oOrd{}, "cost_price")
  773. sum1, _ := engine.In("belong_store_id", ids).Sum(&model.O2oPayToMerchant{}, "actual_pay_amount")
  774. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum+sum1, 4), hasPay, payOrdId
  775. }
  776. }
  777. if zhios_condition_statistics_utils.InArr(zhios_condition_statistics_utils.IntToStr(task.TaskTypePvd), []string{"2"}) { //b2c
  778. sqlTpl := `SELECT os.id
  779. FROM user_relate ur
  780. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  781. LEFT JOIN b2c_ord os on om.id=os.store_manager
  782. WHERE ur.parent_uid = ?
  783. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  784. `
  785. var ids = make([]string, 0)
  786. result, err := db.QueryNativeString(engine, sqlTpl, uid, task.TaskTypeCount)
  787. if err == nil {
  788. for _, v := range result {
  789. ids = append(ids, v["id"])
  790. }
  791. }
  792. if len(ids) > 0 {
  793. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"4"}).Sum(&model.B2cOrd{}, "cost_price")
  794. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum, 4), hasPay, payOrdId
  795. }
  796. }
  797. return "0", hasPay, payOrdId
  798. case 6:
  799. hasPay = true
  800. //TODO::根据 RegionalAgentSchemeTask 表中的 task_id 判断是否完成付费任务
  801. regionalAgentUserOrd, err := db.UserLevelOrderByTaskId(engine, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(task.Id))
  802. if err != nil {
  803. return "", hasPay, payOrdId
  804. }
  805. if regionalAgentUserOrd != nil {
  806. 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 {
  807. return "0", hasPay, payOrdId
  808. }
  809. payOrdId = regionalAgentUserOrd.Id
  810. return "1", hasPay, payOrdId
  811. } else {
  812. return "0", hasPay, payOrdId
  813. }
  814. return "1", hasPay, payOrdId
  815. case 17:
  816. 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")
  817. etime := t.Format("2006-01-02 15:04:05")
  818. // 团队N条线中各有N个相应等级用户
  819. sqlTpl := `select ur.parent_uid,COUNT(*) as count FROM user_relate ur
  820. LEFT JOIN user u on u.uid=ur.uid
  821. LEFT JOIN user_level ul on u.level=ul.id
  822. 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;`
  823. levelList := allLvMap[task.TaskTypeLevelId]
  824. levelWeight := 0
  825. if levelList != nil {
  826. levelWeight = levelList.LevelWeight
  827. }
  828. sql := ""
  829. if task.WithinDays <= 0 {
  830. sql = fmt.Sprintf(sqlTpl, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "")
  831. } else {
  832. 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+"'")
  833. }
  834. results, err := db.QueryNativeString(engine, sql)
  835. if err != nil {
  836. return "", hasPay, payOrdId
  837. }
  838. sqlTpl1 := `select ur.uid FROM user_relate ur
  839. LEFT JOIN user u on u.uid=ur.uid
  840. LEFT JOIN user_level ul on u.level=ul.id
  841. where ur.parent_uid=%s and ur.level=1 and ul.level_weight>=%s %s ;`
  842. sql1 := ""
  843. if task.WithinDays <= 0 {
  844. sql1 = fmt.Sprintf(sqlTpl1, zhios_condition_statistics_utils.AnyToString(uid), zhios_condition_statistics_utils.IntToStr(levelWeight), "")
  845. } else {
  846. 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+"'")
  847. }
  848. results1, err1 := db.QueryNativeString(engine, sql1)
  849. if err1 != nil {
  850. return "", hasPay, payOrdId
  851. }
  852. var firstList = make(map[string]string)
  853. if len(results1) > 0 {
  854. for _, v := range results1 {
  855. firstList[v["uid"]] = "1"
  856. }
  857. }
  858. if len(results) > 0 {
  859. for _, v := range results {
  860. 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"]]))
  861. }
  862. }
  863. count := 0
  864. if len(firstList) > 0 {
  865. for _, v := range firstList {
  866. if zhios_condition_statistics_utils.StrToInt(v) >= task.TaskTypeSecondCount && task.TaskTypeSecondCount > 0 {
  867. count++
  868. }
  869. }
  870. }
  871. return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId
  872. case 18:
  873. 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")
  874. etime := t.Format("2006-01-02 15:04:05")
  875. // 小区消费金额
  876. sqlTpl := `SELECT SUM(molr.goods_price) as price FROM mall_ord_list_relate molr
  877. LEFT JOIN mall_ord mo on mo.ord_id=molr.oid and mo.state in (%s)
  878. WHERE molr.uid in(SELECT uid FROM user_relate WHERE parent_uid=%s and level=1) %s GROUP BY molr.uid ;`
  879. state := "1,2,3"
  880. if task.TaskTypePvd == 1 {
  881. state = "2,3"
  882. }
  883. if task.TaskTypePvd == 2 {
  884. state = "3"
  885. }
  886. sql := ""
  887. if task.WithinDays <= 0 {
  888. sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "")
  889. } else {
  890. sql = fmt.Sprintf(sqlTpl, state, zhios_condition_statistics_utils.AnyToString(uid), "and mo.create_time>='"+stime+"' and mo.create_time<='"+etime+"'")
  891. }
  892. results, err := db.QueryNativeString(engine, sql)
  893. if err != nil {
  894. return "", hasPay, payOrdId
  895. }
  896. var count float64 = 0
  897. if len(results) > 0 {
  898. var max float64 = 0
  899. for _, v := range results {
  900. if zhios_condition_statistics_utils.StrToFloat64(v["price"]) > max {
  901. max = zhios_condition_statistics_utils.StrToFloat64(v["price"])
  902. }
  903. count += zhios_condition_statistics_utils.StrToFloat64(v["price"])
  904. }
  905. count -= max
  906. }
  907. return zhios_condition_statistics_utils.Float64ToStrByPrec(count, 2), hasPay, payOrdId
  908. case 19:
  909. teamCountMinData, _ := db.SysCfgGetOne(engine, "team_count_min")
  910. teamCountMin := 15
  911. if teamCountMinData != nil {
  912. teamCountMin = zhios_condition_statistics_utils.StrToInt(teamCountMinData.Val)
  913. }
  914. teamtotal := db.GetUserTeamTotal(engine, zhios_condition_statistics_utils.AnyToString(uid))
  915. var count = 0
  916. if teamtotal != nil && teamtotal.FirstCount >= teamCountMin && teamtotal.SecondCount >= teamCountMin {
  917. count++
  918. }
  919. sql := `SELECT * FROM user_profile up
  920. LEFT JOIN user_team_total utt ON utt.uid=up.uid
  921. WHERE up.parent_uid=? and utt.first_count>=? and utt.second_count>=?
  922. `
  923. nativeString, _ := db.QueryNativeString(engine, sql, zhios_condition_statistics_utils.AnyToString(uid), teamCountMin, teamCountMin)
  924. if len(nativeString) > 0 {
  925. count += len(nativeString)
  926. }
  927. return zhios_condition_statistics_utils.IntToStr(count), hasPay, payOrdId
  928. default:
  929. return "", hasPay, payOrdId
  930. }
  931. }