timedTaskBuyer.go 21 KB

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