docxTemplater/templategen/templategen.go
Danil Solovyov 5ddc6b166e Init
2022-07-28 20:00:43 +05:00

102 lines
2.3 KiB
Go

package templategen
import (
"amocrm_templategen_back/amo"
"fmt"
"io"
"net/http"
"os"
)
// DownloadDocument - загружает документ в filepath из url
func DownloadDocument(filepath, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func DeleteDocument(filepath string) error {
return os.Remove(filepath)
}
func ShareDocument(filepath string) (error, string) {
return nil, ""
}
func AmoLeadFieldsToRuMap(data *amo.Lead) map[string]interface{} {
result := map[string]interface{}{
"ID": data.Id,
"Сделка": data.Name,
"Бюджет": data.Price,
"Создатель": data.CreatedBy,
}
for k, v := range AmoCustomFieldsToMap(data.CustomFieldsValues) {
result[k] = v
}
return result
}
func AmoContactsFieldsToRuMap(data []amo.Contact) map[string]interface{} {
result := map[string]interface{}{}
for i, contact := range data {
result = map[string]interface{}{
fmt.Sprintf("Контакт[ФИО][%v]", i): contact.Name,
fmt.Sprintf("Контакт[Имя][%v]", i): contact.FirstName,
fmt.Sprintf("Контакт[Фамилия][%v]", i): contact.LastName,
}
for k, v := range AmoCustomFieldsToMap(contact.CustomFieldsValues) {
result[fmt.Sprintf("Контакт[%v][%v]", k, i)] = v
}
}
return result
}
func AmoCustomFieldsToMap(data []amo.CustomField) map[string]interface{} {
result := map[string]interface{}{}
for _, field := range data {
switch len(field.Values) {
case 0:
result[field.FieldName] = ""
case 1:
result[field.FieldName] = field.Values[0].Value
default:
result[field.FieldName] = field.Values
}
}
return result
}
func AmoCompaniesFieldsToRuMap(data []amo.Company) map[string]interface{} {
result := map[string]interface{}{}
for i, company := range data {
result = map[string]interface{}{
fmt.Sprintf("Компания[Название][%v]", i): company.Name,
}
for k, v := range AmoCustomFieldsToMap(company.CustomFieldsValues) {
result[fmt.Sprintf("Компания[%v][%v]", k, i)] = v
}
}
return result
}