timedTaskBuyer.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. //释放
  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. conn.Select(redis_buyer_db)
  58. //遍历bidding表保存到redis
  59. //key:企业名 value:json结构体{"buyer": 1, "buyertel": 1, "buyerperson": 1,"topscopeclass": 1, "buyeraddr": 1,"_id":1}
  60. tmp := make(map[string]interface{})
  61. for cursor.Next(&tmp) {
  62. buyer, ok := tmp["buyer"].(string)
  63. if !ok || utf8.RuneCountInString(buyer) < 4 {
  64. continue
  65. }
  66. //判断redis key是否存在
  67. e_num := conn.Exists(buyer).Val()
  68. //获取字符串_id
  69. mgoId := tmp["_id"].(bson.ObjectId).Hex()
  70. //替换_id
  71. tmp["_id"] = mgoId
  72. //创建value数组
  73. tmps := make([]map[string]interface{}, 0)
  74. if e_num > 0 {
  75. //存量redis的key存在,累加更新
  76. bytes, _ := conn.Get(buyer).Bytes()
  77. json.Unmarshal(bytes, &tmps)
  78. }
  79. tmps = append(tmps, tmp)
  80. bytes, _ := json.Marshal(tmps)
  81. //存量redis的key不存在,新增 key :企业名 val :[]map
  82. if err := conn.Set(buyer, string(bytes), 0).Err(); err != nil {
  83. log.Println(err)
  84. }
  85. }
  86. SourceClient.DestoryMongoConn(SourceClientcc)
  87. //遍历redis
  88. if scan := conn.Scan(0, "", 100); scan.Err() != nil {
  89. log.Println(scan.Err())
  90. return
  91. } else {
  92. iterator := scan.Iterator()
  93. for iterator.Next() {
  94. redisCName := iterator.Val() //redis key 企业名
  95. redisvalueBytes, _ := conn.Get(redisCName).Bytes() //redis val []数组
  96. rValuesMaps := make([]map[string]interface{}, 0)
  97. json.Unmarshal(redisvalueBytes, &rValuesMaps)
  98. //redis查询是否存在
  99. rdb := RedisPool.Get()
  100. rdb.Do("SELECT", redis_buyer_db)
  101. if reply, err := redis.String(rdb.Do("GET", redisCName)); err != nil {
  102. //redis不存在,存到临时表,定时任务处理
  103. FClient.DbName = Config["mgodb_extract_kf"]
  104. for _, vmap := range rValuesMaps {
  105. vmap["_id"] = bson.ObjectIdHex(vmap["_id"].(string))
  106. if err = FClient.SaveForOld("buyer_new", vmap); err != nil {
  107. log.Println("存量 FClient.Save err", err, vmap)
  108. }
  109. }
  110. //log.Println("get redis id err:定时任务处理", err, tmp)
  111. if err := rdb.Close(); err != nil {
  112. log.Println("存量", err)
  113. }
  114. continue
  115. } else {
  116. //redis存在更新合并
  117. if err := rdb.Close(); err != nil {
  118. log.Println(err)
  119. }
  120. //拿到合并后的qyk
  121. FClient.DbName = Config["mgodb_extract_kf"]
  122. oldTmp, b := FClient.FindById(Config["mgo_qyk_buyer"], reply, nil)
  123. if !b || (*oldTmp) == nil|| reply==""||(*oldTmp)["_id"]==nil{
  124. log.Println(redisCName, "存量 redis id 不存在", reply,"数据:",oldTmp)
  125. continue
  126. }
  127. tmpTopscopeclass := []string{}
  128. tmpTopscopeclassMap := make(map[string]bool)
  129. for _, rvaluemaps := range rValuesMaps {
  130. if tclasss, ok := rvaluemaps["topscopeclass"].([]string); ok {
  131. for _, vv := range tclasss {
  132. if len(vv) > 1 {
  133. tmpTopscopeclassMap[vv[:len(vv)-1]] = true
  134. }
  135. }
  136. }
  137. }
  138. for k := range tmpTopscopeclassMap {
  139. tmpTopscopeclass = append(tmpTopscopeclass, k)
  140. }
  141. sort.Strings(tmpTopscopeclass)
  142. esId := (*oldTmp)["_id"].(bson.ObjectId).Hex()
  143. //更新buyerclass合并
  144. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  145. //无值,不更新
  146. } else {
  147. var buyerclass_new, buyerclass_old string
  148. buyerclass_new = tmp["buyerclass"].(string)
  149. buyerclass_old = (*oldTmp)["buyerclass"].(string)
  150. if buyerclass_old == "" {
  151. (*oldTmp)["buyerclass"] = buyerclass_new
  152. } else {
  153. if buyerclass_new != buyerclass_old {
  154. if !strings.Contains(buyerclass_old, buyerclass_new) {
  155. (*oldTmp)["buyerclass"] = buyerclass_old + "," + buyerclass_new //采购单位类型
  156. }
  157. }
  158. }
  159. }
  160. //联系方式合并
  161. contactMaps := make([]interface{}, 0)
  162. if (*oldTmp)["contact"] != nil {
  163. //直接添加联系人,不再判断
  164. if v, ok := (*oldTmp)["contact"].([]interface{}); ok {
  165. contactMaps = append(contactMaps, v...)
  166. }
  167. }
  168. //遍历redis value联系人
  169. for _, rvmap := range rValuesMaps {
  170. var tmpperson, buyertel string
  171. if rvmapperson, ok := rvmap["buyerperson"].(string); ok && rvmapperson != "" {
  172. tmpperson = rvmapperson
  173. } else {
  174. continue
  175. }
  176. if rvmapwintel, ok := rvmap["buyertel"].(string); ok {
  177. buyertel = rvmapwintel
  178. } else {
  179. buyertel = ""
  180. }
  181. if Reg_xing.MatchString(buyertel) || !Reg_tel.MatchString(buyertel) {
  182. buyertel = ""
  183. }
  184. tmpContact := make(map[string]interface{})
  185. tmpContact["infoid"] = rvmap["_id"]
  186. tmpContact["contact_person"] = tmpperson
  187. tmpContact["contact_type"] = "项目联系人"
  188. tmpContact["phone"] = buyertel
  189. tmpclass := make([]string, 0)
  190. if tclasss, ok := rvmap["topscopeclass"].([]string); ok {
  191. for _, vv := range tclasss {
  192. if len(vv) > 1 {
  193. tmpclass = append(tmpclass, vv[:len(vv)-1])
  194. }
  195. }
  196. }
  197. tmpContact["topscopeclass"] = strings.Join(tmpclass, ";")
  198. tmpContact["updatetime"] = time.Now().Unix()
  199. contactMaps = append(contactMaps, tmpContact)
  200. }
  201. (*oldTmp)["contact"] = contactMaps
  202. //mongo更新
  203. (*oldTmp)["updatatime"] = time.Now().Unix()
  204. FClient.DbName = Config["mgodb_extract_kf"]
  205. if !FClient.UpdateById(Config["mgo_qyk_buyer"], esId, bson.M{"$set": oldTmp}) {
  206. log.Println("存量 mongo更新 err", esId, oldTmp)
  207. }
  208. //es更新
  209. delete((*oldTmp), "_id")
  210. if _, err := EsConn.Update().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  211. log.Println("存量 EsConn err :", err)
  212. }
  213. }
  214. }
  215. }
  216. log.Println("存量历史合并执行完成 ok", gtid, lteid)
  217. } else {
  218. //增量处理
  219. overid := gtid
  220. tmp := map[string]interface{}{}
  221. for cursor.Next(&tmp) {
  222. overid = tmp["_id"].(bson.ObjectId).Hex()
  223. //log.Println(tmp["_id"])
  224. buyer, ok := tmp["buyer"].(string)
  225. if !ok || utf8.RuneCountInString(buyer) < 4 {
  226. continue
  227. }
  228. //redis查询是否存在
  229. rdb := RedisPool.Get()
  230. rdb.Do("SELECT", redis_buyer_db)
  231. if reply, err := redis.String(rdb.Do("GET", 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|| reply==""||(*oldTmp)["_id"]==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. errbuyer, ok := tmp["buyer"].(string)
  384. if !ok || errbuyer == "" {
  385. continue
  386. }
  387. //再重新查找redis,存在发udp处理,不存在走新增合并
  388. rdb := RedisPool.Get()
  389. rdb.Do("SELECT", redis_buyer_db)
  390. if _, err := redis.String(rdb.Do("GET", errbuyer)); err == nil {
  391. //redis存在发送udp进行处理
  392. by, _ := json.Marshal(map[string]interface{}{
  393. "gtid": tmpId,
  394. "lteid": tmpId,
  395. "stype": "",
  396. "data_type": "buyer",
  397. "data_info": "add",
  398. })
  399. if e := udpclient.WriteUdp(by, mu.OP_TYPE_DATA, &net.UDPAddr{
  400. IP: net.ParseIP("127.0.0.1"),
  401. Port: Updport,
  402. }); e != nil {
  403. log.Println(e)
  404. }
  405. //存在的话删除tmp mongo表
  406. FClient.DbName = Config["mgodb_extract_kf"]
  407. if DeletedCount := FClient.Del("buyer_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !DeletedCount {
  408. log.Println("删除临时表err:", DeletedCount)
  409. }
  410. if err := rdb.Close(); err != nil {
  411. log.Println(err)
  412. }
  413. continue
  414. } else {
  415. if err = rdb.Close(); err != nil {
  416. log.Println(err)
  417. }
  418. }
  419. //查询redis不存在新增
  420. FClient.DbName = Config["mgodb_enterprise"]
  421. resulttmp, b := FClient.FindOne(Config["mgodb_enterprise_c"], bson.M{"company_name": errbuyer})
  422. if !b || (*resulttmp)["_id"] == nil {
  423. //log.Println(r)
  424. //人工审核
  425. var isok bool
  426. for _, v := range BuerRegOk {
  427. isok = v.MatchString(errbuyer)
  428. if isok {
  429. tmp["buyer_ok"] = 1
  430. break
  431. }
  432. }
  433. if tmp["buyer_ok"] == nil {
  434. tmp["buyer_err"] = 1
  435. }
  436. //匹配不到原始库,存入异常表删除临时表
  437. FClient.DbName = Config["mgodb_extract_kf"]
  438. if err := FClient.SaveForOld("buyer_err", tmp); err != nil {
  439. log.Println("存入异常表错误", err, tmp)
  440. }
  441. if deleteNum := FClient.Del("buyer_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !b {
  442. log.Println("删除临时表错误", deleteNum)
  443. }
  444. continue
  445. } else {
  446. //log.Println(123)
  447. //匹配到原始库,新增 resulttmp buyer
  448. if (*resulttmp)["credit_no"] != nil {
  449. if credit_no, ok := (*resulttmp)["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  450. len(strings.TrimSpace(credit_no)) > 8 {
  451. dataNo := strings.TrimSpace(credit_no)[2:8]
  452. if Addrs[dataNo] != nil {
  453. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  454. if (*resulttmp)["province"] == nil || (*resulttmp)["province"] == "" {
  455. (*resulttmp)["province"] = v["province"]
  456. }
  457. (*resulttmp)["city"] = v["city"]
  458. (*resulttmp)["district"] = v["district"]
  459. }
  460. }
  461. }
  462. }
  463. contacts := make([]map[string]interface{}, 0)
  464. contact := make(map[string]interface{}, 0)
  465. if (*resulttmp)["legal_person"] != nil {
  466. contact["contact_person"] = (*resulttmp)["legal_person"] //联系人
  467. } else {
  468. contact["contact_person"] = "" //联系人
  469. }
  470. contact["contact_type"] = "法定代表人" //法定代表人
  471. //log.Println(1)
  472. if (*resulttmp)["annual_reports"] != nil {
  473. bytes, err := json.Marshal((*resulttmp)["annual_reports"])
  474. if err != nil {
  475. log.Println("annual_reports err:", err)
  476. }
  477. phonetmp := make([]map[string]interface{}, 0)
  478. err = json.Unmarshal(bytes, &phonetmp)
  479. if err != nil {
  480. log.Println("Unmarshal err:", err)
  481. }
  482. for _, vv := range phonetmp {
  483. if vv["company_phone"] != nil {
  484. if vv["company_phone"] == "" {
  485. continue
  486. } else {
  487. contact["phone"] = vv["company_phone"] //联系电话
  488. break
  489. }
  490. } else {
  491. contact["phone"] = "" //联系电话
  492. }
  493. }
  494. }
  495. //log.Println(k, contact["phone"], resulttmp["_id"])
  496. //time.Sleep(10 * time.Second)
  497. if contact["phone"] == nil {
  498. contact["phone"] = "" //联系电话
  499. }
  500. contact["topscopeclass"] = "企业公示" //项目类型
  501. contact["updatetime"] = time.Now().Unix() //更新时间
  502. contact["infoid"] = "" //招标信息id
  503. contacts = append(contacts, contact)
  504. //添加临时表匹配到的联系人
  505. vvv := make(map[string]interface{})
  506. vvv["infoid"] = tmp["_id"].(bson.ObjectId).Hex()
  507. if tmp["buyerperson"] != nil {
  508. vvv["contact_person"] = tmp["buyerperson"]
  509. } else {
  510. vvv["contact_person"] = ""
  511. }
  512. vvv["contact_type"] = "项目联系人"
  513. // "buyer": 1, "buyertel": 1, "buyerperson": 1, "topscopeclass": 1 buyerclass : 1
  514. if tmp["buyertel"] != nil {
  515. vvv["phone"] = tmp["buyertel"]
  516. } else {
  517. vvv["phone"] = ""
  518. }
  519. tmpclass := make([]string, 0)
  520. if tclasss, ok := tmp["topscopeclass"].([]string); ok {
  521. for _, vv := range tclasss {
  522. if len(vv) > 1 {
  523. tmpclass = append(tmpclass, vv[:len(vv)-1])
  524. }
  525. }
  526. }
  527. vvv["topscopeclass"] = strings.Join(tmpclass, ";")
  528. vvv["updatetime"] = time.Now().Unix()
  529. contacts = append(contacts, vvv)
  530. (*resulttmp)["contact"] = contacts
  531. savetmp := make(map[string]interface{}, 0)
  532. for _, sk := range BuyerFields {
  533. if sk == "_id" {
  534. savetmp["tmp"+sk] = (*resulttmp)[sk]
  535. continue
  536. } else if sk == "area_code" {
  537. //行政区划代码
  538. savetmp[sk] = fmt.Sprint((*resulttmp)[sk])
  539. continue
  540. } else if sk == "report_websites" {
  541. //网址
  542. if (*resulttmp)["report_websites"] == nil {
  543. savetmp["website"] = ""
  544. } else {
  545. report_websitesArr := []string{}
  546. if ppms, ok := (*resulttmp)[sk].([]interface{}); ok {
  547. for _, v := range ppms {
  548. if vvv, ok := v.(map[string]interface{}); ok {
  549. if rv, ok := vvv["website_url"].(string); ok {
  550. report_websitesArr = append(report_websitesArr, rv)
  551. }
  552. }
  553. }
  554. }
  555. sort.Strings(report_websitesArr)
  556. savetmp["website"] = strings.Join(report_websitesArr, ";")
  557. }
  558. continue
  559. } else if sk == "wechat_accounts" {
  560. savetmp[sk] = []interface{}{}
  561. continue
  562. } else if sk == "buyer_name" {
  563. if (*resulttmp)["company_name"] == nil {
  564. savetmp[sk] = ""
  565. } else {
  566. savetmp[sk] = (*resulttmp)["company_name"]
  567. }
  568. continue
  569. } else if sk == "address" {
  570. if (*resulttmp)["company_address"] == nil {
  571. savetmp[sk] = ""
  572. } else {
  573. savetmp[sk] = (*resulttmp)["company_address"]
  574. }
  575. continue
  576. }else if sk == "buyerclass" {
  577. if tmp["buyerclass"]==nil {
  578. savetmp[sk] = ""
  579. }else {
  580. savetmp[sk] = tmp["buyerclass"]
  581. }
  582. continue
  583. }
  584. if (*resulttmp)[sk] == nil && sk != "history_name" && sk != "wechat_accounts" &&
  585. sk != "buyer_name" && sk != "address" && sk != "buyerclass"&&
  586. sk != "contact" && sk != "report_websites" {
  587. savetmp[sk] = ""
  588. } else {
  589. savetmp[sk] = (*resulttmp)[sk]
  590. }
  591. }
  592. //tmps = append(tmps, savetmp)
  593. savetmp["updatatime"] = time.Now().Unix()
  594. //保存mongo
  595. FClient.DbName = Config["mgodb_extract_kf"]
  596. saveid := FClient.Save(Config["mgo_qyk_buyer"], savetmp)
  597. if saveid != "" {
  598. //保存redis
  599. rc := RedisPool.Get()
  600. rc.Do("SELECT", redis_buyer_db)
  601. //var _id string
  602. //if v, ok := saveid.(primitive.ObjectID); ok {
  603. // _id = v.Hex()
  604. //}
  605. if _, err := rc.Do("SET", savetmp["buyer_name"], saveid); err != nil {
  606. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["buyer_name"], err)
  607. if err := rc.Close(); err != nil {
  608. log.Println(err)
  609. }
  610. } else {
  611. //保存es
  612. delete(savetmp, "_id")
  613. if err := rc.Close(); err != nil {
  614. log.Println(err)
  615. }
  616. //esConn := elastic.GetEsConn()
  617. //defer elastic.DestoryEsConn(esConn)
  618. if _, err := EsConn.Index().Index(Config["elasticsearch_buyer_index"]).Type(Config["elasticsearch_buyer_type"]).Id(saveid).BodyJson(savetmp).Refresh(true).Do(); err != nil {
  619. log.Println("save es err :", tmp["_id"], savetmp["_id"], err)
  620. } else {
  621. //删除临时表
  622. FClient.DbName = Config["mgodb_extract_kf"]
  623. if deleteNum := FClient.Del("buyer_new", bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  624. log.Println("删除临时表失败", deleteNum)
  625. }
  626. }
  627. }
  628. } else {
  629. log.Println("save mongo err:", saveid, tmp["_id"])
  630. }
  631. }
  632. }
  633. FClient.DestoryMongoConn(fconn)
  634. log.Println("buyer_new,遍历完成")
  635. }
  636. }
  637. FClient.DestoryMongoConn(Fcconn)
  638. t2.Reset(time.Minute)
  639. }
  640. }