rule.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package service
  2. import (
  3. "github.com/gogf/gf/v2/container/gvar"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "log"
  7. "regexp"
  8. "strings"
  9. )
  10. type Rule struct{}
  11. func (r *Rule) Execute(b *BidInfo) (string, int) {
  12. var bidCommonwealth int
  13. var quoteMode string
  14. if b.Type == 0 {
  15. bidCommonwealth = r.bidCommonwealth(b)
  16. quoteMode, _ = r.quoteMode(b)
  17. } else if b.Type == 1 {
  18. quoteMode, _ = r.quoteMode(b)
  19. } else if b.Type == 2 {
  20. bidCommonwealth = r.bidCommonwealth(b)
  21. }
  22. if quoteMode == QuoteMode_Whole && (b.Bidamount > 0 && b.Bidamount < g.Config().MustGet(gctx.New(), "quoteMode.minBidamount").Float64()) {
  23. quoteMode = ""
  24. }
  25. log.Println(b.Id, "规则", "报价模式", quoteMode, "中标联合体", bidCommonwealth)
  26. return quoteMode, bidCommonwealth
  27. }
  28. // 识别报价模式
  29. func (r *Rule) quoteMode(b *BidInfo) (string, map[string]bool) {
  30. quoteModeRules := g.Config().MustGet(gctx.New(), "quoteMode.rules").Maps()
  31. for _, v := range quoteModeRules {
  32. vv := gvar.New(v).MapStrVar()
  33. mustTableKv := vv["mustTableKv"].Maps()
  34. modeName := vv["mode"].String()
  35. for _, table := range b.TableKv {
  36. array := []string{}
  37. for _, row := range table {
  38. mustTableKvMap := map[int]bool{}
  39. for k, v := range row {
  40. if tableV := vv["tableV"].String(); tableV != "" {
  41. matchedK, _ := regexp.MatchString(g.Config().MustGet(gctx.New(), "quoteMode.tableK").String(), k)
  42. if !matchedK {
  43. if tableK := vv["tableK"].String(); tableK != "" {
  44. matchedK, _ = regexp.MatchString(tableK, k)
  45. }
  46. }
  47. matchedV, _ := regexp.MatchString(tableV, v)
  48. if matchedK && matchedV {
  49. log.Println(b.Id, "tableKv", k, v)
  50. return modeName, nil
  51. }
  52. }
  53. //
  54. for _, vv := range vv["tableKv"].Maps() {
  55. vvs := gvar.New(vv).MapStrVar()
  56. tableK := vvs["k"].String()
  57. tableV := vvs["v"].String()
  58. if tableK == "" || tableV == "" {
  59. continue
  60. }
  61. matchedK, _ := regexp.MatchString(tableK, k)
  62. matchedV, _ := regexp.MatchString(tableV, v)
  63. if matchedK && matchedV {
  64. log.Println(b.Id, "tableKv", k, v)
  65. return modeName, nil
  66. }
  67. }
  68. //
  69. for kk, vv := range mustTableKv {
  70. vvs := gvar.New(vv).MapStrVar()
  71. tableK := vvs["k"].String()
  72. tableV := vvs["v"].String()
  73. if tableK == "" || tableV == "" {
  74. continue
  75. }
  76. matchedK, _ := regexp.MatchString(tableK, k)
  77. matchedV, _ := regexp.MatchString(tableV, v)
  78. if matchedK && matchedV {
  79. array = append(array, k+":"+v)
  80. mustTableKvMap[kk] = true
  81. break
  82. }
  83. }
  84. }
  85. for kk, _ := range mustTableKv {
  86. if !mustTableKvMap[kk] {
  87. break
  88. }
  89. if kk == len(mustTableKv)-1 {
  90. log.Println(b.Id, "mustTableKv", strings.Join(array, " "))
  91. return modeName, nil
  92. }
  93. }
  94. }
  95. }
  96. }
  97. for _, v := range quoteModeRules {
  98. vv := gvar.New(v).MapStrVar()
  99. modeName := vv["mode"].String()
  100. detail := b.Detail
  101. for _, v := range vv["clearPatterns"].Strings() {
  102. detail = regexp.MustCompile(v).ReplaceAllString(detail, "")
  103. }
  104. excludePatterns := vv["excludePatterns"].Strings()
  105. if len(excludePatterns) > 0 && !r.matchAnyPattern(b.Id, detail, excludePatterns) {
  106. return modeName, nil
  107. }
  108. lines := strings.Split(detail, "\n")
  109. for _, lineVal := range lines {
  110. if r.matchAnyPattern(b.Id, lineVal, vv["patterns"].Strings()) {
  111. return modeName, nil
  112. }
  113. }
  114. }
  115. return "", nil
  116. }
  117. // 识别中标联合体
  118. func (r *Rule) bidCommonwealth(b *BidInfo) int {
  119. // Step 1: 排除黑名单
  120. if r.matchAnyPattern(b.Id, b.Detail, g.Config().MustGet(gctx.New(), "bidCommonwealth.blacklistPatterns").Strings()) {
  121. return -1
  122. }
  123. detail := b.Detail
  124. for _, v := range g.Config().MustGet(gctx.New(), "bidCommonwealth.clearPatterns").Strings() {
  125. detail = regexp.MustCompile(v).ReplaceAllString(detail, "")
  126. }
  127. if r.matchAnyPattern(b.Id, b.Detail, g.Config().MustGet(gctx.New(), "bidCommonwealth.whitelistPatterns").Strings()) {
  128. return 1
  129. }
  130. return -1
  131. }
  132. // 匹配任意模式
  133. func (r *Rule) matchAnyPattern(_id string, text string, patterns []string) bool {
  134. for _, pattern := range patterns {
  135. if strings.HasPrefix(pattern, "全部包含:") {
  136. vs := strings.Split(strings.TrimPrefix(pattern, "全部包含:"), "+")
  137. index := 0
  138. for _, v := range vs {
  139. if strings.Contains(text, v) {
  140. index++
  141. }
  142. }
  143. if index == len(vs) {
  144. log.Println(_id, "matchAnyPattern", pattern)
  145. return true
  146. }
  147. continue
  148. }
  149. if matched, _ := regexp.MatchString(pattern, text); matched {
  150. log.Println(_id, "matchAnyPattern", pattern)
  151. return true
  152. }
  153. }
  154. return false
  155. }
  156. // 全部匹配
  157. func (r *Rule) matchAllPattern(text string, patterns []string) bool {
  158. count := 0
  159. for _, pattern := range patterns {
  160. if matched, _ := regexp.MatchString(pattern, text); matched {
  161. count++
  162. }
  163. }
  164. if count > 0 && count == len(patterns) {
  165. return true
  166. }
  167. return false
  168. }