timedTaskBuyer.go 22 KB

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