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