subject.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strconv"
  6. "time"
  7. "go-common/library/queue/databus/report"
  8. "go-common/app/admin/main/reply/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. func (s *Service) subject(c context.Context, oid int64, typ int32) (sub *model.Subject, err error) {
  13. if sub, err = s.dao.Subject(c, oid, typ); err != nil {
  14. return
  15. }
  16. if sub == nil {
  17. err = ecode.NothingFound
  18. }
  19. return
  20. }
  21. func (s *Service) subjects(c context.Context, oids []int64, typ int32) (res map[int64]*model.Subject, err error) {
  22. return s.dao.Subjects(c, oids, typ)
  23. }
  24. // Subject get subject info.
  25. func (s *Service) Subject(c context.Context, oid int64, typ int32) (sub *model.Subject, err error) {
  26. if sub, err = s.dao.Subject(c, oid, typ); err != nil {
  27. return
  28. }
  29. // NOTE nothing found return to normal
  30. return
  31. }
  32. // ModifySubState modify subject state
  33. func (s *Service) ModifySubState(c context.Context, adid int64, adName string, oids []int64, typ int32, state int32, remark string) (fails map[int64]string, err error) {
  34. var (
  35. action string
  36. sub *model.Subject
  37. now = time.Now()
  38. )
  39. switch state {
  40. case model.SubStateNormal:
  41. action = model.SujectAllow
  42. case model.SubStateForbid:
  43. action = model.SujectForbid
  44. default:
  45. err = ecode.ReplyIllegalSubState
  46. return
  47. }
  48. fails = make(map[int64]string)
  49. for _, oid := range oids {
  50. sub, err = s.subject(c, oid, typ)
  51. if err != nil {
  52. log.Error("rpSvr.subject(oid,%d,type,%d)error(%v)", oid, typ, err)
  53. fails[oid] = ecode.Cause(err).Message()
  54. err = nil
  55. continue
  56. }
  57. // subject frozen
  58. if sub.AttrVal(model.SubAttrFrozen) == model.AttrYes {
  59. fails[oid] = ecode.ReplySubjectFrozen.Message()
  60. continue
  61. }
  62. if _, err = s.dao.UpSubjectState(c, oid, typ, state, now); err != nil {
  63. log.Error("s.UpSubjectState(%d,%d) error(%v)", oid, typ, err)
  64. return
  65. }
  66. if err = s.dao.DelSubjectCache(c, oid, typ); err != nil {
  67. log.Error("s.dao.DeleteSubjectCache(%d,%d) state:%d error(%v)", oid, typ, err)
  68. }
  69. report.Manager(&report.ManagerInfo{
  70. UID: adid,
  71. Uname: adName,
  72. Business: 41,
  73. Type: int(typ),
  74. Oid: oid,
  75. Ctime: now,
  76. Action: action,
  77. Index: []interface{}{sub.State, state},
  78. Content: map[string]interface{}{"remark": remark},
  79. })
  80. }
  81. return
  82. }
  83. // FreezeSub freeze or unfreeze subject
  84. func (s *Service) FreezeSub(c context.Context, adid int64, adName string, oids []int64, typ int32, freeze int32, remark string) (fails map[int64]string, err error) {
  85. var (
  86. state int32
  87. action string
  88. sub *model.Subject
  89. now = time.Now()
  90. )
  91. fails = make(map[int64]string)
  92. for _, oid := range oids {
  93. if sub, err = s.subject(c, oid, typ); err != nil {
  94. log.Error("rpSvr.subject(oid,%d,type,%d)error(%v)", oid, typ, err)
  95. fails[oid] = ecode.Cause(err).Message()
  96. err = nil
  97. continue
  98. }
  99. // already freeze or already unfreeze
  100. if (sub.AttrVal(model.SubAttrFrozen) == model.AttrYes && freeze == 2) || (sub.AttrVal(model.SubAttrFrozen) == model.AttrNo && freeze != 2) {
  101. continue
  102. }
  103. switch freeze {
  104. case 0:
  105. // unfreeze and allow
  106. sub.AttrSet(model.AttrNo, model.SubAttrFrozen)
  107. state = model.SubStateNormal
  108. action = model.SujectUnfrozenAllow
  109. case 1:
  110. // unfreeze and forbid
  111. sub.AttrSet(model.AttrNo, model.SubAttrFrozen)
  112. state = model.SubStateForbid
  113. action = model.SujectUnfrozenForbid
  114. case 2:
  115. // freeze and forbid
  116. sub.AttrSet(model.AttrYes, model.SubAttrFrozen)
  117. state = model.SubStateForbid
  118. action = model.SujectFrozen
  119. default:
  120. err = ecode.ReplyIllegalSubState
  121. return
  122. }
  123. if _, err = s.dao.UpStateAndAttr(c, oid, typ, state, sub.Attr, now); err != nil {
  124. log.Error("s.UpSubjectAttr(%d,%d,%d,%d) error(%v)", oid, typ, sub.Attr, err)
  125. return
  126. }
  127. if err = s.dao.DelSubjectCache(c, oid, typ); err != nil {
  128. log.Error("s.dao.DeleteSubjectCache(%d,%d) state:%d error(%v)", oid, typ, err)
  129. }
  130. report.Manager(&report.ManagerInfo{
  131. UID: adid,
  132. Uname: adName,
  133. Business: 41,
  134. Type: int(typ),
  135. Oid: oid,
  136. Ctime: now,
  137. Action: action,
  138. Index: []interface{}{sub.State, state},
  139. Content: map[string]interface{}{"remark": remark},
  140. })
  141. }
  142. return
  143. }
  144. // SubjectLog returns operation logs by query parameters
  145. func (s *Service) SubjectLog(c context.Context, sp model.LogSearchParam) (res *model.SubjectLogRes, err error) {
  146. res = &model.SubjectLogRes{
  147. Logs: []*model.SubjectLog{},
  148. }
  149. sp.Action = "subject_allow,subject_forbid,subject_frozen,subject_unfrozen_allow,subject_unfrozen_forbid"
  150. if sp.Pn <= 0 || sp.Ps <= 0 {
  151. return nil, ecode.RequestErr
  152. }
  153. reportData, err := s.dao.ReportLog(c, sp)
  154. if err != nil || reportData == nil {
  155. return
  156. }
  157. res.Page = reportData.Page
  158. res.Sort = reportData.Sort
  159. res.Order = reportData.Order
  160. exists := make(map[int64]bool)
  161. var oids []int64
  162. for _, data := range reportData.Result {
  163. if !exists[data.Oid] {
  164. exists[data.Oid] = true
  165. oids = append(oids, data.Oid)
  166. }
  167. var extra map[string]string
  168. err = json.Unmarshal([]byte(data.Content), &extra)
  169. if err != nil {
  170. log.Error("Subject Operation Log unmarshal failed(%v)", err)
  171. return
  172. }
  173. res.Logs = append(res.Logs, &model.SubjectLog{
  174. AdminID: data.AdminID,
  175. AdminName: data.AdminName,
  176. Oid: strconv.FormatInt(data.Oid, 10),
  177. Type: data.Type,
  178. Remark: extra["remark"],
  179. CTime: data.Ctime,
  180. Action: data.Action,
  181. })
  182. }
  183. if sp.Type != 0 && len(oids) > 0 {
  184. var subjects map[int64]*model.Subject
  185. subjects, err = s.subjects(c, oids, sp.Type)
  186. if err == nil {
  187. for _, data := range res.Logs {
  188. var oid int64
  189. oid, err = strconv.ParseInt(data.Oid, 10, 64)
  190. if err != nil {
  191. log.Error("strconv.ParseInt failed(%v)", err)
  192. return
  193. }
  194. if sub, ok := subjects[oid]; ok && sub != nil {
  195. if sub.AttrVal(model.SubAttrFrozen) == model.AttrYes {
  196. //frozen
  197. data.State = 2
  198. } else if sub.State == model.SubStateForbid {
  199. data.State = model.SubStateForbid
  200. } else {
  201. data.State = model.SubStateNormal
  202. }
  203. }
  204. }
  205. }
  206. }
  207. return
  208. }