Class: SapModel::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/sap_model/product.rb

Class Method Summary collapse

Class Method Details

.find_product(source, id) ⇒ Object

Note:

根据来源,id获得单个商品

根据来源,id获得单个商品

Parameters:

  • source (string)


18
19
20
# File 'lib/sap_model/product.rb', line 18

def self.find_product(source, id)
  Sap::Product.new(source).find(id)
end

.find_product_attachment(source, id, attachment_id) ⇒ Object

Note:

根据来源,id获得单个商品图片

根据来源,id获得单个商品图片

Parameters:

  • source (string)


30
31
32
# File 'lib/sap_model/product.rb', line 30

def self.find_product_attachment(source, id, attachment_id)
  Sap::Product.new(source).find_attachment(id, attachment_id)
end

.get_products(source) ⇒ Object

Note:

根据来源获得所有商品

根据来源获得所有商品

Parameters:

  • source (string)


10
11
12
# File 'lib/sap_model/product.rb', line 10

def self.get_products(source)
  Sap::Product.new(source).list
end

.new_or_update_all_product_infoObject

Note:

根据来源从sap接口获得的商品保存与更新在云店家和上下架信(定时任务)

根据来源从sap接口获得的商品保存与更新在云店家和上下架信息(定时任务)



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sap_model/product.rb', line 178

def self.new_or_update_all_product_info
  shop_ids_map = Sap::Product.new('').check_function_and_shop
  shop_ids_map.keys.each do |k|
    shop_ids = shop_ids_map[k].each do |shop_id|
      Rails.logger.info "new_or_update_all_product_info.....来源#{k}定时任务开始"
      self.new_or_update_all_products_by_source(k, shop_id)
      SapModel::SalesChannel.update_products_status(k, shop_id)
      Rails.logger.info "new_or_update_all_product_info.....来源#{k}定时任务结束"
    end
  end
end

.new_or_update_all_products_by_source(source, shop_id) ⇒ Object

Note:

根据来源从sap接口获得的所有商品保存在云店家

根据来源从sap接口获得的所有商品保存在云店家

Parameters:

  • source (string)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sap_model/product.rb', line 96

def self.new_or_update_all_products_by_source(source, shop_id)
  products = Sap::Product.new(source).list
  products.each do |product|
    params = self.query(product, source, shop_id)
    sku_ids = product.try(:skus).map(&:id)
    specification = []
    if sku_ids.present?
      sku_ids.each do |id|
        sku = SapModel::Sku.find_sku(source,id)
        options = SapModel::Sku.query(sku, source)
        specification = specification<<options
        Rails.logger.info "........specification: #{specification}......"

      end

    end
    self.new_or_update_product(source, product, params, specification)
  end
end

.new_or_update_product(source, product, params, specification) ⇒ Object

Note:

根据来源从sap接口获得的商品保存在云店家(子方法)

根据来源从sap接口获得的商品保存在云店家(子方法)

Parameters:

  • source (string)
  • sap_product (sap_product)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/sap_model/product.rb', line 120

