service.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package newcomer
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/creative/conf"
  6. "go-common/app/interface/main/creative/dao/academy"
  7. "go-common/app/interface/main/creative/dao/account"
  8. "go-common/app/interface/main/creative/dao/archive"
  9. "go-common/app/interface/main/creative/dao/article"
  10. "go-common/app/interface/main/creative/dao/data"
  11. "go-common/app/interface/main/creative/dao/medal"
  12. "go-common/app/interface/main/creative/dao/newcomer"
  13. "go-common/app/interface/main/creative/dao/order"
  14. "go-common/app/interface/main/creative/dao/watermark"
  15. ncMDL "go-common/app/interface/main/creative/model/newcomer"
  16. "go-common/app/interface/main/creative/service"
  17. "go-common/library/conf/env"
  18. "go-common/library/log"
  19. "go-common/library/net/metadata"
  20. "go-common/library/queue/databus/report"
  21. "sync"
  22. )
  23. //Service struct
  24. type Service struct {
  25. c *conf.Config
  26. newc *newcomer.Dao
  27. arc *archive.Dao
  28. art *article.Dao
  29. acc *account.Dao
  30. data *data.Dao
  31. aca *academy.Dao
  32. wm *watermark.Dao
  33. medal *medal.Dao
  34. order *order.Dao
  35. //reward
  36. RewardCache []*ncMDL.Reward
  37. RewardMapCache map[int64]*ncMDL.Reward
  38. RewardTyPIDMapCache map[int8]int64 //存储奖励 类型-分类id对应关系
  39. RewardPIDTyMapCache map[int64]int8 //存储奖励 分类id-类型对应关系
  40. //task
  41. TaskCache []*ncMDL.Task
  42. TaskMapCache map[int64]*ncMDL.Task
  43. TaskTypeMapCache map[int8][]*ncMDL.Task
  44. TaskRewardMapCache map[int64][]*ncMDL.TaskRewardEntity
  45. //taskgroup-reward
  46. TaskGroupRewardMapCache map[int64][]*ncMDL.TaskGroupReward
  47. //gift-reward
  48. GiftRewardMapCache map[int8][]*ncMDL.GiftReward
  49. //initiative check task
  50. checkTaskChan chan int64
  51. checkTaskQueue []int64
  52. checkTaskDone chan struct{}
  53. // task-group
  54. TaskGroupCache []*ncMDL.TaskGroupEntity
  55. TaskGroupMapCache map[int64]*ncMDL.TaskGroupEntity
  56. }
  57. //New get service
  58. func New(c *conf.Config, rpcdaos *service.RPCDaos) *Service {
  59. s := &Service{
  60. c: c,
  61. newc: newcomer.New(c),
  62. arc: rpcdaos.Arc,
  63. art: rpcdaos.Art,
  64. acc: rpcdaos.Acc,
  65. data: data.New(c),
  66. aca: academy.New(c),
  67. wm: watermark.New(c),
  68. medal: medal.New(c),
  69. order: order.New(c),
  70. checkTaskChan: make(chan int64, 1000),
  71. checkTaskQueue: make([]int64, 0, 1000),
  72. checkTaskDone: make(chan struct{}),
  73. }
  74. s.loadRewards()
  75. s.loadTasks()
  76. s.loadTaskGroupRewards()
  77. s.loadGiftRewards()
  78. s.loadTaskRewards()
  79. s.loadTaskGroups()
  80. go s.loadProc()
  81. go s.checkTaskStateByMid()
  82. return s
  83. }
  84. // Ping service
  85. func (s *Service) Ping(c context.Context) (err error) {
  86. if err = s.newc.Ping(c); err != nil {
  87. log.Error("s.newc.Ping err(%v)", err)
  88. }
  89. return
  90. }
  91. // Close dao
  92. func (s *Service) Close() {
  93. s.newc.Close()
  94. s.checkTaskDone <- struct{}{}
  95. }
  96. // loadproc
  97. func (s *Service) loadProc() {
  98. for {
  99. time.Sleep(3 * time.Minute)
  100. s.loadRewards()
  101. s.loadTasks()
  102. s.loadTaskGroupRewards()
  103. s.loadGiftRewards()
  104. s.loadTaskRewards()
  105. s.loadTaskGroups()
  106. }
  107. }
  108. //load tags
  109. func (s *Service) loadRewards() {
  110. res, err := s.newc.Rewards(context.Background())
  111. if err != nil {
  112. log.Error("s.newc.Rewards error(%v)", err)
  113. return
  114. }
  115. if len(res) == 0 {
  116. return
  117. }
  118. s.RewardCache = res
  119. tempRewardMapCache := make(map[int64]*ncMDL.Reward)
  120. tempRewardTyPIDMapCache := make(map[int8]int64)
  121. tempRewardPIDTyMapCache := make(map[int64]int8)
  122. for _, v := range res {
  123. tempRewardMapCache[v.ID] = v
  124. if v.ParentID == 0 {
  125. tempRewardTyPIDMapCache[v.Type] = v.ID
  126. tempRewardPIDTyMapCache[v.ID] = v.Type
  127. }
  128. }
  129. s.RewardMapCache = tempRewardMapCache
  130. s.RewardTyPIDMapCache = tempRewardTyPIDMapCache
  131. s.RewardPIDTyMapCache = tempRewardPIDTyMapCache
  132. }
  133. //load tags
  134. func (s *Service) loadTasks() {
  135. res, err := s.newc.Tasks(context.Background())
  136. if err != nil {
  137. log.Error("s.newc.Tasks error(%v)", err)
  138. return
  139. }
  140. if len(res) == 0 {
  141. return
  142. }
  143. s.TaskCache = res
  144. temp := make(map[int64]*ncMDL.Task)
  145. tempMap := make(map[int8][]*ncMDL.Task)
  146. for _, v := range res {
  147. temp[v.ID] = v
  148. switch v.Type {
  149. case ncMDL.NewcomerTaskType:
  150. tempMap[ncMDL.NewcomerTaskType] = append(tempMap[ncMDL.NewcomerTaskType], v)
  151. case ncMDL.AdvancedTaskType:
  152. tempMap[ncMDL.AdvancedTaskType] = append(tempMap[ncMDL.AdvancedTaskType], v)
  153. case ncMDL.MonthTaskType:
  154. tempMap[ncMDL.MonthTaskType] = append(tempMap[ncMDL.MonthTaskType], v)
  155. }
  156. }
  157. s.TaskMapCache = temp
  158. // 默认分类包含:新手与进阶任务
  159. tempMap[ncMDL.DefualtTaskType] = append(tempMap[ncMDL.DefualtTaskType], tempMap[ncMDL.NewcomerTaskType]...)
  160. tempMap[ncMDL.DefualtTaskType] = append(tempMap[ncMDL.DefualtTaskType], tempMap[ncMDL.AdvancedTaskType]...)
  161. s.TaskTypeMapCache = tempMap
  162. }
  163. // SendRewardReceiveLog for reward receive.
  164. func (s *Service) SendRewardReceiveLog(c context.Context, mid int64) (err error) {
  165. if env.DeployEnv == env.DeployEnvDev {
  166. return
  167. }
  168. uInfo := &report.UserInfo{
  169. Business: 141, //创作中心奖励领取行为日志业务ID
  170. Mid: mid,
  171. Type: 0, //0-激励计划奖品领取
  172. Oid: mid,
  173. Platform: "激励计划",
  174. Ctime: time.Now(),
  175. Action: "incentive_plan_reward_receive", //激励计划奖品领取
  176. Index: []interface{}{mid},
  177. IP: metadata.String(c, metadata.RemoteIP),
  178. }
  179. report.User(uInfo)
  180. log.Info("s.SendRewardReceiveLog mid(%d)|uInfo(%+v)", mid, uInfo)
  181. return
  182. }
  183. // SendPendantReceiveLog for reward receive.
  184. func (s *Service) SendPendantReceiveLog(c context.Context, mid int64) (err error) {
  185. if env.DeployEnv == env.DeployEnvDev {
  186. return
  187. }
  188. uInfo := &report.UserInfo{
  189. Business: 141, //创作中心奖励领取行为日志业务ID
  190. Mid: mid,
  191. Type: 1, //1-头像挂件奖品领取
  192. Oid: mid,
  193. Platform: "头像挂件",
  194. Ctime: time.Now(),
  195. Action: "pendant_reward_receive", //头像挂件奖品领取
  196. Index: []interface{}{mid},
  197. IP: metadata.String(c, metadata.RemoteIP),
  198. }
  199. report.User(uInfo)
  200. log.Info("s.SendPendantReceiveLog mid(%d)|uInfo(%+v)", mid, uInfo)
  201. return
  202. }
  203. //load gift-reward
  204. func (s *Service) loadGiftRewards() {
  205. res, err := s.newc.AllGiftRewards(context.Background())
  206. if err != nil {
  207. log.Error("s.newc.AllGiftRewards error(%v)", err)
  208. return
  209. }
  210. if len(res) == 0 {
  211. return
  212. }
  213. s.GiftRewardMapCache = res
  214. }
  215. //load taskgroup-reward
  216. func (s *Service) loadTaskGroupRewards() {
  217. res, err := s.newc.AllTaskGroupRewards(context.Background())
  218. if err != nil {
  219. log.Error("s.newc.AllTaskGroupRewards error(%v)", err)
  220. return
  221. }
  222. if len(res) == 0 {
  223. return
  224. }
  225. s.TaskGroupRewardMapCache = res
  226. }
  227. // checkTaskStateByMid read bindMid to check task state
  228. func (s *Service) checkTaskStateByMid() {
  229. ticker := time.NewTicker(time.Millisecond * 100)
  230. defer ticker.Stop()
  231. for {
  232. select {
  233. case id := <-s.checkTaskChan:
  234. s.checkTaskQueue = append(s.checkTaskQueue, id)
  235. case <-ticker.C:
  236. if len(s.checkTaskQueue) > 0 {
  237. mid := s.checkTaskQueue[0]
  238. s.checkTaskQueue = s.checkTaskQueue[1:]
  239. s.DriveStateByUser(context.Background(), mid)
  240. }
  241. case <-s.checkTaskDone:
  242. log.Info("checkTaskStateByMid close")
  243. return
  244. }
  245. }
  246. }
  247. // putCheckTask put mid to checkTaskQueue
  248. func (s *Service) putCheckTaskState(mid int64) {
  249. var wg sync.WaitGroup
  250. wg.Add(1)
  251. go func() {
  252. s.checkTaskChan <- mid
  253. wg.Done()
  254. }()
  255. wg.Wait()
  256. }
  257. //load task-group
  258. func (s *Service) loadTaskGroups() {
  259. res, err := s.newc.TaskGroups(context.Background())
  260. if err != nil {
  261. log.Error("s.newc.TaskGroups error(%v)", err)
  262. return
  263. }
  264. if len(res) == 0 {
  265. return
  266. }
  267. s.TaskGroupCache = res
  268. temp := make(map[int64]*ncMDL.TaskGroupEntity)
  269. for _, v := range res {
  270. temp[v.ID] = v
  271. }
  272. s.TaskGroupMapCache = temp
  273. }
  274. //load task-group
  275. func (s *Service) loadTaskRewards() {
  276. res, err := s.newc.TaskRewards(context.Background())
  277. if err != nil {
  278. log.Error("s.newc.TaskRewards error(%v)", err)
  279. return
  280. }
  281. if len(res) == 0 {
  282. return
  283. }
  284. s.TaskRewardMapCache = res
  285. }
  286. // getRewardsByTaskType get rewards by taskType
  287. func (s *Service) getRewardsByTaskType(taskType int8) (res []*ncMDL.Reward) {
  288. res = make([]*ncMDL.Reward, 0)
  289. gifts, ok := s.GiftRewardMapCache[taskType]
  290. if !ok || len(gifts) == 0 {
  291. return
  292. }
  293. for _, v := range gifts {
  294. if v == nil {
  295. continue
  296. }
  297. r, ok := s.RewardMapCache[v.RewardID]
  298. if !ok {
  299. continue
  300. }
  301. res = append(res, r)
  302. }
  303. return
  304. }
  305. // getRewardsByGroupID get reward by groupID
  306. func (s *Service) getRewardsByGroupID(groupID int64) []*ncMDL.Reward {
  307. rewards := make([]*ncMDL.Reward, 0)
  308. rs, ok := s.TaskGroupRewardMapCache[groupID]
  309. if !ok || len(rs) == 0 {
  310. log.Error("getRewardsByGroupID reward not exist groupID(%d)", groupID)
  311. return rewards
  312. }
  313. for _, r := range rs {
  314. if r == nil {
  315. continue
  316. }
  317. if rr, ok := s.RewardMapCache[r.RewardID]; ok {
  318. rewards = append(rewards, rr)
  319. }
  320. }
  321. return rewards
  322. }
  323. // getTaskByGroupID get tasks by groupID
  324. func (s *Service) getTasksByGroupID(groupID int64) (res []*ncMDL.Task) {
  325. res = make([]*ncMDL.Task, 0)
  326. for _, task := range s.TaskCache {
  327. if task == nil {
  328. continue
  329. }
  330. if _, ok := s.TaskGroupMapCache[task.GroupID]; !ok {
  331. continue
  332. }
  333. if task.GroupID == groupID && task.State == ncMDL.NormalState {
  334. res = append(res, task)
  335. }
  336. }
  337. return
  338. }
  339. // getTaskByType get tasks by type
  340. func (s *Service) getTasksByType(ty int8) (res []*ncMDL.Task) {
  341. res = make([]*ncMDL.Task, 0)
  342. for _, task := range s.TaskCache {
  343. if task == nil {
  344. continue
  345. }
  346. if _, ok := s.TaskGroupMapCache[task.GroupID]; !ok {
  347. continue
  348. }
  349. if task.Type == ty && task.State == ncMDL.NormalState {
  350. res = append(res, task)
  351. }
  352. }
  353. return
  354. }
  355. // getTasksInfo get tasks info by type
  356. func (s *Service) getTasksInfoByType(ts []*ncMDL.Task, ty int8) (res []*ncMDL.Task) {
  357. tsMap := make(map[int64]*ncMDL.Task)
  358. for _, t := range ts {
  359. if t == nil {
  360. continue
  361. }
  362. tsMap[t.ID] = t
  363. }
  364. tasks, ok := s.TaskTypeMapCache[ty]
  365. if !ok || len(tasks) == 0 {
  366. return
  367. }
  368. res = make([]*ncMDL.Task, 0, len(tasks))
  369. for _, v := range tasks {
  370. if v == nil || v.State != 0 { //-1-删除;0-正常;1-隐藏
  371. continue
  372. }
  373. if _, ok := s.TaskGroupMapCache[v.GroupID]; !ok { // check taskGroup
  374. continue
  375. }
  376. task := *v
  377. t, ok := tsMap[v.ID]
  378. if !ok {
  379. task.CompleteSate = ncMDL.TaskIncomplete
  380. } else {
  381. task.CompleteSate = t.CompleteSate
  382. }
  383. res = append(res, &task)
  384. }
  385. return
  386. }
  387. // isHiddenTaskType check taskType is hidden
  388. func (s *Service) isHiddenTaskType(ty int8) bool {
  389. tasks, ok := s.TaskTypeMapCache[ty]
  390. if !ok || len(tasks) == 0 {
  391. return true
  392. }
  393. for _, t := range tasks {
  394. if t == nil {
  395. continue
  396. }
  397. _, ok := s.TaskGroupMapCache[t.GroupID]
  398. if !ok {
  399. continue
  400. }
  401. if t.State == 0 {
  402. return false
  403. }
  404. }
  405. return true
  406. }