block.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "go-common/app/admin/main/block/model"
  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.dao.AccountInfo(context.TODO(), 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.dao.SpyScore(context.TODO(), 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.dao.FigureRank(context.TODO(), 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.TODO(), 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.TODO(), 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.TODO(), 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) (status model.BlockStatus, total int, history []*model.BlockHistory, err error) {
  101. var (
  102. start = (pn - 1) * ps
  103. limit = ps
  104. dbHistory []*model.DBHistory
  105. dbUser *model.DBUser
  106. )
  107. if dbUser, err = s.dao.User(c, mid); err != nil {
  108. return
  109. }
  110. if dbUser != nil {
  111. status = dbUser.Status
  112. }
  113. if total, err = s.dao.HistoryCount(c, mid); err != nil {
  114. return
  115. }
  116. if dbHistory, err = s.dao.History(c, mid, start, limit); err != nil {
  117. return
  118. }
  119. for i := range dbHistory {
  120. his := &model.BlockHistory{}
  121. his.ParseDB(dbHistory[i])
  122. history = append(history, his)
  123. }
  124. return
  125. }
  126. // BatchBlock .
  127. func (s *Service) BatchBlock(c context.Context, p *model.ParamBatchBlock) (err error) {
  128. var (
  129. tx *xsql.Tx
  130. duration = time.Duration(p.Duration) * time.Hour * 24
  131. source model.BlockSource
  132. mids []int64
  133. stime = time.Now()
  134. )
  135. if tx, err = s.dao.BeginTX(c); err != nil {
  136. return
  137. }
  138. if p.Source == 1 {
  139. // 系统封禁
  140. source = model.BlockSourceSys
  141. } else if p.Source == 2 {
  142. // 小黑屋封禁
  143. source = model.BlockSourceBlackHouse
  144. if err = s.dao.BlackhouseBlock(context.TODO(), p); err != nil {
  145. return
  146. }
  147. }
  148. for _, mid := range p.MIDs {
  149. mids = append(mids, mid)
  150. theMID := mid
  151. s.cache.Save(func() {
  152. if cacheErr := s.dao.DeleteUserCache(context.TODO(), theMID); cacheErr != nil {
  153. log.Error("%+v", cacheErr)
  154. }
  155. if databusErr := s.accountNotify(context.TODO(), theMID); databusErr != nil {
  156. log.Error("%+v", databusErr)
  157. }
  158. })
  159. 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 {
  160. tx.Rollback()
  161. return
  162. }
  163. }
  164. // 发送站内信
  165. if p.Notify {
  166. s.mission(func() {
  167. if notifyErr := s.notifyMSG(context.TODO(), p.MIDs, source, p.Action, p.Area, p.Reason, p.Duration); notifyErr != nil {
  168. log.Error("%+v", notifyErr)
  169. return
  170. }
  171. })
  172. }
  173. s.mission(func() {
  174. s.AddAuditLog(context.TODO(), p.Action, p.AdminID, p.AdminName, mids, duration, source, p.Area, p.Reason, p.Comment, p.Notify, stime)
  175. })
  176. err = tx.Commit()
  177. return
  178. }
  179. // BatchRemove .
  180. func (s *Service) BatchRemove(c context.Context, p *model.ParamBatchRemove) (err error) {
  181. var (
  182. tx *xsql.Tx
  183. mids []int64
  184. stime = time.Now()
  185. )
  186. if tx, err = s.dao.BeginTX(c); err != nil {
  187. return
  188. }
  189. for _, mid := range p.MIDs {
  190. mids = append(mids, mid)
  191. theMID := mid
  192. s.cache.Save(func() {
  193. if cacheErr := s.dao.DeleteUserCache(context.TODO(), theMID); cacheErr != nil {
  194. log.Error("%+v", cacheErr)
  195. }
  196. if databusErr := s.accountNotify(context.TODO(), theMID); databusErr != nil {
  197. log.Error("%+v", databusErr)
  198. }
  199. })
  200. if err = s.action(c, tx, mid, p.AdminID, p.AdminName, model.BlockSourceRemove, model.BlockAreaNone, "", p.Comment, model.BlockActionAdminRemove, 0, p.Notify, stime); err != nil {
  201. tx.Rollback()
  202. return
  203. }
  204. }
  205. // 发送站内信
  206. if p.Notify {
  207. s.mission(func() {
  208. if notifyErr := s.notifyMSG(context.TODO(), p.MIDs, model.BlockSourceRemove, model.BlockActionAdminRemove, model.BlockAreaNone, "", 0); notifyErr != nil {
  209. log.Error("%+v", notifyErr)
  210. return
  211. }
  212. })
  213. }
  214. s.mission(func() {
  215. s.AddAuditLog(context.TODO(), model.BlockActionAdminRemove, p.AdminID, p.AdminName, mids, 0, model.BlockSourceRemove, model.BlockAreaNone, "", p.Comment, p.Notify, stime)
  216. })
  217. err = tx.Commit()
  218. return
  219. }
  220. // notifyMSG .
  221. func (s *Service) notifyMSG(c context.Context, mids []int64, source model.BlockSource, action model.BlockAction, area model.BlockArea, reason string, days int64) (err error) {
  222. code, title, content := s.MSGInfo(source, action, area, reason, days)
  223. log.Info("block admin title : %s , content : %s , mids : %+v", title, content, mids)
  224. if err = s.dao.SendSysMsg(context.TODO(), code, mids, title, content, ""); err != nil {
  225. return
  226. }
  227. return
  228. }
  229. 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) {
  230. var (
  231. db = &model.DBHistory{
  232. MID: mid,
  233. AdminID: adminID,
  234. AdminName: adminName,
  235. Source: source,
  236. Area: area,
  237. Reason: reason,
  238. Comment: comment,
  239. Action: action,
  240. StartTime: stime,
  241. Duration: int64(duration / time.Second),
  242. Notify: notify,
  243. }
  244. blockStatus model.BlockStatus
  245. )
  246. if err = s.dao.TxInsertHistory(c, tx, db); err != nil {
  247. return
  248. }
  249. switch action {
  250. case model.BlockActionAdminRemove, model.BlockActionSelfRemove:
  251. blockStatus = model.BlockStatusFalse
  252. case model.BlockActionLimit:
  253. switch source {
  254. case model.BlockSourceBlackHouse:
  255. blockStatus = model.BlockStatusCredit
  256. default:
  257. blockStatus = model.BlockStatusLimit
  258. }
  259. s.mission(func() {
  260. if err = s.dao.UpdateAddBlockCount(context.TODO(), mid); err != nil {
  261. log.Error("%+v", err)
  262. }
  263. })
  264. case model.BlockActionForever:
  265. blockStatus = model.BlockStatusForever
  266. s.mission(func() {
  267. if err = s.dao.UpdateAddBlockCount(context.TODO(), mid); err != nil {
  268. log.Error("%+v", err)
  269. }
  270. })
  271. default:
  272. err = errors.Errorf("unknown block action [%d]", action)
  273. return
  274. }
  275. if err = s.dao.TxUpdateUser(c, tx, mid, blockStatus); err != nil {
  276. return
  277. }
  278. return
  279. }
  280. func (s *Service) userID(c context.Context, mid int64) (id string, err error) {
  281. return "N/A", nil
  282. }
  283. func (s *Service) mailInfo(c context.Context, mid int64) (mail string, err error) {
  284. if mail, err = s.dao.MailInfo(c, mid); mail == "" {
  285. mail = "N/A"
  286. }
  287. return
  288. }
  289. // TelInfo .
  290. func (s *Service) telInfo(c context.Context, mid int64) (tel string, err error) {
  291. if tel, err = s.dao.TelInfo(c, mid); err != nil {
  292. return
  293. }
  294. if len(tel) == 0 {
  295. tel = "N/A"
  296. return
  297. }
  298. if len(tel) < 4 {
  299. tel = tel[:1] + "****"
  300. return
  301. }
  302. tel = tel[:3] + "****" + tel[len(tel)-4:]
  303. return
  304. }