def self.new_or_update_product(source, product, params, specification)
  yun_product = ::Product.where(art_no: product.try(:code)).last
  # product_category_grade_1 = ProductCategory.where(source: source, grade: ProductCategory::Grade::THREE).last.try(:id)
  category_name = product.try(:category).try(:name)
  category = ProductCategory.where(source: source, name: category_name).last
  default_category = ProductCategory.where(source: source, grade: ProductCategory::Grade::ONE).last
  product_category1_id = default_category.try(:id)
  product_category2_id = nil
  product_category_id = nil
  if category.present?
    grade = category.try(:grade)
    if grade == ProductCategory::Grade::ONE
      product_category1_id = category.try(:id)
    elsif grade == ProductCategory::Grade::TWO
      product_category2_id = category.try(:id)
      product_category1_id = category.parent.try(:id)
    elsif grade == ProductCategory::Grade::THREE
      product_category_id = category.try(:id)
      product_category2_id = category.parent.try(:id)
      product_category1_id = category.parent.parent.try(:id)

    end
  end
  Rails.logger.info "#product_category1_id#{product_category1_id}"
  # Rails.logger.info "#product_category2_id#{product_category2_id}"
  # Rails.logger.info "#product_category_id#{product_category_id}"

  options = {
      product_category_grade_1: product_category1_id
  }
  if product_category2_id.present?
    options.merge!(product_category_grade_2: product_category2_id)
  end
  if product_category_id.present?
    options.merge!(product_category_grade_3: product_category_id)
  end


  if yun_product.present?
    params = {product: params, specification: specification, id: yun_product.id}
    is_sap_product = true
    ::Product.update_product_for_fesco(params, options, is_sap_product)
    yun_product.total_inventory_quantity = yun_product.try(:product_variants).map{|p| p.inventory_quantity}.sum
    yun_product.save!
    Rails.logger.info ".....update_sap_product........."
  else
    result, product =::Product.add_product_for_fesco(params, options, specification)
    product.total_inventory_quantity = product.try(:product_variants).map{|p| p.inventory_quantity}.sum
    product.save!
    Rails.logger.info ".....add_sap_product........."
    ::Product.create!(options)
  end
rescue => e
  yloge e, "更新' 商品code #{product.try(:code)},art_no#{product.try(:art_no)}, 失败"
end

.new_or_update_product_by_source_and_id(source, id, shop_id) ⇒ Object

Note:

根据来源从sap接口获得的商品和商品规格保存在云店家

根据来源从sap接口获得的商品和商品规格保存在云店家

Parameters:

  • source (string)
  • id (Integer)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sap_model/product.rb', line 63

def self.new_or_update_product_by_source_and_id(source, id, shop_id)
  product = Sap::Product.new(source).find(id)
  Rails.logger.info "#{product}...product"
  if product.kind_of? Hash
  # products.each do |product|
    params = self.query(product, source, shop_id)
    sku_ids = product.try(:skus).map(&:id)
    specification = []
    total_inventory_quantity = 0
    if sku_ids.present?
      sku_ids.each do |id|
        sku = SapModel::Sku.find_sku(source, id)
        options = SapModel::Sku.query(sku, source)
        specification = specification<<options
        total_inventory_quantity += options[:inventory_quantity]
        Rails.logger.info "........specification: #{specification}......"
        Rails.logger.info "total_inventory_quantity...........#{total_inventory_quantity}"

      end
    end
    Rails.logger.info "paramsaaa...........#{params}"
    params.merge!(total_inventory_quantity: total_inventory_quantity)
    Rails.logger.info "paramsbbb...........#{params}"
    self.new_or_update_product(source, product, params, specification)
  # end
  end
rescue => e
  yloge e, "更新' 商品失败 "
end

.post_disableVariant(source, id) ⇒ Object



22
23
24
25
# File 'lib/sap_model/product.rb', line 22

def self.post_disableVariant(source, id)
  Sap::Product.new(source).posts(id)

end

.query(product, source, shop_id) ⇒ Object

Note:

为从sap接口获得的商品和商品规格保存在云店家分装参数

为从sap接口获得的商品和商品规格保存在云店家分装参数

Parameters:

  • sap_product (sap_product)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sap_model/product.rb', line 37

def self.query(product, source, shop_id)
  # if product.try(:status) == 'Active'
  #   status = ::Product::Status::Published
  # else
  #   status = ::Product::Status::Closed
  # end
  # shop_id = Shop.where(source: source, shop_type: Shop::ShopType::SHOP, status: Shop::Status::Open).last.id

  {
      art_no: product.try(:code),
      name: product.try(:name),
      product_type: product.try(:product_type),
      usefor: ProductDetail::Usefor::Product_Intro,
      source: source,
      shop_id: shop_id,
      settling_accounts_type: ::Product::SettlingAccountsType::CASH_AND_POINT,
      act_id: '0'   #sap商品先设为包邮的

  }

end