biddingindex.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. package main
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. //"fmt"
  6. "log"
  7. mu "mfw/util"
  8. "net"
  9. qutil "qfw/util"
  10. elastic "qfw/util/elastic"
  11. "regexp"
  12. "strings"
  13. "sync"
  14. "time"
  15. "gopkg.in/mgo.v2/bson"
  16. )
  17. //对字段处理 bidamount budget
  18. //招标数据表和抽取表一一对应开始更新
  19. func biddingTask(data []byte, mapInfo map[string]interface{}) {
  20. defer qutil.Catch()
  21. q, _ := mapInfo["query"].(map[string]interface{})
  22. bkey, _ := mapInfo["bkey"].(string)
  23. if q == nil {
  24. q = map[string]interface{}{
  25. "_id": bson.M{
  26. "$gt": qutil.StringTOBsonId(mapInfo["gtid"].(string)),
  27. "$lte": qutil.StringTOBsonId(mapInfo["lteid"].(string)),
  28. },
  29. }
  30. }
  31. //连接信息
  32. c, _ := bidding["collect"].(string)
  33. extractc, _ := bidding["extractcollect"].(string)
  34. db, _ := bidding["db"].(string)
  35. extractdb, _ := bidding["extractdb"].(string)
  36. index, _ := bidding["index"].(string)
  37. itype, _ := bidding["type"].(string)
  38. //extract库
  39. extractsession := extractmgo.GetMgoConn(86400)
  40. defer extractmgo.DestoryMongoConn(extractsession)
  41. extractquery := extractsession.DB(extractdb).C(extractc).Find(q).Sort("_id").Iter()
  42. eMap := map[string]map[string]interface{}{}
  43. for tmp := make(map[string]interface{}); extractquery.Next(tmp); {
  44. tid := qutil.BsonIdToSId(tmp["_id"])
  45. eMap[tid] = tmp
  46. tmp = make(map[string]interface{})
  47. }
  48. //bidding库
  49. session := mgo.GetMgoConn(86400)
  50. count, _ := session.DB(db).C(c).Find(&q).Count()
  51. log.Println("查询语句:", q, "同步总数:", count, "elastic库:", index)
  52. n1, n2 := 0, 0
  53. if count < 200000 {
  54. res := make([]map[string]interface{}, 1)
  55. session.DB(db).C(c).Find(q).Select(bson.M{
  56. "projectinfo.attachment": 0,
  57. "contenthtml": 0,
  58. }).All(&res)
  59. mgo.DestoryMongoConn(session)
  60. if len(res) != count {
  61. log.Println("查询结果不一致", "count:", count, "res:", len(res))
  62. time.Sleep(20 * time.Second)
  63. toadd := &net.UDPAddr{
  64. IP: net.ParseIP("127.0.0.1"),
  65. Port: qutil.IntAll(Sysconfig["udpport"]),
  66. }
  67. udpclient.WriteUdp(data, mu.OP_TYPE_DATA, toadd)
  68. } else {
  69. n1, n2 = doIndex(res, eMap, index, itype, db, c, bkey)
  70. if (n1 + n2) != count {
  71. log.Println("任务错误,结果不一致")
  72. }
  73. }
  74. } else {
  75. log.Println("数据量太大,放弃!", count)
  76. mgo.DestoryMongoConn(session)
  77. }
  78. log.Println(mapInfo, "create bidding index...over", "all:", count, "n1:", n1, "n2:", n2)
  79. //go delEs(mapInfo, index, itype, db, c) //删除索引
  80. }
  81. //删除索引
  82. // func delEs(mapInfo map[string]interface{}, index, itype, db, c string) {
  83. // defer qutil.Catch()
  84. // other_delete := false
  85. // if other_index != "" && other_itype != "" {
  86. // other_delete = true
  87. // }
  88. // ids := qutil.ObjToString(mapInfo["ids"])
  89. // idsarr := strings.Split(ids, ",")
  90. // log.Println("delete ids count:", len(idsarr))
  91. // n1 := 0
  92. // update := [][]map[string]interface{}{} //将bidding表中的extracttype改为-1
  93. // set := map[string]interface{}{
  94. // "$set": map[string]interface{}{"extracttype": -1},
  95. // }
  96. // for _, id := range idsarr {
  97. // if id != "" {
  98. // update = append(update, []map[string]interface{}{ //更新
  99. // map[string]interface{}{
  100. // "_id": qutil.StringTOBsonId(id),
  101. // },
  102. // set,
  103. // })
  104. // if elastic.DelById(index, itype, id) {
  105. // n1++
  106. // }
  107. // if other_delete {
  108. // bidding_other_es.DelById(other_index, other_itype, id)
  109. // }
  110. // }
  111. // }
  112. // //更新
  113. // if len(update) > 0 {
  114. // mgo.UpdateBulkAll(db, c, update...)
  115. // }
  116. // log.Println("result delete bidding index...over", "all:", n1)
  117. // }
  118. func doIndex(infos []map[string]interface{}, eMap map[string]map[string]interface{}, index, itype, db, c, bkey string) (int, int) {
  119. qutil.Debug(len(infos))
  120. n1, n2 := 0, 0
  121. //线程池
  122. UpdatesLock := sync.Mutex{}
  123. fields := strings.Split(bidding["fields"].(string), ",")
  124. //更新数组
  125. arr := [][]map[string]interface{}{}
  126. arrEs := []map[string]interface{}{}
  127. //对比两张表数据,减少查询次数
  128. var compare bson.M
  129. log.Println("开始迭代..")
  130. for n, tmp := range infos {
  131. n1++
  132. // if sensitive := qutil.ObjToString(tmp["sensitive"]); sensitive != "" { //bidding中有敏感词,不生索引
  133. // tmp = make(map[string]interface{})
  134. // continue
  135. // }
  136. update := map[string]interface{}{} //要更新的mongo数据
  137. //对比方法----------------
  138. tid := qutil.BsonIdToSId(tmp["_id"])
  139. if eMap[tid] != nil {
  140. compare = eMap[tid]
  141. if qutil.IntAll(compare["dataging"]) == 1 { //extract中dataging=1不生索引
  142. tmp = make(map[string]interface{})
  143. compare = nil
  144. continue
  145. }
  146. delete(eMap, tid)
  147. //更新bidding表,生成索引;bidding表modifyinfo中的字段不更新
  148. modifyinfo := make(map[string]bool)
  149. if tmpmodifyinfo, ok := tmp["modifyinfo"].(map[string]interface{}); ok && tmpmodifyinfo != nil {
  150. for k, _ := range tmpmodifyinfo {
  151. modifyinfo[k] = true
  152. }
  153. }
  154. //更新bidding表,生成索引
  155. for _, k := range fields {
  156. v1 := compare[k] //extract
  157. v2 := tmp[k] //bidding
  158. if v2 == nil && v1 != nil && !modifyinfo[k] {
  159. update[k] = v1
  160. } else if v2 != nil && v1 != nil && !modifyinfo[k] {
  161. //update[k+"_b"] = v2
  162. update[k] = v1
  163. } else if v2 != nil && v1 == nil {
  164. //update[k+"_b"] = v2
  165. }
  166. }
  167. if qutil.IntAll(compare["repeat"]) == 1 {
  168. update["extracttype"] = -1
  169. } else {
  170. update["extracttype"] = 1
  171. }
  172. } else {
  173. compare = nil
  174. if qutil.IntAll(tmp["dataging"]) == 1 { //修改未抽取的bidding数据的dataging
  175. update["dataging"] = 0
  176. }
  177. }
  178. //下面可以多线程跑的--->
  179. //处理分类
  180. if compare != nil { //extract
  181. qutil.Debug("111111111111111111")
  182. subscopeclass, _ := compare["subscopeclass"].([]interface{}) //subscopeclass
  183. if subscopeclass != nil {
  184. //str := ","
  185. m1 := map[string]bool{}
  186. newclass := []string{}
  187. for _, sc := range subscopeclass {
  188. sclass, _ := sc.(string)
  189. if !m1[sclass] {
  190. m1[sclass] = true
  191. //str += sclass + ","
  192. newclass = append(newclass, sclass)
  193. }
  194. }
  195. update["s_subscopeclass"] = strings.Join(newclass, ",")
  196. update["subscopeclass"] = newclass
  197. }
  198. topscopeclass, _ := compare["topscopeclass"].([]interface{}) //topscopeclass
  199. if topscopeclass != nil {
  200. m2 := map[string]bool{}
  201. newclass := []string{}
  202. for _, tc := range topscopeclass {
  203. tclass, _ := tc.(string)
  204. tclass = reg_letter.ReplaceAllString(tclass, "") // 去除字母
  205. if !m2[tclass] {
  206. m2[tclass] = true
  207. newclass = append(newclass, tclass)
  208. }
  209. }
  210. update["s_topscopeclass"] = strings.Join(newclass, ",")
  211. }
  212. //处理中标企业
  213. // winner, _ := compare["winner"].(string)
  214. // m1 := map[string]bool{}
  215. // if winner != "" {
  216. // m1[winner] = true
  217. // }
  218. // package1 := compare["package"]
  219. // if package1 != nil {
  220. // packageM, _ := package1.(map[string]interface{})
  221. // for _, p := range packageM {
  222. // pm, _ := p.(map[string]interface{})
  223. // pw, _ := pm["winner"].(string)
  224. // if pw != "" {
  225. // m1[pw] = true
  226. // }
  227. // }
  228. // }
  229. compare = nil
  230. // if len(m1) > 0 {
  231. // //str := ","
  232. // winnerarr := []string{}
  233. // for k, _ := range m1 {
  234. // //str += k + ","
  235. // winnerarr = append(winnerarr, k)
  236. // }
  237. // update["s_winner"] = strings.Join(winnerarr, ",")
  238. // }
  239. }
  240. //------------------对比结束
  241. //处理key descript
  242. if bkey == "" {
  243. DealInfo(&tmp, &update)
  244. }
  245. qutil.Debug(2222222222222222)
  246. //同时保存到elastic
  247. for tk, tv := range update {
  248. tmp[tk] = tv
  249. }
  250. if tmp["s_winner"] != "" {
  251. sWinnerarr := strings.Split(qutil.ObjToString(tmp["s_winner"]), ",")
  252. var cid []string
  253. for _, w := range sWinnerarr {
  254. if w != "" {
  255. ent, _ := mgostandard.FindOne("qyxy_historyname", map[string]interface{}{"compnay_name": w})
  256. if len(*ent) > 0 {
  257. cid = append(cid, qutil.ObjToString((*ent)["company_id"]))
  258. }
  259. }
  260. }
  261. qutil.Debug(5555555555555)
  262. if len(cid) > 0 {
  263. tmp["entidlist"] = cid
  264. update["entidlist"] = cid
  265. tmp_up := []map[string]interface{}{}
  266. tmp_up = append(tmp_up, map[string]interface{}{"_id": tmp["_id"]})
  267. tmp_up = append(tmp_up, map[string]interface{}{"$set": map[string]interface{}{"entidlist": cid}})
  268. UpdataMgoCache <- tmp_up
  269. }
  270. }
  271. //对projectscope字段的索引处理
  272. ps, _ := tmp["projectscope"].(string)
  273. if len(ps) > ESLEN {
  274. tmp["projectscope"] = string(([]rune(ps))[:4000])
  275. }
  276. //对标的物为空处理
  277. if filetext := getFileText(tmp); len(filetext) > 10 { //attach_text
  278. // if site, _ := tmp["site"].(string); site == "中国招标投标公共服务平台" { //site:中国招标投标公共服务平台 detail替换成filetext 并加入标记filedetail=1
  279. // tmp["detail"] = filetext //更新es中detail
  280. // update["detail"] = filetext //更新mongo中detail
  281. // update["filedetail"] = 1 //mongo中打标记
  282. // }
  283. tmp["filetext"] = filetext
  284. }
  285. if purchasing, ok := tmp["purchasing"].(string); ok && purchasing == "" {
  286. delete(tmp, "purchasing")
  287. }
  288. if purchasinglist, ok := tmp["purchasinglist"].([]interface{}); ok && len(purchasinglist) == 0 {
  289. delete(tmp, "purchasinglist")
  290. }
  291. //数据为空处理
  292. for _, f := range []string{"bidstatus", "city", "district", "channel"} {
  293. if fVal, ok := tmp[f].(string); ok && fVal == "" {
  294. delete(tmp, f)
  295. }
  296. }
  297. UpdatesLock.Lock()
  298. // for k1, _ := range tmp {
  299. // if strings.HasSuffix(k1, "_b") || k1 == "contenthtml" {
  300. // delete(tmp, k1)
  301. // }
  302. // }
  303. go IS.Add("bidding")
  304. if qutil.IntAll(update["extracttype"]) != -1 {
  305. qutil.Debug(44444444444444)
  306. newTmp := map[string]interface{}{} //最终生索引的数据
  307. for field, ftype := range biddingIndexFieldsMap { //
  308. if tmp[field] != nil { //
  309. if field == "projectinfo" {
  310. mp, _ := tmp[field].(map[string]interface{})
  311. if mp != nil {
  312. newmap := map[string]interface{}{}
  313. for k, ktype := range projectinfoFieldsMap {
  314. mpv := mp[k]
  315. if mpv != nil && reflect.TypeOf(mpv).String() == ktype {
  316. newmap[k] = mp[k]
  317. }
  318. }
  319. if len(newmap) > 0 {
  320. newTmp[field] = newmap
  321. }
  322. }
  323. } else if field == "purchasinglist" { //标的物处理
  324. purchasinglist_new := []map[string]interface{}{}
  325. if pcl, _ := tmp[field].([]interface{}); len(pcl) > 0 {
  326. for _, ls := range pcl {
  327. lsm_new := make(map[string]interface{})
  328. lsm := ls.(map[string]interface{})
  329. for pf, pftype := range purchasinglistFieldsMap {
  330. lsmv := lsm[pf]
  331. if lsmv != nil && reflect.TypeOf(lsmv).String() == pftype {
  332. lsm_new[pf] = lsm[pf]
  333. }
  334. }
  335. if lsm_new != nil && len(lsm_new) > 0 {
  336. purchasinglist_new = append(purchasinglist_new, lsm_new)
  337. }
  338. }
  339. }
  340. if len(purchasinglist_new) > 0 {
  341. newTmp[field] = purchasinglist_new
  342. }
  343. } else if field == "winnerorder" { //中标候选
  344. winnerorder_new := []map[string]interface{}{}
  345. if winnerorder, _ := tmp[field].([]interface{}); len(winnerorder) > 0 {
  346. for _, win := range winnerorder {
  347. winMap_new := make(map[string]interface{})
  348. winMap := win.(map[string]interface{})
  349. for wf, wftype := range winnerorderlistFieldsMap {
  350. wfv := winMap[wf]
  351. if wfv != nil && reflect.TypeOf(wfv).String() == wftype {
  352. if wf == "sort" && qutil.Int64All(wfv) > 100 {
  353. continue
  354. }
  355. winMap_new[wf] = winMap[wf]
  356. }
  357. }
  358. if winMap_new != nil && len(winMap_new) > 0 {
  359. winnerorder_new = append(winnerorder_new, winMap_new)
  360. }
  361. }
  362. }
  363. if len(winnerorder_new) > 0 {
  364. newTmp[field] = winnerorder_new
  365. }
  366. } else if field == "qualifies" {
  367. //项目资质
  368. qs := []string{}
  369. if q, _ := tmp[field].([]interface{}); len(q) > 0 {
  370. for _, v := range q {
  371. v1 := v.(map[string]interface{})
  372. qs = append(qs, qutil.ObjToString(v1["key"]))
  373. }
  374. }
  375. if len(qs) > 0 {
  376. newTmp[field] = strings.Join(qs, ",")
  377. }
  378. } else if field == "detail" { //过滤
  379. detail, _ := tmp[field].(string)
  380. if len([]rune(detail)) > detailLength {
  381. detail = detail[:detailLength]
  382. }
  383. newTmp[field] = FilterDetail(detail)
  384. } else if field == "_id" || field == "topscopeclass" { //不做处理
  385. newTmp[field] = tmp[field]
  386. } else if field == "publishtime" || field == "comeintime" {
  387. //字段类型不正确,特别处理
  388. if tmp[field] != nil && qutil.Int64All(tmp[field]) > 0 {
  389. newTmp[field] = qutil.Int64All(tmp[field])
  390. }
  391. } else { //其它字段判断数据类型,不正确舍弃
  392. if fieldval := tmp[field]; reflect.TypeOf(fieldval).String() != ftype {
  393. continue
  394. } else {
  395. newTmp[field] = fieldval
  396. }
  397. }
  398. }
  399. }
  400. // for _, v := range biddingIndexFields { //索引字段
  401. // if tmp[v] != nil {
  402. // if "projectinfo" == v {
  403. // mp, _ := tmp[v].(map[string]interface{})
  404. // if mp != nil {
  405. // newmap := map[string]interface{}{}
  406. // for _, v1 := range projectinfoFields {
  407. // if mp[v1] != nil {
  408. // newmap[v1] = fmt.Sprint(mp[v1])
  409. // }
  410. // }
  411. // if len(newmap) > 0 {
  412. // newTmp[v] = newmap
  413. // }
  414. // // attachments := mp["attachments"]
  415. // // con := ""
  416. // // if attachments != nil {
  417. // // am, _ := attachments.(map[string]interface{})
  418. // // if am != nil {
  419. // // for _, v1 := range am {
  420. // // vm, _ := v1.(map[string]interface{})
  421. // // if vm != nil {
  422. // // c, _ := vm["content"].(string)
  423. // // con += c
  424. // // }
  425. // // }
  426. // // }
  427. // // }
  428. // // con = FilterDetailSpace(con)
  429. // // if con != "" {
  430. // // newTmp["attachments"] = con
  431. // // }
  432. // }
  433. // } else if v == "purchasinglist" { //标的物处理
  434. // purchasinglist_new := []map[string]interface{}{}
  435. // if pcl, _ := tmp[v].([]interface{}); len(pcl) > 0 {
  436. // for _, ls := range pcl {
  437. // lsm_new := make(map[string]interface{})
  438. // lsm := ls.(map[string]interface{})
  439. // for _, pf := range purchasinglistFields {
  440. // if lsm[pf] != nil {
  441. // lsm_new[pf] = lsm[pf]
  442. // }
  443. // }
  444. // if lsm_new != nil && len(lsm_new) > 0 {
  445. // purchasinglist_new = append(purchasinglist_new, lsm_new)
  446. // }
  447. // }
  448. // }
  449. // if len(purchasinglist_new) > 0 {
  450. // newTmp[v] = purchasinglist_new
  451. // }
  452. // /*} else if v == "winnerorder" { //中标候选
  453. // winnerorder_new := []map[string]interface{}{}
  454. // if winnerorder, _ := tmp[v].([]interface{}); len(winnerorder) > 0 {
  455. // for _, win := range winnerorder {
  456. // winMap_new := make(map[string]interface{})
  457. // winMap := win.(map[string]interface{})
  458. // for _, wf := range winnerorderlistFields {
  459. // if wfv := winMap[wf]; wfv != nil {
  460. // if wf == "sort" && qutil.Int64All(wfv) > 100 {
  461. // continue
  462. // }
  463. // winMap_new[wf] = winMap[wf]
  464. // }
  465. // }
  466. // if winMap_new != nil && len(winMap_new) > 0 {
  467. // winnerorder_new = append(winnerorder_new, winMap_new)
  468. // }
  469. // }
  470. // }
  471. // if len(winnerorder_new) > 0 {
  472. // newTmp[v] = winnerorder_new
  473. // }
  474. // */
  475. // } else {
  476. // if v == "detail" {
  477. // detail, _ := tmp[v].(string)
  478. // if len([]rune(detail)) > detailLength {
  479. // detail = detail[:detailLength]
  480. // }
  481. // newTmp[v] = FilterDetail(detail)
  482. // } else {
  483. // newTmp[v] = tmp[v]
  484. // }
  485. // }
  486. // }
  487. // }
  488. arrEs = append(arrEs, newTmp)
  489. qutil.Debug(newTmp)
  490. }
  491. if len(update) > 0 {
  492. delete(update, "winnerorder") //winnerorder不需要更新到bindding表,删除
  493. arr = append(arr, []map[string]interface{}{
  494. map[string]interface{}{
  495. "_id": tmp["_id"],
  496. },
  497. map[string]interface{}{
  498. "$set": update,
  499. },
  500. })
  501. }
  502. if len(arr) >= BulkSize-1 {
  503. mgo.UpdateBulkAll(db, c, arr...)
  504. arr = [][]map[string]interface{}{}
  505. }
  506. if len(arrEs) >= BulkSize-1 {
  507. tmps := arrEs
  508. elastic.BulkSave(index, itype, &tmps, true)
  509. if other_index != "" && other_itype != "" {
  510. bidding_other_es.BulkSave(other_index, other_itype, &tmps, true)
  511. }
  512. if len(multiIndex) == 2 {
  513. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  514. }
  515. arrEs = []map[string]interface{}{}
  516. }
  517. UpdatesLock.Unlock()
  518. if n%100 == 0 {
  519. log.Println("current:", n)
  520. }
  521. tmp = make(map[string]interface{})
  522. }
  523. UpdatesLock.Lock()
  524. if len(arr) > 0 {
  525. mgo.UpdateBulkAll(db, c, arr...)
  526. }
  527. if len(arrEs) > 0 {
  528. tmps := arrEs
  529. elastic.BulkSave(index, itype, &tmps, true)
  530. if other_index != "" && other_itype != "" {
  531. bidding_other_es.BulkSave(other_index, other_itype, &tmps, true)
  532. }
  533. if len(multiIndex) == 2 {
  534. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  535. }
  536. }
  537. UpdatesLock.Unlock()
  538. return n1, n2
  539. }
  540. var client *mu.Client
  541. var reg = regexp.MustCompile("^[0-9a-zA-Z-.]+$")
  542. var reg_space = regexp.MustCompile("(?ism)(<style.*?>.*?</style>)|([.#]?\\w{1,20}\\{.*?\\})|(<.*?>)|(\\\\t)+|\\t|( +)|( +)|(" + string(rune(160)) + "+)")
  543. var reg_row = regexp.MustCompile("(?i)<(tr|div|p)[^>]*?>|(\\n)+")
  544. var reg_dh = regexp.MustCompile("[,]+")
  545. var reg_newdb = regexp.MustCompile("([:,、:,。.;])[,]")
  546. var reg_no = regexp.MustCompile("^[0-9]*$")
  547. var reg_letter = regexp.MustCompile("[a-z]*")
  548. var MSG_SERVER = "123.56.236.148:7070"
  549. var DesLen = 120
  550. func inits() {
  551. ser := qutil.ObjToString(Sysconfig["msg_server"])
  552. if ser != "" {
  553. MSG_SERVER = ser
  554. }
  555. cf := &mu.ClientConfig{
  556. ClientName: "剑鱼抽关键词",
  557. EventHandler: func(p *mu.Packet) {},
  558. MsgServerAddr: MSG_SERVER,
  559. CanHandleEvents: []int{},
  560. OnConnectSuccess: func() {
  561. log.Println("c.")
  562. },
  563. ReadBufferSize: 10,
  564. WriteBufferSize: 10,
  565. }
  566. client, _ = mu.NewClient(cf)
  567. }
  568. //var clientlock = &sync.Mutex{}
  569. var keypool = make(chan bool, 1)
  570. func DealInfo(obj, update *map[string]interface{}) {
  571. defer qutil.Catch()
  572. if (*obj)["keywords"] != nil && (*obj)["description"] != nil {
  573. return
  574. } else {
  575. (*update)["keywords"] = ""
  576. (*update)["description"] = ""
  577. }
  578. title := qutil.ObjToString((*obj)["title"])
  579. var m [][]string
  580. select {
  581. case <-func() <-chan bool {
  582. ch := make(chan bool, 1)
  583. go func(chan bool) {
  584. select {
  585. case keypool <- true:
  586. defer func() {
  587. <-keypool
  588. }()
  589. ret, _ := client.Call("", mu.UUID(8), 4010, mu.SENDTO_TYPE_RAND_RECIVER, title, 1)
  590. json.Unmarshal(ret, &m)
  591. case <-time.After(10 * time.Millisecond):
  592. }
  593. ch <- true
  594. }(ch)
  595. return ch
  596. }():
  597. case <-time.After(40 * time.Millisecond):
  598. }
  599. arr := []string{}
  600. keyword := []string{}
  601. keywordnew := []string{}
  602. for _, tmp := range m {
  603. if reg.MatchString(tmp[0]) {
  604. arr = append(arr, tmp[0])
  605. } else {
  606. if len(arr) > 0 {
  607. str := strings.Join(arr, "")
  608. keyword = append(keyword, str)
  609. arr = []string{}
  610. }
  611. if len(tmp[0]) > 3 && (strings.HasPrefix(tmp[1], "n") || tmp[1] == "v" || tmp[1] == "vn" || strings.HasPrefix(tmp[1], "g")) {
  612. keyword = append(keyword, tmp[0])
  613. }
  614. }
  615. }
  616. for _, v := range keyword {
  617. v = reg_no.ReplaceAllString(v, "")
  618. if len(v) > 0 {
  619. keywordnew = append(keywordnew, v)
  620. }
  621. }
  622. keywords := strings.Join(keywordnew, ",")
  623. (*update)["keywords"] = keywords
  624. content := ""
  625. if (*obj)["detail_bak"] != nil {
  626. content = qutil.ObjToString((*obj)["detail_bak"])
  627. } else {
  628. content = qutil.ObjToString((*obj)["detail"])
  629. }
  630. //内容替换
  631. content = strings.Replace(content, " ", "", -1)
  632. content = reg_space.ReplaceAllString(content, "")
  633. content = reg_row.ReplaceAllString(content, ",")
  634. content = reg_dh.ReplaceAllString(content, ",")
  635. content = reg_newdb.ReplaceAllString(content, "$1")
  636. if strings.HasPrefix(content, ",") {
  637. content = content[1:]
  638. }
  639. //log.Println(content)
  640. tc := []rune(content)
  641. ltc := len(tc)
  642. description := content
  643. if ltc > DesLen {
  644. description = string(tc[:DesLen])
  645. }
  646. (*update)["description"] = description
  647. //保存到数据库
  648. return
  649. }