journal.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "encoding/json"
  6. "fmt"
  7. "go-common/app/admin/main/manager/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. // SearchLogAudit .
  13. func (s *Service) SearchLogAudit(c *bm.Context) (result []byte, err error) {
  14. var (
  15. res *model.LogRes
  16. )
  17. if res, err = s.dao.SearchLogAudit(c); err != nil {
  18. log.Error("s.dao.SearchLogAduit error (%v)", err)
  19. return
  20. }
  21. result, err = outData(res)
  22. return
  23. }
  24. // SearchLogAction .
  25. func (s *Service) SearchLogAction(c *bm.Context) (result []byte, err error) {
  26. var (
  27. res *model.LogRes
  28. )
  29. if res, err = s.dao.SearchLogAction(c); err != nil {
  30. log.Error("s.dao.SearchLogAduit error (%v)", err)
  31. return
  32. }
  33. if res.Code != ecode.OK.Code() {
  34. log.Error("res.Code error (%d)", res.Code)
  35. return
  36. }
  37. result, err = outData(res)
  38. return
  39. }
  40. // OutData .
  41. func outData(res *model.LogRes) (result []byte, err error) {
  42. var resultMap []map[string]interface{}
  43. //Output the data
  44. for _, j := range res.Data.Result {
  45. item := map[string]interface{}{}
  46. decoder := json.NewDecoder(bytes.NewReader(j))
  47. decoder.UseNumber()
  48. decoder.Decode(&item)
  49. item = numberToInt64(item)
  50. resultMap = append(resultMap, item)
  51. }
  52. for parentKey, parentValue := range resultMap {
  53. if extraValue, ok := parentValue["extra_data"]; ok {
  54. if extraData, ok := extraValue.(string); ok {
  55. p := make(map[string]interface{})
  56. if e := json.Unmarshal([]byte(extraData), &p); e == nil {
  57. for childKey, childValue := range backtrace(p) {
  58. resultMap[parentKey][childKey] = childValue
  59. }
  60. }
  61. }
  62. }
  63. }
  64. titleMap := make(map[string]string)
  65. // Iterator title collections
  66. for _, v := range resultMap {
  67. for key := range v {
  68. titleMap[key] = key
  69. }
  70. }
  71. // Get the titles
  72. titleStr := []string{}
  73. for _, v := range titleMap {
  74. titleStr = append(titleStr, v)
  75. }
  76. data := [][]string{}
  77. data = append(data, titleStr)
  78. for _, value := range resultMap {
  79. fields := []string{}
  80. for _, parentTitle := range titleStr {
  81. if value[parentTitle] != nil {
  82. fields = append(fields, fmt.Sprintf("%v", value[parentTitle]))
  83. } else {
  84. fields = append(fields, "")
  85. }
  86. }
  87. data = append(data, fields)
  88. }
  89. buf := bytes.NewBuffer(nil)
  90. w := csv.NewWriter(buf)
  91. for _, record := range data {
  92. if err = w.Write(record); err != nil {
  93. log.Error("w Write (%v) error (%v)", record, err)
  94. return
  95. }
  96. }
  97. w.Flush()
  98. result = buf.Bytes()
  99. return
  100. }
  101. // backtrace .
  102. func backtrace(in map[string]interface{}) (out map[string]interface{}) {
  103. out = make(map[string]interface{})
  104. for k, v := range in {
  105. if z, ok := v.(map[string]interface{}); ok {
  106. for childKey, childValue := range backtrace(z) {
  107. out[childKey] = childValue
  108. }
  109. } else {
  110. out[k] = v
  111. }
  112. }
  113. return
  114. }
  115. // numberToInt64 .
  116. func numberToInt64(in map[string]interface{}) (out map[string]interface{}) {
  117. var err error
  118. out = map[string]interface{}{}
  119. for k, v := range in {
  120. if integer, ok := v.(json.Number); ok {
  121. if out[k], err = integer.Int64(); err != nil {
  122. log.Error("service.log.numberToInt64(%v)(%v)", integer, err)
  123. }
  124. } else {
  125. out[k] = v
  126. }
  127. }
  128. return
  129. }