timedTaskBuyer.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/garyburd/redigo/redis"
  6. "go.mongodb.org/mongo-driver/bson/primitive"
  7. "gopkg.in/mgo.v2/bson"
  8. "log"
  9. mu "mfw/util"
  10. "net"
  11. "qfw/util"
  12. "sort"
  13. "strings"
  14. "time"
  15. )
  16. //之前main方法,只更新
  17. func TaskBuyer(mapinfo *map[string]interface{}) {
  18. defer util.Catch()
  19. gtid, lteid := util.ObjToString((*mapinfo)["gtid"]), util.ObjToString((*mapinfo)["lteid"])
  20. if gtid == "" || lteid == "" {
  21. log.Println(gtid, lteid, "参数错误")
  22. return
  23. }
  24. GId, err := primitive.ObjectIDFromHex(gtid)
  25. LtId, err2 := primitive.ObjectIDFromHex(lteid)
  26. if err != nil || err2 != nil {
  27. log.Println(gtid, lteid, "转换_id错误")
  28. return
  29. }
  30. //udp的id区间查询bidding 中标人 中标联系人 中标联系电话
  31. // topscopeclass项目类型-industry行业类型&&topscopeclass联系人项目类型
  32. // (area地区-province省份 city城市-city城市 district区县-district区县)
  33. // winneraddr-company_address企业地址
  34. SourceClientcc := SourceClient.GetMgoConn()
  35. defer SourceClient.DestoryMongoConn(SourceClientcc)
  36. cursor := SourceClientcc.DB(Config["mgodb_bidding"]).C(Config["mgodb_mgoinit_c"]).Find(bson.M{
  37. "_id": bson.M{
  38. "$gte": GId,
  39. "$lte": LtId,
  40. },
  41. }).Select(bson.M{"buyer": 1, "buyertel": 1, "buyerperson": 1,
  42. "topscopeclass": 1, "buyeraddr": 1}).Iter()
  43. if cursor == nil {
  44. log.Println(cursor)
  45. return
  46. }
  47. //判断是否是存量,是存量走Redis遍历
  48. if v, ok := (*mapinfo)["data_info"].(string); ok && v == "save" {
  49. //存量处理
  50. tmp := map[string]interface{}{}
  51. conn := HisRedisPool.Conn()
  52. defer conn.Close()
  53. //选择redis db
  54. conn.Select(1)
  55. //遍历bidding表保存到redis
  56. // key:_id value:json结构体
  57. for cursor.Next(&tmp) {
  58. if tmp["buyer"] == nil || tmp["buyer"] == "" {
  59. continue
  60. }
  61. bytes, _ := json.Marshal(tmp)
  62. if err := conn.Set(tmp["_id"].(primitive.ObjectID).Hex(), string(bytes), 0).Err(); err != nil {
  63. log.Println(err)
  64. }
  65. }
  66. //遍历redis
  67. if scan := conn.Scan(0, "", 100); scan.Err() != nil {
  68. log.Println(scan.Err())
  69. return
  70. } else {
  71. iterator := scan.Iterator()
  72. for iterator.Next() {
  73. redisId := iterator.Val() //redis key
  74. redisvalue := conn.Get(iterator.Val()).String() //redis val
  75. tmp := make(map[string]interface{})
  76. json.Unmarshal([]byte(redisvalue),&tmp)
  77. //重复增量操作
  78. //redis查询是否存在
  79. rdb := RedisPool.Get()
  80. if reply, err := redis.String(rdb.Do("GET", tmp["buyer"])); err != nil {
  81. //redis不存在,存到临时表,定时任务处理
  82. FClient.DbName = Config["mgodb_extract_kf"]
  83. if tmpid := FClient.Save("buyer_new", tmp); tmpid == nil {
  84. log.Println("存量 FClient.Save err", tmpid)
  85. }
  86. //log.Println("get redis id err:定时任务处理", err, tmp)
  87. if err := rdb.Close(); err != nil {
  88. log.Println("存量",err)
  89. }
  90. //删除存量redis
  91. conn.Del(redisId)
  92. continue
  93. } else {
  94. if err := rdb.Close(); err != nil {
  95. log.Println(err)
  96. }
  97. //拿到合并后的qyk
  98. FClient.DbName = Config["mgodb_extract_kf"]
  99. oldTmp := FClient.FindById(Config["mgo_qyk_c"], reply)
  100. if oldTmp == nil {
  101. log.Println("存量 redis id 不存在",reply,tmp["buyer"])
  102. continue
  103. }
  104. tmpTopscopeclass := []string{}
  105. tmpTopscopeclassMap := make(map[string]bool)
  106. if v, ok := tmp["topscopeclass"].(primitive.A); ok {
  107. for _, vv := range v {
  108. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  109. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  110. }
  111. }
  112. for k := range tmpTopscopeclassMap {
  113. tmpTopscopeclass = append(tmpTopscopeclass, k)
  114. }
  115. }
  116. sort.Strings(tmpTopscopeclass)
  117. //更新buyerclass
  118. esId := oldTmp["_id"].(primitive.ObjectID).Hex()
  119. //更新行业类型
  120. if tmp["buyerperson"] == nil || tmp["buyerperson"] == "" || Reg_xing.MatchString(util.ObjToString(tmp["buyerperson"])) {
  121. //更新buyerclass合并
  122. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  123. //无值,不更新
  124. }else {
  125. //有值
  126. var buyerclass_new,buyerclass_old string
  127. buyerclass_new = tmp["buyerclass"].(string)
  128. buyerclass_old = oldTmp["buyerclass"].(string)
  129. if strings.Contains(buyerclass_old, buyerclass_new) {
  130. oldTmp["buyerclass"] = buyerclass_old + ","+buyerclass_new //采购单位类型
  131. }
  132. }
  133. oldTmp["updatatime"] = time.Now().Unix()
  134. //mongo更新
  135. FClient.DbName = Config["mgodb_extract_kf"]
  136. if !FClient.UpdateById(Config["mgo_qyk_c"], esId, bson.M{"$set": oldTmp}) {
  137. log.Println("mongo更新err", esId)
  138. }
  139. //es更新
  140. delete(oldTmp, "_id")
  141. if _, err := EsConn.Update().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  142. log.Println("update es err:", err)
  143. }
  144. //删除存量redis
  145. conn.Del(redisId)
  146. continue
  147. }
  148. //联系方式合并
  149. var tmpperson, buyertel string
  150. tmpperson = tmp["buyerperson"].(string)
  151. if tmp["buyertel"] == nil || tmp["buyertel"] == "" {
  152. buyertel = ""
  153. } else {
  154. if Reg_xing.MatchString(util.ObjToString(tmp["buyertel"])) || !Reg_tel.MatchString(util.ObjToString(tmp["buyertel"])) {
  155. buyertel = ""
  156. } else {
  157. buyertel = util.ObjToString(tmp["buyertel"])
  158. }
  159. }
  160. contactMaps := make([]interface{}, 0)
  161. if oldTmp["contact"] == nil {
  162. tmpContact := make(map[string]interface{})
  163. tmpContact["infoid"] = redisId
  164. tmpContact["contact_person"] = tmpperson
  165. tmpContact["contact_type"] = "项目联系人"
  166. tmpContact["phone"] = buyertel
  167. tmpContact["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  168. tmpContact["updatetime"] = time.Now().Unix()
  169. contactMaps = append(contactMaps, tmpContact)
  170. } else {
  171. //对比前四项,相等丢弃
  172. if v, ok := oldTmp["contact"].(primitive.A); ok {
  173. var isNotUpdate bool
  174. for _, vv := range v {
  175. if vvv, ok := vv.(map[string]interface{}); ok {
  176. if vvv["contact_person"] == tmpperson && vvv["contact_type"] == "项目联系人" &&
  177. vvv["phone"] == buyertel && vvv["topscopeclass"] == strings.Join(tmpTopscopeclass, ";") {
  178. isNotUpdate = true
  179. vvv["updatetime"] = time.Now().Unix()
  180. }
  181. contactMaps = append(contactMaps, vvv)
  182. }
  183. }
  184. if !isNotUpdate {
  185. vvv := make(map[string]interface{})
  186. vvv["infoid"] = redisId
  187. vvv["contact_person"] = tmp["buyerperson"]
  188. vvv["contact_type"] = "项目联系人"
  189. vvv["phone"] = buyertel
  190. vvv["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  191. vvv["updatetime"] = time.Now().Unix()
  192. contactMaps = append(contactMaps, vvv)
  193. }
  194. }
  195. }
  196. oldTmp["contact"] = contactMaps
  197. //更新buyerclass合并
  198. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  199. //无值,不更新
  200. }else {
  201. //有值
  202. var buyerclass_new,buyerclass_old string
  203. buyerclass_new = tmp["buyerclass"].(string)
  204. buyerclass_old = oldTmp["buyerclass"].(string)
  205. if strings.Contains(buyerclass_old, buyerclass_new) {
  206. oldTmp["buyerclass"] = buyerclass_old + ","+buyerclass_new //采购单位类型
  207. }
  208. }
  209. //mongo更新
  210. oldTmp["updatatime"] = time.Now().Unix()
  211. FClient.DbName = Config["mgodb_extract_kf"]
  212. if !FClient.UpdateById(Config["mgo_qyk_c"], esId, bson.M{"$set": oldTmp}) {
  213. log.Println("存量 mongo更新 err", esId, oldTmp)
  214. }
  215. //es更新
  216. delete(oldTmp, "_id")
  217. if _, err := EsConn.Update().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  218. log.Println("存量 EsConn err :", err)
  219. }
  220. //最后删除redis
  221. conn.Del(redisId)
  222. }
  223. }
  224. }
  225. log.Println("存量历史合并执行完成 ok", gtid, lteid)
  226. } else {
  227. //增量处理
  228. overid := gtid
  229. tmp := map[string]interface{}{}
  230. for cursor.Next(&tmp) {
  231. overid = tmp["_id"].(primitive.ObjectID).Hex()
  232. log.Println(tmp["_id"])
  233. if tmp["buyer"] == nil || tmp["buyer"] == "" {
  234. continue
  235. }
  236. //redis查询是否存在
  237. rdb := RedisPool.Get()
  238. if reply, err := redis.String(rdb.Do("GET", tmp["buyer"])); err != nil {
  239. //redis不存在存到临时表,定时任务处理
  240. FClient.DbName = Config["mgodb_extract_kf"]
  241. if tmpid := FClient.Save("buyer_new", tmp); tmpid == nil {
  242. log.Println("FClient.Save err", tmpid)
  243. }
  244. //log.Println("get redis id err:定时任务处理", err, tmp)
  245. if err := rdb.Close(); err != nil {
  246. log.Println(err)
  247. }
  248. continue
  249. } else {
  250. if err := rdb.Close(); err != nil {
  251. log.Println(err)
  252. }
  253. //拿到合并后的qyk
  254. FClient.DbName = Config["mgodb_extract_kf"]
  255. oldTmp := FClient.FindById(Config["mgo_qyk_c"], reply)
  256. if oldTmp == nil {
  257. log.Println("redis id 不存在")
  258. continue
  259. }
  260. //比较合并
  261. //行业类型
  262. tmpTopscopeclass := []string{}
  263. tmpTopscopeclassMap := make(map[string]bool)
  264. if v, ok := tmp["topscopeclass"].(primitive.A); ok {
  265. for _, vv := range v {
  266. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  267. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  268. }
  269. }
  270. for k := range tmpTopscopeclassMap {
  271. tmpTopscopeclass = append(tmpTopscopeclass, k)
  272. }
  273. }
  274. sort.Strings(tmpTopscopeclass)
  275. esId := oldTmp["_id"].(primitive.ObjectID).Hex()
  276. //更新行业类型 buyerclass合并
  277. if tmp["buyerperson"] == nil || tmp["buyerperson"] == "" || Reg_xing.MatchString(util.ObjToString(tmp["buyerperson"])) {
  278. //更新buyerclass合并
  279. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  280. //无值,不更新
  281. }else {
  282. //有值
  283. var buyerclass_new,buyerclass_old string
  284. buyerclass_new = tmp["buyerclass"].(string)
  285. buyerclass_old = oldTmp["buyerclass"].(string)
  286. if strings.Contains(buyerclass_old, buyerclass_new) {
  287. oldTmp["buyerclass"] = buyerclass_old + ","+buyerclass_new //采购单位类型
  288. }
  289. }
  290. oldTmp["updatatime"] = time.Now().Unix()
  291. //mongo更新
  292. FClient.DbName = Config["mgodb_extract_kf"]
  293. if !FClient.UpdateById(Config["mgo_qyk_c"], esId, bson.M{"$set": oldTmp}) {
  294. log.Println("mongo更新err", esId)
  295. }
  296. //es更新
  297. delete(oldTmp, "_id")
  298. if _, err := EsConn.Update().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  299. log.Println("update es err:", err)
  300. }
  301. continue
  302. }
  303. //联系方式合并
  304. var tmpperson, buyertel string
  305. tmpperson = tmp["buyerperson"].(string)
  306. if tmp["buyertel"] == nil || tmp["buyertel"] == "" {
  307. buyertel = ""
  308. } else {
  309. if Reg_xing.MatchString(util.ObjToString(tmp["buyertel"])) || !Reg_tel.MatchString(util.ObjToString(tmp["buyertel"])) {
  310. buyertel = ""
  311. } else {
  312. buyertel = util.ObjToString(tmp["buyertel"])
  313. }
  314. }
  315. contactMaps := make([]interface{}, 0)
  316. if oldTmp["contact"] == nil {
  317. tmpContact := make(map[string]interface{})
  318. tmpContact["infoid"] = overid
  319. tmpContact["contact_person"] = tmpperson
  320. tmpContact["contact_type"] = "项目联系人"
  321. tmpContact["phone"] = buyertel
  322. tmpContact["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  323. tmpContact["updatetime"] = time.Now().Unix()
  324. contactMaps = append(contactMaps, tmpContact)
  325. } else {
  326. //对比前四项,相等丢弃
  327. if v, ok := oldTmp["contact"].(primitive.A); ok {
  328. var isNotUpdate bool
  329. for _, vv := range v {
  330. if vvv, ok := vv.(map[string]interface{}); ok {
  331. if vvv["contact_person"] == tmpperson && vvv["contact_type"] == "项目联系人" &&
  332. vvv["phone"] == buyertel && vvv["topscopeclass"] == strings.Join(tmpTopscopeclass, ";") {
  333. isNotUpdate = true
  334. vvv["updatetime"] = time.Now().Unix()
  335. }
  336. contactMaps = append(contactMaps, vvv)
  337. }
  338. }
  339. if !isNotUpdate {
  340. vvv := make(map[string]interface{})
  341. vvv["infoid"] = overid
  342. vvv["contact_person"] = tmp["buyerperson"]
  343. vvv["contact_type"] = "项目联系人"
  344. vvv["phone"] = buyertel
  345. vvv["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  346. vvv["updatetime"] = time.Now().Unix()
  347. contactMaps = append(contactMaps, vvv)
  348. }
  349. }
  350. }
  351. oldTmp["contact"] = contactMaps
  352. //更新buyerclass合并
  353. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  354. //无值,不更新
  355. }else {
  356. //有值
  357. var buyerclass_new,buyerclass_old string
  358. buyerclass_new = tmp["buyerclass"].(string)
  359. buyerclass_old = oldTmp["buyerclass"].(string)
  360. if strings.Contains(buyerclass_old, buyerclass_new) {
  361. oldTmp["buyerclass"] = buyerclass_old + ","+buyerclass_new //采购单位类型
  362. }
  363. }
  364. //mongo更新
  365. oldTmp["updatatime"] = time.Now().Unix()
  366. FClient.DbName = Config["mgodb_extract_kf"]
  367. if !FClient.UpdateById(Config["mgo_qyk_c"], esId, bson.M{"$set": oldTmp}) {
  368. log.Println("mongo更新 err", esId, oldTmp)
  369. }
  370. //es更新
  371. delete(oldTmp, "_id")
  372. if _, err := EsConn.Update().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  373. log.Println("EsConn err :", err)
  374. }
  375. }
  376. }
  377. log.Println("增量合并执行完成 ok", gtid, lteid, overid)
  378. }
  379. }
  380. //定时任务
  381. //1.存异常表
  382. //2.合并原始库新增
  383. func TimedTaskBuyer() {
  384. //time.Sleep(time.Hour*70)
  385. t2 := time.NewTimer(time.Second * 5)
  386. for range t2.C {
  387. Fcconn := FClient.GetMgoConn()
  388. defer FClient.DestoryMongoConn(Fcconn)
  389. tmpLast := map[string]interface{}{}
  390. if iter := Fcconn.DB(Config["mgodb_extract_kf"]).C("buyer_new").Find(bson.M{}).Sort("-_id").Limit(1).Iter(); iter != nil {
  391. if !iter.Next(&tmpLast) {
  392. //临时表无数据
  393. log.Println("临时表无数据:")
  394. t2.Reset(time.Minute * 5)
  395. continue
  396. } else {
  397. log.Println("临时表有数据:", tmpLast)
  398. fconn := FClient.GetMgoConn()
  399. defer FClient.DestoryMongoConn(fconn)
  400. cursor := fconn.DB(Config["mgodb_extract_kf"]).C("buyer_new").Find(bson.M{
  401. "_id": bson.M{
  402. "$lte": tmpLast["_id"],
  403. },
  404. }).Sort("_id").Iter()
  405. if cursor == nil {
  406. log.Println("查询失败")
  407. t2.Reset(time.Second * 5)
  408. continue
  409. }
  410. //遍历临时表数据,匹配不到原始库存入异常表
  411. tmp := make(map[string]interface{})
  412. for cursor.Next(&tmp) {
  413. tmpId := tmp["_id"].(primitive.ObjectID).Hex()
  414. //再重新查找redis,存在发udp处理,不存在走新增合并
  415. rdb := RedisPool.Get()
  416. if _, err := redis.String(rdb.Do("GET", tmp["buyer"])); err == nil {
  417. //{"gtid":"57d7ad2f61a0721f152d2ad5","lteid":"5e20968d85a9271abf0ad6c2","stype":""}
  418. //redis存在发送udp进行处理
  419. by, _ := json.Marshal(map[string]interface{}{
  420. "gtid": tmpId,
  421. "lteid": tmpId,
  422. "stype": "",
  423. })
  424. if e := udpclient.WriteUdp(by, mu.OP_TYPE_DATA, &net.UDPAddr{
  425. IP: net.ParseIP("127.0.0.1"),
  426. Port: Updport,
  427. }); e != nil {
  428. log.Println(e)
  429. }
  430. //存在的话删除tmp mongo表
  431. FClient.DbName = Config["mgodb_extract_kf"]
  432. if DeletedCount := FClient.DeleteById("buyer_new", tmpId); DeletedCount == 0 {
  433. log.Println("删除临时表err:", DeletedCount)
  434. }
  435. if err := rdb.Close(); err != nil {
  436. log.Println(err)
  437. }
  438. continue
  439. } else {
  440. if err = rdb.Close(); err != nil {
  441. log.Println(err)
  442. }
  443. }
  444. //查询redis不存在新增
  445. FClient.DbName = Config["mgodb_enterprise"]
  446. //qyxy 企业库 两亿条
  447. resulttmp := FClient.FindOne(Config["mgodb_enterprise_c"], bson.M{"company_name": tmp["buyer"]})
  448. if resulttmp["_id"] == nil {
  449. //log.Println(r)
  450. //匹配不到原始库,存入异常表删除临时表
  451. FClient.DbName = Config["mgodb_extract_kf"]
  452. if saveid := FClient.Save("buyer_err", tmp); saveid == nil {
  453. log.Println("存入异常表错误", tmp)
  454. }
  455. FClient.DbName = Config["mgodb_extract_kf"]
  456. if deleteNum := FClient.DeleteById("buyer_new", tmpId); deleteNum == 0 {
  457. log.Println("删除临时表错误", deleteNum)
  458. }
  459. continue
  460. } else {
  461. //log.Println(123)
  462. //匹配到原始库,新增 resulttmp
  463. if resulttmp["credit_no"] != nil {
  464. if credit_no, ok := resulttmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  465. len(strings.TrimSpace(credit_no)) > 8 {
  466. dataNo := strings.TrimSpace(credit_no)[2:8]
  467. if Addrs[dataNo] != nil {
  468. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  469. if resulttmp["province"] == nil || resulttmp["province"] == "" {
  470. resulttmp["province"] = v["province"]
  471. }
  472. resulttmp["city"] = v["city"]
  473. resulttmp["district"] = v["district"]
  474. }
  475. }
  476. }
  477. }
  478. contacts := make([]map[string]interface{}, 0)
  479. contact := make(map[string]interface{}, 0)
  480. if resulttmp["legal_person"] != nil {
  481. contact["contact_person"] = resulttmp["legal_person"] //联系人
  482. } else {
  483. contact["contact_person"] = "" //联系人
  484. }
  485. contact["contact_type"] = "法定代表人" //法定代表人
  486. //log.Println(1)
  487. if resulttmp["annual_reports"] != nil {
  488. bytes, err := json.Marshal(resulttmp["annual_reports"])
  489. if err != nil {
  490. log.Println("annual_reports err:", err)
  491. }
  492. phonetmp := make([]map[string]interface{}, 0)
  493. err = json.Unmarshal(bytes, &phonetmp)
  494. if err != nil {
  495. log.Println("Unmarshal err:", err)
  496. }
  497. for _, vv := range phonetmp {
  498. if vv["company_phone"] != nil {
  499. if vv["company_phone"] == "" {
  500. continue
  501. } else {
  502. contact["phone"] = vv["company_phone"] //联系电话
  503. break
  504. }
  505. } else {
  506. contact["phone"] = "" //联系电话
  507. }
  508. }
  509. }
  510. //log.Println(k, contact["phone"], resulttmp["_id"])
  511. //time.Sleep(10 * time.Second)
  512. if contact["phone"] == nil {
  513. contact["phone"] = "" //联系电话
  514. }
  515. contact["topscopeclass"] = "企业公示" //项目类型
  516. contact["updatetime"] = time.Now().Unix() //更新时间
  517. contact["infoid"] = "" //招标信息id
  518. contacts = append(contacts, contact)
  519. resulttmp["contact"] = contacts
  520. savetmp := make(map[string]interface{}, 0)
  521. for _, sk := range BuyerFields {
  522. if sk == "_id" {
  523. savetmp["tmp"+sk] = resulttmp[sk]
  524. continue
  525. } else if sk == "area_code" {
  526. //行政区划代码
  527. savetmp[sk] = fmt.Sprint(resulttmp[sk])
  528. continue
  529. } else if sk == "report_websites" {
  530. //网址
  531. if resulttmp["report_websites"] == nil {
  532. savetmp["website"] = ""
  533. } else {
  534. report_websitesArr := []string{}
  535. if ppms, ok := resulttmp[sk].(primitive.A); ok {
  536. for _, v := range ppms {
  537. if vvv, ok := v.(map[string]interface{}); ok {
  538. if rv, ok := vvv["website_url"].(string); ok {
  539. report_websitesArr = append(report_websitesArr, rv)
  540. }
  541. }
  542. }
  543. }
  544. sort.Strings(report_websitesArr)
  545. savetmp["website"] = strings.Join(report_websitesArr, ";")
  546. }
  547. continue
  548. } else if sk == "wechat_accounts" {
  549. savetmp[sk] = []interface{}{}
  550. continue
  551. }else if sk=="buyer_name" {
  552. if resulttmp["company_name"] == nil {
  553. savetmp[sk] = ""
  554. }else {
  555. savetmp[sk] = resulttmp[sk]
  556. }
  557. continue
  558. }else if sk=="address"{
  559. if resulttmp["company_address"] == nil {
  560. savetmp[sk] = ""
  561. }else {
  562. savetmp[sk] = resulttmp[sk]
  563. }
  564. continue
  565. }
  566. if resulttmp[sk] == nil && sk != "history_name" && sk != "wechat_accounts" &&
  567. sk != "buyer_name" && sk != "address" &&
  568. sk != "contact" && sk != "report_websites" {
  569. savetmp[sk] = ""
  570. } else {
  571. savetmp[sk] = resulttmp[sk]
  572. }
  573. }
  574. //tmps = append(tmps, savetmp)
  575. savetmp["updatatime"] = time.Now().Unix()
  576. //保存mongo
  577. FClient.DbName = Config["mgodb_extract_kf"]
  578. saveid := FClient.Save(Config["mgo_qyk_c"], savetmp)
  579. if saveid != nil {
  580. //保存redis
  581. rc := RedisPool.Get()
  582. var _id string
  583. if v, ok := saveid.(primitive.ObjectID); ok {
  584. _id = v.Hex()
  585. }
  586. if _, err := rc.Do("SET", savetmp["buyer_name"], _id); err != nil {
  587. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["buyer_name"], err)
  588. if err := rc.Close(); err != nil {
  589. log.Println(err)
  590. }
  591. } else {
  592. //保存es
  593. delete(savetmp, "_id")
  594. if err := rc.Close(); err != nil {
  595. log.Println(err)
  596. }
  597. //esConn := elastic.GetEsConn()
  598. //defer elastic.DestoryEsConn(esConn)
  599. if _, err := EsConn.Index().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(_id).BodyJson(savetmp).Refresh(true).Do(); err != nil {
  600. log.Println("save es err :", tmp["_id"], savetmp["_id"], err)
  601. } else {
  602. //删除临时表
  603. FClient.DbName = Config["mgodb_extract_kf"]
  604. if deleteNum := FClient.DeleteById("buyer_new", tmpId); deleteNum == 0 {
  605. log.Println("删除临时表失败", deleteNum)
  606. }
  607. }
  608. }
  609. } else {
  610. log.Println("save mongo err:", saveid, tmp["_id"])
  611. }
  612. }
  613. }
  614. }
  615. }
  616. t2.Reset(time.Minute)
  617. }
  618. }