timedTaskBuyer.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. SourceClientcc.Close()
  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. SourceClientcc.Close()
  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. log.Println("增量合并执行完成 ok", gtid, lteid, overid)
  346. }
  347. }
  348. //定时任务 新增
  349. //1.存异常表
  350. //2.合并原始库新增
  351. func TimedTaskBuyer() {
  352. //time.Sleep(time.Hour*70)
  353. t2 := time.NewTimer(time.Second * 5)
  354. for range t2.C {
  355. Fcconn := FClient.GetMgoConn(86400)
  356. defer Fcconn.Close()
  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.Second * 15)
  363. continue
  364. } else {
  365. log.Println("临时表有数据:", tmpLast)
  366. fconn := FClient.GetMgoConn(86400)
  367. cursor := fconn.DB(Config["mgodb_extract_kf"]).C("buyer_new").Find(bson.M{
  368. "_id": bson.M{
  369. "$lte": tmpLast["_id"],
  370. },
  371. }).Sort("_id").Iter()
  372. if cursor == nil {
  373. log.Println("查询失败")
  374. t2.Reset(time.Second * 5)
  375. continue
  376. }
  377. //遍历临时表数据,匹配不到原始库存入异常表
  378. tmp := make(map[string]interface{})
  379. for cursor.Next(&tmp) {
  380. tmpId := tmp["_id"].(bson.ObjectId).Hex()
  381. //再重新查找redis,存在发udp处理,不存在走新增合并
  382. rdb := RedisPool.Get()
  383. rdb.Do("SELECT", Config["redis_buyer_db"])
  384. if _, err := redis.String(rdb.Do("GET", tmp["buyer"])); err == nil {
  385. //redis存在发送udp进行处理
  386. by, _ := json.Marshal(map[string]interface{}{
  387. "gtid": tmpId,
  388. "lteid": tmpId,
  389. "stype": "",
  390. "data_type": "buyer",
  391. "data_info": "add",
  392. })
  393. if e := udpclient.WriteUdp(by, mu.OP_TYPE_DATA, &net.UDPAddr{
  394. IP: net.ParseIP("127.0.0.1"),
  395. Port: Updport,
  396. }); e != nil {
  397. log.Println(e)
  398. }
  399. //存在的话删除tmp mongo表
  400. FClient.DbName = Config["mgodb_extract_kf"]
  401. if DeletedCount := FClient.Del("buyer_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !DeletedCount {
  402. log.Println("删除临时表err:", DeletedCount)
  403. }
  404. if err := rdb.Close(); err != nil {
  405. log.Println(err)
  406. }
  407. continue
  408. } else {
  409. if err = rdb.Close(); err != nil {
  410. log.Println(err)
  411. }
  412. }
  413. //查询redis不存在新增
  414. FClient.DbName = Config["mgodb_enterprise"]
  415. resulttmp, b := FClient.FindOne(Config["mgodb_enterprise_c"], bson.M{"company_name": tmp["buyer"]})
  416. if !b || (*resulttmp)["_id"] == nil {
  417. //log.Println(r)
  418. //匹配不到原始库,存入异常表删除临时表
  419. FClient.DbName = Config["mgodb_extract_kf"]
  420. if err := FClient.SaveForOld("buyer_err", tmp); err != nil {
  421. log.Println("存入异常表错误", err, tmp)
  422. }
  423. if deleteNum := FClient.Del("buyer_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !b {
  424. log.Println("删除临时表错误", deleteNum)
  425. }
  426. continue
  427. } else {
  428. //log.Println(123)
  429. //匹配到原始库,新增 resulttmp
  430. if (*resulttmp)["credit_no"] != nil {
  431. if credit_no, ok := (*resulttmp)["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  432. len(strings.TrimSpace(credit_no)) > 8 {
  433. dataNo := strings.TrimSpace(credit_no)[2:8]
  434. if Addrs[dataNo] != nil {
  435. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  436. if (*resulttmp)["province"] == nil || (*resulttmp)["province"] == "" {
  437. (*resulttmp)["province"] = v["province"]
  438. }
  439. (*resulttmp)["city"] = v["city"]
  440. (*resulttmp)["district"] = v["district"]
  441. }
  442. }
  443. }
  444. }
  445. contacts := make([]map[string]interface{}, 0)
  446. contact := make(map[string]interface{}, 0)
  447. if (*resulttmp)["legal_person"] != nil {
  448. contact["contact_person"] = (*resulttmp)["legal_person"] //联系人
  449. } else {
  450. contact["contact_person"] = "" //联系人
  451. }
  452. contact["contact_type"] = "法定代表人" //法定代表人
  453. //log.Println(1)
  454. if (*resulttmp)["annual_reports"] != nil {
  455. bytes, err := json.Marshal((*resulttmp)["annual_reports"])
  456. if err != nil {
  457. log.Println("annual_reports err:", err)
  458. }
  459. phonetmp := make([]map[string]interface{}, 0)
  460. err = json.Unmarshal(bytes, &phonetmp)
  461. if err != nil {
  462. log.Println("Unmarshal err:", err)
  463. }
  464. for _, vv := range phonetmp {
  465. if vv["company_phone"] != nil {
  466. if vv["company_phone"] == "" {
  467. continue
  468. } else {
  469. contact["phone"] = vv["company_phone"] //联系电话
  470. break
  471. }
  472. } else {
  473. contact["phone"] = "" //联系电话
  474. }
  475. }
  476. }
  477. //log.Println(k, contact["phone"], resulttmp["_id"])
  478. //time.Sleep(10 * time.Second)
  479. if contact["phone"] == nil {
  480. contact["phone"] = "" //联系电话
  481. }
  482. contact["topscopeclass"] = "企业公示" //项目类型
  483. contact["updatetime"] = time.Now().Unix() //更新时间
  484. contact["infoid"] = "" //招标信息id
  485. contacts = append(contacts, contact)
  486. //添加临时表匹配到的联系人
  487. vvv := make(map[string]interface{})
  488. vvv["infoid"] = tmp["_id"].(bson.ObjectId).Hex()
  489. if tmp["buyerperson"] != nil {
  490. vvv["contact_person"] = tmp["buyerperson"]
  491. } else {
  492. vvv["contact_person"] = ""
  493. }
  494. vvv["contact_type"] = "项目联系人"
  495. // "buyer": 1, "buyertel": 1, "buyerperson": 1, "topscopeclass": 1 buyerclass : 1
  496. if tmp["buyertel"] != nil {
  497. vvv["phone"] = tmp["buyertel"]
  498. } else {
  499. vvv["phone"] = ""
  500. }
  501. tmpclass := make([]string, 0)
  502. if tclasss, ok := tmp["topscopeclass"].([]string); ok {
  503. for _, vv := range tclasss {
  504. if len(vv) > 1 {
  505. tmpclass = append(tmpclass, vv[:len(vv)-1])
  506. }
  507. }
  508. }
  509. vvv["topscopeclass"] = strings.Join(tmpclass, ";")
  510. vvv["updatetime"] = time.Now().Unix()
  511. contacts = append(contacts, vvv)
  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].([]interface{}); 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)["company_name"]
  549. }
  550. continue
  551. } else if sk == "address" {
  552. if (*resulttmp)["company_address"] == nil {
  553. savetmp[sk] = ""
  554. } else {
  555. savetmp[sk] = (*resulttmp)["company_address"]
  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_buyer"], savetmp)
  572. if saveid != "" {
  573. //保存redis
  574. rc := RedisPool.Get()
  575. rc.Do("SELECT", Config["redis_buyer_db"])
  576. //var _id string
  577. //if v, ok := saveid.(primitive.ObjectID); ok {
  578. // _id = v.Hex()
  579. //}
  580. if _, err := rc.Do("SET", savetmp["buyer_name"], saveid); err != nil {
  581. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["buyer_name"], err)
  582. if err := rc.Close(); err != nil {
  583. log.Println(err)
  584. }
  585. } else {
  586. //保存es
  587. delete(savetmp, "_id")
  588. if err := rc.Close(); err != nil {
  589. log.Println(err)
  590. }
  591. //esConn := elastic.GetEsConn()
  592. //defer elastic.DestoryEsConn(esConn)
  593. if _, err := EsConn.Index().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(saveid).BodyJson(savetmp).Refresh(true).Do(); err != nil {
  594. log.Println("save es err :", tmp["_id"], savetmp["_id"], err)
  595. } else {
  596. //删除临时表
  597. FClient.DbName = Config["mgodb_extract_kf"]
  598. if deleteNum := FClient.Del("buyer_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  599. log.Println("删除临时表失败", deleteNum)
  600. }
  601. }
  602. }
  603. } else {
  604. log.Println("save mongo err:", saveid, tmp["_id"])
  605. }
  606. }
  607. }
  608. fconn.Close()
  609. }
  610. }
  611. t2.Reset(time.Minute)
  612. }
  613. }