product.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. class Product < ActiveRecord::Base
  2. has_paper_trail
  3. self.table_name = 'products'
  4. belongs_to :product_cat, :foreign_key => :category_id
  5. belongs_to :merchant, :foreign_key => :merchant_id
  6. validates :name,:buy_price,:price,:category_id,:count,:robo_balance_price, presence: true
  7. attr_accessor :v_share_img,:get_size_enum,:gross_interest_rate
  8. after_find :gross_interest_rate
  9. TYPE_ENUM = [["直营","direct_sale"]]
  10. #["代销","user_sale"]
  11. SIZE_ENUM = []
  12. def get_size_enum
  13. =begin
  14. @Size = ProductAttrConfig.where("product_id=? and size_type='size'",self.id).first
  15. if !@Size.blank?
  16. @productAttrs = ProductAttr.where("attr_key_id=?",@Size.id).order("recommend desc")
  17. @productAttrs.each do |attr|
  18. a=[attr.name,attr.id]
  19. SIZE_ENUM.push(a)
  20. end
  21. end
  22. =end
  23. return "--"
  24. end
  25. IMG_STORE_PATH = "product"
  26. def v_share_img=file
  27. unless file.blank?
  28. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  29. file_path = "#{IMG_STORE_PATH}/product/share/#{file_name}"
  30. Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  31. self.share_img = file_path
  32. self.save
  33. end
  34. end
  35. def clean_share_img
  36. file_path = "#{self.share_img}"
  37. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  38. end
  39. def get_share_img
  40. url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.share_img}"
  41. return url
  42. end
  43. def after_destroy
  44. clean_share_img
  45. end
  46. def gross_interest_rate
  47. @Size = ProductAttrConfig.where("product_id=? and size_type='size'",self.id).first
  48. if !@Size.blank?
  49. @productAttrs = ProductAttr.where("attr_key_id=?",@Size.id).order("recommend desc")
  50. @productAttrs.each do |attr|
  51. a=[attr.name,attr.id]
  52. SIZE_ENUM.push(a)
  53. end
  54. end
  55. rate = 0
  56. if self.buy_price != 0 && self.price != 0
  57. rate = (( self.price.to_f - self.buy_price.to_f )/self.buy_price.to_f)*100
  58. end
  59. return ((rate*100).round.to_f/100).to_s + "%"
  60. end
  61. rails_admin do
  62. navigation_label '商品管理'
  63. weight -240
  64. list do
  65. filters [:id,:detail,:status,:name]
  66. field :id
  67. field :merchant_id
  68. field :merchant
  69. field :name do
  70. filterable true
  71. end
  72. field :ptype, :enum do
  73. enum do
  74. TYPE_ENUM
  75. end
  76. end
  77. #field :category_id
  78. field :product_cat
  79. field :detail
  80. field :price do
  81. label "现金价格(元)"
  82. formatted_value do # used in form views
  83. value.to_f / 100
  84. end
  85. end
  86. field :robo_balance_price
  87. field :user_sale_price
  88. field :buy_price
  89. field :gross_interest_rate
  90. field :count
  91. field :recommend
  92. field :status
  93. #field :is_support_poor
  94. field :virtual_sold_count
  95. field :purchase_limit_count
  96. field :share_img do
  97. visible false
  98. formatted_value do
  99. bindings[:view].tag(:img,{:src => bindings[:object].get_share_img,
  100. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  101. :onClick => "javascript:window.open('#{bindings[:object].get_share_img}')"})
  102. end
  103. end
  104. field :seckill_price do
  105. label "秒杀显示原价(元)"
  106. formatted_value do # used in form views
  107. value.to_f / 100
  108. end
  109. end
  110. field :is_only_new
  111. field :video_state
  112. # field :get_size_enum
  113. field :size_id, :enum do
  114. enum do
  115. SIZE_ENUM
  116. end
  117. end
  118. field :color_id
  119. field :relate_product_id
  120. field :show_flag
  121. field :created_at
  122. field :updated_at
  123. end
  124. show do
  125. field :id
  126. field :merchant_id
  127. field :merchant
  128. field :name
  129. field :ptype, :enum do
  130. enum do
  131. TYPE_ENUM
  132. end
  133. end
  134. #field :category_id
  135. field :product_cat
  136. field :detail
  137. field :price do
  138. label "现金价格(元)"
  139. formatted_value do # used in form views
  140. value.to_f / 100
  141. end
  142. end
  143. field :robo_balance_price
  144. field :buy_price
  145. field :user_sale_price
  146. field :count
  147. field :recommend
  148. field :status
  149. #field :is_support_poor
  150. field :virtual_sold_count
  151. field :purchase_limit_count
  152. field :share_content
  153. # field :share_img do
  154. # formatted_value do
  155. # bindings[:view].tag(:img,{:src => bindings[:object].get_share_img,
  156. # :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  157. # :onClick => "javascript:window.open('#{bindings[:object].get_share_img}')"})
  158. # end
  159. # end
  160. field :seckill_start
  161. field :seckill_end
  162. field :seckill_price do
  163. label "秒杀显示原价(元)"
  164. formatted_value do # used in form views
  165. value.to_f / 100
  166. end
  167. end
  168. field :deliver_stop_at
  169. field :deliver_start_at
  170. field :is_only_new
  171. field :specification
  172. field :no_delivery_area
  173. field :video_state
  174. field :video_url
  175. field :size_id
  176. field :color_id
  177. field :relate_product_id
  178. field :show_flag
  179. field :created_at
  180. field :updated_at
  181. end
  182. edit do
  183. field :name
  184. field :ptype, :enum do
  185. enum do
  186. TYPE_ENUM
  187. end
  188. end
  189. #field :category_id
  190. field :merchant_id
  191. field :product_cat
  192. field :detail, :ck_editor
  193. field :price
  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 :v_share_img, :file_upload do
  205. pretty_value do
  206. bindings[:view].tag(:img, {:src => bindings[:object].get_share_img, :class => 'preview'})
  207. end
  208. end
  209. field :seckill_start
  210. field :seckill_end
  211. field :seckill_price
  212. field :deliver_stop_at
  213. field :deliver_start_at
  214. field :is_only_new
  215. field :specification
  216. field :no_delivery_area
  217. field :video_state
  218. field :video_url
  219. field :size_id, :enum do
  220. enum do
  221. SIZE_ENUM
  222. end
  223. end
  224. field :color_id
  225. field :relate_product_id
  226. field :show_flag
  227. end
  228. end
  229. end