assist.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package assist
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/model/assist"
  5. account "go-common/app/service/main/account/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "strconv"
  9. )
  10. // Assists get assists
  11. func (s *Service) Assists(c context.Context, mid int64, ip string) (assists []*assist.Assist, err error) {
  12. var (
  13. mainAssists []*assist.Assist
  14. liveAssists []*assist.LiveAssist
  15. rightsMap = make(map[int64]*assist.Rights, 20)
  16. mainMap = make(map[int64]*assist.Assist, 10)
  17. assistMids []int64
  18. liveAssistMids []int64
  19. liveAssistsTotal map[int64]map[int8]map[int8]int
  20. )
  21. // main assists
  22. if mainAssists, err = s.assist.Assists(c, mid, ip); err != nil {
  23. log.Error("s.assist.Assists err(%v) | mid(%d), ip(%s)", err, mid, ip)
  24. return
  25. }
  26. // live assists
  27. if liveAssists, err = s.assist.LiveAssists(c, mid, ip); err != nil {
  28. log.Error("s.assist.Assists err(%v) | mid(%d), ip(%s)", err, mid, ip)
  29. return
  30. }
  31. // rights info
  32. for _, ass := range mainAssists {
  33. rightsMap[ass.AssistMid] = &assist.Rights{Main: 1}
  34. mainMap[ass.AssistMid] = ass
  35. }
  36. for _, ass := range liveAssists {
  37. if _, ok := rightsMap[ass.AssistMid]; !ok {
  38. rightsMap[ass.AssistMid] = &assist.Rights{Live: 1}
  39. } else {
  40. rightsMap[ass.AssistMid].Live = 1
  41. }
  42. liveAssistMids = append(liveAssistMids, ass.AssistMid)
  43. }
  44. // get live assist total
  45. if len(liveAssistMids) > 0 {
  46. liveAssistsTotal, _ = s.assist.Stat(c, mid, liveAssistMids, ip)
  47. }
  48. // get user info
  49. for mid := range rightsMap {
  50. assistMids = append(assistMids, mid)
  51. }
  52. users, err := s.acc.Infos(c, assistMids, ip)
  53. if err != nil {
  54. log.Error("s.acc.Infos() err(%v)", err)
  55. return
  56. }
  57. for _, ast := range mainAssists {
  58. if _, ok := users[ast.AssistMid]; ok {
  59. ast.AssistAvatar = users[ast.AssistMid].Face
  60. ast.AssistName = users[ast.AssistMid].Name
  61. ast.Banned, _ = s.userBanned(c, ast.AssistMid)
  62. ast.Rights = rightsMap[ast.AssistMid]
  63. assists = append(assists, ast)
  64. }
  65. }
  66. for _, ass := range liveAssists {
  67. if _, ok := mainMap[ass.AssistMid]; !ok {
  68. if _, ok := users[ass.AssistMid]; ok {
  69. ast := &assist.Assist{}
  70. ast.AssistMid = ass.AssistMid
  71. ast.AssistAvatar = users[ass.AssistMid].Face
  72. ast.AssistName = users[ass.AssistMid].Name
  73. ast.CTime = ass.CTime
  74. ast.MTime = ass.CTime
  75. ast.Banned, _ = s.userBanned(c, ass.AssistMid)
  76. ast.Rights = rightsMap[ass.AssistMid]
  77. if _, ok := liveAssistsTotal[ass.AssistMid]; ok {
  78. ast.Total = liveAssistsTotal[ast.AssistMid]
  79. }
  80. assists = append(assists, ast)
  81. }
  82. }
  83. }
  84. return
  85. }
  86. // AssistLogs get all assistlog by Mid, and format by typeid
  87. func (s *Service) AssistLogs(c context.Context, mid, assistMid, pn, ps, stime, etime int64, ip string) (assistLogs []*assist.AssistLog, pager map[string]int64, err error) {
  88. if assistLogs, pager, err = s.assist.AssistLogs(c, mid, assistMid, pn, ps, stime, etime, ip); err != nil {
  89. log.Error("s.creative.Assist err(%v) | mid(%d),assistMid(%d),pn(%d),ps(%d),stime(%d),etime(%d),ip(%s)",
  90. err, mid, assistMid, pn, ps, stime, etime, ip)
  91. return
  92. }
  93. var assistMids []int64
  94. for _, ass := range assistLogs {
  95. assistMids = append(assistMids, ass.AssistMid)
  96. }
  97. users, err := s.acc.Infos(c, assistMids, ip)
  98. if err != nil {
  99. log.Error("s.acc.Profiles() err(%v)", err)
  100. return
  101. }
  102. for _, assist := range assistLogs {
  103. if _, ok := users[assist.AssistMid]; ok {
  104. assist.AssistAvatar = users[assist.AssistMid].Face
  105. assist.AssistName = users[assist.AssistMid].Name
  106. }
  107. }
  108. return
  109. }
  110. // AddAssist add mid to any assist
  111. func (s *Service) AddAssist(c context.Context, mid, assistMid int64, main, live int8, ip, ak, ck string) (err error) {
  112. if main == 1 {
  113. if err = s.addAssist(c, mid, assistMid, ip, ak, ck); err != nil {
  114. return
  115. }
  116. }
  117. if live == 1 {
  118. if err = s.liveAddAssist(c, mid, assistMid, ak, ck, ip); err != nil {
  119. return
  120. }
  121. }
  122. return
  123. }
  124. func (s *Service) addAssist(c context.Context, mid, assistMid int64, ip, ak, ck string) (err error) {
  125. var (
  126. card *account.Card
  127. )
  128. if card, err = s.acc.Card(c, mid, ip); err != nil {
  129. log.Error("s.assist.AddAssist err(%v) | mid(%d), assistMid(%d), ip(%s)", err, mid, assistMid, ip)
  130. return
  131. }
  132. if err = s.assist.AddAssist(c, mid, assistMid, ip, card.Name); err != nil {
  133. log.Error("s.assist.AddAssist err(%v) | mid(%d), assistMid(%d), ip(%s)", err, mid, assistMid, ip)
  134. return
  135. }
  136. return
  137. }
  138. // DelAssist delete all the assist
  139. func (s *Service) DelAssist(c context.Context, mid, assistMid int64, ip, ak, ck string) (err error) {
  140. isMainAss, _ := s.assist.Info(c, mid, assistMid, ip)
  141. if isMainAss == 1 {
  142. err = s.delAssist(c, mid, assistMid, ip, ak, ck)
  143. }
  144. isLiveAss, _ := s.LiveCheckAssist(c, mid, assistMid, ip)
  145. if isLiveAss == 1 {
  146. err = s.liveDelAssist(c, mid, assistMid, ck, ip)
  147. }
  148. return
  149. }
  150. // DelAssist delete the assist
  151. func (s *Service) delAssist(c context.Context, mid, assistMid int64, ip, ak, ck string) (err error) {
  152. var (
  153. card *account.Card
  154. )
  155. if card, err = s.acc.Card(c, mid, ip); err != nil {
  156. log.Error("s.assist.AddAssist err(%v) | mid(%d), assistMid(%d), ip(%s)", err, mid, assistMid, ip)
  157. return
  158. }
  159. if err = s.assist.DelAssist(c, mid, assistMid, ip, card.Name); err != nil {
  160. log.Error("s.assist.DelAssist err(%v) | mid(%d), assistMid(%d), ip(%s)", err, mid, assistMid, ip)
  161. return
  162. }
  163. return
  164. }
  165. // prepare for Live revoc log
  166. func (s *Service) preRevocLogForLive(c context.Context, assistLog *assist.AssistLog) (err error) {
  167. if assistLog.Action == 8 && assistLog.Type == 3 {
  168. var (
  169. objID int64
  170. revcLog = &assist.AssistLog{}
  171. )
  172. if objID, err = strconv.ParseInt(assistLog.ObjectID, 10, 64); err != nil {
  173. log.Error("Atoi err(%v) | mid(%d), assistMid(%d), assistLog.ObjectID(%s)", err, assistLog.Mid, assistLog.AssistMid, assistLog.ObjectID)
  174. return
  175. }
  176. revcLog, err = s.assist.AssistLogObj(c, assistLog.Type, 9, assistLog.Mid, objID)
  177. if err == ecode.AssistLogNotExist {
  178. return nil
  179. }
  180. if err != nil {
  181. log.Error("s.assist.AssistLogObj err(%v) | mid(%d), assistMid(%d), logID(%d)", err, assistLog.Mid, assistLog.AssistMid, assistLog.ID)
  182. return
  183. }
  184. if revcLog != nil {
  185. err = ecode.CreativeAssistLogAlreadyRevoc
  186. return
  187. }
  188. return
  189. }
  190. return
  191. }
  192. // RevocAssistLog cancel this asssist action
  193. func (s *Service) RevocAssistLog(c context.Context, mid, assistMid, logID int64, ck, ip string) (err error) {
  194. var assistLog *assist.AssistLog
  195. if assistLog, err = s.assist.AssistLog(c, mid, assistMid, logID, ip); err != nil {
  196. log.Error("s.assist.AssistLog err(%v) | mid(%d), assistMid(%d), logID(%d), ip(%s)", err, mid, assistMid, logID, ip)
  197. return
  198. }
  199. if err = s.preRevocLogForLive(c, assistLog); err != nil {
  200. log.Error("s.preRevocLogForLive err(%v) | mid(%d), assistMid(%d), logID(%d), ip(%s)", err, mid, assistMid, logID, ip)
  201. return
  202. }
  203. if err = s.revoc(c, assistLog, ck, ip); err != nil {
  204. log.Error("s.revoc err(%v) | assistLog(%+v), ip(%s)", err, assistLog, ip)
  205. return
  206. }
  207. if err = s.assist.RevocAssistLog(c, mid, assistMid, logID, ip); err != nil {
  208. log.Error("s.assist.CancelAssistLog err(%v) | mid(%d), assistMid(%d), logID(%d), ip(%s)", err, mid, assistMid, logID, ip)
  209. return
  210. }
  211. return
  212. }
  213. func (s *Service) userBanned(c context.Context, mid int64) (banned int8, err error) {
  214. var card *account.Card
  215. if card, err = s.acc.Card(c, mid, ""); err != nil {
  216. err = nil
  217. return
  218. }
  219. if card.Silence == 1 {
  220. banned = 1
  221. return
  222. }
  223. return
  224. }
  225. // SetAssist get user assist rights about liveRoom
  226. func (s *Service) SetAssist(c context.Context, mid, assistMid int64, main, live int8, ip, ak, ck string) (err error) {
  227. isMainAss, _ := s.assist.Info(c, mid, assistMid, ip)
  228. if isMainAss == 0 && main == 1 {
  229. err = s.addAssist(c, mid, assistMid, ip, ak, ck)
  230. } else if isMainAss == 1 && main == 0 {
  231. err = s.delAssist(c, mid, assistMid, ip, ak, ck)
  232. }
  233. if err != nil {
  234. return
  235. }
  236. isLiveAss, _ := s.LiveCheckAssist(c, mid, assistMid, ip)
  237. if live == 1 && isLiveAss == 0 {
  238. err = s.liveAddAssist(c, mid, assistMid, ak, ck, ip)
  239. } else if live == 0 && isLiveAss == 1 {
  240. err = s.liveDelAssist(c, mid, assistMid, ck, ip)
  241. }
  242. return
  243. }