This commit is contained in:
Виталий Астахов 2020-08-01 13:56:33 +03:00
commit 50e482fbd4
5 changed files with 210 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.csv
./out
*.exe

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.macaw.me/inhosin/vvod
go 1.14

151
main.go Normal file
View File

@ -0,0 +1,151 @@
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
)
var (
// TOKEN token
TOKEN = "08bb51ff-1eb6-4167-8ed2-eb19fc343703"
// MICROSERVICEURL url to microservie
MICROSERVICEURL = "http://microservice01:8081/api/v1/documents/create?crptDocType=LP_INTRODUCE_OST"
)
// Hello ...
func Hello(name string) string {
if name == "" {
name = "World"
}
return fmt.Sprintf("Hello, %s", name)
}
// ParseFile ...
func ParseFile(fileName string) ([]string, error) {
if fileName == "" {
return nil, errors.New("Empty file path")
}
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
// SplitSlice ...
func SplitSlice(lines []string) error {
l := len(lines)
max := 20
if l >= max {
filesNum := l / max
if l%max > 0 {
filesNum++
}
files := make([]string, filesNum)
start := 0
end := max
for i := 1; i <= filesNum; i++ {
if start+max <= filesNum {
end = start + max
} else {
end = start + l%max
}
fName := fmt.Sprintf("./out/part_%d.csv", i)
if err := MakePartFile(lines[start:end], fName); err != nil {
return err
}
files = append(files, fName)
}
for _, file := range files {
SendToMicroservice(file)
}
}
return nil
}
// SendToMicroservice ...
func SendToMicroservice(fileName string) error {
lines, err := ParseFile(fileName)
if err != nil {
return err
}
products := make([]Cis, 0)
for _, line := range lines {
if line != "" {
cis := NewCis(line)
products = append(products, cis)
}
}
document := NewDocument("122323", products)
data, err := json.Marshal(document)
if err != nil {
return err
}
req, err := http.NewRequest("POST", MICROSERVICEURL, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("token", TOKEN)
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
return nil
}
// MakePartFile ...
func MakePartFile(lines []string, fName string) error {
file, err := os.OpenFile(fName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer file.Close()
for _, line := range lines {
if _, err := file.WriteString(fmt.Sprintln(line)); err != nil {
return err
}
}
return nil
}
func main() {
Hello("")
lines, err := ParseFile("codes.csv")
if err != nil {
fmt.Println(err.Error())
}
if err = SplitSlice(lines); err != nil {
fmt.Println(err.Error())
}
}

26
main_test.go Normal file
View File

@ -0,0 +1,26 @@
package main
import "testing"
func TestParseFile(t *testing.T) {
// assertCorrectMessage := func(t *testing.T, got, want string) {
// t.Helper()
// if got != want {
// t.Errorf("got %q want %q", got, want)
// }
// }
// t.Run("Error is fileName os empty", func(t *testing.T) {
// got := ParseFile("").Error()
// want := "Empty file path"
// assertCorrectMessage(t, got, want)
// })
// t.Run("No error", func(t *testing.T) {
// got := ParseFile("test.csv")
// if got != nil {
// assertCorrectMessage(t, "nil", "nil")
// }
// })
}

27
models.go Normal file
View File

@ -0,0 +1,27 @@
package main
// Cis ...
type Cis struct {
Ki string `json:"ki"`
}
// Document ...
type Document struct {
DocNum string `json:"docnumber"`
Inn string `json:"trade_participant_inn"`
ProductsList []Cis `json:"products_list"`
}
// NewDocument ...
func NewDocument(docNum string, products []Cis) *Document {
return &Document{
DocNum: docNum,
Inn: "5042013346",
ProductsList: products,
}
}
// NewCis ...
func NewCis(cis string) Cis {
return Cis{Ki: cis}
}