main.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/qor5/x/exchange"
  7. "gorm.io/driver/postgres"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/logger"
  10. )
  11. type Variant struct {
  12. gorm.Model
  13. Code string `gorm:"uniqueIndex;not null;"`
  14. ProductID uint
  15. ProductCode string
  16. Price uint64
  17. SellingPrice uint64
  18. Properties []*VariantPropertyShip
  19. }
  20. type VariantPropertyShip struct {
  21. ID uint `gorm:"primary_key"`
  22. CreatedAt time.Time
  23. UpdatedAt time.Time
  24. VariantID uint
  25. VariantPropertyID uint
  26. Value string `gorm:"size:512"`
  27. }
  28. type VariantProperty struct {
  29. gorm.Model
  30. Name string
  31. }
  32. type Product struct {
  33. ID uint `gorm:"primarykey"`
  34. Code string
  35. }
  36. func main() {
  37. var err error
  38. db, err := gorm.Open(postgres.Open(os.Getenv("DB_PARAMS")), &gorm.Config{
  39. DisableForeignKeyConstraintWhenMigrating: true,
  40. })
  41. if err != nil {
  42. panic(err)
  43. }
  44. db.Logger = db.Logger.LogMode(logger.Info)
  45. if err = db.AutoMigrate(&Variant{}, &VariantPropertyShip{}); err != nil {
  46. panic(err)
  47. }
  48. doImport := false
  49. doExport := false
  50. switch os.Args[1] {
  51. // import
  52. case "1":
  53. doImport = true
  54. // export
  55. case "2":
  56. doExport = true
  57. default:
  58. panic("add import/export param")
  59. }
  60. properties := []*VariantProperty{}
  61. if err = db.Find(&properties).Error; err != nil {
  62. panic(err)
  63. }
  64. propertiesNameMap := make(map[string]*VariantProperty)
  65. for i, _ := range properties {
  66. p := properties[i]
  67. propertiesNameMap[p.Name] = p
  68. }
  69. products := []*Product{}
  70. if err = db.Find(&products).Error; err != nil {
  71. panic(err)
  72. }
  73. productsCodeMap := make(map[string]*Product)
  74. for i, _ := range products {
  75. p := products[i]
  76. productsCodeMap[p.Code] = p
  77. }
  78. associations := []string{"Properties"}
  79. metas := []*exchange.Meta{
  80. exchange.NewMeta("Code").Header("JANコード").PrimaryKey(true),
  81. exchange.NewMeta("ProductCode").Header("品番").Setter(func(record interface{}, value string, metaValues exchange.MetaValues) error {
  82. r := record.(*Variant)
  83. r.ProductCode = value
  84. if r.ProductID == 0 {
  85. product, ok := productsCodeMap[value]
  86. if ok {
  87. r.ProductID = product.ID
  88. }
  89. }
  90. return nil
  91. }),
  92. exchange.NewMeta("Price").Header("上代1"),
  93. exchange.NewMeta("SellingPrice").Header("上代2"),
  94. exchange.NewMeta("ColorCode").Header("カラー").
  95. Setter(propertyMetaSetter(propertiesNameMap["ColorCode"].ID)).
  96. Valuer(propertyMetaValuer(propertiesNameMap["ColorCode"].ID)),
  97. exchange.NewMeta("SizeCode").Header("サイズ").
  98. Setter(propertyMetaSetter(propertiesNameMap["SizeCode"].ID)).
  99. Valuer(propertyMetaValuer(propertiesNameMap["SizeCode"].ID)),
  100. exchange.NewMeta("SeasonName").Header("シーズン").
  101. Setter(propertyMetaSetter(propertiesNameMap["SeasonName"].ID)).
  102. Valuer(propertyMetaValuer(propertiesNameMap["SeasonName"].ID)),
  103. exchange.NewMeta("FranceColorCode").Header("仏カラー").
  104. Setter(propertyMetaSetter(propertiesNameMap["FranceColorCode"].ID)).
  105. Valuer(propertyMetaValuer(propertiesNameMap["FranceColorCode"].ID)),
  106. exchange.NewMeta("SaleCornerOnly").Header("SaleCornerOnly").
  107. Setter(propertyMetaSetter(propertiesNameMap["SaleCornerOnly"].ID)).
  108. Valuer(propertyMetaValuer(propertiesNameMap["SaleCornerOnly"].ID)),
  109. exchange.NewMeta("Badge").Header("Badge").
  110. Setter(propertyMetaSetter(propertiesNameMap["Badge"].ID)).
  111. Valuer(propertyMetaValuer(propertiesNameMap["Badge"].ID)),
  112. exchange.NewMeta("AllowShipFromStore").Header("AllowShipFromStore").
  113. Setter(propertyMetaSetter(propertiesNameMap["AllowShipFromStore"].ID)).
  114. Valuer(propertyMetaValuer(propertiesNameMap["AllowShipFromStore"].ID)),
  115. }
  116. if doImport {
  117. importer := exchange.NewImporter(&Variant{}).Associations(associations...).Metas(metas...)
  118. f, err := os.Open(os.Args[2])
  119. if err != nil {
  120. panic(err)
  121. }
  122. defer f.Close()
  123. r, err := exchange.NewCSVReader(f)
  124. if err != nil {
  125. panic(err)
  126. }
  127. err = importer.Exec(db, r)
  128. if err != nil {
  129. panic(err)
  130. }
  131. }
  132. if doExport {
  133. exporter := exchange.NewExporter(&Variant{}).Associations(associations...).Metas(metas...)
  134. f, err := os.Create(fmt.Sprintf("variant-e%s.csv", time.Now().Format("200601021504")))
  135. if err != nil {
  136. panic(err)
  137. }
  138. defer f.Close()
  139. w, err := exchange.NewCSVWriter(f)
  140. if err != nil {
  141. panic(err)
  142. }
  143. err = exporter.Exec(db, w)
  144. if err != nil {
  145. panic(err)
  146. }
  147. }
  148. }
  149. func propertyMetaSetter(propertyID uint) exchange.MetaSetter {
  150. return func(record interface{}, value string, metaValues exchange.MetaValues) error {
  151. r := record.(*Variant)
  152. has := false
  153. for _, p := range r.Properties {
  154. if p.VariantPropertyID == propertyID {
  155. has = true
  156. p.Value = value
  157. break
  158. }
  159. }
  160. if !has {
  161. r.Properties = append(r.Properties, &VariantPropertyShip{
  162. VariantPropertyID: propertyID,
  163. Value: value,
  164. })
  165. }
  166. return nil
  167. }
  168. }
  169. func propertyMetaValuer(propertyID uint) exchange.MetaValuer {
  170. return func(record interface{}) (string, error) {
  171. r := record.(*Variant)
  172. for _, p := range r.Properties {
  173. if p.VariantPropertyID == propertyID {
  174. return p.Value, nil
  175. }
  176. }
  177. return "", nil
  178. }
  179. }