wlog.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/workflow/model"
  7. "go-common/app/admin/main/workflow/model/search"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus/report"
  11. )
  12. const (
  13. _wkfReplyLog = 11
  14. _wkfAuditLog = 12
  15. )
  16. // LastLog .
  17. func (s *Service) LastLog(c context.Context, targets []int64, modules []int) (logs map[int64]string, err error) {
  18. var (
  19. resp *search.AuditLogSearchCommonResult
  20. uids []int64
  21. uNames map[int64]string
  22. )
  23. logs = make(map[int64]string)
  24. if len(targets) == 0 {
  25. return
  26. }
  27. cond := &search.AuditReportSearchCond{
  28. Fields: []string{"int_1", "ctime", "str_0", "uid"},
  29. Business: _wkfAuditLog,
  30. Type: modules,
  31. Order: "ctime",
  32. Sort: "desc",
  33. Int1: targets,
  34. Distinct: "int_1",
  35. IndexTimeType: "year",
  36. IndexTimeFrom: time.Now().AddDate(-1, 0, 0),
  37. IndexTimeEnd: time.Now(),
  38. }
  39. if resp, err = s.dao.SearchAuditReportLog(c, cond); err != nil {
  40. log.Error("s.dao.SearchAuditReportLog(%+v) error(%v)", cond, err)
  41. return
  42. }
  43. if resp == nil {
  44. log.Error("len resp.result == 0")
  45. err = ecode.Degrade
  46. return
  47. }
  48. // wrap uname
  49. for _, l := range resp.Result {
  50. uids = append(uids, l.UID)
  51. }
  52. if uNames, err = s.dao.BatchUNameByUID(c, uids); err != nil {
  53. log.Error("s.dao.SearchUNameByUid(%v) error(%v)", uids, err)
  54. err = nil
  55. }
  56. for _, l := range resp.Result {
  57. logs[l.Int1] = fmt.Sprintf("%s\n操作时间:%s\n操作人:", l.Str0, l.CTime)
  58. if uname, ok := uNames[l.UID]; ok {
  59. logs[l.Int1] = fmt.Sprint(logs[l.Int1], uname)
  60. } else {
  61. logs[l.Int1] = fmt.Sprint(logs[l.Int1], l.UID)
  62. }
  63. }
  64. return
  65. }
  66. // LastLogStat .
  67. func (s *Service) LastLogStat(c context.Context, targets []int64, modules []int, fields []string) (logs map[int64]*search.ReportLog, err error) {
  68. var resp *search.AuditLogSearchCommonResult
  69. logs = make(map[int64]*search.ReportLog)
  70. if len(targets) == 0 {
  71. return
  72. }
  73. cond := &search.AuditReportSearchCond{
  74. Fields: fields,
  75. Business: _wkfAuditLog,
  76. Type: modules,
  77. Order: "ctime",
  78. Sort: "desc",
  79. Int1: targets,
  80. Distinct: "int_1",
  81. IndexTimeType: "year",
  82. IndexTimeFrom: time.Now().AddDate(-1, 0, 0),
  83. IndexTimeEnd: time.Now(),
  84. }
  85. if resp, err = s.dao.SearchAuditReportLog(c, cond); err != nil {
  86. log.Error("s.dao.SearchAuditReportLog(%+v) error(%v)", cond, err)
  87. return
  88. }
  89. if resp == nil {
  90. log.Error("len resp.result == 0")
  91. err = ecode.NothingFound
  92. return
  93. }
  94. for _, l := range resp.Result {
  95. logs[l.Int1] = l
  96. }
  97. return
  98. }
  99. // AllAuditLog search all audit log of target & modules
  100. func (s *Service) AllAuditLog(c context.Context, target int64, modules []int) (logs []*model.WLog, err error) {
  101. var (
  102. resp *search.AuditLogSearchCommonResult
  103. uids []int64
  104. uNames map[int64]string
  105. )
  106. if target == 0 {
  107. return
  108. }
  109. cond := &search.AuditReportSearchCond{
  110. Fields: []string{"int_1", "ctime", "str_0", "uid", "uname"},
  111. Business: _wkfAuditLog,
  112. Type: modules,
  113. Order: "ctime",
  114. Sort: "desc",
  115. Int1: []int64{target},
  116. IndexTimeType: "year",
  117. IndexTimeFrom: time.Now().AddDate(-1, 0, 0),
  118. IndexTimeEnd: time.Now(),
  119. }
  120. if resp, err = s.dao.SearchAuditReportLog(c, cond); err != nil {
  121. log.Error("s.dao.SearchAuditReportLog(%+v) error(%v)", cond, err)
  122. return
  123. }
  124. if resp == nil {
  125. log.Error("len resp.result == 0")
  126. err = ecode.Degrade
  127. return
  128. }
  129. // wrap uname
  130. for _, l := range resp.Result {
  131. uids = append(uids, l.UID)
  132. }
  133. if uNames, err = s.dao.BatchUNameByUID(c, uids); err != nil {
  134. log.Error("s.dao.SearchUNameByUid(%v) error(%v)", uids, err)
  135. err = nil
  136. }
  137. for _, r := range resp.Result {
  138. wl := &model.WLog{
  139. AdminID: r.UID,
  140. Admin: r.UName,
  141. Target: r.Int1,
  142. Remark: r.Str0,
  143. }
  144. t, _ := time.ParseInLocation("2006-01-02 15:04:05", r.CTime, time.Local)
  145. wl.CTime.Scan(t)
  146. wl.Admin = uNames[wl.AdminID]
  147. logs = append(logs, wl)
  148. }
  149. return
  150. }
  151. func (s *Service) writeAuditLog(l *model.WLog) {
  152. var err error
  153. info := &report.ManagerInfo{
  154. Uname: l.Admin,
  155. UID: l.AdminID,
  156. Business: _wkfAuditLog,
  157. Type: int(l.Module),
  158. Oid: l.Oid,
  159. Action: "audit_log",
  160. Ctime: time.Now(),
  161. Index: []interface{}{l.Business, l.Target, l.TimeConsume, l.Mid, l.Remark, l.Note, l.OpType, l.PreRid},
  162. Content: map[string]interface{}{"wlog": l, "param": l.Param, "mids": l.Mids},
  163. }
  164. log.Info("start report audit log target:%v oid:%v uid:%v business:%v mid:%v", l.Target, l.Oid, l.AdminID, l.Business, l.Mid)
  165. if err = report.Manager(info); err != nil {
  166. log.Error("failed to produce report.Manager(%+v), err(%v)", info, err)
  167. }
  168. }
  169. func (s *Service) writeReplyLog(l *model.WLog) {
  170. var err error
  171. info := &report.ManagerInfo{
  172. Uname: l.Admin,
  173. UID: l.AdminID,
  174. Business: _wkfReplyLog,
  175. Type: int(l.Module),
  176. Oid: l.Oid,
  177. Action: "reply_log",
  178. Ctime: time.Now(),
  179. Index: []interface{}{l.Business, l.Target, l.Mid, l.Remark, l.Note},
  180. Content: map[string]interface{}{"wlog": l},
  181. }
  182. log.Info("start report reply log target:%v oid:%v uid:%v business:%v mid:%v", l.Target, l.Oid, l.AdminID, l.Business, l.Mid)
  183. if err = report.Manager(info); err != nil {
  184. log.Error("failed to produce report.Manager(%+v), err(%v)", info, err)
  185. }
  186. }