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

hdl_upgrade.go 32 KiB

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