package main import ( "fmt" "github.com/xuri/excelize/v2" util "jygit.jydev.jianyu360.cn/data_processing/common_utils" "log" "regexp" "strings" ) // matchCompanyCode 根据企业库有的类型,匹配对应标准法人库类型 func matchCompanyCode() { filePath := "company_types_finish.xlsx" // 1. 读取 Excel(获取 A 列数据) f, err := excelize.OpenFile(filePath) if err != nil { log.Fatal("❌ 无法打开 Excel 文件:", err) } defer f.Close() //读取央企 rows, err := f.GetRows("企业库所有类型") if err != nil { log.Fatal("❌ 无法读取 企业所有类型:", err) } // 🔍 正则,用于提取4位数字 re4 := regexp.MustCompile(`\d{4}`) for i := 1; i < len(rows); i++ { name := rows[i][0] log.Println("name", name) // 去除引号 cleanText := strings.ReplaceAll(name, `"`, "") cleanText = strings.TrimSpace(cleanText) // 替换中文括号为英文括号 cleanText = strings.ReplaceAll(cleanText, "(", "(") cleanText = strings.ReplaceAll(cleanText, ")", ")") var code string // 优先提取4位数字 if m := re4.FindString(cleanText); m != "" { code = m } else { // 去除引号后完全匹配 if c, ok := codeMap[cleanText]; ok { code = c } } if code == "" { code = getCodeByCompanyType(cleanText, codeMap) } tt := nameMap[code] f.SetCellValue("企业库所有类型", fmt.Sprintf("D%v", i+1), code) f.SetCellValue("企业库所有类型", fmt.Sprintf("E%v", i+1), tt) } f.Save() } // getCodeType 根据企业名称,获取经营主体代码标签 func getCodeType(name string) (tab1, tab2 string) { where := map[string]interface{}{ "use_flag": 0, "company_status": map[string]interface{}{ "$nin": []string{"注销", "吊销", "吊销,已注销"}, }, "company_name": name, } // 🔍 正则,用于提取4位数字 re4 := regexp.MustCompile(`\d{4}`) top_bases, _ := Mgo181.FindOne("company_base", where) company_type := util.ObjToString((*top_bases)["company_type"]) // 去除引号 cleanText := strings.ReplaceAll(company_type, `"`, "") cleanText = strings.TrimSpace(cleanText) // 替换中文括号为英文括号 cleanText = strings.ReplaceAll(cleanText, "(", "(") cleanText = strings.ReplaceAll(cleanText, ")", ")") cleanText = strings.ReplaceAll(cleanText, "外资比例低于25%", "") // 判断是不是日期(如 2008/10/31) if matched, _ := regexp.MatchString(`^\d{4}/\d{1,2}/\d{1,2}$`, cleanText); matched { log.Printf("跳过日期行: %s", cleanText) return } // 如果包含“年”/“月”/“日”等,也跳过(不提取数字) if strings.Contains(cleanText, "年") || strings.Contains(cleanText, "月") || strings.Contains(cleanText, "日") { log.Printf("跳过含日期描述的行: %s", cleanText) return } var code string // 优先提取4位数字 if m := re4.FindString(cleanText); m != "" { code = m } else { // 去除引号后完全匹配 if c, ok := codeMap[cleanText]; ok { code = c } } if code == "" { code = getCodeByCompanyType(cleanText, codeMap) if code != "" { tab1 = codeZhu[code] tab2 = codeZhu2[code] } } else { tab1 = codeZhu[code] tab2 = codeZhu2[code] } return } // getCompanyType 获取公司类型;央企、国企、央企下属、事业单位、民企 func getCompanyType(name string) (company_type string) { if name == "" { return } if yangMap[name] { company_type = "央企" return } if yangChildMap[name] { company_type = "央企下属" return } where := map[string]interface{}{ "company_name": name, } // special_enterprise 事业单位 enterprise, _ := Mgo181.FindOne("special_enterprise", where) if enterprise != nil && (len(*enterprise)) > 0 { company_type = "事业单位" return } //special_gov_unit 机关单位 gov, _ := Mgo181.FindOne("special_gov_unit", where) if gov != nil && (len(*gov)) > 0 { company_type = "党政机关" return } //special_social_organ 社会团体 soc, _ := Mgo181.FindOne("special_social_organ", where) if soc != nil && (len(*soc)) > 0 { company_type = "社会团体" return } //铁塔智联技术有限公司重庆市分公司 if strings.Contains(name, "铁塔") { if strings.Contains(name, "分公司") { company_type = "地方国企" return } else { company_type = "国企" return } } //company_base where["use_flag"] = 0 bases, _ := Mgo181.Find("company_base", where, nil, nil, false, -1, -1) if bases != nil && (len(*bases)) > 0 { for _, v := range *bases { if strings.Contains(util.ObjToString(v["company_status"]), "吊销") || strings.Contains(util.ObjToString(v["company_status"]), "注销") || util.ObjToString((v)["company_type"]) == "" { continue } else { bty := util.ObjToString((v)["company_type"]) if strings.Contains(bty, "国有独资") || strings.Contains(bty, "国有控股") || bty == "全民所有制" || bty == "集体所有制" || bty == "国有事业单位营业" || bty == "集体事业单位营业" || bty == "国有社团法人营业" || bty == "国有经营单位(非法人)" || bty == "集体经营单位(非法人)" { company_type = "国企" return } // 地方国企 //par_name := getTop(name, false) par_names := getTopNames(name) if len(par_names) > 0 { for _, par_name := range par_names { topwhere := map[string]interface{}{ "use_flag": 0, "company_status": map[string]interface{}{ "$nin": []string{"注销", "吊销", "吊销,已注销"}, }, "company_name": par_name, } top_bases, _ := Mgo181.FindOne("company_base", topwhere) if top_bases == nil && len(*top_bases) == 0 { if strings.Contains(par_name, "(") && strings.Contains(par_name, ")") { par_name = strings.ReplaceAll(par_name, ")", ")") par_name = strings.ReplaceAll(par_name, "(", "(") } } if top_bases != nil && len(*top_bases) > 0 { top_company_type := util.ObjToString((*top_bases)["company_type"]) if strings.Contains(top_company_type, "国有独资") || strings.Contains(top_company_type, "国有控股") || top_company_type == "全民所有制" || top_company_type == "集体所有制" || top_company_type == "国有事业单位营业" || top_company_type == "集体事业单位营业" || top_company_type == "国有社团法人营业" || top_company_type == "国有经营单位(非法人)" || top_company_type == "集体经营单位(非法人)" || top_company_type == "事业单位" { company_type = "地方国企" return } } else { // 去政府机关和事业单位查询 where2 := map[string]interface{}{ "company_name": par_name, } // special_enterprise 事业单位 enterprise, _ := Mgo181.FindOne("special_enterprise", where2) if enterprise != nil && (len(*enterprise)) > 0 { company_type = "地方国企" return } //special_gov_unit 机关单位 gov, _ := Mgo181.FindOne("special_gov_unit", where2) if gov != nil && (len(*gov)) > 0 { company_type = "地方国企" return } } } } } } } //company_type = "民企" return } // getMarketType 获取上市公司类型;A股、新三板、非上市 func getMarketType(name string) (stype string) { stype = "非上市" where := map[string]interface{}{ "company_name": name, "use_flag": 0, } bases, _ := Mgo181.Find("company_base", where, nil, nil, false, -1, -1) if bases != nil && (len(*bases)) > 0 { for _, v := range *bases { if strings.Contains(util.ObjToString(v["company_status"]), "吊销") || strings.Contains(util.ObjToString(v["company_status"]), "注销") { continue } else { list_code := util.ObjToString((v)["list_code"]) if list_code == "" { continue } //新三板股票代码 开头 if strings.HasPrefix(list_code, "43") || strings.HasPrefix(list_code, "83") || strings.HasPrefix(list_code, "87") || strings.HasPrefix(list_code, "88") { stype = "新三板" return } else { stype = "A股" return } } } } return } // getTop 获取最上级公司 func getTop(name string) (top string) { if name == "" { return } currName := name firstSearch := true visited := make(map[string]bool) visited[currName] = true for { topTmp := getParentsByPartner1(currName) topName := util.ObjToString(topTmp["stock_name"]) // 第一次查找就失败,直接返回空 if firstSearch && topName == "" { return } firstSearch = false // 非第一次查找,上级为空,返回当前获取到的上级 if topName == "" { return currName } // 避免循环:如果 topName 与 currName 相同,或 topName 已访问过,说明进入环路 if topName == currName || visited[topName] { return currName } // 如果 topName 不符合要求,比如不含“公司”,也返回当前 if !strings.Contains(topName, "公司") && !strings.Contains(topName, "集团") { return currName } // 更新当前公司名,继续向上找 currName = topName visited[currName] = true } } // getParentsByPartner1 返回投资比例50%以上的上级企业 func getParentsByPartner1(companyName string) (res map[string]interface{}) { q := map[string]interface{}{"company_name": companyName, "use_flag": 0, "is_history": 0, "is_personal": 0} info, _ := Mgo181.Find("company_partner", q, nil, nil, false, -1, -1) if info == nil || len(*info) == 0 { return } for _, v := range *info { // 不是法人企业,直接跳过 if !strings.Contains(util.ObjToString(v["stock_type"]), "法人") { continue } // 投资比例 必须 0.5以上 stock_proportion := util.Float64All(v["stock_proportion"]) if stock_proportion >= 0.5 { return v } } return } // getParentsByPartner 获取母公司,返回投资比例最大的企业 func getParentsByPartner(companyName string) (res map[string]interface{}) { q := map[string]interface{}{"company_name": companyName, "use_flag": 0, "is_history": 0, "is_personal": 0} info, _ := Mgo181.Find("company_partner", q, nil, nil, false, -1, -1) if info == nil || len(*info) == 0 { return } var maxProportion float64 for _, v := range *info { // 不是法人企业,直接跳过 if !strings.Contains(util.ObjToString(v["stock_type"]), "法人") { continue } // 获取比例 stockProportion := util.Float64All(v["stock_proportion"]) // 比较当前比例是否更大 if stockProportion > maxProportion { maxProportion = stockProportion res = v } } return } // getTopCompanyType 寻找上级企业 func getTopNames(name string) (res []string) { if name == "" { return } currName := name firstSearch := true visited := make(map[string]bool) visited[currName] = true for { topTmp := getParentsByPartner(currName) topName := util.ObjToString(topTmp["stock_name"]) // 第一次查找就失败,直接返回空 if firstSearch && topName == "" { return } firstSearch = false // 非第一次查找,上级为空,返回当前获取到的上级 if topName == "" { return } // 避免循环:如果 topName 与 currName 相同,或 topName 已访问过,说明进入环路 if topName == currName || visited[topName] { return } // 更新当前公司名,继续向上找 currName = topName visited[currName] = true res = append(res, topName) } }