product.rb 9.6 KB

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