Просмотр исходного кода

Merge branch 'feature/#1001' into develop

* feature/#1001: (94 commits)
  商品规格 关联增加限制条件
  商品规格 关联增加限制条件
  商品规格 关联增加限制条件
  商品规格 关联增加限制条件
  商品规格 关联增加限制条件
  商品规格 关联增加限制条件
  订单明细增加规格显示
  规格明细增加状态码
  规格明细增加状态码
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  增加复制商品功能
  ...
abiao лет назад: 5
Родитель
Сommit
ecc5a020c2

+ 4 - 0
app/models/order_detail.rb

@@ -81,6 +81,8 @@ class OrderDetail < ActiveRecord::Base
         end
       end
       field :is_zeng
+      field :size_name
+      field :color_name
       field :created_at
       field :updated_at
     end
@@ -98,6 +100,8 @@ class OrderDetail < ActiveRecord::Base
         end
       end
       field :product_name
+      field :size_name
+      field :color_name
       field :created_at
       field :updated_at
     end

+ 103 - 7
app/models/product.rb

@@ -4,13 +4,87 @@ class Product < ActiveRecord::Base
     belongs_to :product_cat, :foreign_key => :category_id
     belongs_to :merchant, :foreign_key => :merchant_id
     validates :name,:buy_price,:price,:category_id,:count,:robo_balance_price, presence: true
-    attr_accessor :v_share_img
+    attr_accessor :v_share_img,:get_size_enum,:gross_interest_rate
+    after_find :get_size_enum
+    after_create :after_create
+    #after_update :after_update
+    before_save :before_save
+    after_destroy :del_picture
+
+    def after_create
+        #主商品默认关联商品
+        if self.show_flag
+            self.relate_product_id = self.id
+            self.save
+        end
+    end
+    def before_save
+        #主商品默认关联商品
+        if self.show_flag
+            self.relate_product_id = self.id
+        end
+    end
+
+    def del_picture
+        # 删除商品图片
+        pictures = ProductPicture.find_by_sql("select * from product_pictures where pic_type=0 and product_id = #{self.id}")
+        # 创建商品图片
+        pictures.each do |u|
+            u.delete
+        end
+    end
+
 
     TYPE_ENUM = [["直营","direct_sale"]]
     #["代销","user_sale"]
+    SIZE_ENUM =  []
+    COLOR_ENUM =  []
+    def get_size_enum
+        SIZE_ENUM.clear
+        if SIZE_ENUM.length ==0 && !self.id.nil?
+            linkSize = ProductAttrConfig.where("product_id=? and size_type='size'",self.relate_product_id).first
+            if !linkSize.blank?
+                productAttrs = ProductAttr.where("attr_key_id=? and status=1 ",linkSize.attr_key_id).order("recommend desc")
+                productAttrs.each do |attr|
+                    a=[attr.name,attr.id]
+                    SIZE_ENUM.push(a)
+                end
+            end
+        end
+
+        COLOR_ENUM.clear
+        if COLOR_ENUM.length==0 && !self.id.nil?
+            linkColor = ProductAttrConfig.where("product_id=? and size_type='color'",self.relate_product_id).first
+            if !linkColor.blank?
+                productColorAttrs = ProductAttr.where("attr_key_id=? and status=1",linkColor.attr_key_id).order("recommend desc")
+                productColorAttrs.each do |color_attr|
+                    b=[color_attr.name,color_attr.id]
+                    COLOR_ENUM.push(b)
+                end
+            end
+        end
+    end
 
     IMG_STORE_PATH = "product"
 
+    def size_name
+        linkSize = ProductAttr.where("id = ?", self.size_id).first
+        if !linkSize.blank?
+            return linkSize.name
+        else
+            return "-"
+        end
+    end
+
+    def color_name
+        linkColor = ProductAttr.where("id = ?", self.color_id).first
+        if !linkColor.blank?
+            return linkColor.name
+        else
+            return "-"
+        end
+    end
+
     def v_share_img=file
         unless file.blank?
         file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
