company_type.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package main
  2. import (
  3. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  4. "log"
  5. "regexp"
  6. "strings"
  7. )
  8. // getCodeType 根据企业名称,获取经营主体代码标签
  9. func getCodeType(name string) (tab1, tab2 string) {
  10. where := map[string]interface{}{
  11. "use_flag": 0,
  12. "company_status": map[string]interface{}{
  13. "$nin": []string{"注销", "吊销", "吊销,已注销"},
  14. },
  15. "company_name": name,
  16. }
  17. // 🔍 正则,用于提取4位数字
  18. re4 := regexp.MustCompile(`\d{4}`)
  19. top_bases, _ := Mgo181.FindOne("company_base", where)
  20. company_type := util.ObjToString((*top_bases)["company_type"])
  21. // 去除引号
  22. cleanText := strings.ReplaceAll(company_type, `"`, "")
  23. cleanText = strings.TrimSpace(cleanText)
  24. // 替换中文括号为英文括号
  25. cleanText = strings.ReplaceAll(cleanText, "(", "(")
  26. cleanText = strings.ReplaceAll(cleanText, ")", ")")
  27. cleanText = strings.ReplaceAll(cleanText, "外资比例低于25%", "")
  28. // 判断是不是日期(如 2008/10/31)
  29. if matched, _ := regexp.MatchString(`^\d{4}/\d{1,2}/\d{1,2}$`, cleanText); matched {
  30. log.Printf("跳过日期行: %s", cleanText)
  31. return
  32. }
  33. // 如果包含“年”/“月”/“日”等,也跳过(不提取数字)
  34. if strings.Contains(cleanText, "年") || strings.Contains(cleanText, "月") || strings.Contains(cleanText, "日") {
  35. log.Printf("跳过含日期描述的行: %s", cleanText)
  36. return
  37. }
  38. var code string
  39. // 优先提取4位数字
  40. if m := re4.FindString(cleanText); m != "" {
  41. code = m
  42. } else {
  43. // 去除引号后完全匹配
  44. if c, ok := codeMap[cleanText]; ok {
  45. code = c
  46. }
  47. }
  48. if code == "" {
  49. code = getCodeByCompanyType(cleanText, codeMap)
  50. if code != "" {
  51. tab1 = codeZhu[code]
  52. tab2 = codeZhu2[code]
  53. }
  54. } else {
  55. tab1 = codeZhu[code]
  56. tab2 = codeZhu2[code]
  57. }
  58. return
  59. }
  60. // getCompanyType 获取公司类型;央企、国企、央企下属、事业单位、民企
  61. func getCompanyType(name string) (company_type string) {
  62. if name == "" {
  63. return
  64. }
  65. if yangMap[name] {
  66. company_type = "央企"
  67. return
  68. }
  69. if yangChildMap[name] {
  70. company_type = "央企下属"
  71. return
  72. }
  73. where := map[string]interface{}{
  74. "company_name": name,
  75. }
  76. // special_enterprise 事业单位
  77. enterprise, _ := Mgo181.FindOne("special_enterprise", where)
  78. if enterprise != nil && (len(*enterprise)) > 0 {
  79. company_type = "事业单位"
  80. return
  81. }
  82. //special_gov_unit 机关单位
  83. gov, _ := Mgo181.FindOne("special_gov_unit", where)
  84. if gov != nil && (len(*gov)) > 0 {
  85. company_type = "党政机关"
  86. return
  87. }
  88. //special_social_organ 社会团体
  89. soc, _ := Mgo181.FindOne("special_social_organ", where)
  90. if soc != nil && (len(*soc)) > 0 {
  91. company_type = "社会团体"
  92. return
  93. }
  94. //company_base
  95. where["use_flag"] = 0
  96. bases, _ := Mgo181.Find("company_base", where, nil, nil, false, -1, -1)
  97. if bases != nil && (len(*bases)) > 0 {
  98. for _, v := range *bases {
  99. if strings.Contains(util.ObjToString(v["company_status"]), "吊销") || strings.Contains(util.ObjToString(v["company_status"]), "注销") || util.ObjToString((v)["company_type"]) == "" {
  100. continue
  101. } else {
  102. bty := util.ObjToString((v)["company_type"])
  103. if strings.Contains(bty, "国有独资") || strings.Contains(bty, "国有控股") ||
  104. bty == "全民所有制" || bty == "集体所有制" || bty == "全民所有制分支机构(非法人)" ||
  105. bty == "集体分支机构(非法人)" || bty == "内资集团" || bty == "国有事业单位营业" || bty == "集体事业单位营业" ||
  106. bty == "国有社团法人营业" || bty == "国有经营单位(非法人)" || bty == "集体经营单位(非法人)" {
  107. company_type = "国企"
  108. return
  109. }
  110. // 地方国企
  111. par_name := getTop(name)
  112. topwhere := map[string]interface{}{
  113. "use_flag": 0,
  114. "company_status": map[string]interface{}{
  115. "$nin": []string{"注销", "吊销", "吊销,已注销"},
  116. },
  117. "company_name": par_name,
  118. }
  119. top_bases, _ := Mgo181.FindOne("company_base", topwhere)
  120. top_company_type := util.ObjToString((*top_bases)["company_type"])
  121. if strings.Contains(top_company_type, "国有独资") || strings.Contains(top_company_type, "国有控股") ||
  122. top_company_type == "全民所有制" || top_company_type == "集体所有制" || top_company_type == "全民所有制分支机构(非法人)" ||
  123. top_company_type == "集体分支机构(非法人)" || top_company_type == "内资集团" || top_company_type == "国有事业单位营业" || top_company_type == "集体事业单位营业" ||
  124. top_company_type == "国有社团法人营业" || top_company_type == "国有经营单位(非法人)" || top_company_type == "集体经营单位(非法人)" || top_company_type == "事业单位" {
  125. company_type = "地方国企"
  126. return
  127. }
  128. }
  129. }
  130. }
  131. //company_type = "民企"
  132. return
  133. }
  134. // getMarketType 获取上市公司类型;A股、新三板、非上市
  135. func getMarketType(name string) (stype string) {
  136. stype = "非上市"
  137. where := map[string]interface{}{
  138. "company_name": name,
  139. "use_flag": 0,
  140. }
  141. bases, _ := Mgo181.Find("company_base", where, nil, nil, false, -1, -1)
  142. if bases != nil && (len(*bases)) > 0 {
  143. for _, v := range *bases {
  144. if strings.Contains(util.ObjToString(v["company_status"]), "吊销") || strings.Contains(util.ObjToString(v["company_status"]), "注销") {
  145. continue
  146. } else {
  147. list_code := util.ObjToString((v)["list_code"])
  148. if list_code == "" {
  149. continue
  150. }
  151. //新三板股票代码 开头
  152. if strings.HasPrefix(list_code, "43") || strings.HasPrefix(list_code, "83") || strings.HasPrefix(list_code, "87") || strings.HasPrefix(list_code, "88") {
  153. stype = "新三板"
  154. return
  155. } else {
  156. stype = "A股"
  157. return
  158. }
  159. }
  160. }
  161. }
  162. return
  163. }
  164. // getTop 获取最上级公司
  165. // getTop 获取最上级公司
  166. func getTop(name string) (top string) {
  167. if name == "" {
  168. return
  169. }
  170. currName := name
  171. firstSearch := true
  172. visited := make(map[string]bool)
  173. visited[currName] = true
  174. for {
  175. topTmp := getParentsByPartner(currName)
  176. topName := util.ObjToString(topTmp["stock_name"])
  177. // 第一次查找就失败,直接返回空
  178. if firstSearch && topName == "" {
  179. return
  180. }
  181. firstSearch = false
  182. // 非第一次查找,上级为空,返回当前获取到的上级
  183. if topName == "" {
  184. return currName
  185. }
  186. // 避免循环:如果 topName 与 currName 相同,或 topName 已访问过,说明进入环路
  187. if topName == currName || visited[topName] {
  188. return currName
  189. }
  190. // 如果 topName 不符合要求,比如不含“公司”,也返回当前
  191. if !strings.Contains(topName, "公司") {
  192. return currName
  193. }
  194. // 更新当前公司名,继续向上找
  195. currName = topName
  196. visited[currName] = true
  197. }
  198. }
  199. // getParentsByPartner 获取母公司,投资比例 50% 以上
  200. func getParentsByPartner(companyName string) (res map[string]interface{}) {
  201. q := map[string]interface{}{"company_name": companyName, "use_flag": 0, "is_history": 0, "is_personal": 0}
  202. info, _ := Mgo181.Find("company_partner", q, nil, nil, false, -1, -1)
  203. if info == nil || len(*info) == 0 {
  204. return
  205. }
  206. for _, v := range *info {
  207. // 不是法人企业,直接跳过
  208. if !strings.Contains(util.ObjToString(v["stock_type"]), "法人") {
  209. continue
  210. }
  211. // 投资比例 必须 0.5以上
  212. stock_proportion := util.Float64All(v["stock_proportion"])
  213. if stock_proportion >= 0.5 {
  214. return v
  215. }
  216. }
  217. return
  218. }