# encoding:utf-8
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users, {:options => 'AUTO_INCREMENT = 600101'} do |t|
      #电话
      t.string   :tel,                :limit => 20,  :null => false      
      #交易密码
      t.string   :trade_pwd, :null => false, :default => "" 
      
      t.boolean  :is_certification,  :default => false
      #身份证号码
      t.string   :identity_card,      :limit => 18
      t.string   :real_name,          :limit => 64

      t.datetime :birthday

      t.string   :nickname,           :limit => 100
      t.string   :city,               :limit => 20
      t.string   :country,            :limit => 20
      t.string   :province,           :limit => 20
      #用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
      t.integer  :sex,                :limit => 1, :default => 0
      t.column   :age,                :integer 
      t.string   :head
      t.integer  :invite_id
      t.integer  :signup_channel_id      
      t.string   :signup_ip
      #是否黑名单，黑名单用户不能提货券和提货券支付商品
      t.boolean  :is_black_user, :default=>0
      t.timestamps
    end
    add_index :users, :tel,    :unique => true
    add_index :users, :invite_id    


    create_table :wx_users do |t|
      t.integer   :user_id
      t.integer   :invite_id, :default => 0
      t.string    :mp_openid,              :limit => 64    
      t.string    :openid,                 :limit => 64  
      t.string    :nickname,            :limit => 100, :null => false
      t.string    :unionid,             :limit => 64,  :null => false
      t.string    :city,                :limit => 20
      t.string    :country,             :limit => 20
      t.string    :province,            :limit => 20
      t.integer   :sex,       :limit => 1, :default => 0
      t.string    :head
      t.boolean   :subscribe
      #是否注册
      t.boolean   :is_regist, :default => 0
      t.integer   :subscribe_time
      t.integer   :unsubscribe_time
      t.integer   :last_conversation_at
      # 其实是注册渠道，不要被字面意思误以为码id
      t.integer   :channel_qrcode_id      
      t.string    :signup_ip
      #邀请小程序码链接
      t.string    :invite_qrcode_url 
      #购买商品佣金
      t.integer   :product_benefit_rate, :default=>5
      #购买项目佣金
      t.integer   :project_benefit_rate, :default=>1

      #二级商品佣金
      t.integer   :second_product_benefit_rate
      #二级项目佣金
      t.integer   :second_project_benefit_rate

      #合伙人 0不是，1是，0曾经是
      t.tinyint   :copartner_state,    :default => 0
      #最新的成为合伙人时间
      t.datetime  :be_copartner_time
      #最新的取消合伙人时间
      t.datetime  :cancel_copartner_time 

      t.timestamps
    end
    add_index :wx_users, :mp_openid  
    add_index :wx_users, :openid  
    add_index :wx_users, :unionid, :unique => true
    add_index :wx_users, :last_conversation_at
    add_index :wx_users, :subscribe
    add_index :wx_users, :channel_qrcode_id
    add_index :wx_users, :invite_id
    add_index :wx_users, :user_id

  end

  def self.down
    drop_table :users
    drop_table :wx_users
  end
end
