product.rb 11 KB

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