@@ -35,7 +109,7 @@ class Product < ActiveRecord::Base
         clean_share_img
     end
 
-    def gross_interest_rate 
+    def gross_interest_rate
         rate = 0
         if self.buy_price != 0 && self.price != 0
             rate = (( self.price.to_f - self.buy_price.to_f )/self.buy_price.to_f)*100
@@ -95,6 +169,10 @@ class Product < ActiveRecord::Base
             end
             field :is_only_new
             field :video_state
+            field :size_name
+            field :color_name
+            field :relate_product_id
+            field :show_flag
             field :created_at
             field :updated_at
         end
@@ -150,6 +228,10 @@ class Product < ActiveRecord::Base
             field :no_delivery_area
             field :video_state
             field :video_url
+            field :size_name
+            field :color_name
+            field :relate_product_id
+            field :show_flag
             field :created_at
             field :updated_at
         end
@@ -176,11 +258,13 @@ class Product < ActiveRecord::Base
             field :virtual_sold_count
             field :purchase_limit_count
             field :share_content
-            # field :v_share_img, :file_upload do
-            #     pretty_value do
-            #         bindings[:view].tag(:img, {:src => bindings[:object].get_share_img, :class => 'preview'})
-            #     end
-            # end
+=begin
+            field :v_share_img, :file_upload do
+                 pretty_value do
+                     bindings[:view].tag(:img, {:src => bindings[:object].get_share_img, :class => 'preview'})
+                end
+            end
+=end
             field :seckill_start
             field :seckill_end
             field :seckill_price
@@ -191,6 +275,18 @@ class Product < ActiveRecord::Base
             field :no_delivery_area
             field :video_state
             field :video_url
+            field :size_id, :enum do
+                enum do
+                    SIZE_ENUM
+                end
+            end
+            field :color_id, :enum do
+                enum do
+                  COLOR_ENUM
+                end
+            end
+            field :relate_product_id
+            field :show_flag
         end
 
     end

+ 43 - 0
app/models/product_attr.rb

@@ -0,0 +1,43 @@
+class ProductAttr < ActiveRecord::Base
+    has_paper_trail
+    self.table_name = 'product_attrs'
+
+    belongs_to :product_attr_key, :foreign_key => :attr_key_id
+    validates :name,:product_attr_key,presence:true
+    
+    rails_admin do
+        navigation_label '商品管理'
+        weight -250
+        parent ProductAttrKey
+        nestable_tree({
+            position_field: :position,
+            max_depth: 2
+        })
+
+        list do 
+            filters [:name,:product_attr_key]
+            field :id
+            field :product_attr_key
+            field :name 
+            field :recommend
+            field :status
+        end
+
+        show do
+            field :id
+            field :product_attr_key
+            field :name
+            field :recommend
+            field :status
+        end
+
+        edit do 
+            field :product_attr_key
+            field :name
+            field :recommend
+            field :status
+        end 
+
+    end
+
+end

+ 70 - 0
app/models/product_attr_config.rb

