product.rb 9.5 KB

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