standarwinner.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // standarwinner
  2. package main
  3. import (
  4. "dbutil/mongo"
  5. "dbutil/redis"
  6. "encoding/json"
  7. "log"
  8. qu "qfw/util"
  9. "strings"
  10. "time"
  11. "unicode/utf8"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. "gopkg.in/mgo.v2/bson"
  14. )
  15. //增量处理
  16. func winnerStandarData(db string, query map[string]interface{}) {
  17. defer qu.Catch()
  18. sess := MongoFrom.GetMgoConn()
  19. defer MongoFrom.Close()
  20. it := sess.DB(db).C(extractcoll).Find(query).Select(bson.M{"repeat": 1, "winner": 1, "winnertel": 1, "winnerperson": 1, "topscopeclass": 1, "package": 1}).Sort("_id").Iter()
  21. index := 0
  22. for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
  23. if qu.IntAll(tmp["repeat"]) > 0 { //重复数据跳过
  24. continue
  25. }
  26. winner := qu.ObjToString(tmp["winner"])
  27. if utf8.RuneCountInString(winner) < 5 {
  28. continue
  29. }
  30. infoid := mongo.BsonTOStringId(tmp["_id"])
  31. topscopeclass, _ := tmp["topscopeclass"].(primitive.A)
  32. entid, _ := redis.GetRedisStr("winner", winnerbd, winner)
  33. winnerperson := qu.ObjToString(tmp["winnerperson"])
  34. winnertel := qu.ObjToString(tmp["winnertel"])
  35. if entid == "" { //新增标准库
  36. savetoerr := true
  37. if winnerperson != "" || winnertel != "" {
  38. v := map[string]interface{}{
  39. "contact_person": winnerperson,
  40. "phone": winnertel,
  41. "topscopeclass": comRepTopscopeclass(topscopeclass),
  42. "infoid": infoid,
  43. }
  44. data := comHisMegerNewData(winner, "winner", []map[string]interface{}{v})
  45. if data != nil {
  46. _id := MongoTo.Save(winnerent, data)
  47. redis.PutRedis("winner", winnerbd, winner, _id.(primitive.ObjectID).Hex(), -1)
  48. savetoerr = false
  49. }
  50. }
  51. if savetoerr {
  52. t := MongoTo.FindOne(winnererr, map[string]interface{}{"name": winner})
  53. if len(t) < 1 {
  54. MongoTo.Save(winnererr, map[string]interface{}{
  55. "name": winner,
  56. "topscopeclass": comRepTopscopeclass(topscopeclass),
  57. "check": comMarkdata(winner, "winner"),
  58. "updatetime": time.Now().Unix(),
  59. })
  60. }
  61. }
  62. } else { //更新标准库
  63. if winnerperson != "" || winnertel != "" {
  64. v := map[string]interface{}{
  65. "contact_person": winnerperson,
  66. "phone": winnertel,
  67. "topscopeclass": comRepTopscopeclass(topscopeclass),
  68. "infoid": infoid,
  69. }
  70. data := winMegerIndustry(entid, v)
  71. MongoTo.UpdateById(winnerent, entid,
  72. map[string]interface{}{
  73. "$set": data,
  74. "$push": map[string]interface{}{"contact": v},
  75. },
  76. )
  77. }
  78. }
  79. //分包处理
  80. if packages, ok := tmp["package"].(map[string]interface{}); ok {
  81. entpacks := getWinnerPacks(infoid, packages, comRepTopscopeclass(topscopeclass))
  82. for name, contact := range entpacks {
  83. entid, _ := redis.GetRedisStr("winner", winnerbd, name)
  84. if entid == "" {
  85. data := comHisMegerNewData(winner, "winner", []map[string]interface{}{contact})
  86. if data != nil {
  87. _id := MongoTo.Save(winnerent, data)
  88. redis.PutRedis("winner", winnerbd, winner, _id.(primitive.ObjectID).Hex(), -1)
  89. }
  90. } else {
  91. data := winMegerIndustry(entid, contact)
  92. MongoTo.UpdateById(winnerent, entid,
  93. map[string]interface{}{
  94. "$set": data,
  95. "$push": map[string]interface{}{"contact": contact},
  96. },
  97. )
  98. }
  99. }
  100. }
  101. tmp = map[string]interface{}{}
  102. if index%100 == 0 {
  103. log.Println("winner index", index)
  104. }
  105. }
  106. log.Println("winner ok index", index)
  107. }
  108. //历史数据处理
  109. func historywinner(db, fromcoll string) {
  110. defer qu.Catch()
  111. log.Println("history start")
  112. sess := MongoFrom.GetMgoConn()
  113. defer MongoFrom.Close()
  114. it := sess.DB(db).C(fromcoll).Find(map[string]interface{}{}).Select(bson.M{"repeat": 1, "winner": 1, "winnertel": 1, "winnerperson": 1, "topscopeclass": 1}).Sort("_id").Iter()
  115. index := 0
  116. for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
  117. if qu.IntAll(tmp["repeat"]) > 0 { //重复数据跳过
  118. continue
  119. }
  120. _id := mongo.BsonTOStringId(tmp["_id"])
  121. winchanbool <- true
  122. go func(tmp map[string]interface{}) {
  123. defer func() {
  124. <-winchanbool
  125. }()
  126. winner := qu.ObjToString(tmp["winner"])
  127. topscopeclass, _ := tmp["topscopeclass"].(primitive.A)
  128. if winner != "" && utf8.RuneCountInString(winner) > 4 {
  129. winnerperson := qu.ObjToString(tmp["winnerperson"])
  130. winnertel := qu.ObjToString(tmp["winnertel"])
  131. b, _ := redis.ExistRedis("winner", winnerbd, winner)
  132. if b {
  133. if winnerperson != "" || winnertel != "" {
  134. strs, _ := redis.GetRedisStr("winner", winnerbd, winner)
  135. ps := []interface{}{}
  136. err := json.Unmarshal([]byte(strs), &ps)
  137. if err == nil {
  138. v := map[string]interface{}{
  139. "contact_person": winnerperson,
  140. "phone": winnertel,
  141. "topscopeclass": comRepTopscopeclass(topscopeclass),
  142. "infoid": _id,
  143. }
  144. ps = append(ps, v)
  145. bs, _ := json.Marshal(ps)
  146. redis.PutRedis("winner", winnerbd, winner, bs, -1)
  147. //log.Println(_id, index, winner)
  148. } else {
  149. log.Println("jsonErr", err)
  150. }
  151. }
  152. } else {
  153. val := []map[string]interface{}{}
  154. if winnerperson != "" || winnertel != "" {
  155. tmp := map[string]interface{}{
  156. "contact_person": winnerperson,
  157. "phone": winnertel,
  158. "topscopeclass": comRepTopscopeclass(topscopeclass),
  159. "infoid": _id,
  160. }
  161. val = append(val, tmp)
  162. }
  163. bs, _ := json.Marshal(val)
  164. redis.PutRedis("winner", winnerbd, winner, bs, -1)
  165. MongoTo.Save(winnererr, map[string]interface{}{
  166. "name": winner,
  167. "topscopeclass": comRepTopscopeclass(topscopeclass),
  168. "updatetime": time.Now().Unix(),
  169. })
  170. }
  171. }
  172. }(tmp)
  173. tmp = map[string]interface{}{}
  174. if index%10000 == 0 {
  175. log.Println("index", index, _id)
  176. }
  177. }
  178. log.Println("history ok index", index)
  179. winStandarHistory(qu.ObjToString(sysconfig["mgotodb"]))
  180. }
  181. //查询winnererr标准化历史数据
  182. func winStandarHistory(db string) {
  183. defer qu.Catch()
  184. log.Println("开始标准化数据--winner", db)
  185. sessto := MongoTo.GetMgoConn()
  186. defer MongoTo.Close()
  187. it := sessto.DB(db).C(winnererr).Find(map[string]interface{}{}).Iter()
  188. index := 0
  189. entnum := 0
  190. for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
  191. err_id := mongo.BsonTOStringId(tmp["_id"])
  192. name := qu.ObjToString(tmp["name"])
  193. winchanbool <- true
  194. go func(tmp map[string]interface{}) {
  195. defer func() {
  196. <-winchanbool
  197. }()
  198. strs, err := redis.GetRedisStr("winner", winnerbd, name)
  199. if err != nil {
  200. return
  201. }
  202. ps := []map[string]interface{}{}
  203. err = json.Unmarshal([]byte(strs), &ps)
  204. if err == nil {
  205. data := comHisMegerNewData(name, "winner", ps)
  206. if data != nil {
  207. MongoTo.Save(winnerent, data)
  208. MongoTo.DeleteById(winnererr, err_id)
  209. entnum++
  210. } else { //未查询到企业,打标记并存表
  211. num := comMarkdata(name, "winner")
  212. tmp["check"] = num
  213. MongoTo.UpdateById(winnererr, err_id, map[string]interface{}{"$set": map[string]interface{}{"check": num}})
  214. }
  215. } else {
  216. log.Println("jsonErr", name, err)
  217. }
  218. }(tmp)
  219. if index%1000 == 0 {
  220. log.Println("标准化历史数据--winner", index, err_id, entnum)
  221. }
  222. tmp = map[string]interface{}{}
  223. }
  224. log.Println("标准化数据完成--winner", index, entnum)
  225. }
  226. //企业数据整合(已有标注信息)
  227. func winMegerIndustry(id string, ps map[string]interface{}) map[string]interface{} {
  228. tmp := MongoEnt.FindById(winnerent, id, bson.M{"industry": 1})
  229. if len(tmp) < 1 {
  230. return nil
  231. }
  232. data := map[string]interface{}{}
  233. industry := tmp["industry"].(primitive.A)
  234. tmpindustry := map[string]bool{}
  235. for _, v := range industry {
  236. tt := qu.ObjToString(v)
  237. tmpindustry[tt] = true
  238. }
  239. if topscopeclass, ok := ps["topscopeclass"].([]interface{}); ok {
  240. for _, v := range topscopeclass {
  241. tt := qu.ObjToString(v)
  242. tmpindustry[tt] = true
  243. }
  244. }
  245. newindustry := []interface{}{}
  246. for k, _ := range tmpindustry {
  247. newindustry = append(newindustry, k)
  248. }
  249. data["industry"] = newindustry
  250. data["updatetime"] = time.Now().Unix()
  251. return data
  252. }
  253. //中标单位分包提取联系方式
  254. func getWinnerPacks(infoid string, packs map[string]interface{}, topscopeclass []interface{}) map[string]map[string]interface{} {
  255. entmappacks := map[string]map[string]interface{}{}
  256. for _, v := range packs {
  257. if tmp, ok := v.(map[string]interface{}); ok {
  258. winner := qu.ObjToString(tmp["winner"])
  259. if utf8.RuneCountInString(winner) < 5 {
  260. continue
  261. }
  262. winnerperson := qu.ObjToString(tmp["winnerperson"])
  263. winnertel := qu.ObjToString(tmp["winnertel"])
  264. if winnerperson != "" || winnertel != "" {
  265. p := map[string]interface{}{
  266. "contact_person": winnerperson,
  267. "phone": winnertel,
  268. "topscopeclass": topscopeclass,
  269. "infoid": infoid,
  270. "extfrom": "package",
  271. }
  272. entmappacks[winner] = p
  273. }
  274. }
  275. }
  276. return entmappacks
  277. }
  278. //数据整合
  279. func comHisMegerNewData(name, datatype string, ps []map[string]interface{}) map[string]interface{} {
  280. tmp := MongoEnt.FindOne("qyxy", map[string]interface{}{"company_name": name})
  281. if len(tmp) < 1 {
  282. return nil
  283. }
  284. data := map[string]interface{}{
  285. "history_name": "",
  286. "credit_no": "",
  287. "company_email": "",
  288. "area_code": qu.ObjToString(tmp["area_code"]),
  289. "province": qu.ObjToString(tmp["province"]),
  290. "city": "",
  291. "district": "",
  292. "company_type": qu.ObjToString(tmp["company_type"]),
  293. "legal_person": qu.ObjToString(tmp["legal_person"]),
  294. "company_phone": "",
  295. "company_address": qu.ObjToString(tmp["company_address"]),
  296. "business_scope": qu.ObjToString(tmp["business_scope"]),
  297. "wechat_accounts": []interface{}{},
  298. "website": "",
  299. "contact": ps,
  300. "comeintime": time.Now().Unix(),
  301. "updatetime": time.Now().Unix(),
  302. }
  303. //统一信用代码
  304. credit_no := strings.TrimSpace(qu.ObjToString(tmp["credit_no"]))
  305. if credit_no != "" {
  306. data["credit_no"] = credit_no
  307. if len(credit_no) > 8 {
  308. dataNo := credit_no[2:8]
  309. if Addrs[dataNo] != nil {
  310. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  311. if data["province"] == "" {
  312. data["province"] = v["province"]
  313. }
  314. data["city"] = v["city"]
  315. data["district"] = v["district"]
  316. }
  317. }
  318. }
  319. }
  320. //从年报中取网址、邮箱、打电话
  321. annual_reports := tmp["annual_reports"]
  322. if annual_reports != nil {
  323. report_websitesArr := []string{}
  324. if anreports, ok := annual_reports.(primitive.A); ok {
  325. for _, anreportmp := range anreports {
  326. if anreport, ok := anreportmp.(map[string]interface{}); ok {
  327. if websites, ok := anreport["report_websites"].(primitive.A); ok {
  328. for _, website := range websites {
  329. if rv, ok := website.(map[string]interface{}); ok {
  330. web := qu.ObjToString(rv["website_url"])
  331. if web != "" {
  332. report_websitesArr = append(report_websitesArr, web)
  333. }
  334. }
  335. }
  336. }
  337. company_email := qu.ObjToString(anreport["company_email"])
  338. if company_email != "" {
  339. data["company_email"] = company_email
  340. }
  341. company_phone := qu.ObjToString(anreport["company_phone"])
  342. if company_phone != "" {
  343. data["company_phone"] = company_phone
  344. }
  345. }
  346. }
  347. }
  348. if len(report_websitesArr) > 0 {
  349. data["website"] = strings.Join(report_websitesArr, ";")
  350. }
  351. }
  352. if datatype == "winner" {
  353. data["company_name"] = name
  354. data["partners"] = tmp["partners"]
  355. establish_date := tmp["establish_date"]
  356. if establish_date != nil {
  357. data["establish_date"] = qu.Int64All(establish_date) / 1000
  358. }
  359. capital := tmp["capital"]
  360. if capital != nil {
  361. data["capital"] = ObjToMoney([]interface{}{capital, ""})[0]
  362. }
  363. industry := make([]string, 0)
  364. tmpindustry := map[string]bool{}
  365. for _, p := range ps {
  366. if ts, ok := (p["topscopeclass"]).([]interface{}); ok {
  367. for _, v := range ts {
  368. tt := qu.ObjToString(v)
  369. tmpindustry[tt] = true
  370. }
  371. }
  372. }
  373. for k, _ := range tmpindustry {
  374. industry = append(industry, k)
  375. }
  376. data["industry"] = industry
  377. } else if datatype == "buyer" {
  378. data["buyer_name"] = name
  379. tmpbuyerclass := map[string]bool{}
  380. for _, p := range ps {
  381. tmpbuyerclass[qu.ObjToString(p["buyerclass"])] = true
  382. }
  383. buyerclass := []interface{}{}
  384. for k, _ := range tmpbuyerclass {
  385. buyerclass = append(buyerclass, k)
  386. }
  387. data["buyerclass"] = buyerclass
  388. data["ranks"] = ""
  389. data["type"] = ""
  390. data["address"] = ""
  391. } else {
  392. data["agency_name"] = name
  393. data["ranks"] = ""
  394. data["type"] = ""
  395. data["address"] = ""
  396. }
  397. return data
  398. }
  399. //根据规则数据打标记
  400. func comMarkdata(name, datatype string) int {
  401. tag := 0 //默认错误
  402. switch datatype {
  403. case "winner":
  404. for _, v := range WinnerRegOk {
  405. isok := v.MatchString(name)
  406. if isok { //匹配ok完,匹配err
  407. errflag := true
  408. for _, vRegErr := range WinnerRegErr {
  409. err := vRegErr.MatchString(name)
  410. if err {
  411. errflag = true
  412. break
  413. }
  414. }
  415. if errflag {
  416. tag = 1
  417. }
  418. }
  419. }
  420. case "buyer":
  421. for _, v := range BuyerRegOk {
  422. isok := v.MatchString(name)
  423. if isok { //匹配ok完,匹配err
  424. errflag := true
  425. for _, vRegErr := range BuyerRegErr {
  426. err := vRegErr.MatchString(name)
  427. if err {
  428. errflag = true
  429. break
  430. }
  431. }
  432. if errflag {
  433. tag = 1
  434. }
  435. }
  436. }
  437. case "agency":
  438. for _, v := range AgencyRegOk {
  439. isok := v.MatchString(name)
  440. if isok { //匹配ok完,匹配err
  441. errflag := true
  442. for _, vRegErr := range AgencyRegErr {
  443. err := vRegErr.MatchString(name)
  444. if err {
  445. errflag = true
  446. break
  447. }
  448. }
  449. if errflag {
  450. tag = 1
  451. }
  452. }
  453. }
  454. default:
  455. }
  456. return tag
  457. }
  458. //过滤行业冗余字符
  459. func comRepTopscopeclass(tops []interface{}) []interface{} {
  460. data := []interface{}{}
  461. for _, v := range tops {
  462. tt := qu.ObjToString(v)
  463. if len(tt) > 1 {
  464. data = append(data, tt[:len(tt)-1])
  465. }
  466. }
  467. return data
  468. }