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