product.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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
  8. after_find :get_size_enum
  9. after_create :after_create
  10. before_save :before_save
  11. after_destroy :del_picture
  12. def after_create
  13. #主商品默认关联商品
  14. if self.show_flag
  15. self.relate_product_id = self.id
  16. self.save
  17. end
  18. end
  19. def before_save
  20. #主商品默认关联商品
  21. if self.show_flag
  22. self.relate_product_id = self.id
  23. end
  24. if self.size_id.nil?
  25. self.size_id = 0
  26. end
  27. if self.color_id.nil?
  28. self.color_id = 0
  29. end
  30. end
  31. def del_picture
  32. # 删除商品图片
  33. pictures = ProductPicture.find_by_sql("select * from product_pictures where pic_type=0 and product_id = #{self.id}")
  34. # 创建商品图片
  35. pictures.each do |u|
  36. u.delete
  37. end
  38. end
  39. TYPE_ENUM = [["直营","direct_sale"],["店铺专区","shop_sale"],["积分专区","cent_sale"]]
  40. SIZE_ENUM = []
  41. COLOR_ENUM = []
  42. def get_size_enum
  43. SIZE_ENUM.clear
  44. if SIZE_ENUM.length ==0 && !self.id.nil?
  45. linkSize = ProductAttrConfig.where("product_id=? and size_type='size'",self.relate_product_id).first
  46. if !linkSize.blank?
  47. productAttrs = ProductAttr.where("attr_key_id=? and status=1 ",linkSize.attr_key_id).order("recommend desc")
  48. productAttrs.each do |attr|
  49. a=[attr.name,attr.id]
  50. SIZE_ENUM.push(a)
  51. end
  52. end
  53. end
  54. COLOR_ENUM.clear
  55. if COLOR_ENUM.length==0 && !self.id.nil?
  56. linkColor = ProductAttrConfig.where("product_id=? and size_type='color'",self.relate_product_id).first
  57. if !linkColor.blank?
  58. productColorAttrs = ProductAttr.where("attr_key_id=? and status=1",linkColor.attr_key_id).order("recommend desc")
  59. productColorAttrs.each do |color_attr|
  60. b=[color_attr.name,color_attr.id]
  61. COLOR_ENUM.push(b)
  62. end
  63. end
  64. end
  65. end
  66. IMG_STORE_PATH = "product"
  67. def size_name
  68. linkSize = ProductAttr.where("id = ?", self.size_id).first
  69. if !linkSize.blank?
  70. return linkSize.name
  71. else
  72. return "-"
  73. end
  74. end
  75. def color_name
  76. linkColor = ProductAttr.where("id = ?", self.color_id).first
  77. if !linkColor.blank?
  78. return linkColor.name
  79. else
  80. return "-"
  81. end
  82. end
  83. def v_share_img=file
  84. unless file.blank?
  85. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  86. file_path = "#{IMG_STORE_PATH}/product/share/#{file_name}"
  87. Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  88. self.share_img = file_path
  89. self.save
  90. end
  91. end
  92. def clean_share_img
  93. file_path = "#{self.share_img}"
  94. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  95. end
  96. def get_share_img
  97. url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.share_img}"
  98. return url
  99. end
  100. def after_destroy
  101. clean_share_img
  102. end
  103. def gross_interest_rate
  104. rate = 0
  105. if self.buy_price != 0 && self.price != 0
  106. rate = (( self.price.to_f - self.buy_price.to_f )/self.buy_price.to_f)*100
  107. end
  108. return ((rate*100).round.to_f/100).to_s + "%"
  109. end
  110. rails_admin do
  111. navigation_label '商品管理'
  112. weight -240
  113. list do
  114. scopes [ptype, "direct_sale"]
  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