commit 50e482fbd4804127e9eef1222288e28c4aa05dac Author: Виталий Астахов Date: Sat Aug 1 13:56:33 2020 +0300 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8358a63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.csv +./out +*.exe \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4b527fc --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.macaw.me/inhosin/vvod + +go 1.14 diff --git a/main.go b/main.go new file mode 100644 index 0000000..e4d0b41 --- /dev/null +++ b/main.go @@ -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()) + } +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..bc259a2 --- /dev/null +++ b/main_test.go @@ -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") + // } + // }) +} diff --git a/models.go b/models.go new file mode 100644 index 0000000..33d6bee --- /dev/null +++ b/models.go @@ -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} +}