product.rb 9.3 KB

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