@@ -0,0 +1,70 @@
+class ProductAttrConfig < ActiveRecord::Base
+    has_paper_trail
+    self.table_name = 'product_attr_configs'
+    belongs_to :product_attr_key, :foreign_key => :attr_key_id
+    validates :size_type,:product_id,:product_attr_key,presence:true
+    attr_accessor :show_products
+
+    PRODUCT_ENUM =  []
+    @products = Product.where("show_flag=1").order("created_at desc")
+    @products.each do |pd|
+        a=[pd.name,pd.id]
+        PRODUCT_ENUM.push(a)
+    end
+    TYPE_ENUM = [["规格一","size"],["规格二","color"]]
+    rails_admin do
+        navigation_label '商品管理'
+        weight -250
+        parent ProductAttrKey
+        nestable_tree({
+            position_field: :position,
+            max_depth: 2
+        })
+
+        list do 
+            filters [:product_id,:product_attr_key]
+            field :id
+            field :product_id, :enum do
+                enum do
+                  PRODUCT_ENUM
+                end
+            end
+            field :product_attr_key
+            field :size_type, :enum do
+                enum do
+                    TYPE_ENUM
+                end
+            end
+        end
+
+        show do
+            field :id
+            field :product_id, :enum do
+                enum do
+                  PRODUCT_ENUM
+                end
+            end
+            field :product_attr_key
+            field :size_type, :enum do
+                enum do
+                    TYPE_ENUM
+                end
+            end
+        end
+
+        edit do
+            field :product_id, :enum do
+                enum do
+                  PRODUCT_ENUM
+                end
+            end
+            field :product_attr_key
+            field :size_type, :enum do
+                enum do
+                    TYPE_ENUM
+                end
+            end
+        end
+    end
+
+end

+ 34 - 0
app/models/product_attr_key.rb

@@ -0,0 +1,34 @@
+class ProductAttrKey < ActiveRecord::Base
+    has_paper_trail
+    self.table_name = 'product_attr_keys'
+    validates :name,presence:true
+    
+    rails_admin do
+        navigation_label '商品管理'
+        weight -250
+        nestable_tree({
+            position_field: :position,
+            max_depth: 2
+        })
+
+        list do 
+            filters [:name,:status]
+            field :id
+            field :name
+            field :status
+        end
+
+        show do
+            field :id
+            field :name
+            field :status
+        end
+
+        edit do 
+            field :name 
+            field :status
+        end 
+
+    end
+
+end

+ 1 - 1
app/models/product_picture.rb

@@ -9,7 +9,7 @@ class ProductPicture < ActiveRecord::Base
 
     IMG_STORE_PATH = "product_pictures" 
 
-    PIC_TYPE_ENUM = [["视频封面图",2],["详情图",1],["轮播图",0]]
+    PIC_TYPE_ENUM = [["详情图",1],["轮播图",0]]
 
     rails_admin do
         navigation_label '商品管理'

+ 6 - 1
config/initializers/rails_admin.rb

@@ -13,6 +13,7 @@ require Rails.root.join('lib', 'rails_admin', 'correct_takecash')
 require Rails.root.join('lib', 'rails_admin', 'update_intro_user')
 require Rails.root.join('lib', 'rails_admin', 'order_dispatch')
 require Rails.root.join('lib', 'rails_admin', 'create_xcx_qrcode')
+require Rails.root.join('lib', 'product', 'copy_product')
 
 
 RailsAdmin::Config::Actions.register(RailsAdmin::Config::Actions::GenerateMenu)
