product.rb 8.2 KB

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