timedTaskWinner.go 21 KB

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