sync_data.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. package task
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "log-server/global"
  11. "log-server/model/log/request"
  12. "log-server/model/task"
  13. "log-server/model/task/control"
  14. request2 "log-server/model/task/request"
  15. "log-server/model/typeManage"
  16. "log-server/service/cache"
  17. "log-server/utils"
  18. "strconv"
  19. "time"
  20. )
  21. type SyncData struct {
  22. GameTask GameTask
  23. cache cache.Cache
  24. common Common
  25. Person typeManage.ResponsiblePerson
  26. }
  27. func (s *SyncData) SyncXmyGameData(date string) (mps map[int]control.XmyPayRequestReplyData, err error) {
  28. xmyUrl := "http://api.sheepsdk.17xmy.com/foreign/api/get_youhua_data.php"
  29. xmyParams := map[string]string{
  30. "start_day": date,
  31. "end_day": date,
  32. }
  33. requestData := new(control.XmyReply)
  34. xmyByteData, err := utils.HttpGet(xmyUrl, xmyParams)
  35. if err != nil {
  36. return
  37. }
  38. _ = json.Unmarshal(xmyByteData, &requestData)
  39. mps = map[int]control.XmyPayRequestReplyData{}
  40. for _, data := range requestData.Data.List {
  41. gameId, _ := strconv.Atoi(data.GameId)
  42. mps[gameId] = data
  43. }
  44. return
  45. }
  46. func (s *SyncData) SyncXmyFreeData(date string, gameId int) (num int, err error) {
  47. xmyFreeUrl := "http://rtd.kfzs.com/fake.php"
  48. xmyFreeParams := map[string]string{
  49. "day": date,
  50. "game_id": strconv.Itoa(gameId),
  51. }
  52. requestData := new(control.XmyFreeReply)
  53. xmyByteData, err := utils.HttpGet(xmyFreeUrl, xmyFreeParams)
  54. if err != nil {
  55. return
  56. }
  57. _ = json.Unmarshal(xmyByteData, &requestData)
  58. if len(requestData.Data) != 0 {
  59. num, _ = strconv.Atoi(requestData.Data[0].Num)
  60. }
  61. return
  62. }
  63. func (s *SyncData) SyncWslGameData(date string) (mps map[string]int, err error) {
  64. wslUrl := "http://148.70.251.170/wsl-A/get_sheep_pay.php"
  65. wslParams := map[string]string{
  66. "times": date,
  67. }
  68. var requestData []control.WslReply
  69. wslByteData, err := utils.HttpGet(wslUrl, wslParams)
  70. if err != nil {
  71. return
  72. }
  73. _ = json.Unmarshal(wslByteData, &requestData)
  74. mps = map[string]int{}
  75. for _, data := range requestData {
  76. num, _ := strconv.Atoi(data.Money)
  77. mps[data.GameId] = num
  78. }
  79. return
  80. }
  81. var taskStatistics = "%s:taskStatistics"
  82. func (s *SyncData) SyncRoomData(date string, gameIdInt int) (ts request.TaskStatistics, err error) {
  83. ctx := context.Background()
  84. key := fmt.Sprintf(taskStatistics, date)
  85. gameIdStr := strconv.Itoa(gameIdInt)
  86. data, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  87. if err != nil {
  88. if err == redis.Nil {
  89. global.GVA_LOG.Info("SyncRoomData not data", zap.Error(err))
  90. } else {
  91. global.GVA_LOG.Error("SyncRoomData fail", zap.Error(err))
  92. return
  93. }
  94. }
  95. _ = json.Unmarshal([]byte(data), &ts)
  96. return
  97. }
  98. // 获取任务完成数据
  99. func (s *SyncData) CompleteTaskData(date string) (mps map[int]task.GameTargetComplete, err error) {
  100. db := global.GVA_DB.Table("game_target_complete")
  101. db = db.Where("create_date = ?", date)
  102. var apiList []task.GameTargetComplete
  103. mps = map[int]task.GameTargetComplete{}
  104. err = db.Order("id desc").Find(&apiList).Error
  105. for _, api := range apiList {
  106. mps[api.TaskId] = api
  107. }
  108. return
  109. }
  110. // 同步每天的任务基础数据
  111. func (s *SyncData) EveryDaySyncTaskData() {
  112. db := global.GVA_DB.Model(&task.GameTask{})
  113. var apiList []task.GameTask
  114. db = db.Where("is_del = ?", -1)
  115. db = db.Where("status = ?", 1)
  116. err := db.Order("id desc").Find(&apiList).Error
  117. if err != nil {
  118. global.GVA_LOG.Error("EveryDaySyncTaskData fail", zap.Error(err))
  119. return
  120. }
  121. date := time.Now().Add(+time.Hour * 24)
  122. var gameTargetCompletes []*task.GameTargetComplete
  123. for _, gameTask := range apiList {
  124. if !errors.Is(global.GVA_DB.Where("task_id = ?", gameTask.TaskId).Where("create_date = ?", date).First(&task.GameTargetComplete{}).Error, gorm.ErrRecordNotFound) {
  125. continue
  126. }
  127. gameTargetComplete := s.GameTask.CreateGameTargetCompleteModel(gameTask, date)
  128. gameTargetCompletes = append(gameTargetCompletes, gameTargetComplete)
  129. }
  130. err = global.GVA_DB.Model(&task.GameTargetComplete{}).Omit("update_time", "game_rate", "is_complete").Create(gameTargetCompletes).Error
  131. if err != nil {
  132. global.GVA_LOG.Error("create GameTargetComplete fail", zap.Error(err))
  133. return
  134. }
  135. return
  136. }
  137. // 定时同步机房群控、小绵羊数据
  138. func (s *SyncData) SyncTaskData() {
  139. db := global.GVA_DB.Model(&task.GameTask{})
  140. var apiList []task.GameTask
  141. db = db.Where("is_del = ?", -1)
  142. db = db.Where("status = ?", 1)
  143. err := db.Order("id desc").Find(&apiList).Error
  144. if err != nil {
  145. global.GVA_LOG.Error("EveryDaySyncTaskData fail", zap.Error(err))
  146. return
  147. }
  148. if len(apiList) == 0 {
  149. global.GVA_LOG.Info("没有任务数据")
  150. return
  151. }
  152. date := time.Now().Format("2006-01-02")
  153. xmyGameData, err := s.SyncXmyGameData(date)
  154. if err != nil {
  155. global.GVA_LOG.Error("SyncTaskData get xmy data fail", zap.Error(err))
  156. return
  157. }
  158. completeTaskData, err := s.CompleteTaskData(date)
  159. if err != nil {
  160. global.GVA_LOG.Error("CompleteTaskData get data fail", zap.Error(err))
  161. return
  162. }
  163. wslData, err := s.SyncWslGameData(date)
  164. if err != nil {
  165. global.GVA_LOG.Error("SyncWslGameData get wsl data fail", zap.Error(err))
  166. return
  167. }
  168. for _, gameTask := range apiList {
  169. var gameTarget task.GameTargetComplete
  170. roomData, _ := s.SyncRoomData(date, gameTask.TaskId)
  171. if gameTask.GameIdXmy != "" {
  172. gameIdXmy, _ := strconv.Atoi(gameTask.GameIdXmy)
  173. xmyGameInfo := xmyGameData[gameIdXmy]
  174. gameTarget.NewComplete, _ = strconv.Atoi(xmyGameInfo.UserNum)
  175. gameTarget.PayComplete, _ = strconv.Atoi(xmyGameInfo.Cnt)
  176. gameTarget.RetainedComplete, _ = strconv.Atoi(xmyGameInfo.ActiveUserNum)
  177. f, _ := strconv.ParseFloat(xmyGameInfo.Amount, 64)
  178. gameTarget.Amount = int(f)
  179. num, _ := s.SyncXmyFreeData(date, gameIdXmy)
  180. if num != 0 {
  181. gameTarget.PayTarget = num
  182. if gameTarget.PayTarget > completeTaskData[gameTask.TaskId].PayTarget {
  183. lastPayAddUpdateTimeKey := fmt.Sprintf(LastPayAddUpdateTimeKey, date, gameTask.TaskId)
  184. _ = s.cache.SetCacheStr(context.Background(), lastPayAddUpdateTimeKey, time.Now().Unix())
  185. // 本次加付费目标数据
  186. lastPayAddNumKey := fmt.Sprintf(LastPayAddNumKey, date, gameTask.TaskId)
  187. _ = s.cache.SetCacheStr(context.Background(), lastPayAddNumKey, gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget)
  188. payErrAddNumKey := fmt.Sprintf(PayErrAddNumKey, date, gameTask.TaskId)
  189. s.common.SaveGameAddFee(gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget, gameTarget.PayTarget, gameTask.TaskName)
  190. // 如果当前付费目标完成,加入一下逻辑
  191. if completeTaskData[gameTask.TaskId].PayTarget <= completeTaskData[gameTask.TaskId].PayComplete {
  192. lastPayCompletedUpdateTimeKey := fmt.Sprintf(LastPayCompletedUpdateTimeKey, date, gameTask.TaskId)
  193. _ = s.cache.SetCacheStr(context.Background(), lastPayCompletedUpdateTimeKey, time.Now().Unix())
  194. _ = s.cache.SetCacheStr(context.Background(), payErrAddNumKey, gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget)
  195. } else {
  196. n, _ := s.cache.GetCacheNum(context.Background(), payErrAddNumKey)
  197. _ = s.cache.SetCacheStr(context.Background(), payErrAddNumKey, gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget+n)
  198. }
  199. }
  200. }
  201. } else {
  202. gameTarget.NewComplete = roomData.NewCompleteLocal
  203. gameTarget.PayComplete = roomData.PayCompleteLocal
  204. gameTarget.RetainedComplete = roomData.NewCompleteLocal + roomData.RetainedCompleteLocal
  205. gameTarget.Amount = gameTask.PayPrice * roomData.PayCompleteLocal
  206. if gameTarget.RetainedComplete < roomData.RetainedComplete {
  207. gameTarget.RetainedComplete = roomData.RetainedComplete
  208. }
  209. }
  210. gameTarget.GameRate = roomData.GameRate
  211. if gameTask.GamePortId == 5 && gameTask.LoginMethod == 2 {
  212. mzGameId := gameTask.MzGameId + "-" + gameTask.MzChannel
  213. if _, ok := wslData[mzGameId]; ok {
  214. gameTarget.PayTarget = wslData[mzGameId]
  215. if gameTarget.PayTarget > completeTaskData[gameTask.TaskId].PayTarget {
  216. lastPayAddUpdateTimeKey := fmt.Sprintf(LastPayAddUpdateTimeKey, date, gameTask.TaskId)
  217. _ = s.cache.SetCacheStr(context.Background(), lastPayAddUpdateTimeKey, time.Now().Unix())
  218. // 本次加付费目标数据
  219. lastPayAddNumKey := fmt.Sprintf(LastPayAddNumKey, date, gameTask.TaskId)
  220. _ = s.cache.SetCacheStr(context.Background(), lastPayAddNumKey, gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget)
  221. payErrAddNumKey := fmt.Sprintf(PayErrAddNumKey, date, gameTask.TaskId)
  222. s.common.SaveGameAddFee(gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget, gameTarget.PayTarget, gameTask.TaskName)
  223. // 如果当前付费目标完成,加入一下逻辑
  224. if completeTaskData[gameTask.TaskId].PayTarget <= completeTaskData[gameTask.TaskId].PayComplete {
  225. lastPayCompletedUpdateTimeKey := fmt.Sprintf(LastPayCompletedUpdateTimeKey, date, gameTask.TaskId)
  226. _ = s.cache.SetCacheStr(context.Background(), lastPayCompletedUpdateTimeKey, time.Now().Unix())
  227. _ = s.cache.SetCacheStr(context.Background(), payErrAddNumKey, gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget)
  228. } else {
  229. n, _ := s.cache.GetCacheNum(context.Background(), payErrAddNumKey)
  230. _ = s.cache.SetCacheStr(context.Background(), payErrAddNumKey, gameTarget.PayTarget-completeTaskData[gameTask.TaskId].PayTarget+n)
  231. }
  232. }
  233. }
  234. }
  235. gameTarget.IsComplete = -1
  236. if gameTarget.RetainedComplete+completeTaskData[gameTask.TaskId].HandRetainedComplete >= completeTaskData[gameTask.TaskId].RetainedTarget && gameTarget.PayComplete >= completeTaskData[gameTask.TaskId].PayTarget && gameTarget.NewComplete+completeTaskData[gameTask.TaskId].HandNewComplete >= completeTaskData[gameTask.TaskId].NewTarget {
  237. gameTarget.IsComplete = 1
  238. }
  239. global.GVA_DB.Model(&task.GameTargetComplete{}).Where("task_id = ?", gameTask.TaskId).Where("create_date = ?", date).Omit("create_date", "update_time", "task_id").Updates(gameTarget)
  240. }
  241. return
  242. }
  243. func (s *SyncData) DayTargetDataStatistics() {
  244. date := time.Now().Add(-time.Hour * 24).Format("2006-01-02")
  245. request1 := request2.GameTaskStatisticsRequest{
  246. GroupKey: "user,gt.game_port_id",
  247. Date: date,
  248. }
  249. gameTargets, err := s.GameTask.EveryDayTargetData(date, request1)
  250. if err != nil {
  251. global.GVA_LOG.Error("DayTargetDataStatistics 统计数据失败", zap.Error(err))
  252. return
  253. }
  254. if len(gameTargets) == 0 {
  255. global.GVA_LOG.Info("没有数据统计", zap.Error(err))
  256. return
  257. }
  258. var gameTargetDates []task.GameTargetStatistics
  259. for _, gameTarget := range gameTargets {
  260. var gameTargetDate task.GameTargetStatistics
  261. gameTargetDate.PayTarget = gameTarget.PayTarget
  262. gameTargetDate.NewTarget = gameTarget.NewTarget
  263. gameTargetDate.RetainedTarget = gameTarget.RetainedTarget
  264. gameTargetDate.PayComplete = gameTarget.PayComplete
  265. gameTargetDate.NewComplete = gameTarget.NewComplete + gameTarget.HandNewComplete
  266. gameTargetDate.RetainedComplete = gameTarget.RetainedComplete + gameTarget.HandRetainedComplete
  267. gameTargetDate.Amount = gameTarget.Amount
  268. gameTargetDate.GamePortId = gameTarget.GamePortId
  269. gameTargetDate.User = gameTarget.User
  270. gameTargetDate.TaskDate = gameTarget.CreateDate
  271. if !errors.Is(global.GVA_DB.Where("user = ?", gameTarget.User).Where("task_date = ?", gameTarget.CreateDate).Where("game_port_id = ?", gameTarget.GamePortId).First(&task.GameTargetStatistics{}).Error, gorm.ErrRecordNotFound) {
  272. err := global.GVA_DB.Where("user = ?", gameTarget.User).Where("task_date = ?", gameTarget.CreateDate).Where("game_port_id = ?", gameTarget.GamePortId).Omit("update_time", "task_date", "user", "game_port_id", "task_month", "task_year").Updates(&gameTargetDate).Error
  273. if err != nil {
  274. global.GVA_LOG.Error("DayTargetDataStatistics 更新统计数据失败", zap.Error(err))
  275. }
  276. continue
  277. }
  278. year, month, _ := time.Now().Date()
  279. gameTargetDate.TaskMonth = int(month)
  280. gameTargetDate.TaskYear = year
  281. gameTargetDates = append(gameTargetDates, gameTargetDate)
  282. }
  283. if len(gameTargetDates) < 1 {
  284. return
  285. }
  286. global.GVA_DB.Omit("update_time").Create(gameTargetDates)
  287. }
  288. var LastMsgSendTimeKey = "%s:lastMsgSendTime" // 上次活跃发送消息时间
  289. var LastNewCompletedKey = "%s:msgSendInfo:%d:lastNewCompleted" // 最后新增完成数量
  290. var LastPayCompletedKey = "%s:msgSendInfo:%d:lastPayCompleted" // 最后支付完成数量
  291. var LastRetainedCompletedKey = "%s:msgSendInfo:%d:lastRetainedCompleted" // 最后留存完成数量
  292. var LastNewCompletedUpdateTimeKey = "%s:msgSendInfo:%d:lastNewCompletedUpdateTime" // 最后新增完成更新时间
  293. var LastPayCompletedUpdateTimeKey = "%s:msgSendInfo:%d:lastPayCompletedUpdateTime" // 最后支付完成更新时间
  294. var LastRetainedCompletedUpdateTimeKey = "%s:msgSendInfo:%d:lastRetainedCompletedUpdateTime" // 最后留存完成更新时间
  295. var LastPayAddUpdateTimeKey = "%s:msgSendInfo:%d:lastPayAddUpdateTime" // 最后加付费时间
  296. var LastPayAddNumKey = "%s:msgSendInfo:%d:lastPayAddNum" // 最后加付费数量
  297. var PayErrAddNumKey = "%s:msgSendInfo:%d:PayErrAddNum" // 支付异常累加数量
  298. var TaskCompletedStatusKey = "%s:taskCompletedStatus" // 任务完成状态-1,1
  299. var LastFreeMsgSendTimeKey = "%s:lastFreeMsgSendTime" // 上次付费发送消息时间
  300. type CompletedInfo struct {
  301. AlsoTarget int //剩余数量
  302. Rate int //时间段做的任务数
  303. TimeRate int // 完成任务数据更新时间
  304. TaskId int
  305. AddPayUpdateTime int
  306. }
  307. // 获取未完成的任务数据
  308. func (s *SyncData) TaskNoCompleteDate(date string) (completesInfo []task.GameTargetComplete, err error) {
  309. db := global.GVA_DB.Table("game_target_complete")
  310. db = db.Where("is_complete = ?", -1)
  311. db = db.Where("create_date = ?", date)
  312. err = db.Find(&completesInfo).Error
  313. if err != nil {
  314. return
  315. }
  316. return
  317. }
  318. func (s *SyncData) TaskMsgSendInitData(ctx context.Context, completesInfo []task.GameTargetComplete, ctime int64, date, taskCompletedStatusKey, lastMsgSendTimeKey string) {
  319. for _, complete := range completesInfo {
  320. lastNewCompletedKey := fmt.Sprintf(LastNewCompletedKey, date, complete.TaskId)
  321. lastPayCompletedKey := fmt.Sprintf(LastPayCompletedKey, date, complete.TaskId)
  322. lastRetainedCompletedKey := fmt.Sprintf(LastRetainedCompletedKey, date, complete.TaskId)
  323. lastPayCompletedUpdateTimeKey := fmt.Sprintf(LastPayCompletedUpdateTimeKey, date, complete.TaskId)
  324. lastNewCompletedUpdateTimeKey := fmt.Sprintf(LastNewCompletedUpdateTimeKey, date, complete.TaskId)
  325. lastRetainedCompletedUpdateTimeKey := fmt.Sprintf(LastRetainedCompletedUpdateTimeKey, date, complete.TaskId)
  326. lastNewCompleted := complete.NewComplete + complete.HandNewComplete //上次新增完成数
  327. _ = s.cache.SetCacheStr(ctx, lastNewCompletedKey, lastNewCompleted)
  328. lastPayCompleted := complete.PayComplete + complete.HandPayComplete //上次支付完成数
  329. _ = s.cache.SetCacheStr(ctx, lastPayCompletedKey, lastPayCompleted)
  330. lastRetainedCompleted := complete.RetainedComplete + complete.HandRetainedComplete //上次活跃完成数
  331. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedKey, lastRetainedCompleted)
  332. //lastNewCompletedUpdateTime := complete.NewComplete //上次新增完成更新时间
  333. _ = s.cache.SetCacheStr(ctx, lastNewCompletedUpdateTimeKey, ctime)
  334. //lastPayCompletedUpdateTime := complete.NewComplete //上次支付完成更新时间
  335. _ = s.cache.SetCacheStr(ctx, lastPayCompletedUpdateTimeKey, ctime)
  336. //lastRetainedCompletedUpdateTime := complete.NewComplete //上次留存完成更新时间
  337. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedUpdateTimeKey, ctime)
  338. //lastPayAddUpdateTime := complete.NewComplete //上次付费增加更新时间
  339. }
  340. _ = s.cache.SetCacheStr(ctx, taskCompletedStatusKey, -1)
  341. _ = s.cache.SetCacheStr(ctx, lastMsgSendTimeKey, time.Now().Unix())
  342. }
  343. func (s *SyncData) TaskMsgSendRetainedData(ctx context.Context, completesInfo []task.GameTargetComplete, ctime int64, date, lastMsgSendTimeKey string) {
  344. var mps = make(map[int]map[string]CompletedInfo)
  345. lastMsgSendTime, _ := s.cache.GetCacheNum(ctx, lastMsgSendTimeKey)
  346. for _, complete := range completesInfo {
  347. lastNewCompletedKey := fmt.Sprintf(LastNewCompletedKey, date, complete.TaskId)
  348. lastRetainedCompletedKey := fmt.Sprintf(LastRetainedCompletedKey, date, complete.TaskId)
  349. lastNewCompletedUpdateTimeKey := fmt.Sprintf(LastNewCompletedUpdateTimeKey, date, complete.TaskId)
  350. lastRetainedCompletedUpdateTimeKey := fmt.Sprintf(LastRetainedCompletedUpdateTimeKey, date, complete.TaskId)
  351. currentNewCompleted := complete.NewComplete + complete.HandNewComplete
  352. currentRetainedCompleted := complete.RetainedComplete + complete.HandRetainedComplete
  353. var RateMp = make(map[string]CompletedInfo)
  354. var newBool = false
  355. var retainedBoll = false
  356. // 处理新增
  357. if complete.NewTarget > currentNewCompleted {
  358. lastNewCompleted, _ := s.cache.GetCacheNum(ctx, lastNewCompletedKey)
  359. alsoNewTarget := complete.NewTarget - currentNewCompleted
  360. newRate := 0
  361. lastNewCompletedUpdateTime, _ := s.cache.GetCacheNum(ctx, lastNewCompletedUpdateTimeKey)
  362. timeRate := int(ctime) - lastNewCompletedUpdateTime
  363. if lastNewCompleted < currentNewCompleted {
  364. newRate = currentNewCompleted - lastNewCompleted
  365. _ = s.cache.SetCacheStr(ctx, lastNewCompletedKey, currentNewCompleted)
  366. _ = s.cache.SetCacheStr(ctx, lastNewCompletedUpdateTimeKey, ctime) //上次新增完成更新时间
  367. }
  368. RateMp["newRate"] = CompletedInfo{
  369. AlsoTarget: alsoNewTarget,
  370. Rate: newRate,
  371. TimeRate: timeRate,
  372. TaskId: complete.TaskId,
  373. }
  374. } else {
  375. newBool = true
  376. // 如果当前新增为0,或者完成也更新时间
  377. _ = s.cache.SetCacheStr(ctx, lastNewCompletedUpdateTimeKey, ctime) //上次新增完成更新时间
  378. }
  379. // 处理留存
  380. if complete.RetainedTarget > currentRetainedCompleted {
  381. lastRetainedCompleted, _ := s.cache.GetCacheNum(ctx, lastRetainedCompletedKey)
  382. alsoRetainedTarget := complete.RetainedTarget - currentRetainedCompleted
  383. retainedRate := 0
  384. lastRetainedCompletedUpdateTime, _ := s.cache.GetCacheNum(ctx, lastRetainedCompletedUpdateTimeKey)
  385. timeRate := int(ctime) - lastRetainedCompletedUpdateTime
  386. if lastRetainedCompleted < currentRetainedCompleted {
  387. retainedRate = currentRetainedCompleted - lastRetainedCompleted
  388. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedKey, currentRetainedCompleted)
  389. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedUpdateTimeKey, ctime) //上次留存完成更新时间
  390. }
  391. RateMp["retainedRate"] = CompletedInfo{
  392. AlsoTarget: alsoRetainedTarget,
  393. Rate: retainedRate,
  394. TimeRate: timeRate,
  395. TaskId: complete.TaskId,
  396. }
  397. } else {
  398. retainedBoll = true
  399. // 如果当前留存为0,或者完成也更新时间
  400. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedUpdateTimeKey, ctime) //上次留存完成更新时间
  401. }
  402. if !newBool || !retainedBoll {
  403. mps[complete.TaskId] = RateMp
  404. }
  405. }
  406. var taskStatistics = "%s:taskStatistics"
  407. var retained = "# 活跃播报 "
  408. if len(mps) == 0 {
  409. global.GVA_LOG.Warn(retained)
  410. }
  411. retained += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
  412. var errMsg = "**以下游戏目标效率为零:**"
  413. var errMsgZ = ""
  414. var sendMsg = map[string]string{}
  415. var errSendMsg = map[string]string{}
  416. var m int // 时间内
  417. m = (int(ctime) - lastMsgSendTime) / 60
  418. for taskId, data := range mps {
  419. key := fmt.Sprintf(taskStatistics, date)
  420. gameIdStr := strconv.Itoa(taskId)
  421. gameTask, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  422. if err != nil {
  423. if err == redis.Nil {
  424. continue
  425. } else {
  426. global.GVA_LOG.Error("TaskMsgSendRetainedData获取缓存任务数据失败", zap.Error(err))
  427. continue
  428. }
  429. }
  430. var taskStatistics request.TaskStatistics
  431. _ = json.Unmarshal([]byte(gameTask), &taskStatistics)
  432. var eMsg = "" //单条异常数据
  433. var isNew = false
  434. var nm int //新增时间内
  435. var rm int //存时间内
  436. var rate string //效率
  437. var newErr = false
  438. var retainedErr = false
  439. var name = taskStatistics.Remark
  440. if info, ok := data["newRate"]; ok {
  441. sendMsg[name] += "\n"
  442. sendMsg[name] += taskStatistics.GameName
  443. sendMsg[name] += ",新增差"
  444. //sendMsg[name] += strconv.Itoa(info.AlsoTarget)
  445. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", info.AlsoTarget)
  446. isNew = true
  447. if info.TimeRate > 60*60*24 {
  448. lastNewCompletedUpdateTimeKey := fmt.Sprintf(LastNewCompletedUpdateTimeKey, date, info.TaskId)
  449. _ = s.cache.SetCacheStr(ctx, lastNewCompletedUpdateTimeKey, ctime) //上次新增完成更新时间
  450. nm = 1
  451. } else {
  452. nm = info.TimeRate / 60
  453. }
  454. rate = strconv.Itoa(info.Rate)
  455. if info.Rate == 0 {
  456. newErr = true
  457. }
  458. }
  459. if info, ok := data["retainedRate"]; ok {
  460. if !isNew {
  461. sendMsg[name] += "\n"
  462. sendMsg[name] += taskStatistics.GameName
  463. rate = strconv.Itoa(info.Rate)
  464. sendMsg[name] += "活跃差"
  465. } else {
  466. rate += "/" + strconv.Itoa(info.Rate)
  467. sendMsg[name] += ",活跃差"
  468. }
  469. if info.TimeRate > 60*60*24 {
  470. lastRetainedCompletedUpdateTimeKey := fmt.Sprintf(LastRetainedCompletedUpdateTimeKey, date, info.TaskId)
  471. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedUpdateTimeKey, ctime) //上次留存完成更新时间
  472. rm = 1
  473. } else {
  474. rm = info.TimeRate / 60
  475. }
  476. //sendMsg[name] += strconv.Itoa(info.AlsoTarget)
  477. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", info.AlsoTarget)
  478. if info.Rate == 0 {
  479. retainedErr = true
  480. }
  481. }
  482. // 5分钟数据没动报异常数据
  483. if newErr && nm > 4 {
  484. eMsg += "\n"
  485. eMsg += taskStatistics.GameName
  486. eMsg += ",新增"
  487. if nm >= 60 {
  488. eMsg += "("
  489. eMsg += strconv.Itoa(nm / 60)
  490. eMsg += "小时内)"
  491. } else {
  492. eMsg += "("
  493. eMsg += strconv.Itoa(nm)
  494. eMsg += "分钟内)"
  495. }
  496. }
  497. if retainedErr && rm > 4 {
  498. if !newErr {
  499. eMsg += "\n"
  500. eMsg += taskStatistics.GameName
  501. }
  502. eMsg += ",活跃"
  503. if rm >= 60 {
  504. eMsg += "("
  505. eMsg += strconv.Itoa(rm / 60)
  506. eMsg += "小时内)"
  507. } else {
  508. eMsg += "("
  509. eMsg += strconv.Itoa(rm)
  510. eMsg += "分钟内)"
  511. }
  512. }
  513. if eMsg != "" {
  514. errMsgZ += eMsg
  515. errSendMsg[name] += eMsg
  516. }
  517. sendMsg[name] += ","
  518. if m >= 60 {
  519. sendMsg[name] += strconv.Itoa(m / 60)
  520. sendMsg[name] += "小时内完成"
  521. } else {
  522. sendMsg[name] += strconv.Itoa(m)
  523. sendMsg[name] += "分钟内完成"
  524. }
  525. //sendMsg[name] += rate
  526. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%s</font>", rate)
  527. }
  528. hour := time.Now().Hour()
  529. msg := ""
  530. mpsPerson, _ := s.Person.GetUserInfo()
  531. //b1 ,_ := json.Marshal(mpsPerson)
  532. //global.GVA_LOG.Warn(string(b1))
  533. var mobile []string
  534. if hour >= 16 {
  535. if len(sendMsg) == 0 {
  536. return
  537. }
  538. if len(mps) != 0 && len(errSendMsg) != 0 {
  539. for name, _ := range errSendMsg {
  540. mobile = append(mobile, mpsPerson[name])
  541. }
  542. }
  543. for name, msg := range sendMsg {
  544. retained += "\n"
  545. retained += "**" + name + "**"
  546. retained += msg
  547. }
  548. msg = retained
  549. if errMsgZ != "" {
  550. msg += "\n"
  551. //msg += errMsg + errMsgZ
  552. msg += fmt.Sprintf("<font color=\"warning\">%s</font>", errMsg+errMsgZ)
  553. }
  554. } else {
  555. if errMsgZ != "" {
  556. //msg += errMsg + errMsgZ
  557. }
  558. if len(mps) != 0 && len(errSendMsg) != 0 {
  559. msg += "# 活跃播报 "
  560. msg += errMsg
  561. for name, m := range errSendMsg {
  562. mobile = append(mobile, mpsPerson[name])
  563. msg += "\n"
  564. msg += "**" + name + "**"
  565. msg += fmt.Sprintf("<font color=\"warning\">%s</font>", m)
  566. }
  567. }
  568. }
  569. //global.GVA_LOG.Warn(msg)
  570. _ = s.cache.SetCacheStr(ctx, lastMsgSendTimeKey, time.Now().Unix())
  571. if msg == "" {
  572. return
  573. }
  574. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  575. var sendData SendMsg
  576. sendData.MsgType = "markdown"
  577. sendData.Markdown.Content = msg
  578. s.SendMsgData(url, sendData)
  579. if len(mobile) != 0 {
  580. var sendTextData SendTextMsg
  581. sendTextData.MsgType = "text"
  582. sendTextData.Text.MentionedMobileList = mobile
  583. //b ,_ := json.Marshal(sendTextData)
  584. //global.GVA_LOG.Warn(string(b))
  585. s.SendMsgData(url, sendTextData)
  586. }
  587. }
  588. func (s *SyncData) TaskMsgSendFreeData(ctx context.Context, completesInfo []task.GameTargetComplete, ctime int64, date, lastFreeMsgSendTimeKey string, isOne bool) {
  589. var RateMp = make(map[int]CompletedInfo)
  590. lastFreeMsgSendTime, _ := s.cache.GetCacheNum(ctx, lastFreeMsgSendTimeKey)
  591. for _, complete := range completesInfo {
  592. lastPayCompletedKey := fmt.Sprintf(LastPayCompletedKey, date, complete.TaskId)
  593. lastPayCompletedUpdateTimeKey := fmt.Sprintf(LastPayCompletedUpdateTimeKey, date, complete.TaskId)
  594. lastPayAddUpdateTimeKey := fmt.Sprintf(LastPayAddUpdateTimeKey, date, complete.TaskId)
  595. currentPayCompleted := complete.PayComplete + complete.HandPayComplete
  596. // 付费处理
  597. if complete.PayTarget != 0 && complete.PayTarget > currentPayCompleted {
  598. lastPayCompleted, _ := s.cache.GetCacheNum(ctx, lastPayCompletedKey)
  599. alsoPayTarget := complete.PayTarget - currentPayCompleted
  600. payRate := 0
  601. lastPayCompletedUpdateTime, _ := s.cache.GetCacheNum(ctx, lastPayCompletedUpdateTimeKey)
  602. lastPayAddUpdateTime, _ := s.cache.GetCacheNum(ctx, lastPayAddUpdateTimeKey)
  603. timeRate := int(ctime) - lastPayCompletedUpdateTime
  604. if lastPayCompleted < currentPayCompleted {
  605. payRate = currentPayCompleted - lastPayCompleted
  606. _ = s.cache.SetCacheStr(ctx, lastPayCompletedKey, currentPayCompleted)
  607. _ = s.cache.SetCacheStr(ctx, lastPayCompletedUpdateTimeKey, ctime) //上次付费更新时间
  608. }
  609. RateMp[complete.TaskId] = CompletedInfo{
  610. AlsoTarget: alsoPayTarget,
  611. Rate: payRate,
  612. TimeRate: timeRate,
  613. TaskId: complete.TaskId,
  614. AddPayUpdateTime: lastPayAddUpdateTime,
  615. }
  616. } else {
  617. // 如果当前付费为0,或者完成也更新时间
  618. _ = s.cache.SetCacheStr(ctx, lastPayCompletedUpdateTimeKey, ctime) //上次付费更新时间
  619. }
  620. }
  621. if isOne {
  622. _ = s.cache.SetCacheStr(ctx, lastFreeMsgSendTimeKey, time.Now().Unix())
  623. return
  624. }
  625. if len(RateMp) == 0 {
  626. global.GVA_LOG.Info("TaskMsgSendFreeData没有查询到未完成付费数据")
  627. return
  628. }
  629. var taskStatistics = "%s:taskStatistics"
  630. var retained = "付费播报 "
  631. retained += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
  632. var errMsg = "**以下游戏付费效率为零:**"
  633. var errMsgZ = ""
  634. var sendMsg = map[string]string{}
  635. var m int //时间内
  636. m = (int(ctime) - lastFreeMsgSendTime) / 60
  637. mpsPerson, _ := s.Person.GetUserInfo()
  638. var mobile []string
  639. for taskId, data := range RateMp {
  640. if data.AlsoTarget <= 0 {
  641. continue
  642. }
  643. key := fmt.Sprintf(taskStatistics, date)
  644. gameIdStr := strconv.Itoa(taskId)
  645. gameTask, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  646. if err != nil {
  647. if err == redis.Nil {
  648. continue
  649. } else {
  650. global.GVA_LOG.Error("TaskMsgSendRetainedData获取缓存任务数据失败", zap.Error(err))
  651. continue
  652. }
  653. }
  654. var taskStatistics request.TaskStatistics
  655. _ = json.Unmarshal([]byte(gameTask), &taskStatistics)
  656. var name = taskStatistics.Remark
  657. sendMsg[name] += "\n"
  658. sendMsg[name] += taskStatistics.GameName
  659. sendMsg[name] += ",付费差"
  660. //sendMsg[name] += strconv.Itoa(data.AlsoTarget)
  661. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", data.AlsoTarget)
  662. if data.TimeRate > 60*60*24 {
  663. lastPayCompletedUpdateTimeKey := fmt.Sprintf(LastPayCompletedUpdateTimeKey, date, data.TaskId)
  664. _ = s.cache.SetCacheStr(ctx, lastPayCompletedUpdateTimeKey, ctime) //上次付费更新时间
  665. data.TimeRate = 60 * 60 * 2
  666. }
  667. // 5分钟数据没动报异常数据
  668. if data.TimeRate/60 >= 2 {
  669. if data.Rate == 0 {
  670. lastPayAddNumKey := fmt.Sprintf(LastPayAddNumKey, date, data.TaskId)
  671. payErrAddNumKey := fmt.Sprintf(PayErrAddNumKey, date, data.TaskId)
  672. numErr, _ := s.cache.GetCacheNum(context.Background(), payErrAddNumKey)
  673. lastAddFeeNumErr, _ := s.cache.GetCacheNum(context.Background(), lastPayAddNumKey)
  674. errMsgZ += "\n"
  675. errMsgZ += taskStatistics.GameName
  676. errMsgZ += "("
  677. if data.TimeRate/60 >= 60 {
  678. errMsgZ += strconv.Itoa(data.TimeRate / 60 / 60)
  679. errMsgZ += "小时内(+" + strconv.Itoa(numErr) + "))"
  680. } else {
  681. errMsgZ += strconv.Itoa(data.TimeRate / 60)
  682. errMsgZ += "分钟内(+" + strconv.Itoa(numErr) + "))"
  683. }
  684. errMsgZ += ","
  685. errMsgZ += "最后加付费时间 "
  686. t := time.Unix(int64(data.AddPayUpdateTime), 0)
  687. errMsgZ += t.Format("15:04:05")
  688. errMsgZ += "(+" + strconv.Itoa(lastAddFeeNumErr) + ")"
  689. if _, ok := mpsPerson[name]; ok {
  690. mobile = append(mobile, mpsPerson[name])
  691. }
  692. }
  693. }
  694. sendMsg[name] += ","
  695. if m >= 60 {
  696. sendMsg[name] += strconv.Itoa(m / 60)
  697. sendMsg[name] += "分钟内完成"
  698. } else {
  699. sendMsg[name] += strconv.Itoa(m)
  700. sendMsg[name] += "分钟内完成"
  701. }
  702. //sendMsg[name] += strconv.Itoa(data.Rate)
  703. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", data.Rate)
  704. }
  705. if len(sendMsg) == 0 {
  706. return
  707. }
  708. for name, msg := range sendMsg {
  709. retained += "\n"
  710. retained += name
  711. retained += msg
  712. }
  713. msg := retained
  714. if errMsgZ != "" {
  715. msg += "\n"
  716. //msg += errMsg + errMsgZ
  717. msg += fmt.Sprintf("<font color=\"warning\">%s</font>", errMsg+errMsgZ)
  718. }
  719. _ = s.cache.SetCacheStr(ctx, lastFreeMsgSendTimeKey, time.Now().Unix())
  720. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  721. var sendData SendMsg
  722. sendData.MsgType = "markdown"
  723. sendData.Markdown.Content = msg
  724. s.SendMsgData(url, sendData)
  725. if len(mobile) != 0 {
  726. var sendTextData SendTextMsg
  727. sendTextData.MsgType = "text"
  728. sendTextData.Text.MentionedMobileList = mobile
  729. s.SendMsgData(url, sendTextData)
  730. }
  731. return
  732. }
  733. func (s *SyncData) SendMsgData(url string, params interface{}) {
  734. _, _ = utils.HttpPost(url, params)
  735. return
  736. }
  737. //活跃新增数据推送
  738. func (s *SyncData) TaskMsgSend() {
  739. date := time.Now().Format("2006-01-02")
  740. ctx := context.Background()
  741. taskCompletedStatusKey := fmt.Sprintf(TaskCompletedStatusKey, date)
  742. taskCompletedStatus, _ := s.cache.GetCacheNum(ctx, taskCompletedStatusKey)
  743. if taskCompletedStatus == 1 {
  744. return
  745. }
  746. completesInfo, err := s.TaskNoCompleteDate(date)
  747. if err != nil {
  748. global.GVA_LOG.Error("TaskMsgSend查询任务数据报错", zap.Error(err))
  749. }
  750. if len(completesInfo) == 0 {
  751. global.GVA_LOG.Info("TaskMsgSend没有查询到未完成数据")
  752. }
  753. lastMsgSendTimeKey := fmt.Sprintf(LastMsgSendTimeKey, date)
  754. b, err := s.cache.ExistsKey(context.Background(), lastMsgSendTimeKey)
  755. if err != nil {
  756. global.GVA_LOG.Error("TaskMsgSend查询任务数据报错", zap.Error(err))
  757. return
  758. }
  759. ctime := time.Now().Unix()
  760. if !b {
  761. s.TaskMsgSendInitData(ctx, completesInfo, ctime, date, taskCompletedStatusKey, lastMsgSendTimeKey)
  762. } else {
  763. lastMsgSendTime, _ := s.cache.GetCacheNum(ctx, lastMsgSendTimeKey)
  764. if int(ctime)-lastMsgSendTime < 60 {
  765. global.GVA_LOG.Error("TaskMsgSend上次数据发送时间还没到5分钟", zap.Error(err))
  766. return
  767. }
  768. s.TaskMsgSendRetainedData(ctx, completesInfo, ctime, date, lastMsgSendTimeKey)
  769. }
  770. }
  771. //活跃新增数据推送
  772. func (s *SyncData) TaskFreeMsgSend() {
  773. date := time.Now().Format("2006-01-02")
  774. ctx := context.Background()
  775. taskCompletedStatusKey := fmt.Sprintf(TaskCompletedStatusKey, date)
  776. taskCompletedStatus, _ := s.cache.GetCacheNum(ctx, taskCompletedStatusKey)
  777. if taskCompletedStatus == 1 {
  778. return
  779. }
  780. completesInfo, err := s.TaskNoCompleteDate(date)
  781. if err != nil {
  782. global.GVA_LOG.Error("TaskMsgSend查询任务数据报错", zap.Error(err))
  783. }
  784. if len(completesInfo) == 0 {
  785. global.GVA_LOG.Info("TaskMsgSend没有查询到未完成数据")
  786. }
  787. lastFreeMsgSendTimeKey := fmt.Sprintf(LastFreeMsgSendTimeKey, date)
  788. ctime := time.Now().Unix()
  789. lastFreeMsgSendTime, _ := s.cache.GetCacheNum(ctx, lastFreeMsgSendTimeKey)
  790. if int(ctime)-lastFreeMsgSendTime < 60 {
  791. global.GVA_LOG.Error("TaskMsgSend上次数据发送时间还没到5分钟", zap.Error(err))
  792. return
  793. }
  794. var isOne = false
  795. if lastFreeMsgSendTime == 0 {
  796. isOne = true
  797. }
  798. s.TaskMsgSendFreeData(ctx, completesInfo, ctime, date, lastFreeMsgSendTimeKey, isOne)
  799. }
  800. func (s *SyncData) CheckTaskCompletedInfo() {
  801. date := time.Now().Format("2006-01-02")
  802. ctx := context.Background()
  803. taskCompletedStatusKey := fmt.Sprintf(TaskCompletedStatusKey, date)
  804. completesInfo, err := s.TaskNoCompleteDate(date)
  805. if err != nil {
  806. global.GVA_LOG.Error("CheckTaskCompletedInfo查询任务数据报错", zap.Error(err))
  807. return
  808. }
  809. status, _ := s.cache.GetCacheNum(ctx, taskCompletedStatusKey)
  810. if status == 1 {
  811. global.GVA_LOG.Info("任务已完成")
  812. return
  813. }
  814. if len(completesInfo) == 0 {
  815. global.GVA_LOG.Info("TaskMsgSend没有查询到未完成数据")
  816. time.Sleep(time.Second * 10)
  817. _ = s.cache.SetCacheStr(ctx, taskCompletedStatusKey, 1)
  818. msg := "# 监控报警 "
  819. msg += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
  820. msg += "\n"
  821. msg += "今日任务目标已完成"
  822. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  823. var sendData SendMsg
  824. sendData.MsgType = "markdown"
  825. sendData.Markdown.Content = msg
  826. s.SendMsgData(url, sendData)
  827. }
  828. }
  829. func (s *SyncData) TaskMsgSendRetainedDataUpdate(ctx context.Context, completesInfo []task.GameTargetComplete, ctime int64, date, lastMsgSendTimeKey string) {
  830. var mps = make(map[int]map[string]CompletedInfo)
  831. lastMsgSendTime, _ := s.cache.GetCacheNum(ctx, lastMsgSendTimeKey)
  832. for _, complete := range completesInfo {
  833. lastNewCompletedKey := fmt.Sprintf(LastNewCompletedKey, date, complete.TaskId)
  834. lastRetainedCompletedKey := fmt.Sprintf(LastRetainedCompletedKey, date, complete.TaskId)
  835. lastNewCompletedUpdateTimeKey := fmt.Sprintf(LastNewCompletedUpdateTimeKey, date, complete.TaskId)
  836. lastRetainedCompletedUpdateTimeKey := fmt.Sprintf(LastRetainedCompletedUpdateTimeKey, date, complete.TaskId)
  837. currentNewCompleted := complete.NewComplete + complete.HandNewComplete
  838. currentRetainedCompleted := complete.RetainedComplete + complete.HandRetainedComplete
  839. var RateMp = make(map[string]CompletedInfo)
  840. var newBool = false
  841. var retainedBoll = false
  842. // 处理新增
  843. if complete.NewTarget > currentNewCompleted {
  844. lastNewCompleted, _ := s.cache.GetCacheNum(ctx, lastNewCompletedKey)
  845. alsoNewTarget := complete.NewTarget - currentNewCompleted
  846. newRate := 0
  847. lastNewCompletedUpdateTime, _ := s.cache.GetCacheNum(ctx, lastNewCompletedUpdateTimeKey)
  848. timeRate := int(ctime) - lastNewCompletedUpdateTime
  849. if lastNewCompleted < currentNewCompleted {
  850. newRate = currentNewCompleted - lastNewCompleted
  851. _ = s.cache.SetCacheStr(ctx, lastNewCompletedKey, currentNewCompleted)
  852. _ = s.cache.SetCacheStr(ctx, lastNewCompletedUpdateTimeKey, ctime) //上次新增完成更新时间
  853. }
  854. RateMp["newRate"] = CompletedInfo{
  855. AlsoTarget: alsoNewTarget,
  856. Rate: newRate,
  857. TimeRate: timeRate,
  858. TaskId: complete.TaskId,
  859. }
  860. } else {
  861. newBool = true
  862. // 如果当前新增为0,或者完成也更新时间
  863. _ = s.cache.SetCacheStr(ctx, lastNewCompletedUpdateTimeKey, ctime) //上次新增完成更新时间
  864. }
  865. // 处理留存
  866. if complete.RetainedTarget > currentRetainedCompleted {
  867. lastRetainedCompleted, _ := s.cache.GetCacheNum(ctx, lastRetainedCompletedKey)
  868. alsoRetainedTarget := complete.RetainedTarget - currentRetainedCompleted
  869. retainedRate := 0
  870. lastRetainedCompletedUpdateTime, _ := s.cache.GetCacheNum(ctx, lastRetainedCompletedUpdateTimeKey)
  871. timeRate := int(ctime) - lastRetainedCompletedUpdateTime
  872. if lastRetainedCompleted < currentRetainedCompleted {
  873. retainedRate = currentRetainedCompleted - lastRetainedCompleted
  874. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedKey, currentRetainedCompleted)
  875. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedUpdateTimeKey, ctime) //上次留存完成更新时间
  876. }
  877. RateMp["retainedRate"] = CompletedInfo{
  878. AlsoTarget: alsoRetainedTarget,
  879. Rate: retainedRate,
  880. TimeRate: timeRate,
  881. TaskId: complete.TaskId,
  882. }
  883. } else {
  884. retainedBoll = true
  885. // 如果当前留存为0,或者完成也更新时间
  886. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedUpdateTimeKey, ctime) //上次留存完成更新时间
  887. }
  888. if !newBool || !retainedBoll {
  889. mps[complete.TaskId] = RateMp
  890. }
  891. }
  892. var taskStatistics = "%s:taskStatistics"
  893. var retained = "# 活跃播报 "
  894. if len(mps) == 0 {
  895. global.GVA_LOG.Warn(retained)
  896. }
  897. retained += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
  898. var errMsg = "**以下游戏目标效率为零:**"
  899. var errMsgZ = ""
  900. var sendMsg = map[string]string{}
  901. var errSendMsg = map[string]string{}
  902. var m int // 时间内
  903. m = (int(ctime) - lastMsgSendTime) / 60
  904. for taskId, data := range mps {
  905. key := fmt.Sprintf(taskStatistics, date)
  906. gameIdStr := strconv.Itoa(taskId)
  907. gameTask, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  908. if err != nil {
  909. if err == redis.Nil {
  910. continue
  911. } else {
  912. global.GVA_LOG.Error("TaskMsgSendRetainedData获取缓存任务数据失败", zap.Error(err))
  913. continue
  914. }
  915. }
  916. var taskStatistics request.TaskStatistics
  917. _ = json.Unmarshal([]byte(gameTask), &taskStatistics)
  918. var eMsg = "" //单条异常数据
  919. var isNew = false
  920. var nm int //新增时间内
  921. var rm int //存时间内
  922. var rate string //效率
  923. var newErr = false
  924. var retainedErr = false
  925. var name = taskStatistics.Remark
  926. if info, ok := data["newRate"]; ok {
  927. sendMsg[name] += "\n"
  928. sendMsg[name] += taskStatistics.GameName
  929. sendMsg[name] += ",新增差"
  930. //sendMsg[name] += strconv.Itoa(info.AlsoTarget)
  931. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", info.AlsoTarget)
  932. isNew = true
  933. if info.TimeRate > 60*60*24 {
  934. lastNewCompletedUpdateTimeKey := fmt.Sprintf(LastNewCompletedUpdateTimeKey, date, info.TaskId)
  935. _ = s.cache.SetCacheStr(ctx, lastNewCompletedUpdateTimeKey, ctime) //上次新增完成更新时间
  936. nm = 1
  937. } else {
  938. nm = info.TimeRate / 60
  939. }
  940. rate = strconv.Itoa(info.Rate)
  941. if info.Rate == 0 {
  942. newErr = true
  943. }
  944. }
  945. if info, ok := data["retainedRate"]; ok {
  946. if !isNew {
  947. sendMsg[name] += "\n"
  948. sendMsg[name] += taskStatistics.GameName
  949. rate = strconv.Itoa(info.Rate)
  950. sendMsg[name] += "活跃差"
  951. } else {
  952. rate += "/" + strconv.Itoa(info.Rate)
  953. sendMsg[name] += ",活跃差"
  954. }
  955. if info.TimeRate > 60*60*24 {
  956. lastRetainedCompletedUpdateTimeKey := fmt.Sprintf(LastRetainedCompletedUpdateTimeKey, date, info.TaskId)
  957. _ = s.cache.SetCacheStr(ctx, lastRetainedCompletedUpdateTimeKey, ctime) //上次留存完成更新时间
  958. rm = 1
  959. } else {
  960. rm = info.TimeRate / 60
  961. }
  962. //sendMsg[name] += strconv.Itoa(info.AlsoTarget)
  963. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", info.AlsoTarget)
  964. if info.Rate == 0 {
  965. retainedErr = true
  966. }
  967. }
  968. // 5分钟数据没动报异常数据
  969. if newErr && nm > 4 {
  970. eMsg += "\n"
  971. eMsg += taskStatistics.GameName
  972. eMsg += ",新增"
  973. if nm >= 60 {
  974. eMsg += "("
  975. eMsg += strconv.Itoa(nm / 60)
  976. eMsg += "小时内)"
  977. } else {
  978. eMsg += "("
  979. eMsg += strconv.Itoa(nm)
  980. eMsg += "分钟内)"
  981. }
  982. }
  983. if retainedErr && rm > 4 {
  984. if !newErr {
  985. eMsg += "\n"
  986. eMsg += taskStatistics.GameName
  987. }
  988. eMsg += ",活跃"
  989. if rm >= 60 {
  990. eMsg += "("
  991. eMsg += strconv.Itoa(rm / 60)
  992. eMsg += "小时内)"
  993. } else {
  994. eMsg += "("
  995. eMsg += strconv.Itoa(rm)
  996. eMsg += "分钟内)"
  997. }
  998. }
  999. if eMsg != "" {
  1000. errMsgZ += eMsg
  1001. errSendMsg[name] += eMsg
  1002. }
  1003. sendMsg[name] += ","
  1004. if m >= 60 {
  1005. sendMsg[name] += strconv.Itoa(m / 60)
  1006. sendMsg[name] += "小时内完成"
  1007. } else {
  1008. sendMsg[name] += strconv.Itoa(m)
  1009. sendMsg[name] += "分钟内完成"
  1010. }
  1011. //sendMsg[name] += rate
  1012. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%s</font>", rate)
  1013. }
  1014. hour := time.Now().Hour()
  1015. msg := ""
  1016. mpsPerson, _ := s.Person.GetUserInfoData()
  1017. //b1 ,_ := json.Marshal(mpsPerson)
  1018. //global.GVA_LOG.Warn(string(b1))
  1019. var mobile []string
  1020. if hour >= 16 {
  1021. if len(sendMsg) == 0 {
  1022. return
  1023. }
  1024. if len(mps) != 0 && len(errSendMsg) != 0 {
  1025. for name, _ := range errSendMsg {
  1026. mobile = append(mobile, mpsPerson[name].MobilePhoneNumber)
  1027. }
  1028. }
  1029. for name, msg := range sendMsg {
  1030. retained += "\n"
  1031. retained += "**" + name + "**"
  1032. retained += msg
  1033. }
  1034. msg = retained
  1035. if errMsgZ != "" {
  1036. msg += "\n"
  1037. //msg += errMsg + errMsgZ
  1038. msg += fmt.Sprintf("<font color=\"warning\">%s</font>", errMsg+errMsgZ)
  1039. }
  1040. for pName, pMsg := range sendMsg {
  1041. if hour >= mpsPerson[pName].StartTime {
  1042. personMsg := pMsg
  1043. if _, ok := errSendMsg[pName]; ok {
  1044. personMsg += fmt.Sprintf("<font color=\"warning\">%s</font>", errSendMsg[pName])
  1045. }
  1046. url := mpsPerson[pName].Url
  1047. var sendPersonData SendMsg
  1048. sendPersonData.MsgType = "markdown"
  1049. sendPersonData.Markdown.Content = personMsg
  1050. s.SendMsgData(url, sendPersonData)
  1051. if mpsPerson[pName].Custodians != "" {
  1052. if _, ok := mpsPerson[mpsPerson[pName].Custodians]; ok {
  1053. custodiansMsg := fmt.Sprintf("<font color=\"warning\">%s的任务</font>", pName)
  1054. custodiansMsg += "\n"
  1055. custodiansMsg += personMsg
  1056. var custodiansData SendMsg
  1057. custodiansData.MsgType = "markdown"
  1058. custodiansData.Markdown.Content = custodiansMsg
  1059. urlCustodians := mpsPerson[mpsPerson[pName].Custodians].Url
  1060. s.SendMsgData(urlCustodians, custodiansData)
  1061. }
  1062. }
  1063. }
  1064. }
  1065. } else {
  1066. if errMsgZ != "" {
  1067. //msg += errMsg + errMsgZ
  1068. }
  1069. if len(mps) != 0 && len(errSendMsg) != 0 {
  1070. msg += "# 活跃播报 "
  1071. msg += errMsg
  1072. for name, m := range errSendMsg {
  1073. mobile = append(mobile, mpsPerson[name].MobilePhoneNumber)
  1074. msg += "\n"
  1075. msg += "**" + name + "**"
  1076. msg += fmt.Sprintf("<font color=\"warning\">%s</font>", m)
  1077. }
  1078. }
  1079. for pName, pMsg := range sendMsg {
  1080. if hour >= mpsPerson[pName].StartTime {
  1081. var personMsg = "# 付费播报 "
  1082. personMsg += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
  1083. personMsg = pMsg
  1084. if _, ok := errSendMsg[pName]; ok {
  1085. personMsg += fmt.Sprintf("<font color=\"warning\">%s</font>", errSendMsg[pName])
  1086. }
  1087. url := mpsPerson[pName].Url
  1088. var sendPersonData SendMsg
  1089. sendPersonData.MsgType = "markdown"
  1090. sendPersonData.Markdown.Content = personMsg
  1091. s.SendMsgData(url, sendPersonData)
  1092. if mpsPerson[pName].Custodians != "" {
  1093. custodiansMsg := fmt.Sprintf("<font color=\"warning\">%s的任务</font>", pName)
  1094. custodiansMsg += "\n"
  1095. custodiansMsg += personMsg
  1096. var custodiansData SendMsg
  1097. custodiansData.MsgType = "markdown"
  1098. custodiansData.Markdown.Content = custodiansMsg
  1099. urlCustodians := mpsPerson[mpsPerson[pName].Custodians].Url
  1100. s.SendMsgData(urlCustodians, custodiansData)
  1101. }
  1102. }
  1103. }
  1104. }
  1105. //global.GVA_LOG.Warn(msg)
  1106. _ = s.cache.SetCacheStr(ctx, lastMsgSendTimeKey, time.Now().Unix())
  1107. if msg == "" {
  1108. return
  1109. }
  1110. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  1111. var sendData SendMsg
  1112. sendData.MsgType = "markdown"
  1113. sendData.Markdown.Content = msg
  1114. s.SendMsgData(url, sendData)
  1115. if len(mobile) != 0 {
  1116. var sendTextData SendTextMsg
  1117. sendTextData.MsgType = "text"
  1118. sendTextData.Text.MentionedMobileList = mobile
  1119. //b ,_ := json.Marshal(sendTextData)
  1120. //global.GVA_LOG.Warn(string(b))
  1121. s.SendMsgData(url, sendTextData)
  1122. }
  1123. }
  1124. func (s *SyncData) TaskMsgSendFreeDataUpdate(ctx context.Context, completesInfo []task.GameTargetComplete, ctime int64, date, lastFreeMsgSendTimeKey string, isOne bool) {
  1125. var RateMp = make(map[int]CompletedInfo)
  1126. lastFreeMsgSendTime, _ := s.cache.GetCacheNum(ctx, lastFreeMsgSendTimeKey)
  1127. for _, complete := range completesInfo {
  1128. lastPayCompletedKey := fmt.Sprintf(LastPayCompletedKey, date, complete.TaskId)
  1129. lastPayCompletedUpdateTimeKey := fmt.Sprintf(LastPayCompletedUpdateTimeKey, date, complete.TaskId)
  1130. lastPayAddUpdateTimeKey := fmt.Sprintf(LastPayAddUpdateTimeKey, date, complete.TaskId)
  1131. currentPayCompleted := complete.PayComplete + complete.HandPayComplete
  1132. // 付费处理
  1133. if complete.PayTarget != 0 && complete.PayTarget > currentPayCompleted {
  1134. lastPayCompleted, _ := s.cache.GetCacheNum(ctx, lastPayCompletedKey)
  1135. alsoPayTarget := complete.PayTarget - currentPayCompleted
  1136. payRate := 0
  1137. lastPayCompletedUpdateTime, _ := s.cache.GetCacheNum(ctx, lastPayCompletedUpdateTimeKey)
  1138. lastPayAddUpdateTime, _ := s.cache.GetCacheNum(ctx, lastPayAddUpdateTimeKey)
  1139. timeRate := int(ctime) - lastPayCompletedUpdateTime
  1140. if lastPayCompleted < currentPayCompleted {
  1141. payRate = currentPayCompleted - lastPayCompleted
  1142. _ = s.cache.SetCacheStr(ctx, lastPayCompletedKey, currentPayCompleted)
  1143. _ = s.cache.SetCacheStr(ctx, lastPayCompletedUpdateTimeKey, ctime) //上次付费更新时间
  1144. }
  1145. RateMp[complete.TaskId] = CompletedInfo{
  1146. AlsoTarget: alsoPayTarget,
  1147. Rate: payRate,
  1148. TimeRate: timeRate,
  1149. TaskId: complete.TaskId,
  1150. AddPayUpdateTime: lastPayAddUpdateTime,
  1151. }
  1152. } else {
  1153. // 如果当前付费为0,或者完成也更新时间
  1154. _ = s.cache.SetCacheStr(ctx, lastPayCompletedUpdateTimeKey, ctime) //上次付费更新时间
  1155. }
  1156. }
  1157. if isOne {
  1158. _ = s.cache.SetCacheStr(ctx, lastFreeMsgSendTimeKey, time.Now().Unix())
  1159. return
  1160. }
  1161. if len(RateMp) == 0 {
  1162. global.GVA_LOG.Info("TaskMsgSendFreeData没有查询到未完成付费数据")
  1163. return
  1164. }
  1165. var taskStatistics = "%s:taskStatistics"
  1166. var retained = "# 付费播报 "
  1167. retained += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
  1168. var errMsg = "**以下游戏付费效率为零:**"
  1169. var errMsgZ = ""
  1170. var sendMsg = map[string]string{}
  1171. var m int //时间内
  1172. m = (int(ctime) - lastFreeMsgSendTime) / 60
  1173. mpsPerson, _ := s.Person.GetUserInfoData()
  1174. var mobile []string
  1175. for taskId, data := range RateMp {
  1176. if data.AlsoTarget <= 0 {
  1177. continue
  1178. }
  1179. key := fmt.Sprintf(taskStatistics, date)
  1180. gameIdStr := strconv.Itoa(taskId)
  1181. gameTask, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  1182. if err != nil {
  1183. if err == redis.Nil {
  1184. continue
  1185. } else {
  1186. global.GVA_LOG.Error("TaskMsgSendRetainedData获取缓存任务数据失败", zap.Error(err))
  1187. continue
  1188. }
  1189. }
  1190. var taskStatistics request.TaskStatistics
  1191. _ = json.Unmarshal([]byte(gameTask), &taskStatistics)
  1192. var name = taskStatistics.Remark
  1193. sendMsg[name] += "\n"
  1194. sendMsg[name] += taskStatistics.GameName
  1195. sendMsg[name] += ",付费差"
  1196. //sendMsg[name] += strconv.Itoa(data.AlsoTarget)
  1197. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", data.AlsoTarget)
  1198. if data.TimeRate > 60*60*24 {
  1199. lastPayCompletedUpdateTimeKey := fmt.Sprintf(LastPayCompletedUpdateTimeKey, date, data.TaskId)
  1200. _ = s.cache.SetCacheStr(ctx, lastPayCompletedUpdateTimeKey, ctime) //上次付费更新时间
  1201. data.TimeRate = 60 * 60 * 2
  1202. }
  1203. // 5分钟数据没动报异常数据
  1204. if data.TimeRate/60 >= 2 {
  1205. if data.Rate == 0 {
  1206. lastPayAddNumKey := fmt.Sprintf(LastPayAddNumKey, date, data.TaskId)
  1207. payErrAddNumKey := fmt.Sprintf(PayErrAddNumKey, date, data.TaskId)
  1208. numErr, _ := s.cache.GetCacheNum(context.Background(), payErrAddNumKey)
  1209. lastAddFeeNumErr, _ := s.cache.GetCacheNum(context.Background(), lastPayAddNumKey)
  1210. errMsgZ += "\n"
  1211. errMsgZ += taskStatistics.GameName
  1212. errMsgZ += "("
  1213. if data.TimeRate/60 >= 60 {
  1214. errMsgZ += strconv.Itoa(data.TimeRate / 60 / 60)
  1215. errMsgZ += "小时内(+" + strconv.Itoa(numErr) + "))"
  1216. } else {
  1217. errMsgZ += strconv.Itoa(data.TimeRate / 60)
  1218. errMsgZ += "分钟内(+" + strconv.Itoa(numErr) + "))"
  1219. }
  1220. errMsgZ += ","
  1221. errMsgZ += "最后加付费时间 "
  1222. t := time.Unix(int64(data.AddPayUpdateTime), 0)
  1223. errMsgZ += t.Format("15:04:05")
  1224. errMsgZ += "(+" + strconv.Itoa(lastAddFeeNumErr) + ")"
  1225. if _, ok := mpsPerson[name]; ok {
  1226. mobile = append(mobile, mpsPerson[name].MobilePhoneNumber)
  1227. }
  1228. }
  1229. }
  1230. sendMsg[name] += ","
  1231. if m >= 60 {
  1232. sendMsg[name] += strconv.Itoa(m / 60)
  1233. sendMsg[name] += "分钟内完成"
  1234. } else {
  1235. sendMsg[name] += strconv.Itoa(m)
  1236. sendMsg[name] += "分钟内完成"
  1237. }
  1238. //sendMsg[name] += strconv.Itoa(data.Rate)
  1239. sendMsg[name] += fmt.Sprintf("<font color=\"warning\">%d</font>", data.Rate)
  1240. }
  1241. if len(sendMsg) == 0 {
  1242. return
  1243. }
  1244. hour := time.Now().Hour()
  1245. for name, msg := range sendMsg {
  1246. retained += "\n"
  1247. retained += name
  1248. retained += msg
  1249. if hour >= mpsPerson[name].StartTime {
  1250. var personMsg = "# 付费播报 "
  1251. personMsg += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
  1252. personMsg = msg
  1253. url := mpsPerson[name].Url
  1254. var sendPersonData SendMsg
  1255. sendPersonData.MsgType = "markdown"
  1256. sendPersonData.Markdown.Content = personMsg
  1257. s.SendMsgData(url, sendPersonData)
  1258. if mpsPerson[name].Custodians != "" {
  1259. if _, ok := mpsPerson[mpsPerson[name].Custodians]; ok {
  1260. custodiansMsg := fmt.Sprintf("<font color=\"warning\">%s的任务</font>", name)
  1261. custodiansMsg += "\n"
  1262. custodiansMsg += personMsg
  1263. var custodiansData SendMsg
  1264. custodiansData.MsgType = "markdown"
  1265. custodiansData.Markdown.Content = custodiansMsg
  1266. urlCustodians := mpsPerson[mpsPerson[name].Custodians].Url
  1267. s.SendMsgData(urlCustodians, custodiansData)
  1268. }
  1269. }
  1270. }
  1271. }
  1272. msg := retained
  1273. if errMsgZ != "" {
  1274. msg += "\n"
  1275. //msg += errMsg + errMsgZ
  1276. msg += fmt.Sprintf("<font color=\"warning\">%s</font>", errMsg+errMsgZ)
  1277. }
  1278. global.GVA_LOG.Warn(msg)
  1279. _ = s.cache.SetCacheStr(ctx, lastFreeMsgSendTimeKey, time.Now().Unix())
  1280. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  1281. var sendData SendMsg
  1282. sendData.MsgType = "markdown"
  1283. sendData.Markdown.Content = msg
  1284. s.SendMsgData(url, sendData)
  1285. if len(mobile) != 0 {
  1286. var sendTextData SendTextMsg
  1287. sendTextData.MsgType = "text"
  1288. sendTextData.Text.MentionedMobileList = mobile
  1289. s.SendMsgData(url, sendTextData)
  1290. }
  1291. return
  1292. }