timedTaskBuyer.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. if tmp["buyer"]=="无" {
  255. log.Println("redis不存在:",tmp["buyer"])
  256. }
  257. FClient.DbName = Config["mgodb_extract_kf"]
  258. if tmpid := FClient.Save("buyer_new", tmp); tmpid == nil {
  259. log.Println("FClient.Save err", tmpid)
  260. }
  261. //log.Println("get redis id err:定时任务处理", err, tmp)
  262. if err := rdb.Close(); err != nil {
  263. log.Println(err)
  264. }
  265. continue
  266. } else {
  267. if err := rdb.Close(); err != nil {
  268. log.Println(err)
  269. }
  270. //拿到合并后的qyk
  271. FClient.DbName = Config["mgodb_extract_kf"]
  272. oldTmp := FClient.FindById(Config["mgo_qyk_buyer"], reply)
  273. if oldTmp == nil {
  274. log.Println("redis id 不存在")
  275. continue
  276. }
  277. //比较合并
  278. //行业类型
  279. tmpTopscopeclass := []string{}
  280. tmpTopscopeclassMap := make(map[string]bool)
  281. if v, ok := tmp["topscopeclass"].(primitive.A); ok {
  282. for _, vv := range v {
  283. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  284. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  285. }
  286. }
  287. for k := range tmpTopscopeclassMap {
  288. tmpTopscopeclass = append(tmpTopscopeclass, k)
  289. }
  290. }
  291. sort.Strings(tmpTopscopeclass)
  292. esId := oldTmp["_id"].(primitive.ObjectID).Hex()
  293. if tmp["buyer"]=="无" {
  294. log.Println("无无无无无无无")
  295. }
  296. //更新行业类型 buyerclass合并
  297. if tmp["buyerperson"] == nil || tmp["buyerperson"] == "" || Reg_xing.MatchString(util.ObjToString(tmp["buyerperson"])) {
  298. //更新buyerclass合并
  299. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  300. //无值,不更新
  301. }else {
  302. var buyerclass_new,buyerclass_old string
  303. buyerclass_new = tmp["buyerclass"].(string)
  304. buyerclass_old = oldTmp["buyerclass"].(string)
  305. if buyerclass_old=="" {
  306. oldTmp["buyerclass"] = buyerclass_new
  307. }else {
  308. if buyerclass_new!=buyerclass_old {
  309. if !strings.Contains(buyerclass_old, buyerclass_new) {
  310. oldTmp["buyerclass"] = buyerclass_old + ","+buyerclass_new //采购单位类型
  311. }
  312. }
  313. }
  314. }
  315. oldTmp["updatatime"] = time.Now().Unix()
  316. //mongo更新
  317. FClient.DbName = Config["mgodb_extract_kf"]
  318. if !FClient.UpdateById(Config["mgo_qyk_buyer"], esId, bson.M{"$set": oldTmp}) {
  319. log.Println("mongo更新err", esId)
  320. }
  321. //es更新
  322. delete(oldTmp, "_id")
  323. if _, err := EsConn.Update().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  324. log.Println("update es err:", err)
  325. }
  326. continue
  327. }
  328. //联系方式合并
  329. var tmpperson, buyertel string
  330. tmpperson = tmp["buyerperson"].(string)
  331. if tmp["buyertel"] == nil || tmp["buyertel"] == "" {
  332. buyertel = ""
  333. } else {
  334. if Reg_xing.MatchString(util.ObjToString(tmp["buyertel"])) || !Reg_tel.MatchString(util.ObjToString(tmp["buyertel"])) {
  335. buyertel = ""
  336. } else {
  337. buyertel = util.ObjToString(tmp["buyertel"])
  338. }
  339. }
  340. contactMaps := make([]interface{}, 0)
  341. if oldTmp["contact"] == nil {
  342. tmpContact := make(map[string]interface{})
  343. tmpContact["infoid"] = overid
  344. tmpContact["contact_person"] = tmpperson
  345. tmpContact["contact_type"] = "项目联系人"
  346. tmpContact["phone"] = buyertel
  347. tmpContact["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  348. tmpContact["updatetime"] = time.Now().Unix()
  349. contactMaps = append(contactMaps, tmpContact)
  350. } else {
  351. //对比前四项,相等丢弃
  352. if v, ok := oldTmp["contact"].(primitive.A); ok {
  353. var isNotUpdate bool
  354. for _, vv := range v {
  355. if vvv, ok := vv.(map[string]interface{}); ok {
  356. if vvv["contact_person"] == tmpperson && vvv["contact_type"] == "项目联系人" &&
  357. vvv["phone"] == buyertel && vvv["topscopeclass"] == strings.Join(tmpTopscopeclass, ";") {
  358. isNotUpdate = true
  359. vvv["updatetime"] = time.Now().Unix()
  360. }
  361. contactMaps = append(contactMaps, vvv)
  362. }
  363. }
  364. if !isNotUpdate {
  365. vvv := make(map[string]interface{})
  366. vvv["infoid"] = overid
  367. vvv["contact_person"] = tmp["buyerperson"]
  368. vvv["contact_type"] = "项目联系人"
  369. vvv["phone"] = buyertel
  370. vvv["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  371. vvv["updatetime"] = time.Now().Unix()
  372. contactMaps = append(contactMaps, vvv)
  373. }
  374. }
  375. }
  376. oldTmp["contact"] = contactMaps
  377. //更新buyerclass合并
  378. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  379. //无值,不更新
  380. }else {
  381. var buyerclass_new,buyerclass_old string
  382. buyerclass_new = tmp["buyerclass"].(string)
  383. buyerclass_old = oldTmp["buyerclass"].(string)
  384. if buyerclass_old=="" {
  385. oldTmp["buyerclass"] = buyerclass_new
  386. }else {
  387. if buyerclass_new!=buyerclass_old {
  388. if !strings.Contains(buyerclass_old, buyerclass_new) {
  389. oldTmp["buyerclass"] = buyerclass_old + ","+buyerclass_new //采购单位类型
  390. }
  391. }
  392. }
  393. }
  394. //mongo更新
  395. oldTmp["updatatime"] = time.Now().Unix()
  396. FClient.DbName = Config["mgodb_extract_kf"]
  397. if !FClient.UpdateById(Config["mgo_qyk_buyer"], esId, bson.M{"$set": oldTmp}) {
  398. log.Println("mongo更新 err", esId, oldTmp)
  399. }
  400. //es更新
  401. delete(oldTmp, "_id")
  402. if _, err := EsConn.Update().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  403. log.Println("EsConn err :", err)
  404. }
  405. }
  406. }
  407. log.Println("增量合并执行完成 ok", gtid, lteid, overid)
  408. }
  409. }
  410. //定时任务
  411. //1.存异常表
  412. //2.合并原始库新增
  413. func TimedTaskBuyer() {
  414. //time.Sleep(time.Hour*70)
  415. t2 := time.NewTimer(time.Second * 5)
  416. for range t2.C {
  417. Fcconn := FClient.GetMgoConn()
  418. defer FClient.DestoryMongoConn(Fcconn)
  419. tmpLast := map[string]interface{}{}
  420. if iter := Fcconn.DB(Config["mgodb_extract_kf"]).C("buyer_new").Find(bson.M{}).Sort("-_id").Limit(1).Iter(); iter != nil {
  421. if !iter.Next(&tmpLast) {
  422. //临时表无数据
  423. log.Println("临时表无数据:")
  424. //t2.Reset(time.Second * 10) //增量
  425. t2.Reset(time.Minute * 5) //存量
  426. continue
  427. } else {
  428. log.Println("临时表有数据:", tmpLast)
  429. fconn := FClient.GetMgoConn()
  430. defer FClient.DestoryMongoConn(fconn)
  431. cursor := fconn.DB(Config["mgodb_extract_kf"]).C("buyer_new").Find(bson.M{
  432. "_id": bson.M{
  433. "$lte": tmpLast["_id"],
  434. },
  435. }).Sort("_id").Iter()
  436. if cursor == nil {
  437. log.Println("查询失败")
  438. t2.Reset(time.Second * 5)
  439. continue
  440. }
  441. //遍历临时表数据,匹配不到原始库存入异常表
  442. tmp := make(map[string]interface{})
  443. for cursor.Next(&tmp) {
  444. tmpId := tmp["_id"].(primitive.ObjectID).Hex()
  445. //再重新查找redis,存在发udp处理,不存在走新增合并
  446. rdb := RedisPool.Get()
  447. rdb.Do("SELECT","2")
  448. if _, err := redis.String(rdb.Do("GET", tmp["buyer"])); err == nil {
  449. //{"gtid":"57d7ad2f61a0721f152d2ad5","lteid":"5e20968d85a9271abf0ad6c2","stype":""}
  450. //redis存在发送udp进行处理
  451. by, _ := json.Marshal(map[string]interface{}{
  452. "gtid": tmpId,
  453. "lteid": tmpId,
  454. "stype": "",
  455. })
  456. if e := udpclient.WriteUdp(by, mu.OP_TYPE_DATA, &net.UDPAddr{
  457. IP: net.ParseIP("127.0.0.1"),
  458. Port: Updport,
  459. }); e != nil {
  460. log.Println(e)
  461. }
  462. //存在的话删除tmp mongo表
  463. FClient.DbName = Config["mgodb_extract_kf"]
  464. if DeletedCount := FClient.DeleteById("buyer_new", tmpId); DeletedCount == 0 {
  465. log.Println("删除临时表err:", DeletedCount)
  466. }
  467. if err := rdb.Close(); err != nil {
  468. log.Println(err)
  469. }
  470. continue
  471. } else {
  472. if err = rdb.Close(); err != nil {
  473. log.Println(err)
  474. }
  475. }
  476. //查询redis不存在新增
  477. FClient.DbName = Config["mgodb_enterprise"]
  478. //qyxy 企业库 两亿条
  479. resulttmp := FClient.FindOne(Config["mgodb_enterprise_c"], bson.M{"company_name": tmp["buyer"]})
  480. if resulttmp["_id"] == nil {
  481. //log.Println(r)
  482. //匹配不到原始库,存入异常表删除临时表
  483. FClient.DbName = Config["mgodb_extract_kf"]
  484. if saveid := FClient.Save("buyer_err", tmp); saveid == nil {
  485. log.Println("存入异常表错误", tmp)
  486. }
  487. FClient.DbName = Config["mgodb_extract_kf"]
  488. if deleteNum := FClient.DeleteById("buyer_new", tmpId); deleteNum == 0 {
  489. log.Println("删除临时表错误", deleteNum)
  490. }
  491. continue
  492. } else {
  493. //log.Println(123)
  494. //匹配到原始库,新增 resulttmp
  495. if resulttmp["credit_no"] != nil {
  496. if credit_no, ok := resulttmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  497. len(strings.TrimSpace(credit_no)) > 8 {
  498. dataNo := strings.TrimSpace(credit_no)[2:8]
  499. if Addrs[dataNo] != nil {
  500. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  501. if resulttmp["province"] == nil || resulttmp["province"] == "" {
  502. resulttmp["province"] = v["province"]
  503. }
  504. resulttmp["city"] = v["city"]
  505. resulttmp["district"] = v["district"]
  506. }
  507. }
  508. }
  509. }
  510. contacts := make([]map[string]interface{}, 0)
  511. contact := make(map[string]interface{}, 0)
  512. if resulttmp["legal_person"] != nil {
  513. contact["contact_person"] = resulttmp["legal_person"] //联系人
  514. } else {
  515. contact["contact_person"] = "" //联系人
  516. }
  517. contact["contact_type"] = "法定代表人" //法定代表人
  518. //log.Println(1)
  519. if resulttmp["annual_reports"] != nil {
  520. bytes, err := json.Marshal(resulttmp["annual_reports"])
  521. if err != nil {
  522. log.Println("annual_reports err:", err)
  523. }
  524. phonetmp := make([]map[string]interface{}, 0)
  525. err = json.Unmarshal(bytes, &phonetmp)
  526. if err != nil {
  527. log.Println("Unmarshal err:", err)
  528. }
  529. for _, vv := range phonetmp {
  530. if vv["company_phone"] != nil {
  531. if vv["company_phone"] == "" {
  532. continue
  533. } else {
  534. contact["phone"] = vv["company_phone"] //联系电话
  535. break
  536. }
  537. } else {
  538. contact["phone"] = "" //联系电话
  539. }
  540. }
  541. }
  542. //log.Println(k, contact["phone"], resulttmp["_id"])
  543. //time.Sleep(10 * time.Second)
  544. if contact["phone"] == nil {
  545. contact["phone"] = "" //联系电话
  546. }
  547. contact["topscopeclass"] = "企业公示" //项目类型
  548. contact["updatetime"] = time.Now().Unix() //更新时间
  549. contact["infoid"] = "" //招标信息id
  550. contacts = append(contacts, contact)
  551. resulttmp["contact"] = contacts
  552. savetmp := make(map[string]interface{}, 0)
  553. for _, sk := range BuyerFields {
  554. if sk == "_id" {
  555. savetmp["tmp"+sk] = resulttmp[sk]
  556. continue
  557. } else if sk == "area_code" {
  558. //行政区划代码
  559. savetmp[sk] = fmt.Sprint(resulttmp[sk])
  560. continue
  561. } else if sk == "report_websites" {
  562. //网址
  563. if resulttmp["report_websites"] == nil {
  564. savetmp["website"] = ""
  565. } else {
  566. report_websitesArr := []string{}
  567. if ppms, ok := resulttmp[sk].(primitive.A); ok {
  568. for _, v := range ppms {
  569. if vvv, ok := v.(map[string]interface{}); ok {
  570. if rv, ok := vvv["website_url"].(string); ok {
  571. report_websitesArr = append(report_websitesArr, rv)
  572. }
  573. }
  574. }
  575. }
  576. sort.Strings(report_websitesArr)
  577. savetmp["website"] = strings.Join(report_websitesArr, ";")
  578. }
  579. continue
  580. } else if sk == "wechat_accounts" {
  581. savetmp[sk] = []interface{}{}
  582. continue
  583. }else if sk=="buyer_name" {
  584. if resulttmp["company_name"] == nil {
  585. savetmp[sk] = ""
  586. }else {
  587. savetmp[sk] = resulttmp["company_name"]
  588. }
  589. continue
  590. }else if sk=="address"{
  591. if resulttmp["company_address"] == nil {
  592. savetmp[sk] = ""
  593. }else {
  594. savetmp[sk] = resulttmp["company_address"]
  595. }
  596. continue
  597. }
  598. if resulttmp[sk] == nil && sk != "history_name" && sk != "wechat_accounts" &&
  599. sk != "buyer_name" && sk != "address" &&
  600. sk != "contact" && sk != "report_websites" {
  601. savetmp[sk] = ""
  602. } else {
  603. savetmp[sk] = resulttmp[sk]
  604. }
  605. }
  606. //tmps = append(tmps, savetmp)
  607. savetmp["updatatime"] = time.Now().Unix()
  608. //保存mongo
  609. FClient.DbName = Config["mgodb_extract_kf"]
  610. saveid := FClient.Save(Config["mgo_qyk_buyer"], savetmp)
  611. if saveid != nil {
  612. //保存redis
  613. rc := RedisPool.Get()
  614. rc.Do("SELECT","2")
  615. var _id string
  616. if v, ok := saveid.(primitive.ObjectID); ok {
  617. _id = v.Hex()
  618. }
  619. if _, err := rc.Do("SET", savetmp["buyer_name"], _id); err != nil {
  620. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["buyer_name"], err)
  621. if err := rc.Close(); err != nil {
  622. log.Println(err)
  623. }
  624. } else {
  625. //保存es
  626. delete(savetmp, "_id")
  627. if err := rc.Close(); err != nil {
  628. log.Println(err)
  629. }
  630. //esConn := elastic.GetEsConn()
  631. //defer elastic.DestoryEsConn(esConn)
  632. if _, err := EsConn.Index().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(_id).BodyJson(savetmp).Refresh(true).Do(); err != nil {
  633. log.Println("save es err :", tmp["_id"], savetmp["_id"], err)
  634. } else {
  635. //删除临时表
  636. FClient.DbName = Config["mgodb_extract_kf"]
  637. if deleteNum := FClient.DeleteById("buyer_new", tmpId); deleteNum == 0 {
  638. log.Println("删除临时表失败", deleteNum)
  639. }
  640. }
  641. }
  642. } else {
  643. log.Println("save mongo err:", saveid, tmp["_id"])
  644. }
  645. }
  646. }
  647. }
  648. }
  649. t2.Reset(time.Minute)
  650. }
  651. }