timedTaskAgency.go 20 KB

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