product.rb 9.2 KB

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