log_coding.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package log
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/go-redis/redis/v8"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "log-server/global"
  9. "log-server/model/log"
  10. "log-server/model/log/request"
  11. "log-server/model/log/response"
  12. "log-server/service/cache"
  13. "strconv"
  14. "time"
  15. )
  16. type ServiceLogCoding struct {
  17. cache cache.Cache
  18. }
  19. var codeListCacheKey = "log:code"
  20. func (s *ServiceLogCoding) CreateCoding(requestCoding request.CodingLogRequest) (err error) {
  21. if !errors.Is(global.GVA_DB.Where("coding = ?", requestCoding.Coding).First(&log.CodingLog{}).Error, gorm.ErrRecordNotFound) {
  22. return errors.New("存在相同coding")
  23. }
  24. coding := new(log.CodingLog)
  25. if requestCoding.ParentId == 0 {
  26. coding.Level = 1
  27. } else {
  28. logCoding := new(log.CodingLog)
  29. err = global.GVA_DB.Where("id = ?", requestCoding.ParentId).First(logCoding).Error
  30. if err != nil {
  31. return err
  32. }
  33. if logCoding.Id == 0 {
  34. return errors.New("没有找到父节点")
  35. }
  36. if logCoding.Level >= 3 {
  37. return errors.New("该节点不能添加下级")
  38. }
  39. coding.Level = logCoding.Level + 1
  40. }
  41. coding.ParentId = requestCoding.ParentId
  42. coding.Coding = requestCoding.Coding
  43. coding.Describe = requestCoding.Describe
  44. coding.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  45. coding.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  46. key := codeListCacheKey
  47. _ = s.cache.DelBatheHsCache(context.Background(), key)
  48. return global.GVA_DB.Create(&coding).Error
  49. }
  50. //@author: [piexlmax](https://github.com/piexlmax)
  51. //@function: UpdateLogCoding
  52. //@description: 根据id更新LogCoding
  53. //@param: CodingLog log.CodingLog
  54. //@return: err error
  55. func (s *ServiceLogCoding) UpdateLogCoding(requestCoding request.CodingLogRequest) (err error) {
  56. logCoding := new(log.CodingLog)
  57. err = global.GVA_DB.Where("id = ?", requestCoding.Id).First(logCoding).Error
  58. if err != nil {
  59. return err
  60. }
  61. var coding = make(map[string]interface{})
  62. coding["coding"] = requestCoding.Coding
  63. coding["describe"] = requestCoding.Describe
  64. coding["update_time"] = time.Now().Format("2006-01-02 15:04:05")
  65. err = global.GVA_DB.Model(&log.CodingLog{}).Where("id", requestCoding.Id).Updates(coding).Error
  66. if err == nil {
  67. key := codeListCacheKey
  68. _ = s.cache.DelBatheHsCache(context.Background(), key)
  69. }
  70. return err
  71. }
  72. func (s *ServiceLogCoding) GetCodeById(id int) (coding response.GetLogCodingReply, err error) {
  73. var api log.CodingLog
  74. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  75. if err != nil {
  76. return
  77. }
  78. coding.Id = api.Id
  79. coding.Coding = api.Coding
  80. coding.Describe = api.Describe
  81. coding.ParentId = api.ParentId
  82. coding.UpdateTime = api.UpdateTime
  83. coding.CreateTime = api.CreateTime
  84. return
  85. }
  86. func (s *ServiceLogCoding) GetCodeLogList(order string, desc bool) (list interface{}, total int64, err error) {
  87. db := global.GVA_DB.Model(&log.CodingLog{})
  88. var apiList []log.CodingLog
  89. err = db.Count(&total).Error
  90. if err != nil {
  91. return apiList, total, err
  92. } else {
  93. if order != "" {
  94. var OrderStr string
  95. // 设置有效排序key 防止sql注入
  96. // 感谢 Tom4t0 提交漏洞信息
  97. orderMap := make(map[string]bool, 4)
  98. orderMap["id"] = true
  99. if orderMap[order] {
  100. if desc {
  101. OrderStr = order + " desc"
  102. } else {
  103. OrderStr = order
  104. }
  105. } else { // didn't matched any order key in `orderMap`
  106. //global.GVA_LOG.Error(fmt.Sprintf("非法的排序字段: %v", order))
  107. return apiList, total, err
  108. }
  109. err = db.Order(OrderStr).Find(&apiList).Error
  110. } else {
  111. err = db.Order("id").Find(&apiList).Error
  112. }
  113. }
  114. var apisReply []*response.GetCodeListReply
  115. if len(apiList) != 0 {
  116. for _, apiInfo := range apiList {
  117. coding := new(response.GetCodeListReply)
  118. coding.Id = apiInfo.Id
  119. coding.Coding = apiInfo.Coding
  120. coding.Describe = apiInfo.Describe
  121. coding.ParentId = apiInfo.ParentId
  122. coding.UpdateTime = apiInfo.UpdateTime
  123. coding.CreateTime = apiInfo.CreateTime
  124. coding.Level = apiInfo.Level
  125. apisReply = append(apisReply, coding)
  126. }
  127. mi := make(map[uint]*response.GetCodeListReply)
  128. for _, item := range apisReply {
  129. mi[item.Id] = item
  130. }
  131. var list1 []*response.GetCodeListReply
  132. for _, item := range apisReply {
  133. if item.ParentId == 0 {
  134. list1 = append(list1, item)
  135. continue
  136. }
  137. if pitem, ok := mi[item.ParentId]; ok {
  138. pitem.Children = append(pitem.Children, item)
  139. }
  140. }
  141. return list1, total, err
  142. }
  143. return apisReply, total, err
  144. }
  145. func (s *ServiceLogCoding) CodeListCache(ctx context.Context, code string) (string, error) {
  146. key := codeListCacheKey
  147. status, err := global.GVA_REDIS.Exists(ctx, key).Result()
  148. if err != nil {
  149. global.GVA_LOG.Error("获取redis code失败!", zap.Error(err))
  150. return "", err
  151. }
  152. if status == 0 {
  153. var apiList []log.CodingLog
  154. err := global.GVA_DB.Model(&log.CodingLog{}).Find(&apiList).Error
  155. if err != nil {
  156. global.GVA_LOG.Error("获取表中 code失败!", zap.Error(err))
  157. return "", err
  158. }
  159. mps := make(map[string]string, len(apiList))
  160. for _, code := range apiList {
  161. c := strconv.Itoa(code.Coding)
  162. mps[c] = code.Describe
  163. }
  164. err = global.GVA_REDIS.HSet(ctx, key, mps).Err()
  165. if err != nil {
  166. global.GVA_LOG.Error("添加到redis code失败!", zap.Error(err))
  167. return "", err
  168. }
  169. }
  170. c, err := global.GVA_REDIS.HGet(ctx, key, code).Result()
  171. if err == redis.Nil {
  172. return code + "没有找数据", nil
  173. }
  174. return c, err
  175. }