standarwinner.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. return data
  251. }
  252. //中标单位分包提取联系方式
  253. func getWinnerPacks(infoid string, packs map[string]interface{}, topscopeclass []interface{}) map[string]map[string]interface{} {
  254. entmappacks := map[string]map[string]interface{}{}
  255. for _, v := range packs {
  256. if tmp, ok := v.(map[string]interface{}); ok {
  257. winner := qu.ObjToString(tmp["winner"])
  258. if utf8.RuneCountInString(winner) < 5 {
  259. continue
  260. }
  261. winnerperson := qu.ObjToString(tmp["winnerperson"])
  262. winnertel := qu.ObjToString(tmp["winnertel"])
  263. if winnerperson != "" || winnertel != "" {
  264. p := map[string]interface{}{
  265. "contact_person": winnerperson,
  266. "phone": winnertel,
  267. "topscopeclass": topscopeclass,
  268. "infoid": infoid,
  269. "extfrom": "package",
  270. }
  271. entmappacks[winner] = p
  272. }
  273. }
  274. }
  275. return entmappacks
  276. }
  277. //数据整合
  278. func comHisMegerNewData(name, datatype string, ps []map[string]interface{}) map[string]interface{} {
  279. tmp := MongoEnt.FindOne("qyxy", map[string]interface{}{"company_name": name})
  280. if len(tmp) < 1 {
  281. return nil
  282. }
  283. data := map[string]interface{}{
  284. "history_name": "",
  285. "credit_no": "",
  286. "area_code": qu.ObjToString(tmp["area_code"]),
  287. "province": qu.ObjToString(tmp["province"]),
  288. "city": "",
  289. "district": "",
  290. "company_type": qu.ObjToString(tmp["company_type"]),
  291. "legal_person": qu.ObjToString(tmp["legal_person"]),
  292. "company_address": qu.ObjToString(tmp["company_address"]),
  293. "business_scope": qu.ObjToString(tmp["business_scope"]),
  294. "wechat_accounts": []interface{}{},
  295. "website": "",
  296. "contact": ps,
  297. "comeintime": time.Now().Unix(),
  298. "updatetime": time.Now().Unix(),
  299. }
  300. //统一信用代码
  301. credit_no := strings.TrimSpace(qu.ObjToString(tmp["credit_no"]))
  302. if credit_no != "" {
  303. data["credit_no"] = credit_no
  304. if len(credit_no) > 8 {
  305. dataNo := credit_no[2:8]
  306. if Addrs[dataNo] != nil {
  307. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  308. if data["province"] == "" {
  309. data["province"] = v["province"]
  310. }
  311. data["city"] = v["city"]
  312. data["district"] = v["district"]
  313. }
  314. }
  315. }
  316. }
  317. //网址
  318. annual_reports := tmp["annual_reports"]
  319. if annual_reports != nil {
  320. report_websitesArr := []string{}
  321. if anreports, ok := annual_reports.([]interface{}); ok {
  322. for _, report_websites := range anreports {
  323. if websites, ok := report_websites.([]interface{}); ok {
  324. for _, website := range websites {
  325. if rv, ok := website.(map[string]interface{}); ok {
  326. web := qu.ObjToString(rv["website_url"])
  327. if web != "" {
  328. report_websitesArr = append(report_websitesArr, web)
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. if len(report_websitesArr) > 0 {
  336. data["website"] = strings.Join(report_websitesArr, ";")
  337. }
  338. }
  339. if datatype == "winner" {
  340. data["company_name"] = name
  341. data["partners"] = tmp["partners"]
  342. establish_date := tmp["establish_date"]
  343. if establish_date != nil {
  344. data["establish_date"] = qu.Int64All(establish_date) / 1000
  345. }
  346. capital := tmp["capital"]
  347. if capital != nil {
  348. data["capital"] = ObjToMoney([]interface{}{capital, ""})[0]
  349. }
  350. industry := make([]string, 0)
  351. tmpindustry := map[string]bool{}
  352. for _, p := range ps {
  353. if ts, ok := (p["topscopeclass"]).([]interface{}); ok {
  354. for _, v := range ts {
  355. tt := qu.ObjToString(v)
  356. tmpindustry[tt] = true
  357. }
  358. }
  359. }
  360. for k, _ := range tmpindustry {
  361. industry = append(industry, k)
  362. }
  363. data["industry"] = industry
  364. } else if datatype == "buyer" {
  365. data["buyer_name"] = name
  366. tmpbuyerclass := map[string]bool{}
  367. for _, p := range ps {
  368. tmpbuyerclass[qu.ObjToString(p["buyerclass"])] = true
  369. }
  370. buyerclass := []interface{}{}
  371. for k, _ := range tmpbuyerclass {
  372. buyerclass = append(buyerclass, k)
  373. }
  374. data["buyerclass"] = buyerclass
  375. data["ranks"] = ""
  376. data["type"] = ""
  377. data["address"] = ""
  378. } else {
  379. data["agency_name"] = name
  380. data["ranks"] = ""
  381. data["type"] = ""
  382. data["address"] = ""
  383. }
  384. return data
  385. }
  386. //根据规则数据打标记
  387. func comMarkdata(name, datatype string) int {
  388. tag := 0 //默认错误
  389. switch datatype {
  390. case "winner":
  391. for _, v := range WinnerRegOk {
  392. isok := v.MatchString(name)
  393. if isok { //匹配ok完,匹配err
  394. errflag := true
  395. for _, vRegErr := range WinnerRegErr {
  396. err := vRegErr.MatchString(name)
  397. if err {
  398. errflag = true
  399. break
  400. }
  401. }
  402. if errflag {
  403. tag = 1
  404. }
  405. }
  406. }
  407. case "buyer":
  408. for _, v := range BuyerRegOk {
  409. isok := v.MatchString(name)
  410. if isok { //匹配ok完,匹配err
  411. errflag := true
  412. for _, vRegErr := range BuyerRegErr {
  413. err := vRegErr.MatchString(name)
  414. if err {
  415. errflag = true
  416. break
  417. }
  418. }
  419. if errflag {
  420. tag = 1
  421. }
  422. }
  423. }
  424. case "agency":
  425. for _, v := range AgencyRegOk {
  426. isok := v.MatchString(name)
  427. if isok { //匹配ok完,匹配err
  428. errflag := true
  429. for _, vRegErr := range AgencyRegErr {
  430. err := vRegErr.MatchString(name)
  431. if err {
  432. errflag = true
  433. break
  434. }
  435. }
  436. if errflag {
  437. tag = 1
  438. }
  439. }
  440. }
  441. default:
  442. }
  443. return tag
  444. }
  445. //过滤行业冗余字符
  446. func comRepTopscopeclass(tops []interface{}) []interface{} {
  447. data := []interface{}{}
  448. for _, v := range tops {
  449. tt := qu.ObjToString(v)
  450. if len(tt) > 1 {
  451. data = append(data, tt[:len(tt)-1])
  452. }
  453. }
  454. return data
  455. }
  456. //
  457. func comUpdateErr(coll, name string, tclass []interface{}) {
  458. if len(tclass) < 1 {
  459. return
  460. }
  461. tmp := MongoTo.FindOne(coll, map[string]interface{}{"name": name})
  462. topscopeclass := tmp["topscopeclass"].(primitive.A)
  463. tmpclass := map[string]bool{}
  464. for _, tc := range topscopeclass {
  465. tmpclass[qu.ObjToString(tc)] = true
  466. }
  467. oldlen := len(tmpclass)
  468. for _, tc := range tclass {
  469. tmpclass[qu.ObjToString(tc)] = true
  470. }
  471. newlen := len(tmpclass)
  472. if oldlen == newlen {
  473. return
  474. }
  475. newclass := []interface{}{}
  476. for _, v := range tmpclass {
  477. newclass = append(newclass, v)
  478. }
  479. MongoTo.Update(coll, map[string]interface{}{"name": name}, map[string]interface{}{
  480. "$set": map[string]interface{}{
  481. "name": name,
  482. "topscopeclass": newclass,
  483. "updatetime": time.Now().Unix(),
  484. },
  485. })
  486. }