checksearchlogic.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "bytes"
  5. "context"
  6. "crypto/md5"
  7. "fmt"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. IC "jyBXBase/rpc/init"
  10. "jyBXBase/rpc/util"
  11. "log"
  12. "net/url"
  13. "sort"
  14. "strings"
  15. "jyBXBase/rpc/bxbase"
  16. "jyBXBase/rpc/internal/svc"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. )
  19. type CheckSearchLogic struct {
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. logx.Logger
  23. }
  24. func NewCheckSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckSearchLogic {
  25. return &CheckSearchLogic{
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. Logger: logx.WithContext(ctx),
  29. }
  30. }
  31. // 校验搜索列表
  32. func (l *CheckSearchLogic) CheckSearch(in *bxbase.AddSearchReq) (res *bxbase.CheckRes, err error) {
  33. res = new(bxbase.CheckRes)
  34. if in.UserId == "" {
  35. res.ErrCode = 1
  36. res.ErrMsg = "用户未登录"
  37. return
  38. }
  39. if in.Keywords == "" && in.AdditionalWords == "" && in.Industry == "" {
  40. res.ErrCode = 1
  41. res.ErrMsg = "请先输入关键词或选择行业"
  42. return
  43. }
  44. log.Println("校验搜索列表:", in)
  45. in.Keywords = strings.Replace(in.Keywords, " ", ",", -1)
  46. query := map[string]interface{}{
  47. "user_id": in.UserId,
  48. }
  49. if IC.Mgo.Count("search_condition", query) >= 10 {
  50. res.ErrCode = 1
  51. res.ErrMsg = "对不起,最多可保存10个筛选条件。"
  52. return
  53. }
  54. in.Keywords = ValueSort(in.Keywords)
  55. in.Area = ValueSort(in.Area)
  56. in.City = ValueSort(in.City)
  57. in.Subtype = ValueSort(in.Subtype)
  58. in.Industry = ValueSort(in.Industry)
  59. in.SelectType = ValueSort(in.SelectType)
  60. in.BuyerClass = ValueSort(in.BuyerClass)
  61. in.NotKey = ValueSort(in.NotKey)
  62. in.AdditionalWords = ValueSort(in.AdditionalWords)
  63. //区域处理成字符串数组
  64. areaArr := []string{}
  65. for k, v := range gconv.Map(in.RegionMap) {
  66. for k1, v1 := range gconv.Map(v) {
  67. for _, v2 := range gconv.Strings(v1) {
  68. areaArr = append(areaArr, fmt.Sprintf("%s_%s_%s", k, k1, v2))
  69. }
  70. }
  71. }
  72. log.Println(areaArr)
  73. in.RegionMap = ValueSort(strings.Join(areaArr, ","))
  74. // 搜索分组为 1-招标采购公告时 2 超前项目
  75. // 当所选择的信息类型是全选时,需要处理成和全部时一样的空串
  76. switch in.SearchGroup {
  77. case util.SearchGroupBidding:
  78. if ValueSort(util.TopTypesBidding) == in.Subtype {
  79. in.Subtype = ""
  80. }
  81. case util.SearchGroupLeadingProject:
  82. if ValueSort(util.TopTypesLeadingProject) == in.Subtype {
  83. in.Subtype = ""
  84. }
  85. }
  86. inMap := common.StructToMapMore(in)
  87. inKey := GetKeysByParam(inMap)
  88. query["in_key"] = inKey
  89. res.Data = inKey
  90. if IC.Mgo.Count("search_condition", query) > 0 {
  91. res.ErrCode = 1
  92. res.ErrMsg = "该条件已保存,无需重复添加。"
  93. }
  94. return
  95. }
  96. func ValueSort(v string) string {
  97. vs := strings.Split(v, ",")
  98. sort.Slice(vs, func(i, j int) bool {
  99. return vs[i] < vs[j]
  100. })
  101. return strings.Join(vs, ",")
  102. }
  103. func GetKeysByParam(param map[string]interface{}) string {
  104. ps := &paramSorter{[]string{}, []string{}}
  105. for k, v := range param {
  106. ps.Keys = append(ps.Keys, k)
  107. ps.Values = append(ps.Values, common.InterfaceToStr(v))
  108. }
  109. ps.Sort()
  110. reqStr := ps.String()
  111. str := percentEncode(reqStr)
  112. str = SP(str, "%3A", "%253A", -1)
  113. return MD5(str)
  114. }
  115. func MD5(str string) string {
  116. data := []byte(str)
  117. has := md5.Sum(data)
  118. md5str := fmt.Sprintf("%x", has)
  119. return md5str
  120. }
  121. var SP = strings.Replace
  122. func percentEncode(str string) string {
  123. str = url.QueryEscape(str)
  124. str = SP(SP(SP(str, "+", "%20", -1), "*", "%2A", -1), "%7E", "~", -1)
  125. return str
  126. }
  127. type paramSorter struct {
  128. Keys []string
  129. Values []string
  130. }
  131. func (ps *paramSorter) String() string {
  132. str := ""
  133. for n, k := range ps.Keys {
  134. str += k + "=" + ps.Values[n]
  135. if n < len(ps.Keys)-1 {
  136. str += "&"
  137. }
  138. }
  139. return str
  140. }
  141. func (ps *paramSorter) Sort() {
  142. sort.Sort(ps)
  143. }
  144. func (ps *paramSorter) Len() int {
  145. return len(ps.Values)
  146. }
  147. func (ps *paramSorter) Less(i, j int) bool {
  148. return bytes.Compare([]byte(ps.Keys[i]), []byte(ps.Keys[j])) < 0
  149. }
  150. func (ps *paramSorter) Swap(i, j int) {
  151. ps.Values[i], ps.Values[j] = ps.Values[j], ps.Values[i]
  152. ps.Keys[i], ps.Keys[j] = ps.Keys[j], ps.Keys[i]
  153. }