timedTaskAgency.go 20 KB

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