| 501 | } |
| 502 | |
| 503 | func (i *ZipCodeImporter) importOperationalUnits(file string) (int, error) { |
| 504 | f, err := os.Open(file) |
| 505 | if err != nil { |
| 506 | return 0, err |
| 507 | } |
| 508 | defer f.Close() |
| 509 | |
| 510 | decoder := transform.NewReader(f, charmap.ISO8859_1.NewDecoder()) |
| 511 | reader := csv.NewReader(decoder) |
| 512 | reader.Comma = '@' |
| 513 | reader.LazyQuotes = true |
| 514 | |
| 515 | wb := db.NewWriteBatch() |
| 516 | defer wb.Cancel() |
| 517 | |
| 518 | count := 0 |
| 519 | batchSize := 5000 |
| 520 | |
| 521 | for { |
| 522 | record, err := reader.Read() |
| 523 | if err == io.EOF { |
| 524 | break |
| 525 | } |
| 526 | if err != nil { |
| 527 | continue |
| 528 | } |
| 529 | // 0 UOP_NU, 1 UFE_SG, 2 LOC_NU, 3 BAI_NU, 4 LOG_NU, 5 UOP_NO, 6 UOP_ENDERECO, 7 CEP, 8 UOP_IN_CP, 9 UOP_NO_ABREV |
| 530 | if len(record) < 8 { |
| 531 | continue |
| 532 | } |
| 533 | cep := i.normalizeCEP(strings.TrimSpace(record[7])) |
| 534 | if cep == "" { |
| 535 | continue |
| 536 | } |
| 537 | districtCode := strings.TrimSpace(record[3]) |
| 538 | localityCode := strings.TrimSpace(record[2]) |
| 539 | uopName := strings.TrimSpace(record[5]) |
| 540 | uopAddress := strings.TrimSpace(record[6]) |
| 541 | |
| 542 | districtName := "" |
| 543 | if d, ok := districts[districtCode]; ok { |
| 544 | districtName = d.Nome |
| 545 | } |
| 546 | cityName := "" |
| 547 | uf := "" |
| 548 | ibgeCode := "" |
| 549 | if l, ok := localities[localityCode]; ok { |
| 550 | cityName = l.Nome |
| 551 | uf = l.UF |
| 552 | ibgeCode = l.CodigoIBGE |
| 553 | } |
| 554 | |
| 555 | cepComplete := CEPCompleto{ |
| 556 | CEP: cep, |
| 557 | Logradouro: uopAddress, |
| 558 | Complemento: "", |
| 559 | Bairro: districtName, |
| 560 | Cidade: cityName, |