product.rb 9.7 KB

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