timedTaskAgency.go 20 KB

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