cronJobs.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package cronjob
  2. import (
  3. "encoding/csv"
  4. "encoding/xml"
  5. "fmt"
  6. "kng_feed_api/helper"
  7. "kng_feed_api/model"
  8. "log"
  9. "math"
  10. "os"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func CreateXMLFeed() {
  17. type Category struct {
  18. Text string `xml:",chardata"`
  19. Id string `xml:"id,attr"`
  20. ParentId string `xml:"parentId,attr"`
  21. URL string `xml:"url,attr"`
  22. }
  23. type Param struct {
  24. Text string `xml:",chardata"`
  25. Name string `xml:"name,attr"`
  26. }
  27. type Offer struct {
  28. Id string `xml:"id,attr"`
  29. Available string `xml:"available,attr"`
  30. Name string `xml:"name"`
  31. URL string `xml:"url"`
  32. CategoryId string `xml:"categoryId"`
  33. Vendor string `xml:"vendor"`
  34. Param []Param `xml:"param"`
  35. }
  36. type Shop struct {
  37. Name string `xml:"name"`
  38. URL string `xml:"url"`
  39. Category []Category `xml:"categories>category"`
  40. Offer []Offer `xml:"offers>offer"`
  41. }
  42. type YamlCatalog struct {
  43. XMLName xml.Name `xml:"yml_catalog"`
  44. Date string `xml:"date,attr"`
  45. Shop Shop `xml:"shop"`
  46. }
  47. fmt.Println("CRON JOB WORK - FROM EXTERNAL FILE ", time.Now())
  48. categoryRows, err := model.GetAllCategories()
  49. if err != nil {
  50. return
  51. }
  52. rows, err := model.GetAllProducts()
  53. if err != nil {
  54. return
  55. }
  56. var ymlCatalog = &YamlCatalog{}
  57. timeNow := time.Now().UTC()
  58. ymlCatalog.Date = timeNow.Format("2006-01-02 15:04")
  59. var shop = &Shop{}
  60. shop.Name = "ООО КНЯГИНЯ"
  61. shop.URL = "https://b2bn.kngnn.ru"
  62. for _, value := range categoryRows {
  63. var category = Category{
  64. Text: value.Name,
  65. ParentId: value.ParentId,
  66. Id: value.Id,
  67. URL: value.Id,
  68. }
  69. shop.Category = append(shop.Category, category)
  70. }
  71. var categoryCrossDocking = Category{
  72. Text: "Кросс-Докинг",
  73. ParentId: "1",
  74. Id: "999",
  75. URL: "999",
  76. }
  77. shop.Category = append(shop.Category, categoryCrossDocking)
  78. for _, value := range rows {
  79. var offer = Offer{
  80. Id: value.CodeCarCaDe,
  81. Available: "true",
  82. Name: value.Name,
  83. URL: "https://b2bn.kngnn.ru/?item-code=" + value.CodeUT10,
  84. CategoryId: value.CategoryId,
  85. Vendor: value.Manufacturer,
  86. }
  87. if offer.CategoryId == "" {
  88. offer.CategoryId = categoryCrossDocking.Id
  89. offer.Param = append(offer.Param, Param{
  90. Text: "1",
  91. Name: "cross_docking",
  92. })
  93. } else {
  94. offer.Param = append(offer.Param, Param{
  95. Text: "0",
  96. Name: "cross_docking",
  97. })
  98. }
  99. offer.Param = append(offer.Param, Param{
  100. Text: value.CodeUT10,
  101. Name: "code",
  102. })
  103. offer.Param = append(offer.Param, Param{
  104. Text: value.CodeUT10,
  105. Name: "code_ut10",
  106. })
  107. offer.Param = append(offer.Param, Param{
  108. Text: value.Manufacturer,
  109. Name: "manufacturer",
  110. })
  111. offer.Param = append(offer.Param, Param{
  112. Text: value.ArticleNumber,
  113. Name: "article_number",
  114. })
  115. offer.Param = append(offer.Param, Param{
  116. Text: value.Brand,
  117. Name: "brand",
  118. })
  119. offer.Param = append(offer.Param, Param{
  120. Text: value.Unit,
  121. Name: "unit",
  122. })
  123. offer.Param = append(offer.Param, Param{
  124. Text: value.GroupLimit,
  125. Name: "group_limit",
  126. })
  127. offer.Param = append(offer.Param, Param{
  128. Text: value.GroupPrice,
  129. Name: "group_price",
  130. })
  131. offer.Param = append(offer.Param, Param{
  132. Text: value.NumberCatalog,
  133. Name: "number_catalog",
  134. })
  135. offer.Param = append(offer.Param, Param{
  136. Text: value.NumberDrawing,
  137. Name: "number_drawing",
  138. })
  139. offer.Param = append(offer.Param, Param{
  140. Text: value.NumberBrand,
  141. Name: "number_brand",
  142. })
  143. offer.Param = append(offer.Param, Param{
  144. Text: value.NumberArticle,
  145. Name: "number_article",
  146. })
  147. offer.Param = append(offer.Param, Param{
  148. Text: value.NumberSuffix,
  149. Name: "number_suffix",
  150. })
  151. offer.Param = append(offer.Param, Param{
  152. Text: strconv.FormatBool(value.Deficit),
  153. Name: "deficit",
  154. })
  155. offer.Param = append(offer.Param, Param{
  156. Text: strconv.FormatFloat(math.Round(value.Width*100)/100, 'f', 6, 64),
  157. Name: "width",
  158. })
  159. offer.Param = append(offer.Param, Param{
  160. Text: strconv.FormatFloat(math.Round(value.Height*100)/100, 'f', 6, 64),
  161. Name: "height",
  162. })
  163. offer.Param = append(offer.Param, Param{
  164. Text: strconv.FormatFloat(math.Round(value.Weight*100)/100, 'f', 6, 64),
  165. Name: "weight",
  166. })
  167. offer.Param = append(offer.Param, Param{
  168. Text: strconv.FormatFloat(math.Round(value.Length*100)/100, 'f', 6, 64),
  169. Name: "length",
  170. })
  171. offer.Param = append(offer.Param, Param{
  172. Text: strconv.FormatFloat(math.Round(value.Multiple*100)/100, 'f', 6, 64),
  173. Name: "multiple",
  174. })
  175. if value.IdNumbers != nil {
  176. numbers := strings.Split(*value.IdNumbers, ";")
  177. for idx, num := range numbers {
  178. offer.Param = append(offer.Param, Param{
  179. Text: num,
  180. Name: "article_number" + strconv.Itoa(idx+1),
  181. })
  182. }
  183. }
  184. if value.Certificates != nil {
  185. certificates := strings.Split(*value.Certificates, ";")
  186. for idx, cert := range certificates {
  187. offer.Param = append(offer.Param, Param{
  188. Text: cert,
  189. Name: "certificate" + strconv.Itoa(idx+1),
  190. })
  191. }
  192. }
  193. shop.Offer = append(shop.Offer, offer)
  194. }
  195. ymlCatalog.Shop = *shop
  196. byteXmlText, err := xml.MarshalIndent(ymlCatalog, " ", " ")
  197. if err != nil {
  198. log.Printf(err.Error())
  199. } else {
  200. var Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
  201. var byteXmlTextWithHeader = []byte(Header + string(byteXmlText))
  202. err := os.WriteFile(filepath.Join(helper.RootDir(), "feed.xml"), byteXmlTextWithHeader, 0666)
  203. if err != nil {
  204. log.Printf(err.Error())
  205. }
  206. }
  207. }
  208. func CreateCSVDeltaFeed() {
  209. log.Println("DELTA FEED WORK - FROM EXTERNAL FILE ", helper.RootDir())
  210. rows, err := model.GetAllAvailability()
  211. if err != nil {
  212. log.Printf("Не удалось получить данные для формирования дельта-фида из базы по причине [%s]", err)
  213. return
  214. }
  215. file, err := os.Create(filepath.Join(helper.RootDir(), "delta.csv"))
  216. if err != nil {
  217. log.Printf("Не удалось сформировать пустой файл дельта-фида по причине [%s]", err)
  218. return
  219. }
  220. defer file.Close()
  221. csvWriter := csv.NewWriter(file)
  222. csvWriter.Comma = ';'
  223. defer csvWriter.Flush()
  224. var records [][]string
  225. for _, row := range rows {
  226. var record []string
  227. record = append(record, row.CodeCarCaDe)
  228. record = append(record, "1")
  229. record = append(record, strconv.Itoa(row.Available))
  230. records = append(records, record)
  231. }
  232. writeError := csvWriter.WriteAll(records)
  233. if writeError != nil {
  234. log.Printf("Не удалось сохранить файл дельта-фида на диск по причине [%s]", writeError)
  235. return
  236. }
  237. }