log.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "strings"
  8. "go-common/app/admin/main/search/dao"
  9. "go-common/app/admin/main/search/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. func convert(params map[string][]string) (res map[string][]interface{}) {
  14. res = make(map[string][]interface{})
  15. for k, v := range params {
  16. var arr []interface{}
  17. for _, m := range v {
  18. if m != "" {
  19. for _, u := range strings.Split(m, ",") {
  20. arr = append(arr, u)
  21. }
  22. }
  23. }
  24. if len(arr) > 0 {
  25. res[k] = arr
  26. }
  27. }
  28. return res
  29. }
  30. // Check .
  31. func (s *Service) Check(appID string, businessID int) (business *model.Business, ok bool) {
  32. business, ok = s.dao.GetLogInfo(appID, businessID)
  33. return
  34. }
  35. func numberToInt64(in map[string]interface{}) (out map[string]interface{}) {
  36. var err error
  37. out = map[string]interface{}{}
  38. for k, v := range in {
  39. if integer, ok := v.(json.Number); ok {
  40. if out[k], err = integer.Int64(); err != nil {
  41. log.Error("service.log.numberToInt64(%v)(%v)", integer, err)
  42. }
  43. } else {
  44. out[k] = v
  45. }
  46. }
  47. return
  48. }
  49. // 获取部门
  50. func (s *Service) uDepTs(c context.Context, res *model.SearchResult, sp *model.LogParams) *model.SearchResult {
  51. var (
  52. uids []string
  53. result []map[string]interface{}
  54. err error
  55. )
  56. result = []map[string]interface{}{}
  57. for _, j := range res.Result {
  58. item := map[string]interface{}{}
  59. decoder := json.NewDecoder(bytes.NewReader(j))
  60. decoder.UseNumber()
  61. if err = decoder.Decode(&item); err != nil {
  62. dao.PromError(fmt.Sprintf("es:%s 搜索LogAudit JSON失败", sp.Bsp.AppID), "s.dao.LogAudit(%v) json error(%v)", sp, err)
  63. }
  64. item = numberToInt64(item)
  65. result = append(result, item)
  66. if _, ok := item["uid"]; ok {
  67. uids = append(uids, fmt.Sprintf("%v", item["uid"]))
  68. }
  69. }
  70. var depRs = &model.UDepTsData{
  71. Data: map[string]string{},
  72. }
  73. if len(uids) > 0 {
  74. if depRs, err = s.dao.UDepTs(c, uids); err != nil {
  75. dao.PromError(fmt.Sprintf("es:%s 搜索LogAudit失败", sp.Bsp.AppID), "s.dao.LogAudit(%v) error(%v)", sp, err)
  76. err = ecode.SearchLogAuditFailed
  77. return res
  78. }
  79. }
  80. for i, j := range result {
  81. result[i]["department"] = ""
  82. if _, ok := j["uid"]; ok {
  83. if m, sok := depRs.Data[fmt.Sprintf("%v", j["uid"])]; sok {
  84. result[i]["department"] = m
  85. }
  86. }
  87. if res.Result[i], err = json.Marshal(j); err != nil {
  88. log.Error("s.dao.LogAudit(%v) json res(%v)", err, j)
  89. }
  90. }
  91. return res
  92. }
  93. // 获取IP地址
  94. func (s *Service) IP(c context.Context, res *model.SearchResult, sp *model.LogParams) *model.SearchResult {
  95. var (
  96. ip []string
  97. result []map[string]interface{}
  98. err error
  99. )
  100. result = []map[string]interface{}{}
  101. for _, j := range res.Result {
  102. item := map[string]interface{}{}
  103. decoder := json.NewDecoder(bytes.NewReader(j))
  104. decoder.UseNumber()
  105. if err = decoder.Decode(&item); err != nil {
  106. dao.PromError(fmt.Sprintf("es:%s 搜索LogUserAction JSON失败", sp.Bsp.AppID), "s.dao.LogUserAction(%v) json error(%v)", sp, err)
  107. }
  108. item = numberToInt64(item)
  109. result = append(result, item)
  110. if _, ok := item["ip"]; ok {
  111. if v := fmt.Sprintf("%v", item["ip"]); v != "" {
  112. ip = append(ip, v)
  113. }
  114. }
  115. }
  116. var ipData = &model.IPData{
  117. Data: map[string]struct {
  118. Country string `json:"country"`
  119. Province string `json:"province"`
  120. City string `json:"city"`
  121. Isp string `json:"isp"`
  122. }{},
  123. }
  124. if len(ip) > 0 {
  125. if ipData, err = s.dao.IP(c, ip); err != nil {
  126. dao.PromError(fmt.Sprintf("es:%s 搜索LogUserAction失败", sp.Bsp.AppID), "s.dao.LogUserAction(%v) error(%v)", sp, err)
  127. err = ecode.SearchLogAuditFailed
  128. return res
  129. }
  130. }
  131. for i, j := range result {
  132. result[i]["location"] = ""
  133. if _, ok := j["ip"]; ok {
  134. if m, sok := ipData.Data[fmt.Sprintf("%v", j["ip"])]; sok {
  135. location := make([]string, 0, 4)
  136. for _, v := range []string{m.Country, m.Province, m.City, m.Isp} {
  137. if v != "" {
  138. location = append(location, v)
  139. }
  140. }
  141. result[i]["location"] = strings.Join(location, "-")
  142. }
  143. }
  144. if res.Result[i], err = json.Marshal(j); err != nil {
  145. log.Error("s.dao.LogUserAction(%v) json res(%v)", err, j)
  146. }
  147. }
  148. return res
  149. }
  150. // LogAudit .
  151. func (s *Service) LogAudit(c context.Context, params map[string][]string, sp *model.LogParams, business *model.Business) (res *model.SearchResult, err error) {
  152. p := convert(params)
  153. if res, err = s.dao.LogAudit(c, p, sp, business); err != nil {
  154. dao.PromError(fmt.Sprintf("es:%s 搜索LogAudit失败", sp.Bsp.AppID), "s.dao.LogAudit(%v) error(%v)", sp, err)
  155. err = ecode.SearchLogAuditFailed
  156. return
  157. }
  158. res = s.uDepTs(c, res, sp)
  159. return
  160. }
  161. // LogAuditGroupBy .
  162. func (s *Service) LogAuditGroupBy(c context.Context, params map[string][]string, sp *model.LogParams, business *model.Business) (res *model.SearchResult, err error) {
  163. p := convert(params)
  164. if v, ok := p["group"]; !ok || len(v) == 0 {
  165. err = ecode.RequestErr
  166. return
  167. }
  168. if res, err = s.dao.LogAuditGroupBy(c, p, sp, business); err != nil {
  169. dao.PromError(fmt.Sprintf("es:%s 搜索LogAuditGroupBy失败", sp.Bsp.AppID), "s.dao.LogAuditGroupBy(%v) error(%v)", sp, err)
  170. err = ecode.SearchLogAuditOidFailed
  171. return
  172. }
  173. if res.Page.Total < int64(res.Page.Ps*(res.Page.Pn-1)) || len(res.Result) == 0 {
  174. res.Result = []json.RawMessage{}
  175. } else if int64(res.Page.Ps*(res.Page.Pn-1)) <= res.Page.Total && res.Page.Total <= int64(res.Page.Ps*res.Page.Pn) {
  176. res.Result = res.Result[res.Page.Ps*(res.Page.Pn-1) : res.Page.Total]
  177. } else {
  178. res.Result = res.Result[res.Page.Ps*(res.Page.Pn-1) : res.Page.Ps*res.Page.Pn]
  179. }
  180. res = s.uDepTs(c, res, sp)
  181. return
  182. }
  183. // LogAuditDelete .
  184. func (s *Service) LogAuditDelete(c context.Context, params map[string][]string, sp *model.LogParams, business *model.Business) (res *model.SearchResult, err error) {
  185. p := convert(params)
  186. if res, err = s.dao.LogAuditDelete(c, p, sp, business); err != nil {
  187. dao.PromError(fmt.Sprintf("es:%s LogAuditDelete失败", sp.Bsp.AppID), "s.dao.LogAuditDelete(%v) error(%v)", sp, err)
  188. err = ecode.SearchLogAuditFailed
  189. return
  190. }
  191. return
  192. }
  193. // LogUserAction .
  194. func (s *Service) LogUserAction(c context.Context, params map[string][]string, sp *model.LogParams, business *model.Business) (res *model.SearchResult, err error) {
  195. p := convert(params)
  196. if res, err = s.dao.LogUserAction(c, p, sp, business); err != nil {
  197. dao.PromError(fmt.Sprintf("es:%s 搜索LogUserAction失败", sp.Bsp.AppID), "s.dao.LogUserAction(%v) error(%v)", sp, err)
  198. err = ecode.SearchLogUserActionFailed
  199. return
  200. }
  201. res = s.IP(c, res, sp)
  202. return
  203. }
  204. // LogUserActionDelete .
  205. func (s *Service) LogUserActionDelete(c context.Context, params map[string][]string, sp *model.LogParams, business *model.Business) (res *model.SearchResult, err error) {
  206. p := convert(params)
  207. if res, err = s.dao.LogUserActionDelete(c, p, sp, business); err != nil {
  208. dao.PromError(fmt.Sprintf("es:%s LogUserActionDelete失败", sp.Bsp.AppID), "s.dao.LogUserActionDelete(%v) error(%v)", sp, err)
  209. err = ecode.SearchLogAuditFailed
  210. return
  211. }
  212. return
  213. }
  214. func (s *Service) LogCount(c context.Context, name string, business int, uid interface{}) {
  215. s.dao.LogCount(c, name, business, uid)
  216. }