main.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/olivere/elastic/v7"
  8. nebula "github.com/vesoft-inc/nebula-go/v3"
  9. "io"
  10. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  12. "log"
  13. "net/http"
  14. "regexp"
  15. "strconv"
  16. "strings"
  17. )
  18. var (
  19. re = regexp.MustCompile(`[-+]?[0-9]*\.?[0-9]+`)
  20. HostList = []nebula.HostAddress{{Host: "114.116.213.97", Port: 9669}}
  21. //HostList = []nebula.HostAddress{{Host: "127.0.0.1", Port: 9669}}
  22. UserName = "root"
  23. PassWord = "jianyu@123"
  24. Mgo181 *mongodb.MongodbSim
  25. //Table_Space = "legal_profile"
  26. Table_Space = "legal_profile"
  27. WorkerCount = 5
  28. BatchSize = 100
  29. )
  30. // Legal 代表公司节点的结构体
  31. type Legal struct {
  32. Id string
  33. Name string
  34. Code string
  35. Type string //类型,企业,事业单哪位,政府部门
  36. State string //状态,有效/无效;不是存续、在营、开业、在册
  37. }
  38. // Invest 代表公司之间的投资关系边的结构体
  39. type Invest struct {
  40. FromCode string
  41. ToCode string
  42. Amount float64
  43. Ratio float64
  44. }
  45. // SuspectInvest 疑似关系
  46. type SuspectInvest struct {
  47. FromCode string `json:"from_code"`
  48. ToCode string `json:"to_code"`
  49. Reason string `json:"reason"` //原因,手机、邮箱相同
  50. }
  51. // InsertSuspectJob 处理疑似投资
  52. type InsertSuspectJob struct {
  53. Relations SuspectInvest
  54. }
  55. // ExecutivesInvest 高管关系结构体
  56. type ExecutivesInvest struct {
  57. FromCode string `json:"from_code"`
  58. ToCode string `json:"to_code"`
  59. Name string `json:"name"` //姓名
  60. }
  61. // InsertJob 批量处理投资关系的点 和 边关系
  62. type InsertJob struct {
  63. Companies []Legal
  64. Relations []Invest
  65. }
  66. type InvestVertex struct { //顶点
  67. id string
  68. company_id string
  69. company_name string
  70. credit_no string
  71. }
  72. type InvestEdge struct { //边
  73. company_id string
  74. company_name string
  75. stock_id string
  76. stock_name string
  77. stock_rate float64
  78. stock_amount float64
  79. stock_level int
  80. stock_type int //0企业股东 1自然人股东
  81. }
  82. // ConnectToNebula 封装数据库连接函数
  83. func ConnectToNebula(hosts []nebula.HostAddress, username, password string) (*nebula.Session, *nebula.ConnectionPool, error) {
  84. // 创建连接池配置
  85. config := nebula.GetDefaultConf()
  86. config.UseHTTP2 = false
  87. config.HandshakeKey = ""
  88. // 初始化连接池
  89. pool, err := nebula.NewConnectionPool(hosts, config, nebula.DefaultLogger{})
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. // 获取会话
  94. session, err := pool.GetSession(username, password)
  95. if err != nil {
  96. pool.Close()
  97. return nil, nil, err
  98. }
  99. return session, pool, nil
  100. }
  101. type CheckRequest struct {
  102. Names []string `json:"names"`
  103. Deep int `json:"deep"`
  104. Stype int `json:"stype"` //0.简易模式,匹配到直接返回;1.匹配完所有的数据
  105. }
  106. type CheckResponse struct {
  107. Code int `json:"code"`
  108. Data []string `json:"data"`
  109. Msg string `json:"msg"`
  110. }
  111. type GraphResponse struct {
  112. Code int `json:"code"`
  113. Data []string `json:"data"`
  114. Msg string `json:"msg"`
  115. Echars *GraphResult `json:"echars"`
  116. }
  117. // SuspectInvestResponse 意思关系返回结果
  118. type SuspectInvestResponse struct {
  119. Code int `json:"code"`
  120. Data []interface{} `json:"data"`
  121. Msg string `json:"msg"`
  122. }
  123. func main() {
  124. handHttp()
  125. return
  126. //dealYS()
  127. //dealTsDongShi() //董事高管
  128. //log.Println("处理完毕")
  129. //return
  130. //InitMgo() //初始化 MongoDB
  131. //dealTsGraph() //处理疑似关系图谱
  132. //dealDsGraph() //处理高管 关系到 图形数据库
  133. //---------------//
  134. //dda()
  135. //dealCompanyBase22()
  136. //dealCompanyBase() //迭代company_base 处理企业数据
  137. //batchDealGraph() // 迭代es 处理企业数据;
  138. //
  139. //log.Println("数据处理完毕!!!!!!!")
  140. //return
  141. //-----------------------------------------//
  142. //3、改造方法,使用连接池,避免session过去//
  143. // 初始化 Gin 路由
  144. //r := gin.Default()
  145. //
  146. //client, err := NewNebulaClient(HostList, UserName, PassWord)
  147. //if err != nil {
  148. // log.Fatal("连接失败:", err)
  149. //}
  150. //defer client.Close()
  151. //// 注册 POST 接口
  152. //// 提供 HTML 页面
  153. ////r.GET("/legal/graph", func(c *gin.Context) {
  154. //// c.HTML(http.StatusOK, "graph.html", nil)
  155. ////})
  156. //r.POST("/check-relations", func(c *gin.Context) {
  157. // var req CheckRequest
  158. // if err := c.ShouldBindJSON(&req); err != nil {
  159. // c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
  160. // return
  161. // }
  162. //
  163. // has, results, err := client.CheckLegalRelationships(req.Names, req.Deep, req.Stype)
  164. // if err != nil {
  165. // res := CheckResponse{
  166. // Code: -1,
  167. // Data: results,
  168. // Msg: "请求失败;" + err.Error(),
  169. // }
  170. // c.JSON(http.StatusInternalServerError, res)
  171. // return
  172. // }
  173. //
  174. // res := CheckResponse{
  175. // Code: 200,
  176. // Data: results,
  177. // }
  178. // if has {
  179. // res.Msg = "存在投资关系"
  180. // } else {
  181. // res.Msg = "不存在投资关系"
  182. // }
  183. //
  184. // c.JSON(http.StatusOK, res)
  185. //})
  186. //// 启动服务
  187. //r.Run(":8080")
  188. }
  189. // handHttp 对外提供 HTTP接口
  190. func handHttp() {
  191. // 初始化 Gin 路由
  192. r := gin.Default()
  193. client, err := NewNebulaClient(HostList, UserName, PassWord)
  194. if err != nil {
  195. log.Fatal("连接失败:", err)
  196. }
  197. defer client.Close()
  198. //1、投资关系查询
  199. r.POST("/check-relations", func(c *gin.Context) {
  200. var req CheckRequest
  201. if err := c.ShouldBindJSON(&req); err != nil {
  202. c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
  203. return
  204. }
  205. has, results, resb, err := client.CheckLegalRelationships(req.Names, req.Deep, req.Stype)
  206. if err != nil {
  207. res := GraphResponse{
  208. Code: -1,
  209. Data: results,
  210. Msg: "请求失败;" + err.Error(),
  211. }
  212. c.JSON(http.StatusInternalServerError, res)
  213. return
  214. }
  215. res := GraphResponse{
  216. Code: 200,
  217. Data: results,
  218. Echars: resb,
  219. }
  220. if has {
  221. res.Msg = "存在投资关系"
  222. } else {
  223. res.Msg = "不存在投资关系"
  224. }
  225. c.JSON(http.StatusOK, res)
  226. })
  227. //2、疑似关系 查询
  228. r.POST("/yisi-relations", func(c *gin.Context) {
  229. var req CheckRequest
  230. if err := c.ShouldBindJSON(&req); err != nil {
  231. c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
  232. return
  233. }
  234. results, err := client.FindSuspectInvestRelationsByNames(req.Names)
  235. if err != nil {
  236. res := map[string]interface{}{
  237. "code": 200,
  238. "msg": "请求失败",
  239. "data": err.Error(),
  240. }
  241. c.JSON(http.StatusInternalServerError, res)
  242. return
  243. } else {
  244. res := map[string]interface{}{
  245. "code": 200,
  246. "msg": "请求成功",
  247. "data": results,
  248. }
  249. c.JSON(http.StatusOK, res)
  250. }
  251. })
  252. //3.董高监 关系
  253. r.POST("/dgj-relations", func(c *gin.Context) {
  254. var req CheckRequest
  255. if err := c.ShouldBindJSON(&req); err != nil {
  256. c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
  257. return
  258. }
  259. results, err := client.FindExecutivesInvestRelationsByNames(req.Names)
  260. if err != nil {
  261. res := map[string]interface{}{
  262. "code": 200,
  263. "msg": "请求失败",
  264. "data": err.Error(),
  265. }
  266. c.JSON(http.StatusInternalServerError, res)
  267. return
  268. } else {
  269. res := map[string]interface{}{
  270. "code": 200,
  271. "msg": "请求成功",
  272. "data": results,
  273. }
  274. c.JSON(http.StatusOK, res)
  275. }
  276. })
  277. //--------------------------//
  278. // 启动服务
  279. r.Run(":8080")
  280. }
  281. // dda 针对某个企业,单独生节点数据
  282. func dda() {
  283. name := "北京拓普丰联信息科技股份有限公司"
  284. rea, resb := GetInvByLevel(name, 5, 0, false)
  285. // 调用封装的连接函数
  286. session, pool, err := ConnectToNebula(HostList, UserName, PassWord)
  287. if err != nil {
  288. log.Fatalf("Failed to connect to Nebula Graph: %v", err)
  289. }
  290. defer pool.Close()
  291. defer session.Release()
  292. for _, v := range rea {
  293. d := Legal{
  294. Id: v.company_id,
  295. Name: v.company_name,
  296. Code: v.credit_no,
  297. Type: "企业",
  298. }
  299. res, err := InsertCompany(session, d)
  300. if err != nil {
  301. log.Println(err, res)
  302. }
  303. }
  304. for _, v := range resb {
  305. d := Invest{
  306. FromCode: v.stock_name,
  307. ToCode: v.company_name,
  308. Amount: v.stock_amount,
  309. Ratio: v.stock_rate,
  310. }
  311. err := InsertInvestRel(session, d)
  312. if err != nil {
  313. log.Println(err, d)
  314. }
  315. }
  316. }
  317. // getQyxytData 获取企业数据
  318. func getQyxytData() {
  319. // 调用封装的连接函数
  320. session, pool, err := ConnectToNebula(HostList, UserName, PassWord)
  321. if err != nil {
  322. log.Fatalf("Failed to connect to Nebula Graph: %v", err)
  323. }
  324. defer pool.Close()
  325. defer session.Release()
  326. url := "http://172.17.4.184:19908"
  327. //url := "http://127.0.0.1:19908"
  328. username := "jybid"
  329. password := "Top2023_JEB01i@31"
  330. index := "qyxy" //索引名称
  331. // 创建 Elasticsearch 客户端
  332. client, err := elastic.NewClient(
  333. elastic.SetURL(url),
  334. elastic.SetBasicAuth(username, password),
  335. elastic.SetSniff(false),
  336. )
  337. if err != nil {
  338. log.Fatalf("创建 Elasticsearch 客户端失败:%s", err)
  339. }
  340. //---------------------------//
  341. //query := elastic.NewBoolQuery()
  342. //query.Must(elastic.NewMatchQuery("business_scope", "招投标代理"))
  343. //query.Must(elastic.NewTermQuery("company_city", "北京市"))
  344. //rangeQuery := elastic.NewRangeQuery("comeintime").Gte("1640966400").Lt("1703952000")
  345. query := elastic.NewBoolQuery().
  346. //北京,天津,河北,上海,江苏,浙江,安徽
  347. //Must(elastic.NewTermQuery("area", "北京市")).
  348. //Must(elastic.NewTermsQuery("subtype", "中标", "单一", "成交", "合同")).
  349. MustNot(
  350. elastic.NewTermQuery("company_type", "个体工商户"),
  351. ).
  352. //Must(elastic.NewTermQuery("company_name", "河南拓普计算机网络工程有限公司"))
  353. Must(elastic.NewTermsQuery("company_area", "河南"))
  354. ctx := context.Background()
  355. //开始滚动搜索
  356. scrollID := ""
  357. scroll := "10m"
  358. searchSource := elastic.NewSearchSource().
  359. Query(query).
  360. Size(10000).
  361. Sort("_doc", true) //升序排序
  362. //Sort("_doc", false) //降序排序
  363. searchService := client.Scroll(index).
  364. Size(10000).
  365. Scroll(scroll).
  366. SearchSource(searchSource)
  367. res, err := searchService.Do(ctx)
  368. if err != nil {
  369. if err == io.EOF {
  370. fmt.Println("没有数据")
  371. } else {
  372. panic(err)
  373. }
  374. }
  375. //defer client.ClearScroll().ScrollId(scrollID).Do(ctx) // 在退出时清理资源
  376. fmt.Println("总数是:", res.TotalHits())
  377. total := 0
  378. for len(res.Hits.Hits) > 0 {
  379. for _, hit := range res.Hits.Hits {
  380. var doc map[string]interface{}
  381. err := json.Unmarshal(hit.Source, &doc)
  382. if err != nil {
  383. log.Printf("解析文档失败:%s", err)
  384. continue
  385. }
  386. company1 := Legal{
  387. Name: util.ObjToString(doc["company_name"]),
  388. Code: util.ObjToString(doc["credit_no"]),
  389. Type: "企业",
  390. }
  391. /**
  392. 1.stock_name_id 为空,直接跳过
  393. 2.stock_name 为空,直接跳过
  394. 3.stock_name 含有 已除名/不适宜/待清理/拟吊销 ,直接跳过
  395. 4.stock_name 不含中文,跳过
  396. */
  397. if util.ObjToString(doc["company_name"]) == "" || util.ObjToString(doc["credit_no"]) == "" {
  398. continue
  399. }
  400. if strings.Contains(util.ObjToString(doc["company_name"]), "已除名") {
  401. continue
  402. }
  403. res1, err1 := InsertCompany(session, company1)
  404. if err != nil {
  405. log.Println("InsertCompany err", res1, err1)
  406. }
  407. //边
  408. if partners, ok := doc["partners"].([]interface{}); ok {
  409. for _, partner := range partners {
  410. if da, ok := partner.(map[string]interface{}); ok {
  411. if util.ObjToString(da["stock_type"]) == "企业法人" {
  412. if util.ObjToString(da["stock_name"]) == "" || util.ObjToString(da["identify_no"]) == "" {
  413. continue
  414. } else {
  415. company2 := Legal{
  416. Name: util.ObjToString(da["stock_name"]),
  417. Code: util.ObjToString(da["identify_no"]),
  418. Type: "企业",
  419. }
  420. res2, err2 := InsertCompany(session, company2)
  421. if err2 != nil {
  422. log.Println("InsertCompany err", res2, err2)
  423. }
  424. //
  425. if err1 != nil || err2 != nil {
  426. continue
  427. }
  428. where := map[string]interface{}{
  429. "company_name": util.ObjToString(doc["company_name"]),
  430. "stock_name": util.ObjToString(da["stock_name"]),
  431. }
  432. ddd, _ := Mgo181.FindOne("company_partner", where)
  433. if len(*ddd) > 0 {
  434. par := *ddd
  435. amount := ParseStockCapital(util.ObjToString(par["stock_capital"]))
  436. investRel := Invest{FromCode: util.ObjToString(da["stock_name"]), ToCode: util.ObjToString(doc["company_name"]), Ratio: util.Float64All(par["stock_proportion"]), Amount: amount}
  437. err = InsertInvestRel(session, investRel)
  438. if err != nil {
  439. log.Println("InsertInvestRel", err, investRel)
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. }
  447. }
  448. total = total + len(res.Hits.Hits)
  449. scrollID = res.ScrollId
  450. res, err = client.Scroll().ScrollId(scrollID).Scroll(scroll).Do(ctx)
  451. log.Println("current count:", total)
  452. if err != nil {
  453. if err == io.EOF {
  454. // 滚动到最后一批数据,退出循环
  455. break
  456. }
  457. log.Println("滚动搜索失败:", err, res)
  458. break // 处理错误时退出循环
  459. }
  460. }
  461. // 在循环外调用 ClearScroll
  462. _, err = client.ClearScroll().ScrollId(scrollID).Do(ctx)
  463. if err != nil {
  464. log.Printf("清理滚动搜索失败:%s", err)
  465. }
  466. log.Println("结束~~~~~~~~~~~~~~~")
  467. }
  468. // dealCompanyPartner 处理企业投资关系
  469. func dealCompanyPartner() {
  470. // 调用封装的连接函数
  471. session, pool, err := ConnectToNebula(HostList, UserName, PassWord)
  472. if err != nil {
  473. log.Fatalf("Failed to connect to Nebula Graph: %v", err)
  474. }
  475. defer pool.Close()
  476. defer session.Release()
  477. //log.Println("session", session)
  478. defer util.Catch()
  479. sess := Mgo181.GetMgoConn()
  480. defer Mgo181.DestoryMongoConn(sess)
  481. it := sess.DB("mixdata").C("company_partner").Find(nil).Select(nil).Iter()
  482. count := 0
  483. //realNum := 0
  484. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  485. if count%10000 == 0 {
  486. log.Println("current:", count, tmp["stock_name"], tmp["company_name"])
  487. }
  488. //个人企业跳过
  489. if util.IntAll(tmp["is_personal"]) == 1 {
  490. continue
  491. }
  492. if util.IntAll(tmp["use_flag"]) > 0 {
  493. continue
  494. }
  495. company1 := Legal{
  496. Name: util.ObjToString(tmp["stock_name"]),
  497. Code: util.ObjToString(tmp["stock_name_id"]),
  498. }
  499. /**
  500. 1.stock_name_id 为空,直接跳过
  501. 2.stock_name 为空,直接跳过
  502. 3.stock_name 含有 已除名/不适宜/待清理/拟吊销 ,直接跳过
  503. 4.stock_name 不含中文,跳过
  504. */
  505. company2 := Legal{
  506. Name: util.ObjToString(tmp["company_name"]),
  507. Code: util.ObjToString(tmp["company_id"]),
  508. }
  509. res1, err1 := InsertCompany(session, company1)
  510. if err != nil {
  511. log.Println("InsertCompany err", res1, err1)
  512. }
  513. res2, err2 := InsertCompany(session, company2)
  514. if err != nil {
  515. log.Println("InsertCompany err", res2, err2)
  516. }
  517. if err1 != nil || err2 != nil {
  518. continue
  519. }
  520. //边
  521. amount := ParseStockCapital(util.ObjToString(tmp["stock_capital"]))
  522. investRel := Invest{FromCode: res1, ToCode: res2, Ratio: util.Float64All(tmp["stock_proportion"]), Amount: amount}
  523. err = InsertInvestRel(session, investRel)
  524. if err != nil {
  525. log.Println("InsertInvestRel", err, investRel)
  526. }
  527. }
  528. }
  529. // InsertCompany 插入公司节点的方法
  530. func InsertCompany(session *nebula.Session, company Legal) (string, error) {
  531. // 构建插入公司节点的查询
  532. //insertCompanyStmt := `
  533. // USE ` + Table_Space + `;
  534. // INSERT VERTEX company(company_id,name) VALUES "%s":("%s", "%s");
  535. //`
  536. //insertCompanyStmt = fmt.Sprintf(insertCompanyStmt, inv.id, inv.company_id, inv.company_name)
  537. query := fmt.Sprintf(`
  538. USE `+Table_Space+`;
  539. INSERT VERTEX Legal(name, code, type, state ) VALUES "%s":("%s", "%s", "%s", "%s")
  540. `, company.Name, company.Name, company.Code, company.Type, company.State)
  541. // 执行查询
  542. result, err := session.Execute(query)
  543. if err != nil {
  544. log.Println("InsertCompany", result)
  545. return "", err
  546. }
  547. // 打印返回结果
  548. //fmt.Println("Insert Company Result:", result)
  549. // 返回节点ID(通常可以通过返回的结果中的 "_vid" 字段获取)
  550. return company.Name, nil
  551. }
  552. // InsertInvestRel 插入投资关系边的方法
  553. func InsertInvestRel(session *nebula.Session, investRel Invest) error {
  554. // 构建插入投资关系边的查询
  555. query := fmt.Sprintf(`
  556. USE `+Table_Space+`;
  557. INSERT EDGE Invest(amount, ratio) VALUES "%s"->"%s":(%f, %f)
  558. `, investRel.FromCode, investRel.ToCode, investRel.Amount, investRel.Ratio)
  559. // 执行查询
  560. result, err := session.Execute(query)
  561. if err != nil {
  562. log.Println("InsertInvestRel", result)
  563. return err
  564. }
  565. // 打印返回结果
  566. //fmt.Println("Insert InvestRel Result:", result)
  567. return nil
  568. }
  569. func ParseStockCapital(raw string) float64 {
  570. raw = strings.TrimSpace(raw)
  571. // 默认单位:万元人民币
  572. exchangeRateUSD := 7.0
  573. // 匹配数值部分(可能带小数)
  574. re := regexp.MustCompile(`([\d.]+)`)
  575. match := re.FindStringSubmatch(raw)
  576. if len(match) < 2 {
  577. return 0
  578. }
  579. value, _ := strconv.ParseFloat(match[1], 64)
  580. // 判断单位并转换
  581. switch {
  582. case strings.Contains(raw, "万美元"):
  583. value *= exchangeRateUSD // 转换成人民币
  584. case strings.Contains(raw, "元") || strings.Contains(raw, "人民币"):
  585. if strings.Contains(raw, "万元") || strings.Contains(raw, "万") {
  586. // 已经是万元单位,无需处理
  587. } else {
  588. // 是“元”,需要除以1万
  589. value = value / 10000
  590. }
  591. default:
  592. // 可能是纯数字,默认视为“万元”
  593. }
  594. return value
  595. }
  596. /*
  597. 根据公司名称和层级向上挖掘,获取顶点和边;
  598. maxLevel 挖掘层级数量;
  599. direction 0:双向挖掘 -1:向上挖掘 1:向下挖掘
  600. person true:保留自然人股东 false:不保留自然人股东
  601. */
  602. func GetInvByLevel(company_name string, maxLevel int, direction int, person bool) (map[string]InvestVertex, []InvestEdge) {
  603. verter := map[string]InvestVertex{}
  604. edges := []InvestEdge{}
  605. if direction == 0 {
  606. v1, e1 := getInvByLevel(company_name, maxLevel, 1, person)
  607. v2, e2 := getInvByLevel(company_name, maxLevel, -1, person)
  608. for k, v := range v1 {
  609. verter[k] = v
  610. }
  611. for k, v := range v2 {
  612. verter[k] = v
  613. }
  614. edges = append(edges, e1...)
  615. edges = append(edges, e2...)
  616. } else {
  617. verter, edges = getInvByLevel(company_name, maxLevel, direction, person)
  618. }
  619. return verter, edges
  620. }
  621. func getInvByLevel(company_name string, maxLevel int, direction int, person bool) (map[string]InvestVertex, []InvestEdge) {
  622. data, _ := Mgo181.FindOne("company_base", map[string]interface{}{
  623. "company_name": company_name,
  624. })
  625. company_id := fmt.Sprint((*data)["company_id"])
  626. credit_no := fmt.Sprint((*data)["credit_no"])
  627. var edges = []InvestEdge{} //记录边
  628. var verter = map[string]InvestVertex{} //有效顶点
  629. // 初始化队列和访问记录
  630. type node struct {
  631. companyID, companyName, creditNo string
  632. level int
  633. }
  634. queue := []node{{companyID: company_id, companyName: company_name, creditNo: credit_no, level: 1}}
  635. visited := make(map[string]bool)
  636. for len(queue) > 0 {
  637. current := queue[0]
  638. if _, ok := verter[current.companyID]; !ok {
  639. verter[current.companyID] = InvestVertex{
  640. id: current.companyID,
  641. company_id: current.companyID,
  642. company_name: current.companyName,
  643. credit_no: current.creditNo,
  644. }
  645. }
  646. queue = queue[1:]
  647. if visited[current.companyID] || current.level > maxLevel { // 防止重复处理和超过最大层级
  648. continue
  649. }
  650. visited[current.companyID] = true
  651. query := map[string]interface{}{"company_id": current.companyID}
  652. if direction > 0 {
  653. query = map[string]interface{}{"stock_name_id": current.companyID}
  654. }
  655. partners, _ := Mgo181.Find("company_partner", query, nil, nil, false, -1, -1)
  656. // 处理股东数据
  657. for _, p := range *partners {
  658. //log.Println(direction, p)
  659. if fmt.Sprint(p["is_history"]) == "1" {
  660. continue
  661. }
  662. // 构建投资关系
  663. inv := InvestEdge{
  664. company_id: fmt.Sprint(p["company_id"]),
  665. company_name: fmt.Sprint(p["company_name"]),
  666. stock_id: fmt.Sprint(p["stock_name_id"]),
  667. stock_name: fmt.Sprint(p["stock_name"]),
  668. stock_rate: convertStockCapitalToFloat(fmt.Sprint(p["stock_proportion"])),
  669. stock_amount: convertStockCapitalToFloat(fmt.Sprint(p["stock_capital"])),
  670. stock_level: current.level,
  671. stock_type: 0, // 默认机构股东
  672. }
  673. edges = append(edges, inv)
  674. // 根据股东类型是否继续挖掘
  675. if fmt.Sprint(p["stock_type"]) == "自然人股东" || convertStockCapitalToFloat(fmt.Sprint(p["is_personal"])) > 0 {
  676. inv.stock_type = 1
  677. if _, ok := verter[inv.stock_id]; !ok && person {
  678. verter[inv.stock_id] = InvestVertex{
  679. id: inv.stock_id,
  680. company_id: inv.stock_id,
  681. company_name: inv.stock_name,
  682. }
  683. }
  684. } else {
  685. where1 := map[string]interface{}{
  686. "company_name": inv.company_name,
  687. }
  688. where2 := map[string]interface{}{
  689. "company_name": inv.stock_name,
  690. }
  691. company, _ := Mgo181.FindOne("company_base", where1)
  692. stock, _ := Mgo181.FindOne("company_base", where2)
  693. // 机构股东加入队列继续穿透
  694. if direction > 0 { //向下挖掘
  695. if !visited[inv.company_id] {
  696. queue = append(queue, node{
  697. companyID: inv.company_id,
  698. companyName: inv.company_name,
  699. creditNo: util.ObjToString((*company)["credit_no"]),
  700. level: current.level + 1,
  701. })
  702. }
  703. } else { //向上挖掘
  704. if !visited[inv.stock_id] {
  705. queue = append(queue, node{
  706. companyID: inv.stock_id,
  707. companyName: inv.stock_name,
  708. creditNo: util.ObjToString((*stock)["credit_no"]),
  709. level: current.level + 1,
  710. })
  711. }
  712. }
  713. }
  714. }
  715. //log.Printf("已处理层级%d,当前队列深度%d", current.level, len(queue))
  716. }
  717. return verter, edges
  718. }
  719. func convertStockCapitalToFloat(str string) float64 {
  720. // 查找匹配的数字
  721. match := re.FindString(str)
  722. if match == "" {
  723. return 0
  724. }
  725. // 将匹配到的数字字符串转换为浮点数
  726. result, err := strconv.ParseFloat(match, 64)
  727. if err != nil {
  728. return 0
  729. }
  730. return result
  731. }