product.rb 9.9 KB

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