@@ -29,6 +30,8 @@ RailsAdmin::Config::Actions.register(RailsAdmin::Config::Actions::CorrectTakecas
 RailsAdmin::Config::Actions.register(RailsAdmin::Config::Actions::UpdateIntroUser)
 RailsAdmin::Config::Actions.register(RailsAdmin::Config::Actions::OrderDispatch)
 RailsAdmin::Config::Actions.register(RailsAdmin::Config::Actions::CreateXcxQrcode)
+RailsAdmin::Config::Actions.register(RailsAdmin::Config::Actions::CopyProduct)
+
 
 
 RailsAdmin.config do |config|
@@ -129,7 +132,9 @@ RailsAdmin.config do |config|
     takecash_at_once do
       only ["TakeCashOrder"]
     end
-
+    copy_product do
+      only ["Product"]
+    end
     add_wxku_commodity do
       only ["WxkuCommodity"]
     end

+ 2 - 0
config/locales/models/order_detail.yml

@@ -16,6 +16,8 @@ zh-CN:
        paid_time: 支付时间
        pay_way: 支付方式
        is_zeng: 是否赠品
+       size_name: 规格一
+       color_name: 规格二
        created_at: 创建时间
        updated_at: 修改时间
 

+ 9 - 1
config/locales/models/product.yml

@@ -17,6 +17,8 @@ zh-CN:
         buy_price: 采购价格(分)
         user_sale_price: 零售价(分)
         gross_interest_rate: 毛利率
+        get_size_enum: 尺码列表
+        #get_color_enum: 颜色列表
         count: 数量
         recommend: 推荐程度
         status: 是否上架
@@ -41,4 +43,10 @@ zh-CN:
         specification: 规格
         no_delivery_area: 不发货地区
         video_state: 显示视频
-        video_url: 视频地址
+        video_url: 视频地址
+        size_id: 规格一
+        color_id: 规格二
+        size_name: 规格一
+        color_name: 规格二
+        relate_product_id: 关联主商品
+        show_flag: 主商品标记

+ 15 - 0
config/locales/models/product_attr.yml

@@ -0,0 +1,15 @@
+zh-CN:
+  activerecord:
+    models:
+      product_attr: 规格明细
+    attributes:
+      product_attr:
+        id: ID 
+        name: 名称
+        attr_key_id: 规格ID
+        product_attr_key: 规格
+        recommend: 推荐程度
+        status: 是否发布
+
+
+    

+ 13 - 0
config/locales/models/product_attr_config.yml

@@ -0,0 +1,13 @@
+zh-CN:
+  activerecord:
+    models:
+      product_attr_config: 主商品规格配置
+    attributes:
+      product_attr_config:
+        id: ID 
+        product: 商品
+        product_id: 商品
+        attr_key_id: 规格ID
+        product_attr_key: 规格
+        show_products: 主商品
+        size_type: 类型

+ 9 - 0
config/locales/models/product_attr_key.yml

@@ -0,0 +1,9 @@
+zh-CN:
+  activerecord:
+    models:
+      product_attr_key: 商品规格定义
+    attributes:
+      product_attr_key:
+        id: ID 
+        name: 规格名称
+        status: 状态

+ 2 - 3
config/locales/rails_admin.zh-CN.yml

@@ -125,7 +125,8 @@ zh-CN:
         menu: "查微信库商品"
       check_wxku_commodity:
         menu: "查询微信库商品导入更新状态"
-
+      copy_product:
+        menu: "复制"
       refuse_takecash:
         title: "%{model_label_plural}拒绝提现"
         menu: "拒绝提现"
@@ -154,8 +155,6 @@ zh-CN:
         menu: "推送"
       push_xcx_tmpl_redirect:
         menu: "推送"
-      copy_project:
-        menu: "复制"
       correct_takecash:
         menu: "提现冲正"
       deposit_unlock:

+ 60 - 0
db/migrate/20200808124641_create_product_attrs.rb

@@ -0,0 +1,60 @@
+# encoding:utf-8
+class CreateProductAttrs < ActiveRecord::Migration
+  def up
+    #规格定义表
+    create_table :product_attr_keys do |t|
+      # 产品名称
+      t.column :name, :string,:limit => 128
+      # 是否发布
+      t.column :status, :boolean, :default=>1
+      t.timestamps
+    end
+
+    #规格明细表
+    create_table :product_attrs do |t|
+      # 规格ID
+      t.column :attr_key_id, :integer, :limit => 8, :default => 0
+      # 规格名称
+      t.column :name, :string,:limit => 500
+      # 排序
+      t.column :recommend, :integer, :limit => 8, :default => 0
+      # 状态
+      t.column :status, :boolean, :default=>1
+      t.timestamps
+    end
+    add_index :product_attrs, :attr_key_id
+
+
+    #主商品规格配置表
+    create_table :product_attr_configs do |t|
+      # 主商品ID
+      t.column :product_id, :integer, :limit => 8, :default => 0
+      # 规格ID
+      t.column :attr_key_id, :integer, :limit => 8, :default => 0
+      # 类型
+      t.column :size_type, :string, :limit => 64
+      t.timestamps
+    end
+
+    add_index :product_attr_configs, :product_id
+    add_index :product_attr_configs, :attr_key_id
+    add_column :products, :size_id, :integer
+    add_column :products, :color_id, :integer
+    add_column :products, :relate_product_id, :integer
+    add_column :products, :show_flag, :boolean
+    add_column :order_details, :size_name, :string
+    add_column :order_details, :color_name, :string
+  end
+
+  def down
+  	drop_table :product_attr_keys
+    drop_table :product_attrs
+    drop_table :product_attr_configs
+    remove_column :products, :size_id
+    remove_column :products, :color_id
+    remove_column :products, :relate_product_id
+    remove_column :products, :show_flag
+    remove_column :order_details, :size_name
+    remove_column :order_details, :color_name
+  end
+end

+ 83 - 0
lib/product/copy_product.rb

@@ -0,0 +1,83 @@
+# encoding:utf-8
+# 商品复制
+module RailsAdmin
+  module Config
+    module Actions     
+      class CopyProduct < RailsAdmin::Config::Actions::Base
+        register_instance_option :visible? do
+          if not bindings[:object].blank?
+            authorized?
+          else
+            authorized?
+          end
+        end
+        # We want the action on members, not the Users collection
+        register_instance_option :member do
+          true
+        end
+
+        register_instance_option :link_icon do
+          'icon-refresh'
+        end
+        # You may or may not want pjax for your action
+        register_instance_option :pjax? do
+          false
+        end
+
+        register_instance_option :controller do
+          Proc.new do
+            if !@object.blank?
+              @copy_product = Product.new
+              @copy_product.name = @object.name
+              @copy_product.ptype = @object.ptype
+              @copy_product.merchant_id = @object.merchant_id
+              @copy_product.category_id = @object.category_id
+              @copy_product.detail = @object.detail
+              @copy_product.price = @object.price
+              @copy_product.robo_balance_price = @object.robo_balance_price
+              @copy_product.buy_price = @object.buy_price
+              @copy_product.user_sale_price = @object.user_sale_price
+              @copy_product.count = @object.count
+              @copy_product.recommend = @object.recommend
+              @copy_product.status = 0
+              @copy_product.virtual_sold_count = @object.virtual_sold_count
+              @copy_product.purchase_limit_count = @object.purchase_limit_count
+              @copy_product.share_content = @object.share_content
+              @copy_product.share_img = @object.share_img
+              @copy_product.deliver_stop_at = @object.deliver_stop_at
+              @copy_product.deliver_start_at = @object.deliver_start_at
+              @copy_product.seckill_start = @object.seckill_start
+              @copy_product.seckill_end = @object.seckill_end
+              @copy_product.seckill_price = @object.seckill_price
+              @copy_product.specification = @object.specification
+              @copy_product.no_delivery_area = @object.no_delivery_area
+              @copy_product.video_state = @object.video_state
+              @copy_product.video_url = @object.video_url
+              @copy_product.show_flag = 0
+              @copy_product.relate_product_id = @object.id
+              begin
+                @copy_product.save
+              rescue Exception => e
+                p "************ copy product save fail . #{e.to_s}"
+              end
+              
+              # 复制商品图片
+              pictures = ProductPicture.find_by_sql("select * from product_pictures where pic_type=0 and product_id = #{@object.id} limit 1")
+              # 创建商品图片
+              pictures.each do |u|
+                ProductPicture.create({
+                    :product_id => @copy_product.id,
+                    :img => u.img,
+                    :pic_type => u.pic_type,
+                    :sort => 0,
+                  })
+              end              
+              redirect_to :back, notice: "已复制!"
+              # redirect_to "https://d5ctest.oss-cn-shanghai.aliyuncs.com/temp/20180910.xlsx"
+            end
+          end
+        end
+      end
+    end
+  end
+end