product.rb 9.8 KB

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