bidding_es.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. package main
  2. import (
  3. "encoding/json"
  4. "esindex/config"
  5. "esindex/oss"
  6. "fmt"
  7. "github.com/spf13/viper"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.uber.org/zap"
  10. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  13. "jygit.jydev.jianyu360.cn/data_processing/common_utils/udp"
  14. "reflect"
  15. "regexp"
  16. "strconv"
  17. "strings"
  18. "sync"
  19. "time"
  20. "unicode/utf8"
  21. )
  22. var (
  23. TimeV1 = regexp.MustCompile("^(\\d{4})[年.]?$")
  24. TimeV2 = regexp.MustCompile("^(\\d{4})[年./-]?(\\d{1,2})[月./-]?$")
  25. TimeV3 = regexp.MustCompile("^(\\d{4})[年./-]?(\\d{1,2})[月./-]?(\\d{1,2})[日]?$")
  26. TimeClear = regexp.MustCompile("[年|月|日|/|.|-]")
  27. filterSpace = regexp.MustCompile("<[^>]*?>|[\\s\u3000\u2003\u00a0]")
  28. date1 = regexp.MustCompile("20[0-2][0-9][年|\\-/.][0-9]{1,2}[月|\\-/.][0-9]{1,2}[日]?")
  29. HtmlReg = regexp.MustCompile("<[^>]+>")
  30. )
  31. // biddingDetailTask 针对 detail contenthtml 二个字段的索引;bidding_detail
  32. func biddingDetailTask(mapInfo map[string]interface{}) {
  33. defer util.Catch()
  34. //stype := util.ObjToString(mapInfo["stype"])
  35. q, _ := mapInfo["query"].(map[string]interface{})
  36. if q == nil {
  37. q = map[string]interface{}{
  38. "_id": map[string]interface{}{
  39. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  40. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  41. },
  42. }
  43. } else {
  44. //针对gte/lte,单独转换
  45. q = convertToMongoID(q)
  46. }
  47. ch := make(chan bool, 10)
  48. wg := &sync.WaitGroup{}
  49. //bidding库
  50. biddingConn := MgoB.GetMgoConn()
  51. count, _ := biddingConn.DB(MgoB.DbName).C(config.Conf.DB.MongoB.Coll).Find(&q).Count()
  52. log.Info("biddingDetailTask", zap.Int64("同步总数:", count))
  53. it := biddingConn.DB(config.Conf.DB.MongoB.Dbname).C(config.Conf.DB.MongoB.Coll).Find(&q).Select(nil).Iter()
  54. c1, index := 0, 0
  55. var indexLock sync.Mutex
  56. for tmp := make(map[string]interface{}); it.Next(tmp); c1++ {
  57. if c1%1000 == 0 {
  58. log.Info("biddingDetailTask", zap.Int("current:", c1))
  59. log.Info("biddingDetailTask", zap.Any("current:_id =>", tmp["_id"]))
  60. }
  61. ch <- true
  62. wg.Add(1)
  63. // 创建一个新的map用于goroutine,避免重用
  64. docCopy := make(map[string]interface{})
  65. for k, v := range tmp {
  66. docCopy[k] = v
  67. }
  68. go func(tmp map[string]interface{}) {
  69. defer func() {
  70. <-ch
  71. wg.Done()
  72. }()
  73. indexLock.Lock()
  74. index++
  75. indexLock.Unlock()
  76. //
  77. detail, _ := tmp["detail"].(string)
  78. detail = filterSpace.ReplaceAllString(detail, "")
  79. detail_new := ""
  80. if tmp["cleartag"] != nil {
  81. if tmp["cleartag"].(bool) {
  82. text, _ := FilterDetail(detail)
  83. detail_new = util.ObjToString(tmp["title"]) + " " + text
  84. } else {
  85. detail_new = util.ObjToString(tmp["title"]) + " " + detail
  86. }
  87. } else {
  88. text, _ := FilterDetail(detail)
  89. detail_new = util.ObjToString(tmp["title"]) + " " + text
  90. }
  91. //
  92. insert := map[string]interface{}{
  93. "detail": detail_new,
  94. "id": mongodb.BsonIdToSId(tmp["_id"]),
  95. "_id": mongodb.BsonIdToSId(tmp["_id"]),
  96. "contenthtml": tmp["contenthtml"],
  97. }
  98. //
  99. saveDetailEsPool <- insert
  100. }(docCopy)
  101. tmp = map[string]interface{}{}
  102. }
  103. wg.Wait()
  104. log.Info("biddingDetailTask over", zap.Int("count", c1), zap.Int("index", index))
  105. }
  106. func biddingTask(mapInfo map[string]interface{}) {
  107. defer util.Catch()
  108. // 同时处理详情索引
  109. go biddingDetailTask(mapInfo)
  110. stype := util.ObjToString(mapInfo["stype"])
  111. q, _ := mapInfo["query"].(map[string]interface{})
  112. if q == nil {
  113. q = map[string]interface{}{
  114. "_id": map[string]interface{}{
  115. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  116. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  117. },
  118. }
  119. } else {
  120. //针对gte/lte,单独转换
  121. q = convertToMongoID(q)
  122. }
  123. ch := make(chan bool, 10)
  124. wg := &sync.WaitGroup{}
  125. //bidding库
  126. biddingConn := MgoB.GetMgoConn()
  127. count, _ := biddingConn.DB(MgoB.DbName).C(config.Conf.DB.MongoB.Coll).Find(&q).Count()
  128. log.Info("bidding表", zap.Int64("同步总数:", count))
  129. it := biddingConn.DB(config.Conf.DB.MongoB.Dbname).C(config.Conf.DB.MongoB.Coll).Find(&q).Select(map[string]interface{}{
  130. "contenthtml": 0,
  131. }).Iter()
  132. c1, index := 0, 0
  133. var indexLock sync.Mutex
  134. for tmp := make(map[string]interface{}); it.Next(tmp); c1++ {
  135. if c1%1000 == 0 {
  136. log.Info("biddingTask", zap.Int("current:", c1))
  137. log.Info("biddingTask", zap.Any("current:_id =>", tmp["_id"]))
  138. }
  139. ch <- true
  140. wg.Add(1)
  141. // 创建一个新的map用于goroutine,避免重用
  142. docCopy := make(map[string]interface{})
  143. for k, v := range tmp {
  144. docCopy[k] = v
  145. }
  146. go func(tmp map[string]interface{}) {
  147. defer func() {
  148. <-ch
  149. wg.Done()
  150. }()
  151. if sensitive := util.ObjToString(tmp["sensitive"]); sensitive == "测试" { //bidding中有敏感词,不生索引
  152. tmp = make(map[string]interface{})
  153. return
  154. }
  155. //只针对增量数据处理;全量数据 需要用extracttype字段判断
  156. //7: 重复数据
  157. //8: 不重
  158. //if util.IntAll(tmp["dataprocess"]) != 8 {
  159. // return
  160. //}
  161. //// 增量数据使用上面判断;全量数据使用下面配置
  162. //-1:重复 ,1:不重复 ,0:入库 9:分类
  163. if util.IntAll(tmp["extracttype"]) != 1 {
  164. return
  165. }
  166. // 优选字段,和 extracttype 一致,isprefer 1:优选 -1:取消优选
  167. //if util.IntAll(tmp["isprefer"]) != 1 {
  168. // return
  169. //}
  170. //针对产权数据,暂时不入es 索引库
  171. if util.IntAll(tmp["infoformat"]) == 3 {
  172. return
  173. }
  174. /**
  175. 数据抽取时,有的数据的发布时间是之前的,属于增量历史数据,在判重和同步到bidding表是,会添加history_updatetime
  176. 字段,所以下面判断才会处理
  177. */
  178. if stype == "bidding_history" && tmp["history_updatetime"] == nil {
  179. return
  180. }
  181. //开启OSS时,detail需要重新获取
  182. if config.Conf.Env.Oss {
  183. id := mongodb.BsonIdToSId(tmp["_id"])
  184. val := oss.OssGetObject(id, config.Conf.DB.Oss.DetailBucket)
  185. tmp["detail"] = val
  186. }
  187. indexLock.Lock()
  188. index++
  189. indexLock.Unlock()
  190. newTmp, update := GetEsField(tmp, stype)
  191. newTmp["dataweight"] = 0 //索引数据新增 jy置顶字段
  192. newTmp["old_preferid"] = tmp["old_preferid"] //上次优选ID,需要es 删除
  193. //针对中国政府采购网,单独处理
  194. if util.ObjToString(tmp["site"]) == "中国政府采购网" {
  195. objectType := MatchService(tmp)
  196. if objectType != "" {
  197. newTmp["object_type"] = objectType
  198. }
  199. }
  200. newTmp["stype"] = stype
  201. //
  202. //if stype == "bidding" || stype == "bidding_history" || stype == "index-by-id" {
  203. // newTmp["stype"] = stype
  204. // //之前存在pici,就不在添加
  205. // if pici, ok := tmp["pici"]; ok {
  206. // newTmp["pici"] = pici
  207. // //log.Info("dddddddd", zap.Any("bidding_id", tmp["_id"]), zap.Any("pici", pici))
  208. // } else {
  209. // newTmp["pici"] = time.Now().Unix()
  210. // update["pici"] = time.Now().Unix()
  211. // }
  212. //
  213. //}
  214. //todo 处理中国移动定制标签
  215. if len(globalRegs) > 0 && len(MatchArr) > 0 {
  216. gs, _, _ := TaskTags(tmp, globalRegs)
  217. if len(gs) > 0 {
  218. tags, match, add := TaskTags(tmp, MatchArr)
  219. if len(tags) > 0 {
  220. newTmp["mobile_tag"] = tags
  221. update["mobile_tag"] = tags
  222. log.Info("biddingTask", zap.Any(mongodb.BsonIdToSId(tmp["_id"]), match+","+add))
  223. }
  224. }
  225. }
  226. //
  227. saveEsPool <- newTmp
  228. if len(update) > 0 {
  229. updateBiddingPool <- map[string]interface{}{
  230. "_id": tmp["_id"],
  231. "set": update,
  232. }
  233. //if util.ObjToString(newTmp["spidercode"]) == "a_jyxxfbpt_gg" {
  234. // // 剑鱼信息发布数据 通过udp通知信息发布程序
  235. // go UdpMethod(mongodb.BsonIdToSId(newTmp["_id"]))
  236. //}
  237. }
  238. }(docCopy)
  239. tmp = map[string]interface{}{}
  240. }
  241. wg.Wait()
  242. log.Info("biddingTask over", zap.Int("count", c1), zap.Int("index", index))
  243. //更新状态
  244. if stype == "bidding" {
  245. uq := bson.M{"gtid": bson.M{"$gte": util.ObjToString(mapInfo["gtid"])},
  246. "lteid": bson.M{"$lte": util.ObjToString(mapInfo["lteid"])}}
  247. if config.Conf.Env.Ai {
  248. MgoBOld.Update("bidding_processing_ids", uq, bson.M{"$set": bson.M{"dataprocess_ai": 8, "updatetime": time.Now().Unix(), "index_num": index}}, false, true)
  249. } else {
  250. MgoB.Update("bidding_processing_ids", uq, bson.M{"$set": bson.M{"dataprocess": 9, "updatetime": time.Now().Unix(), "index_num": index}}, false, true)
  251. }
  252. }
  253. ////发送udp,附件补采 才需要
  254. //data := map[string]interface{}{
  255. // "stype": "update",
  256. // "gtid": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  257. // "lteid": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  258. //}
  259. //target := &net.UDPAddr{
  260. // Port: 1782,
  261. // IP: net.ParseIP("127.0.0.1"),
  262. //}
  263. //bytes, _ := json.Marshal(data)
  264. //err := UdpClient.WriteUdp(bytes, udp.OP_TYPE_DATA, target)
  265. //if err != nil {
  266. // log.Info("biddingTask ", zap.Any("WriteUdp err", err), zap.Any("target", target))
  267. //}
  268. //
  269. //log.Info("biddingTask ", zap.Any("target", target), zap.Any("data", data))
  270. //
  271. //
  272. //重采平台需要
  273. //mapInfo["stype"] = ""
  274. //datas, _ := json.Marshal(mapInfo)
  275. //var next = &net.UDPAddr{
  276. // IP: net.ParseIP("127.0.0.1"),
  277. // Port: 1910,
  278. //}
  279. //log.Info("bidding index es over", zap.Any("es", next), zap.String("mapinfo", string(datas)))
  280. //}
  281. }
  282. // biddingAllTask 补充存量数据
  283. func biddingAllTask(mapInfo map[string]interface{}) {
  284. defer util.Catch()
  285. stype := util.ObjToString(mapInfo["stype"])
  286. q, _ := mapInfo["query"].(map[string]interface{})
  287. if q == nil {
  288. q = map[string]interface{}{
  289. "_id": map[string]interface{}{
  290. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  291. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  292. },
  293. }
  294. } else {
  295. //针对gte/lte,单独转换
  296. q = convertToMongoID(q)
  297. }
  298. ch := make(chan bool, 50)
  299. wg := &sync.WaitGroup{}
  300. //bidding库
  301. biddingConn := MgoB.GetMgoConn()
  302. it := biddingConn.DB(config.Conf.DB.MongoB.Dbname).C(config.Conf.DB.MongoB.Coll).Find(&q).Select(map[string]interface{}{
  303. "contenthtml": 0,
  304. }).Iter()
  305. c1, index := 0, 0
  306. var indexLock sync.Mutex
  307. for tmp := make(map[string]interface{}); it.Next(tmp); c1++ {
  308. if c1%20000 == 0 {
  309. log.Info("biddingAllTask", zap.Int("current:", c1))
  310. log.Info("biddingAllTask", zap.Any("current:_id =>", tmp["_id"]))
  311. }
  312. ch <- true
  313. wg.Add(1)
  314. go func(tmp map[string]interface{}) {
  315. defer func() {
  316. <-ch
  317. wg.Done()
  318. }()
  319. if sensitive := util.ObjToString(tmp["sensitive"]); sensitive == "测试" { //bidding中有敏感词,不生索引
  320. tmp = make(map[string]interface{})
  321. return
  322. }
  323. // 针对存量数据,重复数据不进索引
  324. if util.IntAll(tmp["extracttype"]) == -1 {
  325. return
  326. }
  327. //针对产权数据,暂时不入es 索引库
  328. if util.IntAll(tmp["infoformat"]) == 3 {
  329. return
  330. }
  331. //开启OSS时,detail需要重新获取
  332. if config.Conf.Env.Oss {
  333. id := mongodb.BsonIdToSId(tmp["_id"])
  334. val := oss.OssGetObject(id, config.Conf.DB.Oss.DetailBucket)
  335. tmp["detail"] = val
  336. }
  337. indexLock.Lock()
  338. index++
  339. indexLock.Unlock()
  340. newTmp, update := GetEsField(tmp, stype)
  341. newTmp["old_preferid"] = tmp["old_preferid"] //上次优选ID,需要es 删除
  342. //针对中国政府采购网,单独处理
  343. if util.ObjToString(tmp["site"]) == "中国政府采购网" {
  344. objectType := MatchService(tmp)
  345. if objectType != "" {
  346. newTmp["object_type"] = objectType
  347. }
  348. }
  349. //todo 处理中国移动定制标签
  350. if len(globalRegs) > 0 && len(MatchArr) > 0 {
  351. gs, _, _ := TaskTags(tmp, globalRegs)
  352. if len(gs) > 0 {
  353. tags, match, add := TaskTags(tmp, MatchArr)
  354. if len(tags) > 0 {
  355. newTmp["mobile_tag"] = tags
  356. update["mobile_tag"] = tags
  357. log.Info("biddingTask", zap.Any(mongodb.BsonIdToSId(tmp["_id"]), match+","+add))
  358. }
  359. }
  360. }
  361. newTmp["dataweight"] = 0 //索引数据新增 jy置顶字段
  362. if len(update) > 0 {
  363. updateBiddingPool <- map[string]interface{}{
  364. "_id": tmp["_id"],
  365. "set": update,
  366. }
  367. }
  368. saveEsPool <- newTmp
  369. }(tmp)
  370. tmp = map[string]interface{}{}
  371. }
  372. wg.Wait()
  373. log.Info("biddingAllTask over", zap.Int("count", c1), zap.Int("index", index))
  374. }
  375. // biddingAllDataTask 处理配置文件的存量数据
  376. func biddingAllDataTask() {
  377. type Biddingall struct {
  378. Coll string
  379. Gtid string
  380. Lteid string
  381. }
  382. type RoutinesConf struct {
  383. Num int
  384. }
  385. type AllConf struct {
  386. All map[string]Biddingall
  387. Routines RoutinesConf
  388. }
  389. var all AllConf
  390. viper.SetConfigFile("biddingall.toml")
  391. viper.SetConfigName("biddingall") // 配置文件名称(无扩展名)
  392. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  393. viper.AddConfigPath("./")
  394. err := viper.ReadInConfig() // 查找并读取配置文件
  395. if err != nil { // 处理读取配置文件的错误
  396. fmt.Println("ReadInConfig err =>", err)
  397. return
  398. }
  399. err = viper.Unmarshal(&all)
  400. if err != nil {
  401. fmt.Println("biddingAllDataTask Unmarshal err =>", err)
  402. return
  403. }
  404. for k, conf := range all.All {
  405. go dealData(conf.Coll, conf.Gtid, conf.Lteid, k, all.Routines.Num)
  406. }
  407. }
  408. func dealData(coll, gtid, lteid, kword string, routines int) {
  409. ch := make(chan bool, routines)
  410. wg := &sync.WaitGroup{}
  411. q := map[string]interface{}{
  412. "_id": map[string]interface{}{
  413. "$gt": mongodb.StringTOBsonId(gtid),
  414. "$lte": mongodb.StringTOBsonId(lteid),
  415. },
  416. }
  417. log.Info("dealData", zap.Any(kword, q))
  418. biddingConn := MgoB.GetMgoConn()
  419. it := biddingConn.DB(config.Conf.DB.MongoB.Dbname).C(coll).Find(&q).Select(map[string]interface{}{
  420. "contenthtml": 0,
  421. }).Iter()
  422. c1, index := 0, 0
  423. var indexLock sync.Mutex
  424. for tmp := make(map[string]interface{}); it.Next(tmp); c1++ {
  425. if c1%20000 == 0 {
  426. log.Info(kword, zap.Int("current:", c1))
  427. log.Info(kword, zap.Any("current:_id =>", tmp["_id"]))
  428. }
  429. ch <- true
  430. wg.Add(1)
  431. go func(tmp map[string]interface{}) {
  432. defer func() {
  433. <-ch
  434. wg.Done()
  435. }()
  436. if sensitive := util.ObjToString(tmp["sensitive"]); sensitive == "测试" { //bidding中有敏感词,不生索引
  437. tmp = make(map[string]interface{})
  438. return
  439. }
  440. // 针对存量数据,重复数据不进索引
  441. if util.IntAll(tmp["extracttype"]) == -1 {
  442. return
  443. }
  444. //针对产权数据,暂时不入es 索引库
  445. if util.IntAll(tmp["infoformat"]) == 3 {
  446. return
  447. }
  448. indexLock.Lock()
  449. index++
  450. indexLock.Unlock()
  451. newTmp, update := GetEsField(tmp, "biddingall")
  452. //针对中国政府采购网,单独处理
  453. if util.ObjToString(tmp["site"]) == "中国政府采购网" {
  454. objectType := MatchService(tmp)
  455. if objectType != "" {
  456. newTmp["object_type"] = objectType
  457. }
  458. }
  459. newTmp["dataweight"] = 0 //索引数据新增 jy置顶字段
  460. if len(update) > 0 {
  461. updateBiddingPool <- map[string]interface{}{
  462. "_id": tmp["_id"],
  463. "set": update,
  464. }
  465. }
  466. //saveEsPool <- newTmp
  467. saveBiddingAllPool <- newTmp
  468. }(tmp)
  469. tmp = map[string]interface{}{}
  470. }
  471. wg.Wait()
  472. log.Info(fmt.Sprintf("%s over", kword), zap.Int("count", c1), zap.Int("index", index))
  473. }
  474. func biddingTaskById(mapInfo map[string]interface{}) {
  475. defer util.Catch()
  476. stype := util.ObjToString(mapInfo["stype"])
  477. infoid := util.ObjToString(mapInfo["infoid"])
  478. tmp, _ := MgoB.FindById(config.Conf.DB.MongoB.Coll, infoid, map[string]interface{}{"contenthtml": 0})
  479. ////判断是否是预处理数据;pre_id 是标识
  480. //if config.Conf.Env.OpenPre {
  481. // if pre_id, ok := (*tmp)["pre_id"]; ok {
  482. // preID := util.ObjToString(pre_id)
  483. // if preID != "" {
  484. // deletePreEsData(preID)
  485. // }
  486. // }
  487. //}
  488. if sensitive := util.ObjToString((*tmp)["sensitive"]); sensitive == "测试" { //bidding中有敏感词,不生索引
  489. return
  490. }
  491. //开启OSS时,detail需要重新获取
  492. if config.Conf.Env.Oss {
  493. id := mongodb.BsonIdToSId((*tmp)["_id"])
  494. val := oss.OssGetObject(id, config.Conf.DB.Oss.DetailBucket)
  495. (*tmp)["detail"] = val
  496. }
  497. if util.IntAll((*tmp)["extracttype"]) == 1 {
  498. newTmp, update := GetEsField(*tmp, stype)
  499. newTmp["dataweight"] = 0 //索引数据新增 jy置顶字段
  500. newTmp["old_preferid"] = (*tmp)["old_preferid"] //上次优选ID,需要es 删除
  501. if len(update) > 0 {
  502. //updateBiddingPool <- []map[string]interface{}{{
  503. // "_id": mongodb.StringTOBsonId(infoid),
  504. //},
  505. // {"$set": update},
  506. //}
  507. }
  508. saveEsPool <- newTmp
  509. }
  510. log.Info("biddingTaskById over", zap.Any("mapInfo", mapInfo))
  511. }
  512. // GetEsField @Description ES字段
  513. // @Author J 2022/6/7 11:34 AM
  514. func GetEsField(tmp map[string]interface{}, stype string) (map[string]interface{}, map[string]interface{}) {
  515. newTmp := make(map[string]interface{})
  516. update := make(map[string]interface{}) // bidding 修改字段
  517. saveErr := make(map[string]interface{})
  518. //for field, ftype := range config.Conf.DB.Es.FieldEs {
  519. for field, ftype := range BiddingField {
  520. if tmp[field] != nil { //
  521. if field == "purchasinglist" { //标的物处理
  522. purchasinglist_new := []map[string]interface{}{}
  523. if pcl, _ := tmp[field].([]interface{}); len(pcl) > 0 {
  524. for _, ls := range pcl {
  525. lsm_new := make(map[string]interface{})
  526. lsm := ls.(map[string]interface{})
  527. for pf, pftype := range BiddingLevelField[field] {
  528. lsmv := lsm[pf]
  529. if lsmv != nil && reflect.TypeOf(lsmv).String() == pftype {
  530. lsm_new[pf] = lsm[pf]
  531. }
  532. }
  533. if lsm_new != nil && len(lsm_new) > 0 {
  534. purchasinglist_new = append(purchasinglist_new, lsm_new)
  535. }
  536. }
  537. }
  538. if len(purchasinglist_new) > 0 {
  539. newTmp[field] = purchasinglist_new
  540. }
  541. } else if field == "procurementlist" {
  542. if tmp["procurementlist"] != nil {
  543. var arr []interface{}
  544. plist := tmp["procurementlist"].([]interface{})
  545. for _, p := range plist {
  546. p1 := p.(map[string]interface{})
  547. p2 := make(map[string]interface{})
  548. for k, v := range BiddingLevelField[field] {
  549. if k == "projectname" && util.ObjToString(p1[k]) == "" {
  550. p2[k] = util.ObjToString(tmp["projectname"])
  551. } else if k == "buyer" && util.ObjToString(p1[k]) == "" && util.ObjToString(tmp["buyer"]) != "" {
  552. p2[k] = util.ObjToString(tmp["buyer"])
  553. } else if k == "expurasingtime" && util.ObjToString(p1[k]) != "" {
  554. res := getMethod(util.ObjToString(p1[k]))
  555. if res != 0 {
  556. p2[k] = res
  557. }
  558. } else if p1[k] != nil && reflect.TypeOf(p1[k]).String() == v {
  559. p2[k] = p1[k]
  560. }
  561. }
  562. arr = append(arr, p2)
  563. }
  564. if len(arr) > 0 {
  565. newTmp[field] = arr
  566. }
  567. }
  568. } else if field == "projectscope" {
  569. ps, _ := tmp["projectscope"].(string)
  570. newTmp["projectscope"] = ps
  571. //新版本已无需记录长度
  572. //if len(ps) > pscopeLength {
  573. // saveErr["projectscope"] = ps
  574. // saveErr["projectscope_length"] = len(ps)
  575. //}
  576. } else if field == "winnerorder" { //中标候选
  577. winnerorder_new := []map[string]interface{}{}
  578. if winnerorder, _ := tmp[field].([]interface{}); len(winnerorder) > 0 {
  579. for _, win := range winnerorder {
  580. winMap_new := make(map[string]interface{})
  581. winMap := win.(map[string]interface{})
  582. for wf, wftype := range BiddingLevelField[field] {
  583. wfv := winMap[wf]
  584. if wfv != nil && reflect.TypeOf(wfv).String() == wftype {
  585. if wf == "sort" && util.Int64All(wfv) > 100 {
  586. continue
  587. }
  588. winMap_new[wf] = winMap[wf]
  589. }
  590. }
  591. if winMap_new != nil && len(winMap_new) > 0 {
  592. winnerorder_new = append(winnerorder_new, winMap_new)
  593. }
  594. }
  595. }
  596. if len(winnerorder_new) > 0 {
  597. newTmp[field] = winnerorder_new
  598. }
  599. } else if field == "qualifies" {
  600. //项目资质
  601. qs := []string{}
  602. if q, _ := tmp[field].([]interface{}); len(q) > 0 {
  603. for _, v := range q {
  604. v1 := v.(map[string]interface{})
  605. qs = append(qs, util.ObjToString(v1["key"]))
  606. }
  607. }
  608. if len(qs) > 0 {
  609. newTmp[field] = strings.Join(qs, ",")
  610. }
  611. } else if field == "bidopentime" {
  612. if tmp[field] != nil && tmp["bidendtime"] == nil {
  613. newTmp["bidendtime"] = tmp[field]
  614. newTmp[field] = tmp[field]
  615. } else if tmp[field] == nil && tmp["bidendtime"] != nil {
  616. newTmp["bidendtime"] = tmp[field]
  617. newTmp[field] = tmp["bidendtime"]
  618. } else {
  619. if tmp["bidopentime"] != nil {
  620. newTmp[field] = tmp["bidopentime"]
  621. }
  622. }
  623. } else if field == "detail" { //过滤
  624. detail, _ := tmp[field].(string)
  625. detail = filterSpace.ReplaceAllString(detail, "")
  626. // 不需要再保存记录长度
  627. //if len(detail) > pscopeLength {
  628. // saveErr["detail"] = detail
  629. // saveErr["detail_length"] = len(detail)
  630. //}
  631. if tmp["cleartag"] != nil {
  632. if tmp["cleartag"].(bool) {
  633. text, _ := FilterDetail(detail)
  634. newTmp[field] = util.ObjToString(tmp["title"]) + " " + text
  635. } else {
  636. newTmp[field] = util.ObjToString(tmp["title"]) + " " + detail
  637. }
  638. } else {
  639. text, b := FilterDetail(detail)
  640. newTmp[field] = util.ObjToString(tmp["title"]) + " " + text
  641. update["cleartag"] = b
  642. }
  643. } else if field == "topscopeclass" || field == "entidlist" {
  644. newTmp[field] = tmp[field]
  645. } else if field == "_id" {
  646. newTmp["_id"] = mongodb.BsonIdToSId(tmp["_id"])
  647. newTmp["id"] = mongodb.BsonIdToSId(tmp["_id"])
  648. } else if field == "publishtime" || field == "comeintime" {
  649. //字段类型不正确,特别处理
  650. if tmp[field] != nil && util.Int64All(tmp[field]) > 0 {
  651. newTmp[field] = util.Int64All(tmp[field])
  652. }
  653. } else if field == "package" {
  654. //分包信息处理
  655. packages := dealPackage(tmp)
  656. if len(packages) > 0 {
  657. newTmp["package"] = packages
  658. newTmp["subpackage"] = 1
  659. }
  660. } else if field == "infoformat" {
  661. newTmp[field] = tmp[field]
  662. } else { //其它字段判断数据类型,不正确舍弃
  663. if fieldval := tmp[field]; reflect.TypeOf(fieldval).String() != ftype && ftype != "" {
  664. continue
  665. } else {
  666. if fieldval != "" {
  667. newTmp[field] = fieldval
  668. }
  669. }
  670. }
  671. }
  672. }
  673. // 附件内容长度不做限制,大于长度限制做记录
  674. filetext := getFileText(tmp)
  675. if len([]rune(filetext)) > 10 {
  676. //去除 空格
  677. newTmp["filetext"] = strings.Replace(filetext, " ", "", -1)
  678. if len([]rune(filetext)) > fileLength {
  679. //saveErr["filetext"] = filetext
  680. saveErr["filetext_length"] = len([]rune(filetext))
  681. }
  682. }
  683. YuceEndtime(newTmp) // 预测结果时间
  684. //if stype == "bidding" || stype == "bidding_history" || stype == "index-by-id" {
  685. // newTmp["createtime"] = time.Now().Unix() // es库数据创建时间,只有增量数据有
  686. // newTmp["pici"] = time.Now().Unix() //createtime跟pici一样,为了剑鱼功能需要,并行存在一段时间,之后可以删掉createtime
  687. // update["pici"] = time.Now().Unix()
  688. //}
  689. if len(saveErr) > 0 {
  690. saveErr["infoid"] = mongodb.BsonIdToSId(tmp["_id"])
  691. saveErr["time"] = time.Now().Unix()
  692. saveErrBidPool <- saveErr
  693. }
  694. return newTmp, update
  695. }
  696. // @Description 采购意向 预计采购时间处理
  697. // @Author J 2022/6/7 8:04 PM
  698. func getMethod(str string) int64 {
  699. // Handle "YYYY" format
  700. if TimeV1.MatchString(str) {
  701. arr := TimeV1.FindStringSubmatch(str)
  702. st := arr[1] + "0000"
  703. parseInt, err := strconv.ParseInt(st, 10, 64)
  704. if err == nil {
  705. return parseInt
  706. }
  707. }
  708. // Handle "YYYYMM" or "YYYY/MM" or "YYYY-MM" or "YYYY.MM" format
  709. if TimeV2.MatchString(str) {
  710. arr := TimeV2.FindStringSubmatch(str)
  711. year := arr[1]
  712. month := arr[2]
  713. if len(month) == 1 {
  714. month = "0" + month
  715. }
  716. str2 := year + month + "00"
  717. parseInt, err := strconv.ParseInt(str2, 10, 64)
  718. if err == nil {
  719. return parseInt
  720. }
  721. }
  722. // Handle "YYYYMMDD" or "YYYY/MM/DD" or "YYYY-MM-DD" or "YYYY.MM.DD" format
  723. if TimeV3.MatchString(str) {
  724. match := TimeV3.FindStringSubmatch(str)
  725. if len(match) >= 4 {
  726. year := match[1]
  727. month := match[2]
  728. day := match[3]
  729. if len(month) == 1 {
  730. month = "0" + month
  731. }
  732. if len(day) == 1 {
  733. day = "0" + day
  734. }
  735. dateStr := year + month + day
  736. parseInt, err := strconv.ParseInt(dateStr, 10, 64)
  737. if err == nil {
  738. return parseInt
  739. }
  740. }
  741. }
  742. return 0
  743. }
  744. func FilterDetail(text string) (string, bool) {
  745. b := false // 清理标记
  746. for _, s := range config.Conf.DB.Es.DetailFilter {
  747. reg := regexp.MustCompile(s)
  748. if reg.MatchString(text) {
  749. text = reg.ReplaceAllString(text, "")
  750. if !b {
  751. b = true
  752. }
  753. }
  754. }
  755. return text, b
  756. }
  757. // @Description 附件内容
  758. // @Author J 2022/6/7 1:54 PM
  759. func getFileText(tmp map[string]interface{}) (filetext string) {
  760. if attchMap, ok := tmp["attach_text"].(map[string]interface{}); attchMap != nil && ok {
  761. for _, tmpData1 := range attchMap {
  762. if tmpData2, ok := tmpData1.(map[string]interface{}); tmpData2 != nil && ok {
  763. for _, result := range tmpData2 {
  764. if resultMap, ok := result.(map[string]interface{}); resultMap != nil && ok {
  765. if attach_url := util.ObjToString(resultMap["attach_url"]); attach_url != "" {
  766. bs := oss.OssGetObject(attach_url, mongodb.BsonIdToSId(tmp["_id"])) //oss读数据
  767. //附件总长度限制550000,其中最后一个文件长度限制50000
  768. size := config.Conf.DB.Oss.Filesize
  769. if size <= 0 {
  770. size = 500000
  771. }
  772. if utf8.RuneCountInString(filetext+bs) < 50000+size {
  773. filetext += bs + "\n"
  774. } else {
  775. if len(bs) > 50000 {
  776. filetext += bs[0:50000]
  777. } else {
  778. filetext += bs
  779. }
  780. }
  781. //附件总长度限制550000
  782. if utf8.RuneCountInString(filetext) >= 50000+size {
  783. return
  784. }
  785. //正式环境
  786. //if utf8.RuneCountInString(filetext+bs) < fileLength {
  787. // filetext += bs + "\n"
  788. //} else {
  789. // if utf8.RuneCountInString(bs) > fileLength {
  790. // filetext = bs[0:fileLength]
  791. // } else {
  792. // filetext = bs
  793. // }
  794. // break
  795. //}
  796. }
  797. }
  798. }
  799. }
  800. }
  801. }
  802. return
  803. }
  804. // 预测结果时间
  805. func YuceEndtime(tmp map[string]interface{}) {
  806. flag := false
  807. flag2 := false
  808. falseOld := false
  809. scopeOld := []string{"服务采购_法律咨询", "服务采购_会计", "服务采购_物业", "服务采购_审计", "服务采购_安保", "服务采购_仓储物流",
  810. "服务采购_广告宣传印刷"}
  811. scope := []string{"信息技术_运维服务", "信息技术_软件开发", "信息技术_系统集成及安全", "信息技术_其他"}
  812. titles := []string{"短信服务", "短信发送服务"}
  813. details := []string{"短信发送服务", "短信服务平台", "短信服务项目"}
  814. subscopeclass := util.ObjToString(tmp["s_subscopeclass"])
  815. //1.先判断老的
  816. for _, v := range scopeOld {
  817. if strings.Contains(subscopeclass, v) {
  818. falseOld = true
  819. break
  820. }
  821. }
  822. //2.判断满足 s_subscopeclass 条件
  823. for _, v := range scope {
  824. if strings.Contains(subscopeclass, v) {
  825. flag = true
  826. break
  827. }
  828. }
  829. //不满足旧的,判断新的规则
  830. if !falseOld {
  831. //满足 s_subscopeclass ,再去判断title detail
  832. if flag {
  833. title := util.ObjToString(tmp["title"])
  834. for _, v := range titles {
  835. if strings.Contains(title, v) {
  836. flag2 = true
  837. }
  838. }
  839. if !flag2 {
  840. detail := util.ObjToString(tmp["detail"])
  841. for _, v := range details {
  842. if strings.Contains(detail, v) {
  843. flag2 = true
  844. }
  845. }
  846. }
  847. }
  848. if !flag2 {
  849. return
  850. }
  851. }
  852. subtype := util.ObjToString(tmp["subtype"])
  853. if subtype == "成交" || subtype == "合同" {
  854. // yucestarttime、yuceendtime
  855. yucestarttime, yuceendtime := int64(0), int64(0)
  856. // 项目周期中
  857. if util.ObjToString(tmp["projectperiod"]) != "" {
  858. dateStr := date1.FindAllString(util.ObjToString(tmp["projectperiod"]), -1)
  859. if len(dateStr) == 2 {
  860. sdate := FormatDateStr(dateStr[0])
  861. edate := FormatDateStr(dateStr[1])
  862. if sdate < edate && sdate != 0 && edate != 0 {
  863. yucestarttime = sdate
  864. yuceendtime = edate
  865. }
  866. }
  867. }
  868. if yucestarttime > 0 && yuceendtime > yucestarttime {
  869. tmp["yuceendtime"] = yuceendtime
  870. return
  871. }
  872. // 预测开始时间 合同签订日期
  873. if yucestarttime == 0 {
  874. if util.IntAll(tmp["signaturedate"]) <= 0 {
  875. if util.IntAll(tmp["publishtime"]) <= 0 {
  876. return
  877. } else {
  878. yucestarttime = util.Int64All(tmp["publishtime"])
  879. }
  880. } else {
  881. yucestarttime = util.Int64All(tmp["signaturedate"])
  882. }
  883. }
  884. // 预测结束时间
  885. if yucestarttime > 0 && yuceendtime == 0 {
  886. if util.IntAll(tmp["project_duration"]) > 0 && util.ObjToString(tmp["project_timeunit"]) != "" {
  887. yuceendtime = YcEndTime(yucestarttime, util.IntAll(tmp["project_duration"]), util.ObjToString(tmp["project_timeunit"]))
  888. tmp["yuceendtime"] = yuceendtime
  889. }
  890. }
  891. }
  892. }
  893. func FormatDateStr(ds string) int64 {
  894. ds = strings.Replace(ds, "年", "-", -1)
  895. ds = strings.Replace(ds, "月", "-", -1)
  896. ds = strings.Replace(ds, "日", "", -1)
  897. ds = strings.Replace(ds, "/", "-", -1)
  898. ds = strings.Replace(ds, ".", "-", -1)
  899. layout1 := "2006-1-2"
  900. location, err := time.ParseInLocation(util.Date_Short_Layout, ds, time.Local)
  901. if err != nil {
  902. location, err := time.ParseInLocation(layout1, ds, time.Local)
  903. if err != nil {
  904. log.Error("FormatDateStr", zap.Error(err))
  905. return 0
  906. } else {
  907. return location.Unix()
  908. }
  909. } else {
  910. return location.Unix()
  911. }
  912. }
  913. func YcEndTime(starttime int64, num int, unit string) int64 {
  914. yuceendtime := int64(0)
  915. if unit == "日历天" || unit == "天" || unit == "日" {
  916. yuceendtime = starttime + int64(num*86400)
  917. } else if unit == "周" {
  918. yuceendtime = time.Unix(starttime, 0).AddDate(0, 0, num*7).Unix()
  919. } else if unit == "月" {
  920. yuceendtime = time.Unix(starttime, 0).AddDate(0, num, 0).Unix()
  921. } else if unit == "年" {
  922. yuceendtime = time.Unix(starttime, 0).AddDate(num, 0, 0).Unix()
  923. } else if unit == "工作日" {
  924. n := num / 7 * 2
  925. yuceendtime = time.Unix(starttime, 0).AddDate(0, 0, num+n).Unix()
  926. }
  927. return yuceendtime
  928. }
  929. // UdpMethod @Description rpc调用信息发布程序接口
  930. // @Author J 2022/4/13 9:13 AM
  931. func UdpMethod(id string) {
  932. mapinfo := map[string]interface{}{
  933. "infoid": id,
  934. "stype": "jyfb_data_over",
  935. }
  936. datas, _ := json.Marshal(mapinfo)
  937. log.Info("UdpMethod", zap.Any("JyUdpAddr", JyUdpAddr), zap.String("mapinfo", string(datas)))
  938. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, JyUdpAddr)
  939. }
  940. // MatchService 针对中国招标网,匹配关键词打标签,object_type,货物、服务、工程,jsondata.item
  941. func MatchService(tmp map[string]interface{}) (res string) {
  942. if jsondata, ok := tmp["jsondata"]; ok {
  943. if da, ok := jsondata.(map[string]interface{}); ok {
  944. if item, ok := da["item"]; ok {
  945. services := []string{"货物", "服务", "工程"}
  946. for _, v := range services {
  947. if strings.Contains(util.ObjToString(item), v) {
  948. return v
  949. }
  950. }
  951. }
  952. }
  953. }
  954. return
  955. }
  956. // dealPackage 处理package 字段
  957. func dealPackage(tmp map[string]interface{}) (newpackages []map[string]interface{}) {
  958. package1, ok1 := tmp["package"]
  959. s_winner, ok2 := tmp["s_winner"]
  960. bidamount, ok3 := tmp["bidamount"]
  961. var innerWinners = make([]string, 0)
  962. var biaoAmounts = make([]float64, 0)
  963. // 三个字段都存在
  964. if ok3 && ok2 && ok1 {
  965. packageMap, ok := package1.(map[string]interface{})
  966. if ok {
  967. if len(packageMap) >= 2 {
  968. var packages = make([]map[string]interface{}, 0)
  969. //var newTmp = make(map[string]interface{})
  970. winner_amount_count := 0
  971. for _, pack := range packageMap {
  972. var newPackage = make(map[string]interface{})
  973. pac, okk := pack.(map[string]interface{})
  974. if okk {
  975. _, okk1 := pac["winner"]
  976. _, okk2 := pac["bidamount"]
  977. _, okk3 := pac["name"]
  978. if okk1 {
  979. innerWinners = append(innerWinners, util.ObjToString(pac["winner"]))
  980. }
  981. if okk2 {
  982. biaoAmounts = append(biaoAmounts, util.Float64All(pac["bidamount"]))
  983. }
  984. //winner bidamount 二个字段都存在
  985. if okk1 && okk2 {
  986. winner_amount_count++
  987. newPackage["winner"] = pac["winner"]
  988. newPackage["bidamount"] = pac["bidamount"]
  989. if okk3 {
  990. newPackage["name"] = pac["name"]
  991. }
  992. packages = append(packages, newPackage)
  993. }
  994. }
  995. }
  996. //出现次数大于1
  997. if winner_amount_count > 1 {
  998. swinner := util.ObjToString(s_winner)
  999. swinners := strings.Split(swinner, ",")
  1000. //判断里外 winner 是否相等
  1001. eq := StringSliceValuesEqual(swinners, innerWinners)
  1002. if eq {
  1003. //判断金额相等
  1004. if Float64Equal1Precision(Float64SliceSum(biaoAmounts), util.Float64All(bidamount)) {
  1005. newpackages = packages
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. return
  1013. }