2022-07-28 15:00:43 +00:00
|
|
|
package templategen
|
|
|
|
|
|
|
|
import (
|
2022-09-15 13:53:55 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/Pena-Co-Ltd/amocrm_templategen_back/amo"
|
|
|
|
docTemp "github.com/opencontrol/doc-template"
|
2022-07-28 15:00:43 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-09-15 13:53:55 +00:00
|
|
|
"time"
|
2022-07-28 15:00:43 +00:00
|
|
|
)
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
const (
|
|
|
|
TempDownloaded = "./tmp/downloaded"
|
|
|
|
TempGenerated = "./tmp/generated"
|
|
|
|
)
|
|
|
|
|
2022-11-12 10:58:54 +00:00
|
|
|
func GenerateDocName(name string) string {
|
2022-11-21 19:15:59 +00:00
|
|
|
return fmt.Sprintf("%v_%v.docx", name, time.Now().UnixNano())
|
2022-09-15 13:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateFile(filename string, data interface{}) error {
|
|
|
|
dc, err := docTemp.GetTemplate(TempDownloaded + "/" + filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.Parse()
|
|
|
|
|
|
|
|
return dc.Execute(TempGenerated+"/"+filename, data)
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
// 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,
|
2022-08-10 13:53:34 +00:00
|
|
|
"Название": data.Name,
|
2022-07-28 15:00:43 +00:00
|
|
|
"Бюджет": data.Price,
|
|
|
|
"Создатель": data.CreatedBy,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range AmoCustomFieldsToMap(data.CustomFieldsValues) {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:53:34 +00:00
|
|
|
func AmoContactsFieldsToRuMap(data []amo.Contact) []interface{} {
|
|
|
|
result := []interface{}{}
|
|
|
|
|
|
|
|
for _, contact := range data {
|
|
|
|
contactMap := map[string]interface{}{
|
|
|
|
"ФИО": contact.Name,
|
|
|
|
"Имя": contact.FirstName,
|
|
|
|
"Фамилия": contact.LastName,
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range AmoCustomFieldsToMap(contact.CustomFieldsValues) {
|
2022-08-10 13:53:34 +00:00
|
|
|
contactMap[k] = v
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
2022-08-10 13:53:34 +00:00
|
|
|
|
|
|
|
result = append(result, contactMap)
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:53:34 +00:00
|
|
|
func AmoCompaniesFieldsToRuMap(data []amo.Company) []interface{} {
|
|
|
|
result := []interface{}{}
|
|
|
|
for _, company := range data {
|
|
|
|
companyMap := map[string]interface{}{
|
|
|
|
"Название": company.Name,
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range AmoCustomFieldsToMap(company.CustomFieldsValues) {
|
2022-08-10 13:53:34 +00:00
|
|
|
companyMap[k] = v
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
2022-08-10 13:53:34 +00:00
|
|
|
|
|
|
|
result = append(result, companyMap)
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|