credit_conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package conf
  2. import (
  3. "go-common/library/log"
  4. "strconv"
  5. )
  6. //CreditConf 信用分数配置
  7. type CreditConf struct {
  8. CalculateConf *CreditScoreCalculateConfig
  9. ArticleRule *ArticleRuleConf
  10. }
  11. //AfterLoad load之后进行一些计算和整理
  12. func (c *CreditConf) AfterLoad() {
  13. c.CalculateConf.AfterLoad()
  14. c.ArticleRule.AfterLoad()
  15. }
  16. //CreditScoreCalculateConfig 分数计算配置
  17. type CreditScoreCalculateConfig struct {
  18. // 时间衰减因子
  19. // [离今年的差值] = 权重值
  20. TimeWeight map[string]int
  21. TimeWeight2 map[int]int
  22. }
  23. //AfterLoad after load
  24. func (c *CreditScoreCalculateConfig) AfterLoad() {
  25. c.TimeWeight2 = make(map[int]int)
  26. for k, v := range c.TimeWeight {
  27. key, _ := strconv.Atoi(k)
  28. c.TimeWeight2[key] = v
  29. }
  30. }
  31. //StateMachineState 状态机数据
  32. type StateMachineState struct {
  33. State int
  34. Round int
  35. Reason int
  36. }
  37. //ArticleRuleConf 稿件记分规则
  38. type ArticleRuleConf struct {
  39. AcceptOptypeData []int
  40. RejectOpTypeData []int
  41. // [score] ->[ optype list ]
  42. OptypeScoreData map[string][]int
  43. AcceptOptypeMap map[int]struct{}
  44. RejectOptypeMap map[int]struct{}
  45. // [optype] -> score
  46. OptypeScoreMap map[int]int
  47. InitState StateMachineState
  48. ArticleMaxOpenCount int
  49. }
  50. //AfterLoad after load
  51. func (a *ArticleRuleConf) AfterLoad() {
  52. a.AcceptOptypeMap = make(map[int]struct{})
  53. a.RejectOptypeMap = make(map[int]struct{})
  54. a.OptypeScoreMap = make(map[int]int)
  55. for _, v := range a.AcceptOptypeData {
  56. a.AcceptOptypeMap[v] = struct{}{}
  57. }
  58. for _, v := range a.RejectOpTypeData {
  59. a.RejectOptypeMap[v] = struct{}{}
  60. }
  61. for k, varr := range a.OptypeScoreData {
  62. var score, err = strconv.ParseInt(k, 10, 64)
  63. if err != nil {
  64. log.Error("score config wrong, k=%s", k)
  65. return
  66. }
  67. for _, v := range varr {
  68. a.OptypeScoreMap[v] = int(score)
  69. }
  70. }
  71. }
  72. //GetScore get score by type, opType, reason
  73. func (a *ArticleRuleConf) GetScore(typ int, opType int, reason int) (score int) {
  74. var s, ok = a.OptypeScoreMap[opType]
  75. if !ok {
  76. s = 0
  77. }
  78. score = s
  79. return
  80. }
  81. //IsRejected is article reject
  82. func (a *ArticleRuleConf) IsRejected(typ int, opType int, reason int) (res bool) {
  83. _, res = a.RejectOptypeMap[opType]
  84. return
  85. }
  86. //IsAccepted is article accepted
  87. func (a *ArticleRuleConf) IsAccepted(typ int, opType int, reason int) (res bool) {
  88. _, res = a.AcceptOptypeMap[opType]
  89. return
  90. }