class CreateCashBalanceTables < ActiveRecord::Migration
  def up
    #收益表(回购、代销、邀请佣金)
    create_table :cash_balances do |t|
    	#用户ID
    	t.column :wx_uid, :integer, :null=>false	
    	#变动金额(分)
    	t.column :count, :integer, :null=>false
    	#来源
    	t.column :source, :string, :limit=>64
    	#关联ID
    	t.column :relate_id, :string
    	#备注
    	t.column :remark,:string
    	t.timestamps
    end

    add_index :cash_balances, :wx_uid

    #提现订单表
    create_table :take_cash_orders do |t|     
      # 用户ID
      t.column   :wx_uid, :integer, :null => false  
      # 流水号
      t.column   :order_id, :string, :limit => 64, :null => false
      # 交易单号
      t.column   :trade_no, :string, :limit => 64      
      # 提现金额
      t.column   :count, :integer, :limit => 8, :default => 0
      # 提现状态 0未支付，1已支付
      t.integer :pay_state, :limit => 1, :default => 0  
      # 审核状态 0未审核，1同意 2拒绝===========================> 提现失败(pay_state:0,audit_state:2), 提现中(pay_state:0,audit_state:0,1), 已完成(pay_state:1, audit_state:1)
      t.integer :audit_state, :limit => 1, :default => 0  
      # 支付时间 s
      t.column :paied_at, :integer 
      # 预计处理时间
      t.column :expc_pay_at, :datetime    
      # 中文描述
      t.column :remark, :string           

      t.timestamps 
    end

    add_index :take_cash_orders, :wx_uid
    add_index :take_cash_orders, :order_id    

    #邀请佣金表
    create_table :invite_benefit_orders do |t|
        #收益人id
        t.column :benefit_wx_uid, :integer, :null=>false
        #贡献人id
        t.column :wx_uid, :integer, :null=>false
        #直接二级贡献人id
        t.column :ind_wx_uid, :integer, :null=>false
        #佣金金额(分)
        t.column :count, :integer, :null=>false
        #消费金额(分)
        t.column :amount, :integer, :null=>false
        #来源
        t.column :source, :string, :limit=>64
        #关联ID
        t.column :relate_id, :string
        #是否入账
        t.column :is_enter_balance, :boolean, :null=>false, :default=>false
        #进账时间
        t.column :enter_time, :datetime
        #时间戳
        t.timestamps
    end

    add_index :invite_benefit_orders, :benefit_wx_uid
  end

  def down
	drop_table :cash_balances
  	drop_table :take_cash_orders
  	drop_table :invite_benefit_orders
  end
end
