search.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. package public
  2. //
  3. //import (
  4. // "encoding/json"
  5. // "errors"
  6. // "fmt"
  7. // "log"
  8. // "math"
  9. // util "app.yhyue.com/moapp/jybase/common"
  10. // elastic "app.yhyue.com/moapp/jybase/esv1"
  11. // "qfw/util/jy"
  12. // "app.yhyue.com/moapp/jybase/redis"
  13. // "sort"
  14. // "strconv"
  15. // "strings"
  16. // "sync"
  17. // "time"
  18. //
  19. // "go.mongodb.org/mongo-driver/bson"
  20. //)
  21. //
  22. ///*筛选条件--关键词*/
  23. //type KeyWord struct {
  24. // Keyword string `json:"keyword"` //关键词
  25. // Appended []string `json:"appended"` //附加词
  26. // Exclude []string `json:"exclude"` //排除词
  27. //}
  28. //
  29. ///*筛选条件*/
  30. //type SieveCondition struct {
  31. // Id string `json:"id"`
  32. // PublishTime string `json:"publishtime"` //发布时间
  33. // Area []string `json:"area"` //地区-省份
  34. // City []string `json:"city"` //地区-城市
  35. // Region []string `json:"region"` //地区-省份+城市
  36. // Industry []string `json:"industry"` //行业
  37. // Keyword []KeyWord `json:"keywords"` //关键词
  38. // Buyer []string `json:"buyer"` //招标单位(采购单位)
  39. // Buyerclass []string `json:"buyerclass"` //采购单位类型
  40. // Winner []string `json:"winner"` //中标单位
  41. // ComeInTime int64 `json:"comeintime"` //入库时间(秒)
  42. // OpenId string `json:"openid"` //用户openid
  43. // MinPrice string `json:"minprice"` //金额——最少
  44. // MaxPrice string `json:"maxprice"` //金额——最多
  45. // SelectType string `json:"selectType"` //筛选(正文 or 标题)
  46. // Subtype string `json:"subtype"` //信息类型
  47. // Comeinfrom string `json:"comeinfrom"` //查询来源
  48. //}
  49. //
  50. //const (
  51. // INDEX = "bidding"
  52. // TYPE = "bidding"
  53. // bidSearch_sort = `{"publishtime":-1}`
  54. //)
  55. //
  56. //var ExportTable string = "export_search"
  57. //
  58. ////获取数据导出查询语句
  59. //func getDataExportSql(scd *SieveCondition) string {
  60. // multi_match := `{"multi_match": {"query": %s,"type": "phrase", "fields": [%s]}}`
  61. // query := `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}}}`
  62. // query_bool_should := `{"bool":{"should":[%s],"minimum_should_match": 1}}`
  63. // query_price := `{"bool":{"must":[{"range":{"bidamount":{%s}}}]}},{"bool":{"must":[{"range":{"budget":{%s}}}],"must_not":[{"range":{"bidamount":{"gte":-1}}}]}}`
  64. // query_bool_must := `{"terms":{"%s":[%s]}}`
  65. // query_bool_must_and := `{"bool":{"must":[%s]%s}}`
  66. // gte := `"gte": %s`
  67. // lte := `"lte": %s`
  68. //
  69. // bools := []string{}
  70. // musts := []string{fmt.Sprintf(`{"range":{"comeintime":{"lt":%d}}}`, scd.ComeInTime)}
  71. // //省份
  72. // areaCity := []string{}
  73. // if len(scd.Area) > 0 {
  74. // areaquery := `{"terms":{"area":[`
  75. // for k, v := range scd.Area {
  76. // if k > 0 {
  77. // areaquery += `,`
  78. // }
  79. // areaquery += `"` + v + `"`
  80. // }
  81. // areaquery += `]}}`
  82. // areaCity = append(areaCity, areaquery)
  83. // }
  84. // //城市
  85. // if len(scd.City) > 0 {
  86. // areaquery := `{"terms":{"city":[`
  87. // for k, v := range scd.City {
  88. // if k > 0 {
  89. // areaquery += `,`
  90. // }
  91. // areaquery += `"` + v + `"`
  92. // }
  93. // areaquery += `]}}`
  94. // areaCity = append(areaCity, areaquery)
  95. // }
  96. // if len(areaCity) > 0 {
  97. // musts = append(musts, fmt.Sprintf(query_bool_should, strings.Join(areaCity, ",")))
  98. // }
  99. // //检索日期
  100. //
  101. // starttime := ""
  102. // now := time.Unix(scd.ComeInTime, 0)
  103. // endtime := fmt.Sprintf("%d", now.Unix())
  104. // if scd.PublishTime == "lately-7" { //最近7天
  105. // starttime = fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local).Unix())
  106. // } else if scd.PublishTime == "lately-30" { //最近30天
  107. // starttime = fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day()-30, 0, 0, 0, 0, time.Local).Unix())
  108. // } else if scd.PublishTime == "thisyear" { //去年
  109. // starttime = fmt.Sprint(time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, time.Local).Unix())
  110. // endtime = fmt.Sprint(time.Date(now.Year()-1, 12, 31, 23, 59, 59, 0, time.Local).Unix())
  111. // } else if strings.Contains(scd.PublishTime, "_") { //设置检索日期
  112. // starttime = strings.Split(scd.PublishTime, "_")[0]
  113. // endTime_tmp := now
  114. // if etime := strings.Split(scd.PublishTime, "_")[1]; etime != "" {
  115. // etTime := time.Unix(util.Int64All(etime), 0)
  116. // endTime_tmp = time.Date(etTime.Year(), etTime.Month(), etTime.Day()+1, 0, 0, 0, 0, time.Local)
  117. // }
  118. // //结束时间必须小于筛选时间
  119. // if endTime_tmp.After(now) {
  120. // endTime_tmp = now
  121. // }
  122. // endtime = fmt.Sprintf("%d", endTime_tmp.Unix())
  123. // }
  124. // timequery := `{"range":{"publishtime":{`
  125. // if starttime != "" {
  126. // timequery += `"gte":` + starttime
  127. // }
  128. // if starttime != "" && endtime != "" {
  129. // timequery += `,`
  130. // }
  131. // if endtime != "" {
  132. // timequery += `"lt":` + endtime
  133. // }
  134. // timequery += `}}}`
  135. // musts = append(musts, timequery)
  136. //
  137. // if scd.Subtype != "" {
  138. // subquery := `{"terms":{"subtype":[`
  139. // for k, v := range strings.Split(scd.Subtype, ",") {
  140. // if k > 0 {
  141. // subquery += `,`
  142. // }
  143. // subquery += `"` + v + `"`
  144. // }
  145. // subquery += `]}}`
  146. // musts = append(musts, subquery)
  147. // }
  148. // if len(scd.Industry) > 0 {
  149. // musts = append(musts, fmt.Sprintf(query_bool_must, "s_subscopeclass", `"`+strings.Join(scd.Industry, `","`)+`"`))
  150. // }
  151. // if len(scd.Buyer) > 0 {
  152. // musts = append(musts, fmt.Sprintf(query_bool_must, "buyer", `"`+strings.Join(scd.Buyer, `","`)+`"`))
  153. // }
  154. // if len(scd.Buyerclass) > 0 {
  155. // musts = append(musts, fmt.Sprintf(query_bool_must, "buyerclass", `"`+strings.Join(scd.Buyerclass, `","`)+`"`))
  156. // }
  157. // if len(scd.Winner) > 0 {
  158. // musts = append(musts, fmt.Sprintf(query_bool_must, "s_winner", `"`+strings.Join(scd.Winner, `","`)+`"`))
  159. // }
  160. // _minPrice := ""
  161. // _maxPrice := ""
  162. // if scd.MinPrice != "" || scd.MaxPrice != "" {
  163. // sq := ``
  164. // if scd.MinPrice != "" {
  165. // min, _ := strconv.ParseFloat(scd.MinPrice, 64)
  166. // _minPrice = fmt.Sprintf("%.0f", min*10000)
  167. // if _minPrice == "0" {
  168. // _minPrice = ""
  169. // }
  170. // }
  171. // if scd.MaxPrice != "" {
  172. // max, _ := strconv.ParseFloat(scd.MaxPrice, 64)
  173. // _maxPrice = fmt.Sprintf("%.0f", max*10000)
  174. // if _maxPrice == "0" {
  175. // _maxPrice = ""
  176. // }
  177. // }
  178. // if _minPrice != "" {
  179. // sq += fmt.Sprintf(gte, _minPrice)
  180. // }
  181. // if _minPrice != "" && _maxPrice != "" {
  182. // sq += `,`
  183. // }
  184. // if _maxPrice != "" {
  185. // sq += fmt.Sprintf(lte, _maxPrice)
  186. // }
  187. // if _minPrice != "" || _maxPrice != "" {
  188. // query_price := fmt.Sprintf(query_bool_should, fmt.Sprintf(query_price, sq, sq))
  189. // musts = append(musts, query_price)
  190. // }
  191. // }
  192. // boolsNum := 0
  193. // //should
  194. // if len(scd.Keyword) > 0 {
  195. // boolsNum = 1
  196. // if scd.SelectType == "" || scd.SelectType == "all" {
  197. // scd.SelectType = "detail\", \"title"
  198. // }
  199. // multi_match = fmt.Sprintf(multi_match, "%s", "\""+scd.SelectType+"\"")
  200. //
  201. // if scd.Comeinfrom == "supersearchPage" {
  202. // var keywordArr []string
  203. // if strings.Contains(scd.Keyword[0].Keyword, "+") {
  204. // keywordArr = strings.Split(scd.Keyword[0].Keyword, "+")
  205. // } else if strings.Contains(scd.Keyword[0].Keyword, " ") {
  206. // keywordArr = strings.Split(scd.Keyword[0].Keyword, " ")
  207. // }
  208. // if len(keywordArr) > 1 {
  209. // KeyWordSearch := KeyWord{}
  210. // for _, v := range keywordArr {
  211. // KeyWordSearch.Appended = append(KeyWordSearch.Appended, v)
  212. // }
  213. // scd.Keyword = []KeyWord{KeyWordSearch}
  214. // }
  215. // }
  216. //
  217. // for _, v := range scd.Keyword {
  218. // shoulds := []string{}
  219. // must_not := []string{}
  220. // //附加词
  221. // if v.Keyword != "" {
  222. // shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+v.Keyword+"\""))
  223. // }
  224. //
  225. // for _, vv := range v.Appended {
  226. // shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
  227. // }
  228. //
  229. // //排除词
  230. // for _, vv := range v.Exclude {
  231. // must_not = append(must_not, fmt.Sprintf(multi_match, "\""+vv+"\""))
  232. // }
  233. //
  234. // //添加
  235. // if len(shoulds) > 0 {
  236. // notStr := ""
  237. // if len(must_not) > 0 {
  238. // notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
  239. // }
  240. // bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(shoulds, ","), notStr))
  241. // }
  242. // }
  243. // }
  244. // qstr := fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","), boolsNum)
  245. // return qstr
  246. //}
  247. //
  248. //func getSqlObjFromId(_id string) *SieveCondition {
  249. // var (
  250. // query *map[string]interface{}
  251. // ok bool
  252. // )
  253. // if query, ok = MQFW.FindById(ExportTable, _id, nil); !ok {
  254. // return nil
  255. // }
  256. // return &SieveCondition{
  257. // Id: _id,
  258. // Keyword: getKeyWordArrFromDbResult((*query)["keywords"]),
  259. // Industry: getStringArrFromDbResult((*query)["industry"]),
  260. // MinPrice: util.ObjToString((*query)["minprice"]),
  261. // MaxPrice: util.ObjToString((*query)["maxprice"]),
  262. // Subtype: util.ObjToString((*query)["subtype"]),
  263. // Area: getStringArrFromDbResult((*query)["area"]),
  264. // City: getStringArrFromDbResult((*query)["city"]),
  265. // SelectType: util.ObjToString((*query)["selectType"]),
  266. // PublishTime: util.ObjToString((*query)["publishtime"]),
  267. // Buyer: getStringArrFromDbResult((*query)["buyer"]),
  268. // Buyerclass: getStringArrFromDbResult((*query)["buyerclass"]),
  269. // Winner: getStringArrFromDbResult((*query)["winner"]),
  270. // ComeInTime: util.Int64All((*query)["comeintime"]),
  271. // Comeinfrom: util.ObjToString((*query)["comeinfrom"]),
  272. // }
  273. //}
  274. //
  275. ////数据导出-查询结果数量
  276. //func GetDataExportSearchCountUseId(_id string) (count int) {
  277. // scd := getSqlObjFromId(_id)
  278. // qstr := getDataExportSql(scd)
  279. // log.Printf("GetDataExportSearchCountUseId-%s-count:%d-sql:%s\n", _id, count, qstr)
  280. // if isNullSearch(scd) {
  281. // return ExConf.MsgMaxCount
  282. // }
  283. // count = int(elastic.Count(INDEX, TYPE, qstr))
  284. // //超级搜索一致的检索(防止数据导出和超级搜索数据量不一致)
  285. // if scd.Comeinfrom == "supersearchPage" && (len(scd.Keyword) != 0 || len(scd.Industry) != 0) {
  286. // if len(scd.Keyword) != 0 {
  287. // searchTextSize := 0
  288. // if len(scd.Keyword) > 0 {
  289. // searchTextSize = len([]rune(scd.Keyword[0].Keyword))
  290. // }
  291. // if searchTextSize > 3 && count < 50 {
  292. // var res *[]map[string]interface{}
  293. // if count > 0 {
  294. // res = doSearch(qstr, 0, count, "")
  295. // }
  296. // secondKWS := jy.HttpEs(scd.Keyword[0].Keyword, "ik_smart", DbConf.Elasticsearch.Main.Address)
  297. // scd.Keyword[0].Keyword = secondKWS
  298. // scd.SelectType = "title"
  299. // qstr = getDataExportSql(scd)
  300. // res2 := doSearch(qstr, 0, 100, "")
  301. // result := len(*delRepeatMapArr(res, res2))
  302. // if result > 100 {
  303. // result = 100
  304. // }
  305. // log.Printf("GetDataExportSearchCountUseId-%s-count:%d-分词-sql:%s\n", _id, result, qstr)
  306. // return result
  307. // }
  308. // return
  309. // }
  310. // }
  311. // log.Printf("GetDataExportSearchCountUseId-%s-count:%d\n", _id, count)
  312. // return
  313. //}
  314. //
  315. ////合并map数据,去重
  316. //func delRepeatMapArr(res *[]map[string]interface{}, res2 *[]map[string]interface{}) *[]map[string]interface{} {
  317. // if res != nil {
  318. // for _, v := range *res {
  319. // for n, m := range *res2 {
  320. // if util.ObjToString(v["_id"]) == util.ObjToString(m["_id"]) {
  321. // *res2 = append((*res2)[0:n], (*res2)[n+1:]...)
  322. // break
  323. // }
  324. // }
  325. // }
  326. // *res = append(*res, *res2...)
  327. // } else {
  328. // res = res2
  329. // }
  330. // return res
  331. //}
  332. //
  333. ////查询条件是否为空
  334. //func isNullSearch(scd *SieveCondition) (isNull bool) {
  335. // if scd.PublishTime == "" && len(scd.Area) == 0 && len(scd.Industry) == 0 && len(scd.Keyword) == 0 && len(scd.Buyer) == 0 && len(scd.Winner) == 0 && scd.MinPrice == "" && scd.MaxPrice == "" && scd.Subtype == "" && len(scd.City) == 0 {
  336. // isNull = true
  337. // }
  338. // return isNull
  339. //}
  340. //
  341. ///*
  342. // * 数据导出 查询结果
  343. // * _id 数据库查询条件记录id
  344. // * dataType 1-普通字段 2-高级字段
  345. // * webdomain 三级页域名
  346. // * count 返回数量 (-1:预览数据查询)
  347. // */
  348. //var EntTable = "winner_enterprise"
  349. //
  350. //func GetDataExportSearchResultUseId(_id, dataType string, count int) (*[]map[string]interface{}, []KeyWord) {
  351. // defer util.Catch()
  352. // var res []map[string]interface{}
  353. // var kws []KeyWord
  354. // scd := getSqlObjFromId(_id)
  355. // //获取查询语句
  356. // qstr := getDataExportSql(scd)
  357. // log.Printf("GetDataExportSearchResultUseId-%s-sql:%s\n", scd.Id, qstr)
  358. // kws = scd.Keyword
  359. // //数据预览数据查询
  360. // if scd.Comeinfrom == "supersearchPage" && len(scd.Keyword) == 0 && len(scd.Industry) == 0 {
  361. // //空查询
  362. // obj := redis.Get("other", "export_news")
  363. // if obj != nil {
  364. // res = util.ObjArrToMapArr(obj.([]interface{}))
  365. // } else {
  366. // res = *doSearch(qstr, 0, 500, "2")
  367. // if len(res) > 0 {
  368. // redis.Put("other", "export_news", res, 7200)
  369. // }
  370. // }
  371. // return &res, kws
  372. // } else if scd.Comeinfrom == "supersearchPage" {
  373. // //超级搜索非空查询
  374. // count = int(elastic.Count(INDEX, TYPE, qstr))
  375. // searchTextSize := 0
  376. // if len(scd.Keyword) > 0 {
  377. // searchTextSize = len([]rune(scd.Keyword[0].Keyword))
  378. // }
  379. // if searchTextSize > 3 && count < 50 {
  380. // var res *[]map[string]interface{}
  381. // if count > 0 {
  382. // res = doSearch(qstr, 0, count, "")
  383. // }
  384. // secondKWS := jy.HttpEs(scd.Keyword[0].Keyword, "ik_smart", DbConf.Elasticsearch.Main.Address)
  385. // scd.Keyword[0].Keyword = secondKWS
  386. // scd.SelectType = "title"
  387. // qstr = getDataExportSql(scd)
  388. // log.Printf("GetDataExportSearchResultUseId-%s-分词-sql:%s\n", scd.Id, qstr)
  389. // res2 := doSearch(qstr, 0, 100, "")
  390. // if len(*res2) > 100 {
  391. // res2_temp := (*res2)[:100]
  392. // res2 = &res2_temp
  393. // }
  394. // return delRepeatMapArr(res, res2), kws
  395. // }
  396. // }
  397. // //非空查询
  398. // res = *doSearch(qstr, 0, 500, dataType)
  399. // return &res, kws
  400. //}
  401. //
  402. //func FormatExportData(data *[]map[string]interface{}, webdomain string, dataType string, EntArr []string) *[]map[string]interface{} {
  403. // //格式化输出
  404. // for _, v := range *data {
  405. // //有中标企业 且 高级字段查询
  406. // if len(EntArr) > 0 && dataType == "2" {
  407. // //查询企业公示 法人 公司电话 公司邮箱地址
  408. // query := bson.M{"company_name": bson.M{"$in": EntArr}} //
  409. // if entData, ok := Mgo_Ent.Find(EntTable, query, nil, `{"company_name":1,"company_email":1,"legal_person":1,"company_phone":1}`, false, -1, -1); ok {
  410. // if entData != nil && *entData != nil && len(*entData) > 0 {
  411. // for _, ev := range *entData {
  412. // if v["s_winner"] == ev["company_name"] {
  413. // legal_person := ""
  414. // if ev["legal_person"] != nil && ev["legal_person"].(string) != "" {
  415. // legal_person = ev["legal_person"].(string)
  416. // var xx = "*"
  417. // switch len([]rune(legal_person)) {
  418. // case 3:
  419. // xx = "**"
  420. // case 4:
  421. // xx = "***"
  422. // }
  423. // legal_person = string([]rune(legal_person)[:1]) + xx
  424. // }
  425. // company_phone := ""
  426. // if ev["company_phone"] != nil && ev["company_phone"].(string) != "" {
  427. // company_phone = ev["company_phone"].(string)
  428. // if len([]rune(company_phone)) > 7 {
  429. // company_phone = company_phone[:7] + "****"
  430. // } else {
  431. // company_phone = "****"
  432. // }
  433. // }
  434. // company_email := ""
  435. // if ev["company_email"] != nil && ev["company_email"].(string) != "无" {
  436. // company_email = ev["company_email"].(string)
  437. // if len(strings.Split(company_email, "@")) > 1 {
  438. // company_email = "******" + "@" + strings.Split(company_email, "@")[1]
  439. // }
  440. // }
  441. // v["legal_person"] = legal_person
  442. // v["company_phone"] = company_phone
  443. // v["company_email"] = company_email
  444. // }
  445. // }
  446. // }
  447. // }
  448. // }
  449. // //====================字段补漏=========================
  450. // if v["toptype"] == "结果" && dataType == "2" && !(v["agency"] != nil && v["budget"] != nil && v["buyerperson"] != nil && v["buyertel"] != nil) {
  451. // r := elastic.Get("projectset", "projectset", fmt.Sprintf(`{"query":{"term":{"list.infoid":"%s"}},"_source": ["list"]}`, v["_id"]))
  452. // if len(*r) > 0 {
  453. // MsgList := (*r)[0]["list"]
  454. // if MsgList != nil {
  455. // list := util.ObjArrToMapArr(MsgList.([]interface{}))
  456. // for _, vv := range list {
  457. // if vv["subtype"] == "招标" {
  458. // if v["agency"] == nil && vv["agency"] != nil {
  459. // v["agency"] = vv["agency"]
  460. // }
  461. // if v["budget"] == nil && vv["budget"] != nil {
  462. // v["budget"] = vv["budget"]
  463. // }
  464. // if v["buyerperson"] == nil && vv["buyerperson"] != nil {
  465. // v["buyerperson"] = vv["buyerperson"]
  466. // }
  467. // if v["buyertel"] == nil && vv["buyertel"] != nil {
  468. // v["buyertel"] = vv["buyertel"]
  469. // }
  470. // break
  471. // }
  472. // }
  473. // }
  474. // }
  475. // }
  476. // if v["area"] == "A" {
  477. // v["area"] = "全国"
  478. // }
  479. // if v["bidamount"] != nil {
  480. // v["bidamount"] = formatFloat(util.Float64All(v["bidamount"]))
  481. // }
  482. // if v["budget"] != nil {
  483. // v["budget"] = formatFloat(util.Float64All(v["budget"]))
  484. // }
  485. // if v["publishtime"] != nil {
  486. // date := v["publishtime"]
  487. // v["publishtime"] = util.FormatDateWithObj(&date, util.Date_Short_Layout)
  488. // }
  489. // if v["bidopentime"] != nil {
  490. // date := v["bidopentime"]
  491. // v["bidopentime"] = util.FormatDateWithObj(&date, util.Date_Short_Layout)
  492. // }
  493. // if v["_id"] != nil {
  494. // v["url"] = webdomain + "/article/content/" + util.CommonEncodeArticle("content", v["_id"].(string)) + ".html"
  495. // }
  496. // if v["currency"] == "" || v["currency"] == nil {
  497. // v["currency"] = "人民币"
  498. // }
  499. // if v["projectscope"] != "" && v["projectscope"] != nil {
  500. // str := ClearHtml.ReplaceAllString(v["projectscope"].(string), "")
  501. // str = ClearOther.ReplaceAllString(str, "")
  502. // str = strings.Replace(str, " ", "", -1)
  503. // if len([]rune(str)) > 100 {
  504. // str = util.SubString(str, 0, 100) + "..."
  505. // }
  506. // v["projectscope"] = str
  507. // }
  508. // if v["detail"] != "" && v["detail"] != nil {
  509. // str := ClearHtml.ReplaceAllString(v["detail"].(string), "")
  510. // str = ClearOther.ReplaceAllString(str, "")
  511. // str = strings.Replace(str, " ", "", -1)
  512. // if len([]rune(str)) > 100 {
  513. // str = util.SubString(str, 0, 100) + "..."
  514. // }
  515. // v["detail"] = str
  516. // }
  517. // if v["title"] != "" && v["title"] != nil {
  518. // str := ClearHtml.ReplaceAllString(v["title"].(string), "")
  519. // str = ClearOther.ReplaceAllString(str, "")
  520. // str = strings.Replace(str, " ", "", -1)
  521. // if len([]rune(str)) > 100 {
  522. // str = util.SubString(str, 0, 100) + "..."
  523. // }
  524. // v["title"] = str
  525. // }
  526. // if v["subtype"] == nil && v["toptype"] != nil {
  527. // v["subtype"] = v["toptype"]
  528. // }
  529. // }
  530. // return data
  531. //}
  532. //
  533. ////保留到0.01分
  534. //func formatFloat(value float64) string {
  535. // str := strings.TrimRight(fmt.Sprintf("%.7f", value*10000/100000000), "0")
  536. // if str[len(str)-1:] == "." {
  537. // return str[:len(str)-1]
  538. // }
  539. // return str
  540. //}
  541. //
  542. //func FormatExportDatas(data *[]map[string]interface{}, webdomain string, dataType string, entId int) *[]map[string]interface{} {
  543. // //格式化输出
  544. // var (
  545. // entexportPool = make(chan bool, 20)
  546. // entexportWaitGroup = &sync.WaitGroup{}
  547. // // newsDatalen = make(chan bool, 20000)
  548. // // datalock = sync.RWMutex{}
  549. // )
  550. // log.Println("补充信息开始")
  551. // for _, v := range *data {
  552. // entexportWaitGroup.Add(1)
  553. // entexportPool <- true
  554. // go func(v map[string]interface{}) {
  555. // defer func() {
  556. // entexportWaitGroup.Done()
  557. // <-entexportPool
  558. // // datalock.Unlock()
  559. // }()
  560. // // datalock.Lock()
  561. // //二次验证
  562. // // id := util.ObjToString(v["_id"])
  563. // // isExist, err := redis.Exists("other", "entexportdata_"+id+"_"+fmt.Sprintln(entId))
  564. // // if err != nil {
  565. // // log.Println("企业搜索数据导出redis判重失败")
  566. // // } else if !isExist {
  567. // // newsDatalen <- true
  568. // // }
  569. // //
  570. // //有中标企业 且 高级字段查询
  571. // if dataType == "2" {
  572. // //查询企业公示 法人 公司电话 公司邮箱地址
  573. // // query := bson.M{"company_name": bson.M{"$in": EntArr}} //
  574. // s_winner := strings.Split(util.ObjToString(v["s_winner"]), ",")[0]
  575. // query := bson.M{"company_name": s_winner} //
  576. // if entData, ok := Mgo_Ent.Find(EntTable, query, nil, `{"company_name":1,"company_email":1,"legal_person":1,"company_phone":1}`, false, -1, -1); ok {
  577. // if entData != nil && *entData != nil && len(*entData) > 0 {
  578. // for _, ev := range *entData {
  579. // if v["s_winner"] == ev["company_name"] {
  580. // legal_person := ""
  581. // if ev["legal_person"] != nil {
  582. // legal_person = ev["legal_person"].(string)
  583. // }
  584. // company_phone := ""
  585. // if ev["company_phone"] != nil {
  586. // company_phone = ev["company_phone"].(string)
  587. // }
  588. // company_email := ""
  589. // if ev["company_email"] != nil && ev["company_email"] != "无" {
  590. // company_email = ev["company_email"].(string)
  591. // }
  592. // v["legal_person"] = legal_person
  593. // v["company_phone"] = company_phone
  594. // v["company_email"] = company_email
  595. // }
  596. // }
  597. // }
  598. // }
  599. // }
  600. // //====================字段补漏=========================
  601. // if v["toptype"] == "结果" && dataType == "2" && !(v["agency"] != nil && v["budget"] != nil && v["buyerperson"] != nil && v["buyertel"] != nil) {
  602. // r := elastic.Get("projectset", "projectset", fmt.Sprintf(`{"query":{"term":{"list.infoid":"%s"}},"_source": ["list"]}`, v["_id"]))
  603. // if len(*r) > 0 {
  604. // MsgList := (*r)[0]["list"]
  605. // if MsgList != nil {
  606. // list := util.ObjArrToMapArr(MsgList.([]interface{}))
  607. // for _, vv := range list {
  608. // if vv["subtype"] == "招标" {
  609. // if v["agency"] == nil && vv["agency"] != nil {
  610. // v["agency"] = vv["agency"]
  611. // }
  612. // if v["budget"] == nil && vv["budget"] != nil {
  613. // v["budget"] = vv["budget"]
  614. // }
  615. // if v["buyerperson"] == nil && vv["buyerperson"] != nil {
  616. // v["buyerperson"] = vv["buyerperson"]
  617. // }
  618. // if v["buyertel"] == nil && vv["buyertel"] != nil {
  619. // v["buyertel"] = vv["buyertel"]
  620. // }
  621. // break
  622. // }
  623. // }
  624. // }
  625. // }
  626. // }
  627. // if v["area"] == "A" {
  628. // v["area"] = "全国"
  629. // }
  630. // // if v["bidamount"] != nil {
  631. // // v["bidamount"] = fmt.Sprintf("%g", util.Float64All(v["bidamount"]))
  632. // // }
  633. // // if v["budget"] != nil {
  634. // // v["budget"] = fmt.Sprintf("%g", util.Float64All(v["budget"]))
  635. // // }
  636. // if v["publishtime"] != nil {
  637. // date := v["publishtime"]
  638. // v["publishtime"] = util.FormatDateWithObj(&date, util.Date_Short_Layout)
  639. // }
  640. // if v["bidopentime"] != nil {
  641. // date := v["bidopentime"]
  642. // v["bidopentime"] = util.FormatDateWithObj(&date, util.Date_Short_Layout)
  643. // }
  644. // if v["currency"] == "" || v["currency"] == nil {
  645. // v["currency"] = "人民币"
  646. // }
  647. // if v["subtype"] == nil && v["toptype"] != nil {
  648. // v["subtype"] = v["toptype"]
  649. // }
  650. // if v["detail"] != "" && v["detail"] != nil {
  651. // str := ClearHtml.ReplaceAllString(v["detail"].(string), "")
  652. // str = ClearOther.ReplaceAllString(str, "")
  653. // str = strings.Replace(str, " ", "", -1)
  654. // v["detail"] = str
  655. // }
  656. // if v["_id"] != nil {
  657. // v["url"] = webdomain + "/article/content/" + util.CommonEncodeArticle("content", v["_id"].(string)) + ".html"
  658. // }
  659. // }(v)
  660. // }
  661. // entexportWaitGroup.Wait()
  662. // log.Println("补充信息结束")
  663. // return data
  664. //}
  665. //
  666. //func doSearch(sql string, start, count int, dataType string) *[]map[string]interface{} {
  667. // if sql != "" {
  668. // //筛选字段
  669. // if dataType != "" {
  670. // dataexport_field := `"_id","title","detail","area","city","publishtime","projectname","buyer","s_winner","bidamount","subtype","toptype"`
  671. // if dataType == "2" {
  672. // dataexport_field += `,"href","projectcode","buyerperson","buyertel","budget","bidopentime","agency","projectscope","winnerperson","winnertel"`
  673. // }
  674. // sql = sql[:len(sql)-1] + `,"_source":[` + dataexport_field + "]}"
  675. // }
  676. // //分页排序
  677. // sql = sql[:len(sql)-1] + `,"sort": {"publishtime":"desc"},"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(count) + "}"
  678. // }
  679. // return elastic.Get(INDEX, TYPE, sql)
  680. //}
  681. //
  682. //func getKeyWordArrFromDbResult(k interface{}) (arr []KeyWord) {
  683. // if k == nil {
  684. // return
  685. // }
  686. // kArr := k.([]interface{})
  687. // for _, v := range kArr {
  688. // kw := KeyWord{}
  689. // b, e := json.Marshal(v)
  690. // if e != nil {
  691. // log.Println(e.Error())
  692. // }
  693. // json.Unmarshal(b, &kw)
  694. // arr = append(arr, kw)
  695. // }
  696. // return
  697. //}
  698. //
  699. //func getStringArrFromDbResult(c interface{}) (arr []string) {
  700. // if c != nil {
  701. // cArr := c.([]interface{})
  702. // arr = util.ObjArrToStringArr(cArr)
  703. // }
  704. // return
  705. //}
  706. //
  707. ////获取结果,空字段最少的数据
  708. //func ScreenData(arr *[]map[string]interface{}, dataType string, resultNum int, kws []KeyWord) (res []map[string]interface{}) {
  709. // AllMap := map[int][]map[string]interface{}{}
  710. //
  711. // NoKwsMap := map[int][]map[string]interface{}{}
  712. // lastNum := resultNum
  713. // for _, v := range *arr {
  714. // emptyNum := countOfTheEmpty(v, dataType)
  715. // if emptyNum == -1 {
  716. // continue
  717. // }
  718. // if len(kws) > 0 && kws[0].Keyword != "" {
  719. // var kwsFlag = true
  720. // for _, vk := range kws {
  721. // if strings.Contains(util.ObjToString(v["title"]), strings.Replace(vk.Keyword, "+", "", -1)) {
  722. // kwsFlag = false
  723. // continue
  724. // }
  725. // }
  726. // if kwsFlag {
  727. // if NoKwsMap[emptyNum] == nil {
  728. // NoKwsMap[emptyNum] = []map[string]interface{}{v}
  729. // } else {
  730. // NoKwsMap[emptyNum] = append(NoKwsMap[emptyNum], v)
  731. // }
  732. // continue
  733. // }
  734. // }
  735. // if AllMap[emptyNum] == nil {
  736. // AllMap[emptyNum] = []map[string]interface{}{v}
  737. // continue
  738. // }
  739. // AllMap[emptyNum] = append(AllMap[emptyNum], v)
  740. // }
  741. // //获取key
  742. // keys := []int{}
  743. // for k, _ := range AllMap {
  744. // keys = append(keys, k)
  745. // }
  746. // sort.Ints(keys)
  747. // log.Println("空字段数量", keys)
  748. // //选取结果
  749. // for _, v := range keys {
  750. // if len(AllMap[v]) >= resultNum {
  751. // return append(res, AllMap[v][:resultNum]...)
  752. // } else {
  753. // resultNum = resultNum - len(AllMap[v])
  754. // tmp := append(res, AllMap[v][:len(AllMap[v])]...)
  755. // res = tmp
  756. // }
  757. // }
  758. // if len(res) < lastNum {
  759. // resultNum = lastNum - len(res)
  760. // //获取key
  761. // Nokeys := []int{}
  762. // for k, _ := range NoKwsMap {
  763. // Nokeys = append(Nokeys, k)
  764. // }
  765. // sort.Ints(Nokeys)
  766. // log.Println("没关键词的空字段数量", Nokeys)
  767. // //选取结果
  768. // for _, v := range Nokeys {
  769. // if len(NoKwsMap[v]) >= resultNum {
  770. // return append(res, NoKwsMap[v][:resultNum]...)
  771. // } else {
  772. // resultNum = resultNum - len(NoKwsMap[v])
  773. // tmp := append(res, NoKwsMap[v][:len(NoKwsMap[v])]...)
  774. // res = tmp
  775. // }
  776. // }
  777. // }
  778. // return res
  779. //}
  780. //func countOfTheEmpty(m map[string]interface{}, dataType string) int {
  781. // MsgType := m["subtype"]
  782. // // if MsgType == "拟建" {
  783. // // return -1
  784. // // }
  785. // //计算空字段数量
  786. // var count int = 0
  787. // //高级字段包
  788. // if dataType == "2" {
  789. // if m["href"] == "" || m["href"] == nil {
  790. // count++
  791. // }
  792. // if m["projectcode"] == "" || m["projectcode"] == nil {
  793. // count++
  794. // }
  795. // if m["buyerperson"] == "" || m["buyerperson"] == nil {
  796. // count++
  797. // }
  798. // if m["buyertel"] == "" || m["buyertel"] == nil {
  799. // count++
  800. // }
  801. // if m["budget"] == "" || m["budget"] == nil {
  802. // count++
  803. // }
  804. // if m["bidopentime"] == "" || m["bidopentime"] == nil {
  805. // count++
  806. // }
  807. // if m["agency"] == "" || m["agency"] == nil {
  808. // count++
  809. // }
  810. // if m["projectscope"] == "" || m["projectscope"] == nil {
  811. // count++
  812. // }
  813. // }
  814. // if m["city"] == "" || m["city"] == nil {
  815. // count++
  816. // }
  817. // if m["publishtime"] == "" || m["publishtime"] == nil {
  818. // count++
  819. // }
  820. // if m["projectname"] == "" || m["projectname"] == nil {
  821. // count++
  822. // }
  823. // if m["buyer"] == "" || m["buyer"] == nil {
  824. // count++
  825. // }
  826. // if m["s_winner"] == "" || m["s_winner"] == nil {
  827. // if MsgType != "招标" {
  828. // count++
  829. // }
  830. // }
  831. // if m["bidamount"] == "" || m["bidamount"] == nil {
  832. // if MsgType != "招标" {
  833. // count++
  834. // }
  835. // }
  836. // if m["subtype"] == "" || m["subtype"] == nil {
  837. // count++
  838. // }
  839. // return count
  840. //}
  841. //
  842. //func GetEntDataExportCount(_id string, entId, entUserId, limitNum, current int, isFirst bool) (count, newCount int, data *[]map[string]interface{}) {
  843. // defer util.Catch()
  844. // var (
  845. // searchsWaitGroup = &sync.WaitGroup{}
  846. // searchsPool = make(chan bool, 20)
  847. // // res = &[]map[string]interface{}{}
  848. // newCountPool = make(chan bool, 20000)
  849. // )
  850. // count = GetDataExportSearchCountUseId(_id)
  851. // log.Println("count", count)
  852. // if count > ExConf.MsgMaxCount {
  853. // count = ExConf.MsgMaxCount
  854. // }
  855. // dataType := "2"
  856. // //数据导出数据查询
  857. //
  858. // res, err := GetDataExportSearchResult(_id, dataType, count)
  859. // if err != nil {
  860. // log.Println("企业数据导出错误 ", err)
  861. // return 0, 0, nil
  862. // }
  863. // // secondCount := 0
  864. //
  865. // // isOK := true
  866. // // if secondCount > current {
  867. // // isOK = false
  868. // // }
  869. // for _, v := range *res {
  870. // searchsWaitGroup.Add(1)
  871. // searchsPool <- true
  872. // id := util.ObjToString(v["_id"])
  873. // go func(id string) {
  874. // defer func() {
  875. // searchsWaitGroup.Done()
  876. // <-searchsPool
  877. // }()
  878. // isExist, err := redis.Exists("other", "entexportdata_"+id+"_"+fmt.Sprintln(entId))
  879. // if err != nil {
  880. // log.Println("企业搜索数据导出redis判重失败")
  881. // } else if isExist {
  882. // log.Println("数据重复,id ", id, "entid ", entId, "userid ", entUserId)
  883. // return
  884. // }
  885. // newCountPool <- true
  886. // if !isFirst {
  887. // redis.Put("other", "entexportdata_"+id+"_"+fmt.Sprintln(entId), 1, -1)
  888. // }
  889. // }(id)
  890. // if !isFirst {
  891. // delete(v, "_id")
  892. // v["entid"] = entId
  893. // v["userid"] = entUserId
  894. // v["infoid"] = id
  895. // v["createtime"] = time.Now().Unix()
  896. // }
  897. // }
  898. // searchsWaitGroup.Wait()
  899. // log.Println("企业数据导出--数据遍历完成")
  900. // newCount = len(newCountPool)
  901. // log.Println("new", newCount)
  902. // data = res
  903. // return
  904. //}
  905. //
  906. //func GetDataExportSearchResult(id string, dataType string, checkCount int) (*[]map[string]interface{}, error) {
  907. // defer util.Catch()
  908. // var (
  909. // onceSearchCount = 500
  910. // searchPool = make(chan bool, 20)
  911. // res []map[string]interface{}
  912. // )
  913. // //获取查询语句
  914. // scd := getSqlObjFromId(id)
  915. // if scd == nil {
  916. // return nil, errors.New("GetDataExportSearchResult-获取查询条件")
  917. // }
  918. // qstr := getDataExportSql(scd)
  919. // log.Printf("GetDataExportSearchResult-%s-sql:%s\n", scd.Id, qstr)
  920. // //数据导出数据查询
  921. // if checkCount > onceSearchCount { //分批次查询
  922. // batchNum := util.IntAll(math.Ceil(float64(checkCount) / float64(onceSearchCount)))
  923. // var searchWaitGroup = &sync.WaitGroup{}
  924. // var lock sync.Mutex
  925. // for n := 0; n < batchNum; n++ {
  926. // searchWaitGroup.Add(1)
  927. // searchPool <- true
  928. // go func(start int) {
  929. // defer func() {
  930. // searchWaitGroup.Done()
  931. // <-searchPool
  932. // }()
  933. // checkNum, checkOk := onceSearchCount, false
  934. // if start == (batchNum - 1) {
  935. // if checkCount%onceSearchCount != 0 {
  936. // checkNum = checkCount % onceSearchCount
  937. // }
  938. // }
  939. //
  940. // var tmp *[]map[string]interface{}
  941. // for i := 0; i < 3; i++ {
  942. // tmp = doSearch(qstr, start*onceSearchCount, onceSearchCount, dataType)
  943. // if tmp != nil && (len(*tmp) == checkNum) { //校验数据量是否够
  944. // checkOk = true
  945. // break
  946. // }
  947. // }
  948. // if tmp == nil {
  949. // log.Printf("GetDataExportSearchResult-%s-第%d页数据查询结果为空\n", scd.Id, start+1)
  950. // return
  951. // }
  952. // if checkOk {
  953. // log.Printf("GetDataExportSearchResult-%s-第%d页数据加载完成,共%d条\n", scd.Id, start+1, len(*tmp))
  954. // } else {
  955. // log.Printf("GetDataExportSearchResult-%s-第%d页数据加载异常,共%d条,预期%d条\n", scd.Id, start+1, len(*tmp), checkNum)
  956. // }
  957. // lock.Lock()
  958. // res = append(res, *tmp...)
  959. // lock.Unlock()
  960. // }(n)
  961. // }
  962. // searchWaitGroup.Wait()
  963. // log.Printf("GetDataExportSearchResult-%s-分批次加载数据总量为%d\n", scd.Id, len(res))
  964. // } else {
  965. // tmp := doSearch(qstr, 0, checkCount, dataType)
  966. // if tmp == nil || len(*tmp) == 0 {
  967. // log.Printf("GetDataExportSearchResult-%s-一次性加载数据异常\n", scd.Id)
  968. // } else {
  969. // res = *tmp
  970. // log.Printf("GetDataExportSearchResult-%s-一次性加载数据总量为%d\n", scd.Id, len(res))
  971. // }
  972. // }
  973. // //超级搜索一致的检索(防止数据导出和超级搜索数据量不一致)
  974. // if scd.Comeinfrom == "supersearchPage" && (len(scd.Keyword) != 0 || len(scd.Industry) != 0) {
  975. // if len(scd.Keyword) != 0 {
  976. // num := len(res)
  977. // searchTextSize := 0
  978. // if len(scd.Keyword) > 0 {
  979. // searchTextSize = len([]rune(scd.Keyword[0].Keyword))
  980. // }
  981. // if searchTextSize > 3 && num < 50 {
  982. // secondKWS := jy.HttpEs(scd.Keyword[0].Keyword, "ik_smart", DbConf.Elasticsearch.Main.Address)
  983. // scd.Keyword[0].Keyword = secondKWS
  984. // scd.SelectType = "title"
  985. // qstr = getDataExportSql(scd)
  986. // log.Printf("GetDataExportSearchResult-%s-分词查询-sql:%s\n", scd.Id, qstr)
  987. // res2 := doSearch(qstr, 0, 100, "")
  988. // res = *delRepeatMapArr(&res, res2)
  989. // if len(res) > 100 {
  990. // res = res[:100]
  991. // }
  992. // }
  993. // }
  994. // }
  995. // //校验数量
  996. // if checkCount != len(res) {
  997. // return nil, fmt.Errorf("GetDataExportSearchResult-%s-数据总量校验异常,期望:%d,实际:%d", scd.Id, checkCount, len(res))
  998. // //发邮件
  999. // }
  1000. // return &res, nil
  1001. //}