product.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. class Product < ActiveRecord::Base
  2. has_paper_trail
  3. self.table_name = 'products'
  4. #Thread.current[:current_user] = @current_user
  5. scope :active, -> { where(ptype: "cent_sale") }
  6. scope :inactive, -> { where(ptype: "direct_sale") }
  7. belongs_to :product_cat, :foreign_key => :category_id
  8. belongs_to :merchant, :foreign_key => :merchant_id
  9. validates :name,:buy_price,:price,:category_id,:count,:robo_balance_price, presence: true
  10. attr_accessor :v_share_img,:get_size_enum,:gross_interest_rate
  11. # after_find :get_size_enum
  12. after_create :after_create
  13. before_save :before_save
  14. after_destroy :del_picture
  15. def after_create
  16. #主商品默认关联商品
  17. if self.show_flag
  18. self.relate_product_id = self.id
  19. self.save
  20. end
  21. end
  22. def before_save
  23. #主商品默认关联商品
  24. if self.show_flag
  25. self.relate_product_id = self.id
  26. end
  27. if self.size_id.nil?
  28. self.size_id = 0
  29. end
  30. if self.color_id.nil?
  31. self.color_id = 0
  32. end
  33. end
  34. def del_picture
  35. # 删除商品图片
  36. pictures = ProductPicture.find_by_sql("select * from product_pictures where pic_type=0 and product_id = #{self.id}")
  37. # 创建商品图片
  38. pictures.each do |u|
  39. u.delete
  40. end
  41. end
  42. TYPE_ENUM = [["直营","direct_sale"],["店铺专区","shop_sale"],["积分专区","cent_sale"]]
  43. SIZE_ENUM = []
  44. COLOR_ENUM = []
  45. def get_size_enum
  46. SIZE_ENUM.clear
  47. if SIZE_ENUM.length ==0 && !self.id.nil?
  48. linkSize = ProductAttrConfig.where("product_id=? and size_type='size'",self.relate_product_id).first
  49. if !linkSize.blank?
  50. productAttrs = ProductAttr.where("attr_key_id=? and status=1 ",linkSize.attr_key_id).order("recommend desc")
  51. productAttrs.each do |attr|
  52. a=[attr.name,attr.id]
  53. SIZE_ENUM.push(a)
  54. end
  55. end
  56. end
  57. COLOR_ENUM.clear
  58. if COLOR_ENUM.length==0 && !self.id.nil?
  59. linkColor = ProductAttrConfig.where("product_id=? and size_type='color'",self.relate_product_id).first
  60. if !linkColor.blank?
  61. productColorAttrs = ProductAttr.where("attr_key_id=? and status=1",linkColor.attr_key_id).order("recommend desc")
  62. productColorAttrs.each do |color_attr|
  63. b=[color_attr.name,color_attr.id]
  64. COLOR_ENUM.push(b)
  65. end
  66. end
  67. end
  68. end
  69. IMG_STORE_PATH = "product"
  70. def size_name
  71. linkSize = ProductAttr.where("id = ?", self.size_id).first
  72. if !linkSize.blank?
  73. return linkSize.name
  74. else
  75. return "-"
  76. end
  77. end
  78. def color_name
  79. linkColor = ProductAttr.where("id = ?", self.color_id).first
  80. if !linkColor.blank?
  81. return linkColor.name
  82. else
  83. return "-"
  84. end
  85. end
  86. def v_share_img=file
  87. unless file.blank?
  88. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  89. file_path = "#{IMG_STORE_PATH}/product/share/#{file_name}"
  90. Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  91. self.share_img = file_path
  92. self.save
  93. end
  94. end
  95. def clean_share_img
  96. file_path = "#{self.share_img}"
  97. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  98. end
  99. def get_share_img
  100. url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.share_img}"
  101. return url
  102. end
  103. def after_destroy
  104. clean_share_img
  105. end
  106. def gross_interest_rate
  107. rate = 0
  108. if self.buy_price != 0 && self.price != 0
  109. rate = (( self.price.to_f - self.buy_price.to_f )/self.buy_price.to_f)*100
  110. end
  111. return rate
  112. end
  113. rails_admin do
  114. navigation_label '商品管理'
  115. weight -240
  116. list do
  117. #scopes [:inactive]
  118. filters [:id,:detail,:status,:name]
  119. field :id
  120. #field :merchant_id
  121. #field :merchant
  122. field :name do
  123. filterable true
  124. end
  125. field :ptype, :enum do
  126. enum do
  127. TYPE_ENUM
  128. end
  129. end
  130. #field :category_id
  131. field :product_cat
  132. field :detail
  133. field :price do
  134. label "现金价格(元)"
  135. formatted_value do # used in form views
  136. value.to_f / 100
  137. end
  138. end
  139. #field :robo_balance_price
  140. field :user_sale_price
  141. # field :buy_price
  142. #field :gross_interest_rate
  143. field :count
  144. field :recommend
  145. field :status
  146. #field :is_support_poor
  147. field :virtual_sold_count
  148. field :purchase_limit_count
  149. field :share_img do
  150. visible false
  151. formatted_value do
  152. bindings[:view].tag(:img,{:src => bindings[:object].get_share_img,
  153. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  154. :onClick => "javascript:window.open('#{bindings[:object].get_share_img}')"})
  155. end
  156. end
  157. field :seckill_price do
  158. label "秒杀显示原价(元)"
  159. formatted_value do # used in form views
  160. value.to_f / 100
  161. end
  162. end
  163. #field :is_only_new
  164. #ield :video_state
  165. field :size_name
  166. field :color_name
  167. field :relate_product_id
  168. field :show_flag
  169. field :live
  170. field :sale_nums
  171. field :single_purch_limit
  172. field :created_at
  173. field :updated_at
  174. end
  175. show do
  176. field :id
  177. field :merchant_id
  178. field :merchant
  179. field :name
  180. field :ptype, :enum do
  181. enum do
  182. TYPE_ENUM
  183. end
  184. end
  185. #field :category_id
  186. field :product_cat
  187. field :detail
  188. field :price do
  189. label "现金价格(元)"
  190. formatted_value do # used in form views
  191. value.to_f / 100
  192. end
  193. end
  194. field :robo_balance_price
  195. field :buy_price
  196. field :user_sale_price
  197. field :count
  198. field :recommend
  199. field :status
  200. #field :is_support_poor
  201. field :virtual_sold_count
  202. field :purchase_limit_count
  203. field :share_content
  204. # field :share_img do
  205. # formatted_value do
  206. # bindings[:view].tag(:img,{:src => bindings[:object].get_share_img,
  207. # :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  208. # :onClick => "javascript:window.open('#{bindings[:object].get_share_img}')"})
  209. # end
  210. # end
  211. field :seckill_start
  212. field :seckill_end
  213. field :seckill_price do
  214. label "秒杀显示原价(元)"
  215. formatted_value do # used in form views
  216. value.to_f / 100
  217. end
  218. end
  219. field :deliver_stop_at
  220. field :deliver_start_at
  221. field :is_only_new
  222. field :specification
  223. field :no_delivery_area
  224. field :video_state
  225. field :video_url
  226. field :size_name
  227. field :color_name
  228. field :relate_product_id
  229. field :show_flag
  230. field :live
  231. field :single_purch_limit
  232. field :created_at
  233. field :updated_at
  234. end
  235. edit do
  236. field :name
  237. field :ptype, :enum do
  238. enum do
  239. TYPE_ENUM
  240. end
  241. end
  242. #field :category_id
  243. field :merchant_id
  244. field :product_cat
  245. field :detail, :ck_editor
  246. field :price
  247. field :robo_balance_price
  248. field :buy_price
  249. field :user_sale_price
  250. field :count
  251. field :recommend
  252. field :status
  253. #field :is_support_poor
  254. field :virtual_sold_count
  255. field :purchase_limit_count
  256. field :share_content
  257. =begin
  258. field :v_share_img, :file_upload do
  259. pretty_value do
  260. bindings[:view].tag(:img, {:src => bindings[:object].get_share_img, :class => 'preview'})
  261. end
  262. end
  263. =end
  264. field :seckill_start
  265. field :seckill_end
  266. field :seckill_price
  267. field :deliver_stop_at
  268. field :deliver_start_at
  269. field :is_only_new
  270. field :specification
  271. field :no_delivery_area
  272. field :video_state
  273. field :video_url
  274. field :size_id, :enum do
  275. enum do
  276. SIZE_ENUM
  277. end
  278. end
  279. field :color_id, :enum do
  280. enum do
  281. COLOR_ENUM
  282. end
  283. end
  284. field :relate_product_id
  285. field :show_flag
  286. field :live
  287. field :single_purch_limit
  288. end
  289. end
  290. end