product.rb 8.6 KB

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