product.rb 9.2 KB

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