timedTaskBuyer.go 21 KB

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