| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package cronjob
- import (
- "encoding/csv"
- "encoding/xml"
- "fmt"
- "kng_feed_api/helper"
- "kng_feed_api/model"
- "log"
- "math"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- )
- func CreateXMLFeed() {
- type Category struct {
- Text string `xml:",chardata"`
- Id string `xml:"id,attr"`
- ParentId string `xml:"parentId,attr"`
- URL string `xml:"url,attr"`
- }
- type Param struct {
- Text string `xml:",chardata"`
- Name string `xml:"name,attr"`
- }
- type Offer struct {
- Id string `xml:"id,attr"`
- Available string `xml:"available,attr"`
- Name string `xml:"name"`
- URL string `xml:"url"`
- CategoryId string `xml:"categoryId"`
- Vendor string `xml:"vendor"`
- Param []Param `xml:"param"`
- }
- type Shop struct {
- Name string `xml:"name"`
- URL string `xml:"url"`
- Category []Category `xml:"categories>category"`
- Offer []Offer `xml:"offers>offer"`
- }
- type YamlCatalog struct {
- XMLName xml.Name `xml:"yml_catalog"`
- Date string `xml:"date,attr"`
- Shop Shop `xml:"shop"`
- }
- fmt.Println("CRON JOB WORK - FROM EXTERNAL FILE ", time.Now())
- categoryRows, err := model.GetAllCategories()
- if err != nil {
- return
- }
- rows, err := model.GetAllProducts()
- if err != nil {
- return
- }
- var ymlCatalog = &YamlCatalog{}
- timeNow := time.Now().UTC()
- ymlCatalog.Date = timeNow.Format("2006-01-02 15:04")
- var shop = &Shop{}
- shop.Name = "ООО КНЯГИНЯ"
- shop.URL = "https://b2bn.kngnn.ru"
- for _, value := range categoryRows {
- var category = Category{
- Text: value.Name,
- ParentId: value.ParentId,
- Id: value.Id,
- URL: value.Id,
- }
- shop.Category = append(shop.Category, category)
- }
- var categoryCrossDocking = Category{
- Text: "Кросс-Докинг",
- ParentId: "1",
- Id: "999",
- URL: "999",
- }
- shop.Category = append(shop.Category, categoryCrossDocking)
- for _, value := range rows {
- var offer = Offer{
- Id: value.CodeCarCaDe,
- Available: "true",
- Name: value.Name,
- URL: "https://b2bn.kngnn.ru/?item-code=" + value.CodeUT10,
- CategoryId: value.CategoryId,
- Vendor: value.Manufacturer,
- }
- if offer.CategoryId == "" {
- offer.CategoryId = categoryCrossDocking.Id
- offer.Param = append(offer.Param, Param{
- Text: "1",
- Name: "cross_docking",
- })
- } else {
- offer.Param = append(offer.Param, Param{
- Text: "0",
- Name: "cross_docking",
- })
- }
- offer.Param = append(offer.Param, Param{
- Text: value.CodeUT10,
- Name: "code",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.CodeUT10,
- Name: "code_ut10",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.Manufacturer,
- Name: "manufacturer",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.ArticleNumber,
- Name: "article_number",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.Brand,
- Name: "brand",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.Unit,
- Name: "unit",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.GroupLimit,
- Name: "group_limit",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.GroupPrice,
- Name: "group_price",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.NumberCatalog,
- Name: "number_catalog",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.NumberDrawing,
- Name: "number_drawing",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.NumberBrand,
- Name: "number_brand",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.NumberArticle,
- Name: "number_article",
- })
- offer.Param = append(offer.Param, Param{
- Text: value.NumberSuffix,
- Name: "number_suffix",
- })
- offer.Param = append(offer.Param, Param{
- Text: strconv.FormatBool(value.Deficit),
- Name: "deficit",
- })
- offer.Param = append(offer.Param, Param{
- Text: strconv.FormatFloat(math.Round(value.Width*100)/100, 'f', 6, 64),
- Name: "width",
- })
- offer.Param = append(offer.Param, Param{
- Text: strconv.FormatFloat(math.Round(value.Height*100)/100, 'f', 6, 64),
- Name: "height",
- })
- offer.Param = append(offer.Param, Param{
- Text: strconv.FormatFloat(math.Round(value.Weight*100)/100, 'f', 6, 64),
- Name: "weight",
- })
- offer.Param = append(offer.Param, Param{
- Text: strconv.FormatFloat(math.Round(value.Length*100)/100, 'f', 6, 64),
- Name: "length",
- })
- offer.Param = append(offer.Param, Param{
- Text: strconv.FormatFloat(math.Round(value.Multiple*100)/100, 'f', 6, 64),
- Name: "multiple",
- })
- if value.IdNumbers != nil {
- numbers := strings.Split(*value.IdNumbers, ";")
- for idx, num := range numbers {
- offer.Param = append(offer.Param, Param{
- Text: num,
- Name: "article_number" + strconv.Itoa(idx+1),
- })
- }
- }
- if value.Certificates != nil {
- certificates := strings.Split(*value.Certificates, ";")
- for idx, cert := range certificates {
- offer.Param = append(offer.Param, Param{
- Text: cert,
- Name: "certificate" + strconv.Itoa(idx+1),
- })
- }
- }
- shop.Offer = append(shop.Offer, offer)
- }
- ymlCatalog.Shop = *shop
- byteXmlText, err := xml.MarshalIndent(ymlCatalog, " ", " ")
- if err != nil {
- log.Printf(err.Error())
- } else {
- var Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
- var byteXmlTextWithHeader = []byte(Header + string(byteXmlText))
- err := os.WriteFile(filepath.Join(helper.RootDir(), "feed.xml"), byteXmlTextWithHeader, 0666)
- if err != nil {
- log.Printf(err.Error())
- }
- }
- }
- func CreateCSVDeltaFeed() {
- log.Println("DELTA FEED WORK - FROM EXTERNAL FILE ", helper.RootDir())
- rows, err := model.GetAllAvailability()
- if err != nil {
- log.Printf("Не удалось получить данные для формирования дельта-фида из базы по причине [%s]", err)
- return
- }
- file, err := os.Create(filepath.Join(helper.RootDir(), "delta.csv"))
- if err != nil {
- log.Printf("Не удалось сформировать пустой файл дельта-фида по причине [%s]", err)
- return
- }
- defer file.Close()
- csvWriter := csv.NewWriter(file)
- csvWriter.Comma = ';'
- defer csvWriter.Flush()
- var records [][]string
- for _, row := range rows {
- var record []string
- record = append(record, row.CodeCarCaDe)
- record = append(record, "1")
- record = append(record, strconv.Itoa(row.Available))
- records = append(records, record)
- }
- writeError := csvWriter.WriteAll(records)
- if writeError != nil {
- log.Printf("Не удалось сохранить файл дельта-фида на диск по причине [%s]", writeError)
- return
- }
- }
|