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

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