| 584 | } |
| 585 | |
| 586 | func (i *ZipCodeImporter) importCPC(file string) (int, error) { |
| 587 | f, err := os.Open(file) |
| 588 | if err != nil { |
| 589 | return 0, err |
| 590 | } |
| 591 | defer f.Close() |
| 592 | |
| 593 | decoder := transform.NewReader(f, charmap.ISO8859_1.NewDecoder()) |
| 594 | reader := csv.NewReader(decoder) |
| 595 | reader.Comma = '@' |
| 596 | reader.LazyQuotes = true |
| 597 | |
| 598 | wb := db.NewWriteBatch() |
| 599 | defer wb.Cancel() |
| 600 | |
| 601 | count := 0 |
| 602 | batchSize := 5000 |
| 603 | |
| 604 | for { |
| 605 | record, err := reader.Read() |
| 606 | if err == io.EOF { |
| 607 | break |
| 608 | } |
| 609 | if err != nil { |
| 610 | continue |
| 611 | } |
| 612 | // 0 CPC_NU, 1 UFE_SG, 2 LOC_NU, 3 CPC_NO, 4 CPC_ENDERECO, 5 CEP |
| 613 | if len(record) < 6 { |
| 614 | continue |
| 615 | } |
| 616 | cep := i.normalizeCEP(strings.TrimSpace(record[5])) |
| 617 | if cep == "" { |
| 618 | continue |
| 619 | } |
| 620 | localityCode := strings.TrimSpace(record[2]) |
| 621 | cpcName := strings.TrimSpace(record[3]) |
| 622 | cpcAddress := strings.TrimSpace(record[4]) |
| 623 | |
| 624 | cityName := "" |
| 625 | uf := "" |
| 626 | ibgeCode := "" |
| 627 | if l, ok := localities[localityCode]; ok { |
| 628 | cityName = l.Nome |
| 629 | uf = l.UF |
| 630 | ibgeCode = l.CodigoIBGE |
| 631 | } |
| 632 | |
| 633 | cepComplete := CEPCompleto{ |
| 634 | CEP: cep, |
| 635 | Logradouro: cpcAddress, |
| 636 | Complemento: "", |
| 637 | Bairro: "", |
| 638 | Cidade: cityName, |
| 639 | UF: uf, |
| 640 | CodigoIBGE: ibgeCode, |
| 641 | TipoOrigem: "cpc", |
| 642 | NomeOrigem: cpcName, |
| 643 | } |