timedTaskAgency.go 21 KB

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