standarwinner.go 15 KB

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