timedTaskAgency.go 21 KB

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