checksearchlogic.go 4.1 KB

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