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

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