product.rb 13 KB

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