timedTaskAgency.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. //释放
  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. SourceClient.DestoryMongoConn(SourceClientcc)
  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. conn.Select(redis_agency_db)
  58. //遍历bidding表保存到redis
  59. //key:企业名 value:json结构体{"agency": 1, "agencytel": 1, "agencyperson": 1,"topscopeclass": 1, "agencyaddr": 1,"_id":1}
  60. tmp := make(map[string]interface{})
  61. for cursor.Next(&tmp) {
  62. agency, ok := tmp["agency"].(string)
  63. if !ok || utf8.RuneCountInString(agency) < 4 {
  64. continue
  65. }
  66. //判断redis key是否存在
  67. e_num := conn.Exists(agency).Val()
  68. //获取字符串_id
  69. mgoId := tmp["_id"].(bson.ObjectId).Hex()
  70. //替换_id
  71. tmp["_id"] = mgoId
  72. //创建value数组
  73. tmps := make([]map[string]interface{}, 0)
  74. if e_num > 0 {
  75. //存量redis的key存在,累加更新
  76. bytes, _ := conn.Get(agency).Bytes()
  77. json.Unmarshal(bytes, &tmps)
  78. }
  79. tmps = append(tmps, tmp)
  80. bytes, _ := json.Marshal(tmps)
  81. //存量redis的key不存在,新增 key :企业名 val :[]map
  82. if err := conn.Set(agency, string(bytes), 0).Err(); err != nil {
  83. log.Println(err)
  84. }
  85. }
  86. SourceClient.DestoryMongoConn(SourceClientcc)
  87. //遍历redis
  88. if scan := conn.Scan(0, "", 100); scan.Err() != nil {
  89. log.Println(scan.Err())
  90. return
  91. } else {
  92. iterator := scan.Iterator()
  93. for iterator.Next() {
  94. redisCName := iterator.Val() //redis key 企业名
  95. redisvalueBytes, _ := conn.Get(redisCName).Bytes() //redis val []数组
  96. rValuesMaps := make([]map[string]interface{}, 0)
  97. json.Unmarshal(redisvalueBytes, &rValuesMaps)
  98. //redis查询是否存在
  99. rdb := RedisPool.Get()
  100. rdb.Do("SELECT", redis_agency_db)
  101. if reply, err := redis.String(rdb.Do("GET", redisCName)); err != nil {
  102. //redis不存在,存到临时表,定时任务处理
  103. FClient.DbName = Config["mgodb_extract_kf"]
  104. for _, vmap := range rValuesMaps {
  105. vmap["_id"] = bson.ObjectIdHex(vmap["_id"].(string))
  106. if err = FClient.SaveForOld("agency_new", vmap); err != nil {
  107. log.Println("存量 FClient.Save err", err, vmap)
  108. }
  109. }
  110. //log.Println("get redis id err:定时任务处理", err, tmp)
  111. if err := rdb.Close(); err != nil {
  112. log.Println("存量", err)
  113. }
  114. continue
  115. } else {
  116. //redis存在更新合并
  117. if err := rdb.Close(); err != nil {
  118. log.Println(err)
  119. }
  120. //拿到合并后的qyk
  121. FClient.DbName = Config["mgodb_extract_kf"]
  122. oldTmp, b := FClient.FindById(Config["mgo_qyk_agency"], reply, nil)
  123. if !b || (*oldTmp) == nil|| reply==""||(*oldTmp)["_id"]==nil{
  124. log.Println(redisCName, "存量 redis id 不存在", reply)
  125. continue
  126. }
  127. tmpTopscopeclass := []string{}
  128. tmpTopscopeclassMap := make(map[string]bool)
  129. for _, rvaluemaps := range rValuesMaps {
  130. if tclasss, ok := rvaluemaps["topscopeclass"].([]string); ok {
  131. for _, vv := range tclasss {
  132. if len(vv) > 1 {
  133. tmpTopscopeclassMap[vv[:len(vv)-1]] = true
  134. }
  135. }
  136. }
  137. }
  138. for k := range tmpTopscopeclassMap {
  139. tmpTopscopeclass = append(tmpTopscopeclass, k)
  140. }
  141. sort.Strings(tmpTopscopeclass)
  142. esId := (*oldTmp)["_id"].(bson.ObjectId).Hex()
  143. //联系方式合并
  144. contactMaps := make([]interface{}, 0)
  145. if (*oldTmp)["contact"] != nil {
  146. //直接添加联系人,不再判断
  147. if v, ok := (*oldTmp)["contact"].([]interface{}); ok {
  148. contactMaps = append(contactMaps, v...)
  149. }
  150. }
  151. //遍历redis value联系人
  152. for _, rvmap := range rValuesMaps {
  153. var tmpperson, agencytel string
  154. if rvmapperson, ok := rvmap["agencyperson"].(string); ok && rvmapperson != "" {
  155. tmpperson = rvmapperson
  156. } else {
  157. continue
  158. }
  159. if rvmapwintel, ok := rvmap["agencytel"].(string); ok {
  160. agencytel = rvmapwintel
  161. } else {
  162. agencytel = ""
  163. }
  164. if Reg_xing.MatchString(agencytel) || !Reg_tel.MatchString(agencytel) {
  165. agencytel = ""
  166. }
  167. tmpContact := make(map[string]interface{})
  168. tmpContact["infoid"] = rvmap["_id"]
  169. tmpContact["contact_person"] = tmpperson
  170. tmpContact["contact_type"] = "项目联系人"
  171. tmpContact["phone"] = agencytel
  172. tmpclass := make([]string, 0)
  173. if tclasss, ok := rvmap["topscopeclass"].([]string); ok {
  174. for _, vv := range tclasss {
  175. if len(vv) > 1 {
  176. tmpclass = append(tmpclass, vv[:len(vv)-1])
  177. }
  178. }
  179. }
  180. tmpContact["topscopeclass"] = strings.Join(tmpclass, ";")
  181. tmpContact["updatetime"] = time.Now().Unix()
  182. contactMaps = append(contactMaps, tmpContact)
  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_agency"], 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. }
  197. }
  198. }
  199. log.Println("存量历史合并执行完成 ok", gtid, lteid)
  200. } else {
  201. //增量处理
  202. overid := gtid
  203. tmp := map[string]interface{}{}
  204. for cursor.Next(&tmp) {
  205. overid = tmp["_id"].(bson.ObjectId).Hex()
  206. //log.Println(tmp["_id"])
  207. agency, ok := tmp["agency"].(string)
  208. if !ok || utf8.RuneCountInString(agency) < 4 {
  209. continue
  210. }
  211. //redis查询是否存在
  212. rdb := RedisPool.Get()
  213. rdb.Do("SELECT", redis_agency_db)
  214. if reply, err := redis.String(rdb.Do("GET", 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|| reply==""||(*oldTmp)["_id"]==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. SourceClient.DestoryMongoConn(SourceClientcc)
  312. log.Println("增量合并执行完成 ok", gtid, lteid, overid)
  313. }
  314. }
  315. //定时任务 新增
  316. //1.存异常表
  317. //2.合并原始库新增
  318. func TimedTaskAgency() {
  319. //time.Sleep(time.Hour*70)
  320. t2 := time.NewTimer(time.Second * 5)
  321. for range t2.C {
  322. Fcconn := FClient.GetMgoConn(86400)
  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.Minute * 5)
  329. FClient.DestoryMongoConn(Fcconn)
  330. continue
  331. } else {
  332. log.Println("临时表有数据:", tmpLast)
  333. fconn := FClient.GetMgoConn(86400)
  334. cursor := fconn.DB(Config["mgodb_extract_kf"]).C("agency_new").Find(bson.M{
  335. "_id": bson.M{
  336. "$lte": tmpLast["_id"],
  337. },
  338. }).Sort("_id").Iter()
  339. if cursor == nil {
  340. log.Println("查询失败")
  341. t2.Reset(time.Second * 5)
  342. FClient.DestoryMongoConn(fconn)
  343. continue
  344. }
  345. //遍历临时表数据,匹配不到原始库存入异常表
  346. tmp := make(map[string]interface{})
  347. for cursor.Next(&tmp) {
  348. tmpId := tmp["_id"].(bson.ObjectId).Hex()
  349. erragency, ok := tmp["agency"].(string)
  350. if !ok || erragency == "" {
  351. continue
  352. }
  353. //再重新查找redis,存在发udp处理,不存在走新增合并
  354. rdb := RedisPool.Get()
  355. rdb.Do("SELECT", redis_agency_db)
  356. if _, err := redis.String(rdb.Do("GET", erragency)); err == nil {
  357. //redis存在发送udp进行处理
  358. by, _ := json.Marshal(map[string]interface{}{
  359. "gtid": tmpId,
  360. "lteid": tmpId,
  361. "stype": "",
  362. "data_type": "agency",
  363. "data_info": "add",
  364. })
  365. if e := udpclient.WriteUdp(by, mu.OP_TYPE_DATA, &net.UDPAddr{
  366. IP: net.ParseIP("127.0.0.1"),
  367. Port: Updport,
  368. }); e != nil {
  369. log.Println(e)
  370. }
  371. //存在的话删除tmp mongo表
  372. FClient.DbName = Config["mgodb_extract_kf"]
  373. if DeletedCount := FClient.Del("agency_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !DeletedCount {
  374. log.Println("删除临时表err:", DeletedCount)
  375. }
  376. if err := rdb.Close(); err != nil {
  377. log.Println(err)
  378. }
  379. continue
  380. } else {
  381. if err = rdb.Close(); err != nil {
  382. log.Println(err)
  383. }
  384. }
  385. //查询redis不存在新增
  386. FClient.DbName = Config["mgodb_enterprise"]
  387. resulttmp, b := FClient.FindOne(Config["mgodb_enterprise_c"], bson.M{"company_name": erragency})
  388. if !b || (*resulttmp)["_id"] == nil {
  389. //log.Println(r)
  390. //人工审核正则
  391. var isok bool
  392. for _, v := range AgencyRegOk {
  393. isok = v.MatchString(erragency)
  394. if isok {
  395. tmp["agency_ok"] = 1
  396. break
  397. }
  398. }
  399. if tmp["agency_ok"] == nil{
  400. tmp["agency_err"] =1
  401. }
  402. //匹配不到原始库,存入异常表删除临时表
  403. FClient.DbName = Config["mgodb_extract_kf"]
  404. //winner_err winner_ok buyer_err buyer_err agency_err agency_ok
  405. if err := FClient.SaveForOld("agency_err", tmp); err != nil {
  406. log.Println("存入异常表错误", err, tmp)
  407. }
  408. if deleteNum := FClient.Del("agency_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !b {
  409. log.Println("删除临时表错误", deleteNum)
  410. }
  411. continue
  412. } else {
  413. //log.Println(123)
  414. //匹配到原始库,新增 resulttmp
  415. if (*resulttmp)["credit_no"] != nil {
  416. if credit_no, ok := (*resulttmp)["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  417. len(strings.TrimSpace(credit_no)) > 8 {
  418. dataNo := strings.TrimSpace(credit_no)[2:8]
  419. if Addrs[dataNo] != nil {
  420. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  421. if (*resulttmp)["province"] == nil || (*resulttmp)["province"] == "" {
  422. (*resulttmp)["province"] = v["province"]
  423. }
  424. (*resulttmp)["city"] = v["city"]
  425. (*resulttmp)["district"] = v["district"]
  426. }
  427. }
  428. }
  429. }
  430. contacts := make([]map[string]interface{}, 0)
  431. contact := make(map[string]interface{}, 0)
  432. if (*resulttmp)["legal_person"] != nil {
  433. contact["contact_person"] = (*resulttmp)["legal_person"] //联系人
  434. } else {
  435. contact["contact_person"] = "" //联系人
  436. }
  437. contact["contact_type"] = "法定代表人" //法定代表人
  438. //log.Println(1)
  439. if (*resulttmp)["annual_reports"] != nil {
  440. bytes, err := json.Marshal((*resulttmp)["annual_reports"])
  441. if err != nil {
  442. log.Println("annual_reports err:", err)
  443. }
  444. phonetmp := make([]map[string]interface{}, 0)
  445. err = json.Unmarshal(bytes, &phonetmp)
  446. if err != nil {
  447. log.Println("Unmarshal err:", err)
  448. }
  449. for _, vv := range phonetmp {
  450. if vv["company_phone"] != nil {
  451. if vv["company_phone"] == "" {
  452. continue
  453. } else {
  454. contact["phone"] = vv["company_phone"] //联系电话
  455. break
  456. }
  457. } else {
  458. contact["phone"] = "" //联系电话
  459. }
  460. }
  461. }
  462. //log.Println(k, contact["phone"], resulttmp["_id"])
  463. //time.Sleep(10 * time.Second)
  464. if contact["phone"] == nil {
  465. contact["phone"] = "" //联系电话
  466. }
  467. contact["topscopeclass"] = "企业公示" //项目类型
  468. contact["updatetime"] = time.Now().Unix() //更新时间
  469. contact["infoid"] = "" //招标信息id
  470. contacts = append(contacts, contact)
  471. //添加临时表匹配到的联系人
  472. vvv := make(map[string]interface{})
  473. vvv["infoid"] = tmp["_id"].(bson.ObjectId).Hex()
  474. if tmp["agencyperson"] != nil {
  475. vvv["contact_person"] = tmp["agencyperson"]
  476. } else {
  477. vvv["contact_person"] = ""
  478. }
  479. vvv["contact_type"] = "项目联系人"
  480. // "agency": 1, "agencytel": 1, "agencyperson": 1, "topscopeclass": 1
  481. if tmp["agencytel"] != nil {
  482. vvv["phone"] = tmp["agencytel"]
  483. } else {
  484. vvv["phone"] = ""
  485. }
  486. tmpclass := make([]string, 0)
  487. if tclasss, ok := tmp["topscopeclass"].([]string); ok {
  488. for _, vv := range tclasss {
  489. if len(vv) > 1 {
  490. tmpclass = append(tmpclass, vv[:len(vv)-1])
  491. }
  492. }
  493. }
  494. vvv["topscopeclass"] = strings.Join(tmpclass, ";")
  495. vvv["updatetime"] = time.Now().Unix()
  496. contacts = append(contacts, vvv)
  497. (*resulttmp)["contact"] = contacts
  498. savetmp := make(map[string]interface{}, 0)
  499. for _, sk := range AgencyFields {
  500. if sk == "_id" {
  501. savetmp["tmp"+sk] = (*resulttmp)[sk]
  502. continue
  503. } else if sk == "area_code" {
  504. //行政区划代码
  505. savetmp[sk] = fmt.Sprint((*resulttmp)[sk])
  506. continue
  507. } else if sk == "report_websites" {
  508. //网址
  509. if (*resulttmp)["report_websites"] == nil {
  510. savetmp["website"] = ""
  511. } else {
  512. report_websitesArr := []string{}
  513. if ppms, ok := (*resulttmp)[sk].([]interface{}); ok {
  514. for _, v := range ppms {
  515. if vvv, ok := v.(map[string]interface{}); ok {
  516. if rv, ok := vvv["website_url"].(string); ok {
  517. report_websitesArr = append(report_websitesArr, rv)
  518. }
  519. }
  520. }
  521. }
  522. sort.Strings(report_websitesArr)
  523. savetmp["website"] = strings.Join(report_websitesArr, ";")
  524. }
  525. continue
  526. } else if sk == "wechat_accounts" {
  527. savetmp[sk] = []interface{}{}
  528. continue
  529. } else if sk == "agency_name" {
  530. if (*resulttmp)["company_name"] == nil {
  531. savetmp[sk] = ""
  532. } else {
  533. savetmp[sk] = (*resulttmp)["company_name"]
  534. }
  535. continue
  536. } else if sk == "address" {
  537. if (*resulttmp)["company_address"] == nil {
  538. savetmp[sk] = ""
  539. } else {
  540. savetmp[sk] = (*resulttmp)["company_address"]
  541. }
  542. continue
  543. }
  544. if (*resulttmp)[sk] == nil && sk != "history_name" && sk != "wechat_accounts" &&
  545. sk != "agency_name" && sk != "address" &&
  546. sk != "contact" && sk != "report_websites" {
  547. savetmp[sk] = ""
  548. } else {
  549. savetmp[sk] = (*resulttmp)[sk]
  550. }
  551. }
  552. //tmps = append(tmps, savetmp)
  553. savetmp["updatatime"] = time.Now().Unix()
  554. //保存mongo
  555. FClient.DbName = Config["mgodb_extract_kf"]
  556. saveid := FClient.Save(Config["mgo_qyk_agency"], savetmp)
  557. if saveid != "" {
  558. //保存redis
  559. rc := RedisPool.Get()
  560. rc.Do("SELECT", redis_agency_db)
  561. //var _id string
  562. //if v, ok := saveid.(primitive.ObjectID); ok {
  563. // _id = v.Hex()
  564. //}
  565. if _, err := rc.Do("SET", savetmp["agency_name"], saveid); err != nil {
  566. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["agency_name"], err)
  567. if err := rc.Close(); err != nil {
  568. log.Println(err)
  569. }
  570. } else {
  571. //保存es
  572. delete(savetmp, "_id")
  573. if err := rc.Close(); err != nil {
  574. log.Println(err)
  575. }
  576. //esConn := elastic.GetEsConn()
  577. //defer elastic.DestoryEsConn(esConn)
  578. if _, err := EsConn.Index().Index(Config["elasticsearch_agency_index"]).Type(Config["elasticsearch_agency_type"]).Id(saveid).BodyJson(savetmp).Refresh(true).Do(); err != nil {
  579. log.Println("save es err :", tmp["_id"], savetmp["_id"], err)
  580. } else {
  581. //删除临时表
  582. FClient.DbName = Config["mgodb_extract_kf"]
  583. if deleteNum := FClient.Del("agency_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  584. log.Println("删除临时表失败", deleteNum)
  585. }
  586. }
  587. }
  588. } else {
  589. log.Println("save mongo err:", saveid, tmp["_id"])
  590. }
  591. }
  592. }
  593. FClient.DestoryMongoConn(fconn)
  594. log.Println("buyer_new,遍历完成")
  595. }
  596. }
  597. FClient.DestoryMongoConn(Fcconn)
  598. t2.Reset(time.Minute)
  599. }
  600. }