product.rb 10.0 KB

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