|
|
@@ -4,13 +4,87 @@ class Product < ActiveRecord::Base
|
|
|
belongs_to :product_cat, :foreign_key => :category_id
|
|
|
belongs_to :merchant, :foreign_key => :merchant_id
|
|
|
validates :name,:buy_price,:price,:category_id,:count,:robo_balance_price, presence: true
|
|
|
- attr_accessor :v_share_img
|
|
|
+ attr_accessor :v_share_img,:get_size_enum,:gross_interest_rate
|
|
|
+ after_find :get_size_enum
|
|
|
+ after_create :after_create
|
|
|
+ #after_update :after_update
|
|
|
+ before_save :before_save
|
|
|
+ after_destroy :del_picture
|
|
|
+
|
|
|
+ def after_create
|
|
|
+ #主商品默认关联商品
|
|
|
+ if self.show_flag
|
|
|
+ self.relate_product_id = self.id
|
|
|
+ self.save
|
|
|
+ end
|
|
|
+ end
|
|
|
+ def before_save
|
|
|
+ #主商品默认关联商品
|
|
|
+ if self.show_flag
|
|
|
+ self.relate_product_id = self.id
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ def del_picture
|
|
|
+ # 删除商品图片
|
|
|
+ pictures = ProductPicture.find_by_sql("select * from product_pictures where pic_type=0 and product_id = #{self.id}")
|
|
|
+ # 创建商品图片
|
|
|
+ pictures.each do |u|
|
|
|
+ u.delete
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
|
|
|
TYPE_ENUM = [["直营","direct_sale"]]
|
|
|
#["代销","user_sale"]
|
|
|
+ SIZE_ENUM = []
|
|
|
+ COLOR_ENUM = []
|
|
|
+ def get_size_enum
|
|
|
+ SIZE_ENUM.clear
|
|
|
+ if SIZE_ENUM.length ==0 && !self.id.nil?
|
|
|
+ linkSize = ProductAttrConfig.where("product_id=? and size_type='size'",self.relate_product_id).first
|
|
|
+ if !linkSize.blank?
|
|
|
+ productAttrs = ProductAttr.where("attr_key_id=? and status=1 ",linkSize.attr_key_id).order("recommend desc")
|
|
|
+ productAttrs.each do |attr|
|
|
|
+ a=[attr.name,attr.id]
|
|
|
+ SIZE_ENUM.push(a)
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ COLOR_ENUM.clear
|
|
|
+ if COLOR_ENUM.length==0 && !self.id.nil?
|
|
|
+ linkColor = ProductAttrConfig.where("product_id=? and size_type='color'",self.relate_product_id).first
|
|
|
+ if !linkColor.blank?
|
|
|
+ productColorAttrs = ProductAttr.where("attr_key_id=? and status=1",linkColor.attr_key_id).order("recommend desc")
|
|
|
+ productColorAttrs.each do |color_attr|
|
|
|
+ b=[color_attr.name,color_attr.id]
|
|
|
+ COLOR_ENUM.push(b)
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
|
|
|
IMG_STORE_PATH = "product"
|
|
|
|
|
|
+ def size_name
|
|
|
+ linkSize = ProductAttr.where("id = ?", self.size_id).first
|
|
|
+ if !linkSize.blank?
|
|
|
+ return linkSize.name
|
|
|
+ else
|
|
|
+ return "-"
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ def color_name
|
|
|
+ linkColor = ProductAttr.where("id = ?", self.color_id).first
|
|
|
+ if !linkColor.blank?
|
|
|
+ return linkColor.name
|
|
|
+ else
|
|
|
+ return "-"
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
def v_share_img=file
|
|
|
unless file.blank?
|
|
|
file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
|
|
|
@@ -35,7 +109,7 @@ class Product < ActiveRecord::Base
|
|
|
clean_share_img
|
|
|
end
|
|
|
|
|
|
- def gross_interest_rate
|
|
|
+ def gross_interest_rate
|
|
|
rate = 0
|
|
|
if self.buy_price != 0 && self.price != 0
|
|
|
rate = (( self.price.to_f - self.buy_price.to_f )/self.buy_price.to_f)*100
|
|
|
@@ -95,6 +169,10 @@ class Product < ActiveRecord::Base
|
|
|
end
|
|
|
field :is_only_new
|
|
|
field :video_state
|
|
|
+ field :size_name
|
|
|
+ field :color_name
|
|
|
+ field :relate_product_id
|
|
|
+ field :show_flag
|
|
|
field :created_at
|
|
|
field :updated_at
|
|
|
end
|
|
|
@@ -150,6 +228,10 @@ class Product < ActiveRecord::Base
|
|
|
field :no_delivery_area
|
|
|
field :video_state
|
|
|
field :video_url
|
|
|
+ field :size_name
|
|
|
+ field :color_name
|
|
|
+ field :relate_product_id
|
|
|
+ field :show_flag
|
|
|
field :created_at
|
|
|
field :updated_at
|
|
|
end
|
|
|
@@ -176,11 +258,13 @@ class Product < ActiveRecord::Base
|
|
|
field :virtual_sold_count
|
|
|
field :purchase_limit_count
|
|
|
field :share_content
|
|
|
- # field :v_share_img, :file_upload do
|
|
|
- # pretty_value do
|
|
|
- # bindings[:view].tag(:img, {:src => bindings[:object].get_share_img, :class => 'preview'})
|
|
|
- # end
|
|
|
- # end
|
|
|
+=begin
|
|
|
+ field :v_share_img, :file_upload do
|
|
|
+ pretty_value do
|
|
|
+ bindings[:view].tag(:img, {:src => bindings[:object].get_share_img, :class => 'preview'})
|
|
|
+ end
|
|
|
+ end
|
|
|
+=end
|
|
|
field :seckill_start
|
|
|
field :seckill_end
|
|
|
field :seckill_price
|
|
|
@@ -191,6 +275,18 @@ class Product < ActiveRecord::Base
|
|
|
field :no_delivery_area
|
|
|
field :video_state
|
|
|
field :video_url
|
|
|
+ field :size_id, :enum do
|
|
|
+ enum do
|
|
|
+ SIZE_ENUM
|
|
|
+ end
|
|
|
+ end
|
|
|
+ field :color_id, :enum do
|
|
|
+ enum do
|
|
|
+ COLOR_ENUM
|
|
|
+ end
|
|
|
+ end
|
|
|
+ field :relate_product_id
|
|
|
+ field :show_flag
|
|
|
end
|
|
|
|
|
|
end
|