product.rb 10 KB

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