product.rb 8.6 KB

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