block.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package block
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. model "go-common/app/admin/main/member/model/block"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. "github.com/pkg/errors"
  11. )
  12. // Search .
  13. func (s *Service) Search(c context.Context, mids []int64) (infos []*model.BlockInfo, err error) {
  14. var (
  15. users []*model.DBUser
  16. userDetails []*model.DBUserDetail
  17. eg errgroup.Group
  18. mapMu sync.Mutex
  19. userMap = make(map[int64]*model.BlockInfo)
  20. )
  21. if users, err = s.dao.Users(c, mids); err != nil {
  22. return
  23. }
  24. if userDetails, err = s.dao.UserDetails(c, mids); err != nil {
  25. return
  26. }
  27. infos = make([]*model.BlockInfo, 0, len(mids))
  28. for _, m := range mids {
  29. mid := m
  30. eg.Go(func() (err error) {
  31. info := &model.BlockInfo{
  32. MID: mid,
  33. }
  34. // 1. account 数据
  35. if info.Nickname, info.TelStatus, info.Level, info.RegTime, err = s.AccountInfo(context.Background(), mid); err != nil {
  36. log.Error("%+v", err)
  37. err = nil
  38. }
  39. if info.Nickname == "" {
  40. log.Info("user mid(%d) not found", info.MID)
  41. return
  42. }
  43. // 2. 封禁状态
  44. for i := range users {
  45. if users[i].MID == mid {
  46. info.ParseStatus(users[i])
  47. break
  48. }
  49. }
  50. // 3. 封禁次数
  51. for i := range userDetails {
  52. if userDetails[i].MID == mid {
  53. info.BlockCount = userDetails[i].BlockCount
  54. break
  55. }
  56. }
  57. // 4. spy 分值
  58. if info.SpyScore, err = s.SpyScore(context.Background(), mid); err != nil {
  59. log.Error("%+v", err)
  60. err = nil
  61. info.SpyScore = -1
  62. }
  63. // 5. figure 排名
  64. if info.FigureRank, err = s.FigureRank(context.Background(), mid); err != nil {
  65. log.Error("%+v", err)
  66. err = nil
  67. info.FigureRank = -1
  68. }
  69. // 6. extra 额外账号信息
  70. if info.Tel, err = s.telInfo(context.Background(), mid); err != nil {
  71. log.Error("%+v", err)
  72. err = nil
  73. info.Tel = "N/A"
  74. }
  75. if info.Mail, err = s.mailInfo(context.Background(), mid); err != nil {
  76. log.Error("%+v", err)
  77. err = nil
  78. info.Mail = "N/A"
  79. }
  80. if info.Username, err = s.userID(context.Background(), mid); err != nil {
  81. log.Error("%+v", err)
  82. err = nil
  83. info.Username = "N/A"
  84. }
  85. mapMu.Lock()
  86. userMap[info.MID] = info
  87. mapMu.Unlock()
  88. return
  89. })
  90. }
  91. eg.Wait()
  92. for _, mid := range mids {
  93. if info, ok := userMap[mid]; ok {
  94. infos = append(infos, info)
  95. }
  96. }
  97. return
  98. }
  99. // History .
  100. func (s *Service) History(c context.Context, mid int64, ps, pn int, desc bool) (history *model.BlockDetail, err error) {
  101. var (
  102. start = (pn - 1) * ps
  103. limit = ps
  104. dbHistory []*model.DBHistory
  105. dbUser *model.DBUser
  106. )
  107. history = &model.BlockDetail{}
  108. if dbUser, err = s.dao.User(c, mid); err != nil {
  109. return
  110. }
  111. if dbUser != nil {
  112. history.Status = dbUser.Status
  113. }
  114. if history.Total, err = s.dao.HistoryCount(c, mid); err != nil {
  115. return
  116. }
  117. if dbHistory, err = s.dao.History(c, mid, start, limit, desc); err != nil {
  118. return
  119. }
  120. history.History = make([]*model.BlockHistory, 0, len(dbHistory))
  121. for i := range dbHistory {
  122. his := &model.BlockHistory{}
  123. his.ParseDB(dbHistory[i])
  124. history.History = append(history.History, his)
  125. }
  126. return
  127. }
  128. // BatchBlock .
  129. func (s *Service) BatchBlock(c context.Context, p *model.ParamBatchBlock) (err error) {
  130. var (
  131. tx *xsql.Tx
  132. duration = time.Duration(p.Duration) * time.Hour * 24
  133. source model.BlockSource
  134. stime = time.Now()
  135. )
  136. if tx, err = s.dao.BeginTX(c); err != nil {
  137. return
  138. }
  139. if p.Source == model.BlockMgrSourceSys {
  140. // 系统封禁
  141. source = model.BlockSourceSys
  142. } else if p.Source == model.BlockMgrSourceCredit {
  143. // 小黑屋封禁
  144. source = model.BlockSourceBlackHouse
  145. if err = s.dao.BlackhouseBlock(context.Background(), p); err != nil {
  146. return
  147. }
  148. }
  149. for _, mid := range p.MIDs {
  150. if err = s.action(c, tx, mid, p.AdminID, p.AdminName, source, p.Area, p.Reason, p.Comment, p.Action, duration, p.Notify, stime); err != nil {
  151. tx.Rollback()
  152. return
  153. }
  154. }
  155. if err = tx.Commit(); err != nil {
  156. return
  157. }
  158. // 发送站内信
  159. if p.Notify {
  160. s.mission(func() {
  161. if notifyErr := s.notifyMSG(context.Background(), p.MIDs, source, p.Action, p.Area, p.Reason, p.Duration); notifyErr != nil {
  162. log.Error("%+v", notifyErr)
  163. return
  164. }
  165. })
  166. }
  167. s.mission(func() {
  168. s.AddAuditLog(context.Background(), p.Action, p.AdminID, p.AdminName, p.MIDs, duration, source, p.Area, p.Reason, p.Comment, p.Notify, stime)
  169. })
  170. s.asyncPurgeCache(p.MIDs)
  171. return
  172. }
  173. // BatchRemove .
  174. func (s *Service) BatchRemove(c context.Context, p *model.ParamBatchRemove) (err error) {
  175. var (
  176. tx *xsql.Tx
  177. stime = time.Now()
  178. )
  179. if tx, err = s.dao.BeginTX(c); err != nil {
  180. return
  181. }
  182. for _, mid := range p.MIDs {
  183. if err = s.action(c, tx, mid, p.AdminID, p.AdminName, model.BlockSourceManager, model.BlockAreaNone, "", p.Comment, model.BlockActionAdminRemove, 0, p.Notify, stime); err != nil {
  184. tx.Rollback()
  185. return
  186. }
  187. }
  188. if err = tx.Commit(); err != nil {
  189. return
  190. }
  191. // 发送站内信
  192. if p.Notify {
  193. s.mission(func() {
  194. if notifyErr := s.notifyMSG(context.Background(), p.MIDs, model.BlockSourceManager, model.BlockActionAdminRemove, model.BlockAreaNone, "", 0); notifyErr != nil {
  195. log.Error("%+v", notifyErr)
  196. return
  197. }
  198. })
  199. }
  200. s.mission(func() {
  201. s.AddAuditLog(context.Background(), model.BlockActionAdminRemove, p.AdminID, p.AdminName, p.MIDs, 0, model.BlockSourceManager, model.BlockAreaNone, "", p.Comment, p.Notify, stime)
  202. })
  203. s.asyncPurgeCache(p.MIDs)
  204. return
  205. }
  206. // notifyMSG .
  207. func (s *Service) notifyMSG(c context.Context, mids []int64, source model.BlockSource, action model.BlockAction, area model.BlockArea, reason string, days int64) (err error) {
  208. code, title, content := s.MSGInfo(source, action, area, reason, days)
  209. log.Info("block admin title : %s , content : %s , mids : %+v", title, content, mids)
  210. if err = s.dao.SendSysMsg(context.Background(), code, mids, title, content, ""); err != nil {
  211. return
  212. }
  213. return
  214. }
  215. func (s *Service) action(c context.Context, tx *xsql.Tx, mid int64, adminID int64, adminName string, source model.BlockSource, area model.BlockArea, reason, comment string, action model.BlockAction, duration time.Duration, notify bool, stime time.Time) (err error) {
  216. var (
  217. db = &model.DBHistory{
  218. MID: mid,
  219. AdminID: adminID,
  220. AdminName: adminName,
  221. Source: source,
  222. Area: area,
  223. Reason: reason,
  224. Comment: comment,
  225. Action: action,
  226. StartTime: stime,
  227. Duration: int64(duration / time.Second),
  228. Notify: notify,
  229. }
  230. blockStatus model.BlockStatus
  231. )
  232. if err = s.dao.TxInsertHistory(c, tx, db); err != nil {
  233. return
  234. }
  235. switch action {
  236. case model.BlockActionAdminRemove, model.BlockActionSelfRemove:
  237. blockStatus = model.BlockStatusFalse
  238. case model.BlockActionLimit:
  239. switch source {
  240. case model.BlockSourceBlackHouse:
  241. blockStatus = model.BlockStatusCredit
  242. default:
  243. blockStatus = model.BlockStatusLimit
  244. }
  245. s.mission(func() {
  246. if err = s.dao.UpdateAddBlockCount(context.Background(), mid); err != nil {
  247. log.Error("%+v", err)
  248. }
  249. })
  250. case model.BlockActionForever:
  251. blockStatus = model.BlockStatusForever
  252. s.mission(func() {
  253. if err = s.dao.UpdateAddBlockCount(context.Background(), mid); err != nil {
  254. log.Error("%+v", err)
  255. }
  256. })
  257. default:
  258. err = errors.Errorf("unknown block action [%d]", action)
  259. return
  260. }
  261. if err = s.dao.TxUpdateUser(c, tx, mid, blockStatus); err != nil {
  262. return
  263. }
  264. return
  265. }
  266. func (s *Service) userID(c context.Context, mid int64) (id string, err error) {
  267. return "N/A", nil
  268. }
  269. func (s *Service) mailInfo(c context.Context, mid int64) (mail string, err error) {
  270. if mail, err = s.dao.MailInfo(c, mid); mail == "" {
  271. mail = "N/A"
  272. }
  273. return
  274. }
  275. // TelInfo .
  276. func (s *Service) telInfo(c context.Context, mid int64) (tel string, err error) {
  277. if tel, err = s.dao.TelInfo(c, mid); err != nil {
  278. return
  279. }
  280. if len(tel) == 0 {
  281. tel = "N/A"
  282. return
  283. }
  284. if len(tel) < 4 {
  285. tel = tel[:1] + "****"
  286. return
  287. }
  288. tel = tel[:3] + "****" + tel[len(tel)-4:]
  289. return
  290. }
  291. func (s *Service) asyncPurgeCache(mids []int64) {
  292. _ = s.cache.Do(context.Background(), func(ctx context.Context) {
  293. for _, mid := range mids {
  294. if cacheErr := s.dao.DeleteUserCache(ctx, mid); cacheErr != nil {
  295. log.Error("%+v", cacheErr)
  296. }
  297. if cacheErr := s.dao.DeleteUserDetailCache(ctx, mid); cacheErr != nil {
  298. log.Error("%+v", cacheErr)
  299. }
  300. if databusErr := s.accountNotify(ctx, mid); databusErr != nil {
  301. log.Error("%+v", databusErr)
  302. }
  303. }
  304. })
  305. }