purchase.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. package service
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. elastic "app.yhyue.com/moapp/jybase/es"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
  8. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/entity"
  9. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  10. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/model/es"
  11. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/type/bxcore"
  12. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/util"
  13. "encoding/json"
  14. "fmt"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. "log"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. type Purchase struct {
  22. AppId string ` json:"appId,omitempty"` //剑鱼默认10000
  23. UserId string ` json:"userId,omitempty"` //用户id
  24. Phone string ` json:"phone,omitempty"` //手机号
  25. BaseUserId int64 ` json:"baseUserId,omitempty"` //base_user_id 新用户id
  26. EntId int64 ` json:"entId,omitempty"` //企业id 没有企业 企业id=0
  27. EntUserId int64 ` json:"entUserId,omitempty"` //企业用户id 当前企业下的员工id 没有企业默认0
  28. AccountId int64 ` json:"accountId,omitempty"` //账户id
  29. EntAccountId int64 ` json:"entAccountId,omitempty"` //企业账户id
  30. PositionType int64 ` json:"positionType,omitempty"` //职位类型 0个人 1企业
  31. PositionId int64 ` json:"positionId,omitempty"` //职位id
  32. MgoUserId string ` json:"mgoUserId,omitempty"` //原userId
  33. PageNum int64 ` json:"pageNum,omitempty"` //当前页码
  34. PageSize int64 ` json:"pageSize,omitempty"` //每页数量
  35. PublishTime string ` json:"publishTime,omitempty"` //发布时间
  36. SelectType string ` json:"selectType,omitempty"` //搜索范围:
  37. DomainFirstType string ` json:"domainFirstType,omitempty"` //领域 一级
  38. DomainSecondType string ` json:"domainSecondType,omitempty"` //领域 二级
  39. DomainThirdType string ` json:"domainThirdType,omitempty"` //领域 三级
  40. DeadlineStatus int64 ` json:"deadlineStatus,omitempty"` //报名截止状态
  41. DeadlineTime string ` json:"deadlineTime,omitempty"` //报名截止时间
  42. DeadlineStart int64 ` json:"deadlineStart,omitempty"` //报名截止时间-开始
  43. DeadlineEnd int64 ` json:"deadlineEnd,omitempty"` //报名截止时间-结束
  44. DeliveryArea string ` json:"deliveryArea,omitempty"` //交付地点-省份
  45. DeliveryCity string ` json:"deliveryCity,omitempty"` //交付地点-城市
  46. DeliveryDistrict string ` json:"deliveryDistrict,omitempty"` //交付地点-县区
  47. ProjectArea string ` json:"projectArea,omitempty"` //项目地区-省份
  48. ProjectCity string ` json:"projectCity,omitempty"` //项目地区-城市
  49. ProjectDistrict string ` json:"projectDistrict,omitempty"` //项目地区-县区
  50. Industry string ` json:"industry,omitempty"` //行业
  51. FileExists int64 ` json:"fileExists,omitempty"` //是否有附件
  52. Publisher int64 ` json:"publisher,omitempty"` //发布者:可选:全部、用户发布:1、平台发布:2。
  53. KeyWords string ` json:"keyWords,omitempty"` //关键词:多个空格隔开(主)
  54. AdditionalWords string ` json:"additionalWords,omitempty"` //关键词:附加关键词(副:五组,每组最多15个字符)
  55. SearchMode int64 ` json:"searchMode,omitempty"` //搜索模式:0:精准搜索;1:模糊搜索
  56. WordsMode int64 ` json:"wordsMode,omitempty"` //搜索关键词模式;默认0:包含所有,1:包含任意
  57. UserAgent string ` json:"userAgent,omitempty"` //请求头信息
  58. Platform string ` json:"platform,omitempty"` //请求平台
  59. TipMsg string `json:"tipMsg"` //关键词提示信息
  60. HighlightWords string `json:"highlightWords"` //需要高亮的词,多个,号分割
  61. Total int64 `json:"total"` //数据总量
  62. IsPay bool `json:"isPay"` //是否是付费用户
  63. IsPower bool `json:"isPower"` //是否有权限
  64. }
  65. func NewPurchase(in *bxcore.PurchaseReq) *Purchase {
  66. baseUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
  67. accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
  68. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  69. return &Purchase{
  70. AppId: in.AppId,
  71. UserId: in.UserId,
  72. Phone: in.Phone,
  73. BaseUserId: baseUserId,
  74. EntId: in.EntId,
  75. EntUserId: in.EntUserId,
  76. AccountId: accountId,
  77. EntAccountId: in.EntAccountId,
  78. PositionType: in.PositionType,
  79. PositionId: positionId,
  80. MgoUserId: in.MgoUserId,
  81. PageNum: in.PageNum,
  82. PageSize: in.PageSize,
  83. PublishTime: in.PublishTime,
  84. SelectType: in.SelectType,
  85. DomainFirstType: in.DomainFirstType,
  86. DomainSecondType: in.DomainSecondType,
  87. DomainThirdType: in.DomainThirdType,
  88. DeadlineStatus: in.DeadlineStatus,
  89. DeadlineTime: in.DeadlineTime,
  90. DeliveryArea: in.DeliveryArea,
  91. DeliveryCity: in.DeliveryCity,
  92. DeliveryDistrict: in.DeliveryDistrict,
  93. ProjectArea: in.ProjectArea,
  94. ProjectCity: in.ProjectCity,
  95. ProjectDistrict: in.ProjectDistrict,
  96. Industry: in.Industry,
  97. FileExists: in.FileExists,
  98. Publisher: in.Publisher,
  99. KeyWords: in.KeyWords,
  100. AdditionalWords: in.AdditionalWords,
  101. SearchMode: in.SearchMode,
  102. WordsMode: in.WordsMode,
  103. UserAgent: in.UserAgent,
  104. Platform: in.Platform,
  105. }
  106. }
  107. var (
  108. AreaLabelLink = "/list/%s/%s.html"
  109. noPower = "点击查看"
  110. )
  111. // 缓存
  112. func (p *Purchase) HistoryKeywords() {
  113. var (
  114. cacheKey = fmt.Sprintf("history_purchase_v_%s", p.UserId)
  115. keys []string
  116. code = "other"
  117. isExist = map[string]bool{}
  118. duplicateRemoval = func(values []string) (keys []string) {
  119. for _, kv := range values {
  120. kv = strings.TrimSpace(kv)
  121. if isExist[kv] {
  122. continue
  123. }
  124. isExist[kv] = true
  125. keys = append(keys, kv)
  126. }
  127. return
  128. }
  129. )
  130. values := redis.GetStr(code, cacheKey)
  131. if values != "" {
  132. keys = append(keys, duplicateRemoval(strings.Split(values, ","))...)
  133. }
  134. if p.KeyWords != "" {
  135. keys = append(keys, duplicateRemoval(strings.Split(p.KeyWords, ","))...)
  136. }
  137. if p.AdditionalWords != "" {
  138. keys = append(keys, duplicateRemoval(strings.Split(p.AdditionalWords, ","))...)
  139. }
  140. if len(keys) > 10 {
  141. keys = keys[len(keys)-10:]
  142. }
  143. ok := redis.Put(code, cacheKey, strings.Join(keys, ","), -1)
  144. if !ok {
  145. logx.Info(" history keywords err:", keys)
  146. }
  147. }
  148. // 格式化
  149. func (p *Purchase) PurchaseListFormat(res []map[string]interface{}) (list []*bxcore.PurchaseList) {
  150. for _, rv := range res {
  151. logx.Info("title:", MC.InterfaceToStr(rv["title"]))
  152. id := MC.InterfaceToStr(rv["source_id"]) //拆分后 多条数据会来自一条bidding数据嘛,把bidding的id记一个source_id;mongo 库中bidding的_id
  153. if id == "" {
  154. continue
  155. }
  156. area := MC.InterfaceToStr(rv["area"])
  157. city := MC.InterfaceToStr(rv["city"])
  158. district := MC.InterfaceToStr(rv["district"])
  159. regionUrl := ""
  160. if district != "" && IC.DistrictMap[district] != "" {
  161. regionUrl = fmt.Sprintf(AreaLabelLink, "city", IC.DistrictMap[district])
  162. } else if city != "" && IC.CityMap[city] != "" {
  163. regionUrl = fmt.Sprintf(AreaLabelLink, "city", IC.CityMap[city])
  164. } else if area != "" && IC.AreaMap[area] != "" {
  165. regionUrl = fmt.Sprintf(AreaLabelLink, "area", IC.AreaMap[area])
  166. }
  167. var price int64
  168. if budget, ok := rv["budget"].(float64); ok && budget > 0 { //预算
  169. price = int64(budget)
  170. }
  171. if bidAmount, ok := rv["bidamount"].(float64); ok && bidAmount > 0 { //中标金额
  172. price = int64(bidAmount)
  173. }
  174. //deliver_area 交付省份//deliver_city 交付城市//deliver_district 交付区县
  175. deliveryLoc := fmt.Sprintf("%s%s%s", MC.InterfaceToStr(rv["deliver_area"]), MC.InterfaceToStr(rv["deliver_city"]), MC.InterfaceToStr(rv["deliver_district"]))
  176. buyer := noPower
  177. buyerTel := noPower
  178. if p.IsPower {
  179. buyer = MC.InterfaceToStr(rv["buyer"])
  180. buyerTel = MC.InterfaceToStr(rv["buyertel"])
  181. }
  182. isValidFile, _ := rv["isValidFile"].(bool)
  183. //buyerPerson := MC.InterfaceToStr(rv["buyerperson"])
  184. //agency := MC.InterfaceToStr(rv["agency"])
  185. //agencyTel := MC.InterfaceToStr(rv["agencytel"])
  186. //agencyPerson := MC.InterfaceToStr(rv["agencyperson"])
  187. //中标信息
  188. var winnerInfo []*bxcore.WinnerInfo
  189. winnerList := MC.ObjToString(rv["s_winner"]) //中标企业名称集合
  190. if winnerList != "" && len(strings.Split(winnerList, ",")) > 0 {
  191. for wk, wv := range strings.Split(winnerList, ",") {
  192. var (
  193. winnerId = ""
  194. )
  195. if rv["entidlist"] != nil {
  196. if entIdList := MC.ObjArrToStringArr(rv["entidlist"].([]interface{})); len(entIdList) > wk { //中标企业id集合
  197. winnerId = entIdList[wk]
  198. }
  199. }
  200. if IC.C.SearchWinner.Switch && winnerId == "" {
  201. continue
  202. }
  203. if entity.RegWinner.MatchString(wv) {
  204. continue
  205. }
  206. winnerInfo = append(winnerInfo, &bxcore.WinnerInfo{
  207. Winner: wv, //中标企业 需要单独处理
  208. WinnerTel: MC.ObjToString(rv["winnertel"]), //中标企业联系电话
  209. WinnerPerson: MC.ObjToString(rv["winnerperson"]), //中标企业联系人
  210. WinnerId: MC.If(winnerId != "" && len([]rune(winnerId)) > 12, encrypt.EncodeArticleId2ByCheck(winnerId), "").(string), //中标企业加密id 存在winnerId 异常的情况
  211. })
  212. }
  213. }
  214. list = append(list, &bxcore.PurchaseList{
  215. Id: encrypt.EncodeArticleId2ByCheck(id),
  216. Area: MC.InterfaceToStr(rv["area"]),
  217. City: MC.InterfaceToStr(rv["city"]),
  218. District: MC.InterfaceToStr(rv["district"]),
  219. RegionUrl: regionUrl,
  220. BuyerClass: MC.InterfaceToStr(rv["buyerclass"]),
  221. PublishTime: MC.Int64All(rv["publishtime"]),
  222. FileExists: isValidFile,
  223. Title: MC.InterfaceToStr(rv["title"]),
  224. Price: price,
  225. Buyer: buyer,
  226. BuyerTel: buyerTel,
  227. SignEndTime: MC.Int64All(rv["signendtime"]), //报名截止时间
  228. DeliveryLoc: deliveryLoc,
  229. Industry: util.IndustryFormat(p.Industry, strings.Trim(MC.ObjToString(rv["s_subscopeclass"]), ",")),
  230. PublicType: MC.InterfaceToStr(rv["public_type"]),
  231. //BuyerPerson: buyerPerson,
  232. //Agency: agency,
  233. //AgencyTel: agencyTel,
  234. //AgencyPerson: agencyPerson,
  235. //WinnerInfo: winnerInfo,
  236. //BidEndTime: MC.Int64All(rv["bidendtime"]),
  237. //BidOpenTime: MC.Int64All(rv["bidopentime"]), //开标时间,
  238. })
  239. }
  240. return
  241. }
  242. func (p *Purchase) PurchaseQuery() (query string) {
  243. var (
  244. wordsMusts, wordsShould, must, mustNot []string
  245. selectTypeArr = strings.Split(p.SelectType, ",")
  246. selectType = fmt.Sprintf(`"%s"`, strings.Join(selectTypeArr, "\",\""))
  247. )
  248. //发布时间
  249. if len(strings.Split(p.PublishTime, "-")) > 1 {
  250. var (
  251. timeQuery string
  252. startTime, endTime = strings.Split(p.PublishTime, "-")[0], strings.Split(p.PublishTime, "-")[1]
  253. )
  254. if startTime != "" || endTime != "" {
  255. timeQuery += `{"range":{"publishtime":{`
  256. if startTime != "" {
  257. timeQuery += `"gte":` + startTime
  258. }
  259. if startTime != "" && endTime != "" {
  260. timeQuery += `,`
  261. }
  262. if endTime != "" {
  263. timeQuery += `"lt":` + endTime
  264. }
  265. timeQuery += `}}}`
  266. }
  267. if timeQuery != "" {
  268. must = append(must, timeQuery)
  269. }
  270. }
  271. // 关键词
  272. if p.KeyWords != "" {
  273. var (
  274. keyWordsMusts []string
  275. )
  276. for _, v := range strings.Split(p.KeyWords, IC.C.JYKeyMark) {
  277. if elastic.ReplaceYH(v) == "" {
  278. continue
  279. }
  280. keyWordsMusts = append(keyWordsMusts, fmt.Sprintf(fmt.Sprintf(es.MultiMatch, "%s", selectType), elastic.ReplaceYH(v)))
  281. }
  282. //搜索关键词模式;默认0:包含所有,1:包含任意
  283. if p.WordsMode == 1 {
  284. wordsShould = append(wordsShould, fmt.Sprintf(elastic.NgramMust, strings.Join(keyWordsMusts, ",")))
  285. } else {
  286. wordsMusts = append(wordsMusts, keyWordsMusts...)
  287. }
  288. }
  289. //附加词
  290. if p.AdditionalWords != "" {
  291. for _, aws := range strings.Split(p.AdditionalWords, ",") {
  292. //多组附加词,每组间,号隔开。每组内如果关键词中间有空格,自动分词
  293. var (
  294. addWordsMusts []string
  295. )
  296. for _, v := range strings.Split(aws, IC.C.JYKeyMark) {
  297. if elastic.ReplaceYH(v) == "" {
  298. continue
  299. }
  300. addWordsStr := fmt.Sprintf(fmt.Sprintf(es.MultiMatch, "%s", selectType), elastic.ReplaceYH(v))
  301. if p.WordsMode == 0 {
  302. wordsMusts = append(wordsMusts, addWordsStr)
  303. } else {
  304. addWordsMusts = append(addWordsMusts, addWordsStr)
  305. }
  306. }
  307. //搜索关键词模式;默认0:包含所有,1:包含任意
  308. if len(addWordsMusts) > 0 {
  309. wordsShould = append(wordsShould, fmt.Sprintf(elastic.NgramMust, strings.Join(addWordsMusts, ",")))
  310. }
  311. }
  312. }
  313. //搜索关键词模式;默认0:包含所有,1:包含任意
  314. //包含任意一组
  315. if len(wordsShould) > 0 {
  316. must = append(must, fmt.Sprintf(es.QueryBoolShould, strings.Join(wordsShould, ",")))
  317. } else if len(wordsMusts) > 0 {
  318. must = append(must, fmt.Sprintf(elastic.NgramMust, strings.Join(wordsMusts, ",")))
  319. }
  320. //行业
  321. if p.Industry != "" {
  322. must = append(must, fmt.Sprintf(es.QueryBoolMustA, "s_subscopeclass", `"`+strings.ReplaceAll(p.Industry, ",", `","`)+`"`))
  323. }
  324. //附件
  325. if p.FileExists != 0 {
  326. switch p.FileExists {
  327. case 1: //有附件
  328. must = append(must, fmt.Sprintf(es.QueryBoolMustTermBool, "isValidFile", true))
  329. case -1: //无附件
  330. mustNot = append(mustNot, fmt.Sprintf(es.QueryBoolMustTermBool, "isValidFile", true))
  331. }
  332. }
  333. //项目地区
  334. var (
  335. areaQuery []string
  336. areaTerms = `{"terms":{"%s":["%s"]}}`
  337. )
  338. if p.ProjectArea != "" {
  339. areaQuery = append(areaQuery, fmt.Sprintf(areaTerms, "area", strings.ReplaceAll(p.ProjectArea, ",", "\",\"")))
  340. }
  341. if p.ProjectCity != "" {
  342. areaQuery = append(areaQuery, fmt.Sprintf(areaTerms, "city", strings.ReplaceAll(p.ProjectCity, ",", "\",\"")))
  343. }
  344. if p.ProjectDistrict != "" {
  345. for _, v := range strings.Split(p.ProjectDistrict, ",") {
  346. if len(strings.Split(v, "_")) > 1 {
  347. cityName := strings.Split(v, "_")[0]
  348. districtName := strings.Split(v, "_")[1]
  349. queryBoolMustAndDistrict := `{"bool":{"must":[{"terms":{"city":["%s"]}},{"terms":{"district":["%s"]}}]}}`
  350. areaQuery = append(areaQuery, fmt.Sprintf(queryBoolMustAndDistrict, cityName, districtName))
  351. }
  352. }
  353. }
  354. if len(areaQuery) > 0 {
  355. must = append(must, fmt.Sprintf(es.QueryBoolShould, strings.Join(areaQuery, ",")))
  356. }
  357. //交付地点
  358. areaQuery = []string{}
  359. if p.DeliveryArea != "" {
  360. areaQuery = append(areaQuery, fmt.Sprintf(areaTerms, "deliver_area", strings.ReplaceAll(p.DeliveryArea, ",", "\",\"")))
  361. }
  362. if p.DeliveryCity != "" {
  363. areaQuery = append(areaQuery, fmt.Sprintf(areaTerms, "deliver_city", strings.ReplaceAll(p.DeliveryCity, ",", "\",\"")))
  364. }
  365. if p.DeliveryDistrict != "" {
  366. for _, v := range strings.Split(p.DeliveryDistrict, ",") {
  367. if len(strings.Split(v, "_")) > 1 {
  368. cityName := strings.Split(v, "_")[0]
  369. districtName := strings.Split(v, "_")[1]
  370. queryBoolMustAndDistrict := `{"bool":{"must":[{"terms":{"deliver_city":["%s"]}},{"terms":{"deliver_district":["%s"]}}]}}`
  371. areaQuery = append(areaQuery, fmt.Sprintf(queryBoolMustAndDistrict, cityName, districtName))
  372. }
  373. }
  374. }
  375. if len(areaQuery) > 0 {
  376. must = append(must, fmt.Sprintf(es.QueryBoolShould, strings.Join(areaQuery, ",")))
  377. }
  378. //发布方式
  379. if p.Publisher > 0 {
  380. must = append(must, fmt.Sprintf(es.QueryBoolMustTerm, "public_type", fmt.Sprintf("%d", p.Publisher)))
  381. }
  382. //截止时间
  383. if p.DeadlineStart > 0 || p.DeadlineEnd > 0 {
  384. var timeQuery string
  385. timeQuery += `{"range":{"signendtime":{`
  386. if p.DeadlineStart > 0 {
  387. timeQuery += fmt.Sprintf(`"gte":%d`, p.DeadlineStart)
  388. }
  389. if p.DeadlineStart > 0 && p.DeadlineEnd > 0 {
  390. timeQuery += `,`
  391. }
  392. if p.DeadlineEnd > 0 {
  393. timeQuery += fmt.Sprintf(`"lt":%d`, p.DeadlineEnd)
  394. }
  395. timeQuery += `}}}`
  396. must = append(must, timeQuery)
  397. }
  398. //领域
  399. areaQuery = []string{}
  400. if p.DomainFirstType != "" {
  401. areaQuery = append(areaQuery, fmt.Sprintf(areaTerms, "domain_firsttype", strings.ReplaceAll(p.DomainFirstType, ",", "\",\"")))
  402. }
  403. if p.DomainSecondType != "" {
  404. areaQuery = append(areaQuery, fmt.Sprintf(areaTerms, "domain_secondtype", strings.ReplaceAll(p.DomainSecondType, ",", "\",\"")))
  405. }
  406. if p.DomainThirdType != "" {
  407. areaQuery = append(areaQuery, fmt.Sprintf(areaTerms, "domain_thirdtype", strings.ReplaceAll(p.DomainThirdType, ",", "\",\"")))
  408. }
  409. if len(areaQuery) > 0 {
  410. must = append(must, fmt.Sprintf(es.QueryBoolShould, strings.Join(areaQuery, ",")))
  411. }
  412. //直采 采购信息 搜索
  413. query = fmt.Sprintf(es.Query, strings.Join(must, ","), strings.Join(mustNot, ","))
  414. log.Println("zc-query:", query)
  415. return
  416. }
  417. func (p *Purchase) FindDataFromES() (int64, []map[string]interface{}, error) {
  418. var (
  419. start = int((p.PageNum - 1) * p.PageSize)
  420. total int64
  421. list *[]map[string]interface{}
  422. err error
  423. fields = es.PurchaseBaseFieldForPayer
  424. )
  425. if IC.C.PurchaseDiffSwitch {
  426. if !p.IsPay {
  427. fields = es.PurchaseBaseField
  428. }
  429. }
  430. biddingSearch := es.SearchByES{
  431. Index: es.PurchaseIndex,
  432. IType: es.PurchaseType,
  433. Query: p.PurchaseQuery(),
  434. FindFields: p.SelectType,
  435. Order: es.PurchaseSearchSort,
  436. Fields: fields,
  437. Start: start,
  438. Limit: int(p.PageSize),
  439. Count: 0, //高亮正文数量
  440. HighLight: false, //是否高亮正文
  441. }
  442. total, list = biddingSearch.GetAllByNgramWithCount(es.LoginTypePay)
  443. fmt.Println(total, "-------------------", list)
  444. if total == 0 || list == nil {
  445. return 0, nil, fmt.Errorf("暂无数据")
  446. }
  447. return total, *list, err
  448. }
  449. var (
  450. redisCode = "newother"
  451. purchaseCacheKey = "purchase_cache_data"
  452. purchaseCacheExpire = 4 * 60 * 60
  453. )
  454. func (p *Purchase) GetPurchaseData() (list []*bxcore.PurchaseList, err error) {
  455. var (
  456. res []map[string]interface{}
  457. total int64
  458. )
  459. if p.IsEmptySearch() {
  460. //查缓存
  461. cacheBytes, err := redis.GetBytes(redisCode, purchaseCacheKey)
  462. start := int((p.PageNum - 1) * p.PageSize)
  463. if err == nil && len(*cacheBytes) > start {
  464. end := int(p.PageNum * p.PageSize)
  465. if end > len(*cacheBytes) {
  466. end = len(*cacheBytes)
  467. }
  468. total = int64(len(*cacheBytes))
  469. err = json.Unmarshal((*cacheBytes)[start:end], &res)
  470. }
  471. if len(res) == 0 {
  472. p.PageNum = 1
  473. pageSize := p.PageSize
  474. start = int((p.PageNum - 1) * p.PageSize)
  475. end := int(p.PageNum * p.PageSize)
  476. p.PageSize = int64(util.SearchPageSize * util.SearchMaxPageNum)
  477. total, res, err = p.FindDataFromES()
  478. log.Println(total, "----------缓存数据:", len(res))
  479. if total > 0 && len(res) > 0 {
  480. var b []byte
  481. b, err = json.Marshal(res)
  482. log.Println(b, "----------------002---------------------", err)
  483. if err == nil && len(b) > 0 {
  484. err = redis.PutBytes(redisCode, purchaseCacheKey, &b, purchaseCacheExpire)
  485. }
  486. if end > len(res) {
  487. end = len(res)
  488. }
  489. res = res[start:end]
  490. }
  491. p.PageSize = pageSize
  492. }
  493. } else {
  494. //实时查询
  495. total, res, err = p.FindDataFromES()
  496. }
  497. if len(res) > 0 {
  498. p.Total = total
  499. total = int64(util.SearchPageSize * util.SearchMaxPageNum)
  500. if p.Total > total {
  501. p.Total = total
  502. }
  503. list = p.PurchaseListFormat(res)
  504. }
  505. return
  506. }
  507. // 关键词 附加词 行业
  508. func (p *Purchase) IsEmptySearch() bool {
  509. if p.KeyWords != "" || p.AdditionalWords != "" || p.Industry != "" {
  510. return false
  511. }
  512. return true
  513. }
  514. var (
  515. purchaseSelectTypeMap = map[string]bool{
  516. "title": true,
  517. "purchasing": true,
  518. }
  519. tipMsg = "“%s“及其后面的字词均被忽略,因为剑鱼标讯的查询限制在%d个汉字以内。"
  520. )
  521. // 筛选条件格式化
  522. func (p *Purchase) FilterCriteriaFormat() {
  523. if p.UserId != "" {
  524. //判断用户身份
  525. userInfo := IC.Middleground.PowerCheckCenter.Check(p.AppId, p.MgoUserId, p.BaseUserId, p.AccountId, p.EntId, p.PositionType, p.PositionId)
  526. if userInfo != nil {
  527. p.IsPay = !userInfo.Free.IsFree
  528. }
  529. res := IC.Middleground.ResourceCenter.Haspowers(p.AccountId, p.EntAccountId, p.EntId, p.EntUserId)
  530. if res != nil {
  531. p.IsPower = strings.Contains(strings.Join(res.Powers, ","), IC.C.PurchaseCode)
  532. }
  533. }
  534. //搜索范围
  535. if p.SelectType != "" {
  536. var selectTypes []string
  537. for _, sv := range strings.Split(p.SelectType, ",") {
  538. if purchaseSelectTypeMap[sv] {
  539. selectTypes = append(selectTypes, sv)
  540. }
  541. }
  542. if len(selectTypes) > 0 {
  543. p.SelectType = strings.Join(selectTypes, ",")
  544. }
  545. }
  546. if p.SelectType == "" {
  547. p.SelectType = "title"
  548. }
  549. // p.SearchMode 搜索模式:0:精准搜索;1:模糊搜索
  550. // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
  551. // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
  552. if p.SearchMode < 0 || p.SearchMode > 1 {
  553. p.SearchMode = 0
  554. }
  555. // p.WordsMode 搜索关键词模式;默认0:包含所有,1:包含任意
  556. if p.WordsMode < 0 || p.WordsMode > 1 {
  557. p.WordsMode = 0
  558. }
  559. //发布时间--默认最近一年
  560. if p.PublishTime == "" {
  561. p.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-1, 0, 0).Unix(), time.Now().Unix())
  562. }
  563. //默认每页数据量
  564. if p.PageSize <= 0 || p.PageSize > 100 {
  565. p.PageSize = 50
  566. }
  567. //第一页
  568. if p.PageNum <= 0 || p.PageNum > int64(IC.C.DefaultBidInfo.Count)/p.PageSize {
  569. p.PageNum = 1
  570. }
  571. //行业
  572. if p.Industry != "" {
  573. p.Industry = strings.TrimSpace(p.Industry)
  574. //P510 行业:其它
  575. if qt := jy.IndustryHandle(p.Industry); len(qt) > 0 {
  576. p.Industry = fmt.Sprintf("%s,%s", p.Industry, strings.Join(qt, ","))
  577. }
  578. }
  579. var (
  580. searchWords []string
  581. )
  582. //关键词
  583. if p.KeyWords != "" {
  584. p.KeyWords = strings.TrimSpace(p.KeyWords)
  585. _, p.TipMsg, p.KeyWords = util.InterceptSearchKW(p.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), true)
  586. if p.TipMsg != "" {
  587. p.TipMsg = fmt.Sprintf(tipMsg, p.TipMsg, MC.IntAllDef(IC.C.KeywordsLimit, 35))
  588. }
  589. // in.SearchMode 搜索模式:0:精准搜索;1:模糊搜索
  590. // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
  591. // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
  592. //主关键词词组
  593. if p.SearchMode == 1 {
  594. if ikWords := util.HttpEs(p.KeyWords, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
  595. p.KeyWords = jy.KeywordsProcessing(ikWords, IC.C.JYKeyMark)
  596. }
  597. }
  598. searchWords = append(searchWords, p.KeyWords)
  599. }
  600. //附加词 每组附加词不能超过15个字符
  601. if p.AdditionalWords != "" {
  602. var additionalWords []string
  603. for _, ak := range strings.Split(p.AdditionalWords, ",") {
  604. if len([]rune(ak)) > 15 {
  605. additionalWords = append(additionalWords, string([]rune(ak)[:15]))
  606. } else {
  607. additionalWords = append(additionalWords, ak)
  608. }
  609. }
  610. p.AdditionalWords = strings.Join(additionalWords, ",") //分组不变
  611. //多组附加词,每组间,号隔开。每组内如果关键词中间有空格,自动分词
  612. if p.SearchMode == 1 {
  613. var (
  614. addWords []string
  615. )
  616. for _, awv := range strings.Split(p.AdditionalWords, ",") {
  617. if strings.TrimSpace(awv) != "" {
  618. if ikWords := util.HttpEs(awv, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
  619. addWords = append(addWords, jy.KeywordsProcessing(ikWords, IC.C.JYKeyMark))
  620. }
  621. }
  622. }
  623. if len(addWords) > 0 {
  624. p.AdditionalWords = strings.Join(addWords, ",")
  625. }
  626. }
  627. searchWords = append(searchWords, strings.Split(p.AdditionalWords, ",")...)
  628. }
  629. p.HighlightWords = strings.Join(searchWords, ",")
  630. //报名截止
  631. if p.DeadlineStatus < 0 || p.DeadlineStatus > 2 {
  632. p.DeadlineStatus = 0
  633. }
  634. if len(strings.Split(p.DeadlineTime, "-")) > 1 {
  635. p.DeadlineStart, _ = strconv.ParseInt(strings.Split(p.DeadlineTime, "-")[0], 10, 64)
  636. p.DeadlineEnd, _ = strconv.ParseInt(strings.Split(p.DeadlineTime, "-")[1], 10, 64)
  637. if p.DeadlineEnd < p.DeadlineStart {
  638. var deadline = p.DeadlineStart
  639. p.DeadlineStart = p.DeadlineEnd
  640. p.DeadlineEnd = deadline
  641. }
  642. }
  643. switch p.DeadlineStatus {
  644. case 1: //未截止
  645. if p.DeadlineEnd < time.Now().Unix() {
  646. p.DeadlineStart = time.Now().Unix()
  647. p.DeadlineEnd = 0
  648. }
  649. case 2: //已截止
  650. if p.DeadlineEnd == 0 || p.DeadlineEnd > time.Now().Unix() {
  651. p.DeadlineStart = 0
  652. p.DeadlineEnd = time.Now().Unix()
  653. }
  654. }
  655. //空搜索 其它筛选条件 无效
  656. if p.IsEmptySearch() {
  657. //
  658. p.DomainFirstType = ""
  659. p.DomainSecondType = ""
  660. p.DomainThirdType = ""
  661. p.DeadlineStatus = 0
  662. p.DeadlineStart = 0
  663. p.DeadlineEnd = 0
  664. p.DeliveryArea = ""
  665. p.DeliveryCity = ""
  666. p.DeliveryDistrict = ""
  667. p.ProjectArea = ""
  668. p.ProjectCity = ""
  669. p.ProjectDistrict = ""
  670. p.FileExists = 0
  671. p.Publisher = 0
  672. }
  673. }