product.rb 8.2 KB

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