timedTaskWinner.go 22 KB

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