package main import ( "log" "math" "reflect" "regexp" "strconv" "strings" "time" ) var regString = `(\d+[.,]{0,}\d+)` var reg, _ = regexp.Compile(regString) // 从解析文档中提取全部项目信息 func getProjectItemFromFilejx() { //list, _ := db.Find(cf.YusuanInfo, `{"handle":{"$exists":1}}`, nil, nil, false, -1, -1) list, _ := db.Find(cf.YusuanInfo, `{ "file_path" : "2024年/北京/北京市/政府/2024_北京市经济和信息化局_1.xlsx" }`, nil, nil, false, -1, -1) pronum := 0 for k, data := range *list { tmp, _ := data["performance_result"].([]interface{}) if len(tmp) > 0 { for _, v := range tmp { t, _ := v.(map[string]interface{}) tmp3, _ := t["sheetdata"].([]interface{}) for _, v := range tmp3 { item, _ := v.(map[string]interface{}) budget, ok := item["budget"] if ok { ty := reflect.TypeOf(budget).Kind() switch ty { case reflect.Int: item["budget_f"] = float64(budget.(int)) case reflect.Int32: item["budget_f"] = float64(budget.(int32)) case reflect.String: budget_f := budget.(string) if strings.Contains(budget_f, ",") { budget_f = strings.ReplaceAll(budget_f, ",", "") } rs, _ := strconv.ParseFloat(budget_f, 64) item["budget_f"] = rs default: item["budget_f"] = budget } unit, _ := item["unit"].(string) if strings.Contains(unit, "万") { item["budget_f"] = math.Round(item["budget_f"].(float64) * 10000) } } else { total_amount, ok := item["total_amount"] if ok { ty := reflect.TypeOf(total_amount).Kind() switch ty { case reflect.Int: item["budget_f"] = float64(total_amount.(int)) case reflect.Int32: item["budget_f"] = float64(total_amount.(int32)) case reflect.String: budget_f := total_amount.(string) if strings.Contains(budget_f, ",") { budget_f = strings.ReplaceAll(budget_f, ",", "") } rs, _ := strconv.ParseFloat(budget_f, 64) item["budget_f"] = rs default: item["budget_f"] = total_amount } unit, _ := item["unit"].(string) if strings.Contains(unit, "万") { item["budget_f"] = math.Round(item["budget_f"].(float64) * 10000) } } } for _, f := range cf.Projectitem_field { item[f] = data[f] } pronum++ db.Save(cf.ProjectItem, item) } } } if k%1000 == 0 { log.Printf("加载数据量为:%d,生成项目数据量为:%d", k, pronum) } } } // 匹配客户项目信息 type Data struct { ismatch bool isnotmatch bool matchkey map[string]bool matchkey_field map[string]bool matchkey_add map[string]bool matchkey_add_field map[string]bool notmatchkey map[string]bool notmatchkey_field map[string]bool rule_index string } func getCustomerMatchProject() { list, _ := db.Find(cf.ProjectItem, nil, nil, nil, false, -1, -1) pipienum := 0 for k, tmp := range *list { d := &Data{ matchkey: map[string]bool{}, matchkey_field: map[string]bool{}, matchkey_add: map[string]bool{}, matchkey_add_field: map[string]bool{}, notmatchkey: map[string]bool{}, notmatchkey_field: map[string]bool{}, } for _, rule := range ucf.Rules { //关键词 mkey := strings.Split(rule.Match, ",") for _, field := range rule.MatchField.Field { content, _ := tmp[field].(string) //关键词匹配 for _, key := range mkey { keys := strings.Split(key, "&&") ismatch := true for _, v := range keys { if !strings.Contains(content, v) { ismatch = false break } } if ismatch { d.ismatch = true d.matchkey[key] = true d.matchkey_field[field] = true } } } //处理附件词、排除词 if d.ismatch { d.rule_index = rule.Index //附加词 addkey := strings.Split(rule.MatchAdd, ",") if len(addkey) > 0 { //附加词匹配 for _, key := range addkey { for _, field := range rule.MatchAddField.Field { content, _ := tmp[field].(string) if strings.Contains(content, key) { d.matchkey_add[key] = true d.matchkey_add_field[field] = true } } } } //排除词 notkey := strings.Split(rule.NotMatch, ",") if len(notkey) > 0 { //附加词匹配 for _, key := range notkey { for _, field := range rule.NotMatchField.Field { content, _ := tmp[field].(string) if strings.Contains(content, key) { d.notmatchkey[key] = true d.notmatchkey_field[field] = true } } } } } } if d.ismatch { //提取 pipienum++ tmp["matchkey"] = getKeyMap(d.matchkey) tmp["matchkey_filed"] = getKeyMap(d.matchkey_field) tmp["matchkey_add"] = getKeyMap(d.matchkey_add) tmp["matchkey_add_filed"] = getKeyMap(d.matchkey_add_field) tmp["matchkey_not"] = getKeyMap(d.notmatchkey) tmp["matchkey_not_filed"] = getKeyMap(d.notmatchkey_field) tmp["rule_index"] = d.rule_index tmp["comeintime"] = time.Now().Unix() db.Save(ucf.Totbale, tmp) } else { //不提取 } if k%1000 == 0 { log.Printf("加载数量为:%d,匹配数据量为:%d", k, pipienum) } } log.Println(ucf.User, pipienum, "匹配项目已处理完成") } func getKeyMap(m map[string]bool) string { ll := []string{} for k, _ := range m { ll = append(ll, k) } return strings.Join(ll, ",") } // 修复预算 func xiuyusuan() { list, _ := db.Find("project_hp", nil, nil, nil, false, -1, -1) for _, tmp := range *list { total_budget, ok := tmp["total_budget"].(string) if ok { if strings.Contains(total_budget, ",") { log.Println(total_budget) total_budget = strings.ReplaceAll(total_budget, ",", "") } budget, _ := strconv.ParseFloat(total_budget, 64) log.Println(total_budget, budget) unit, _ := tmp["unit"].(string) if strings.Contains(unit, "万") { budget = budget * 10000 } //log.Println(total_budget, budget, tmp["_id"]) db.Update( "project_hp", map[string]interface{}{"_id": tmp["_id"]}, map[string]interface{}{"$set": map[string]interface{}{"total_budget_f": budget}}, true, false, ) } } }