timedTaskBuyer.go 22 KB

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