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