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