# encoding:utf-8
class KaijiangController < BaseController
  before_filter :authenticate_admin_user!

  def index
    # 项目ID
    pid = params[:pid].to_i
    @project = Project.where("id=?",pid).first
    #开奖人数
    @num = params[:num].to_i
    #开奖人数
    @all_num = params[:all].to_i
    #重庆时彩号码
    @code = params[:code].to_i
    # 项目的投资人总数-排重
    # sql = "select count(DISTINCT user_id) as count from d5c_project_joins where project_id = " + pid + " and state = 1 and is_deleted=0 and is_refunded = 0"
    sql = "select FROM_UNIXTIME(paied_at,'%Y%m%d%H%i') c from d5c_project_joins where project_id = " + pid.to_s + " and state = 1 and is_deleted=0 and is_refunded = 0 group by user_id order by paied_at asc, created_at asc limit #{@all_num};"
    @items = ProjectJoin.find_by_sql(sql)
    @count = @items.size
    @sum = 0
    @items.each do |item|
      @sum = @sum + item.c.to_i
    end
    if @count > 0
      code_date = @items[1].c
      @code_date = "#{code_date[0...4]}-#{code_date[4...6]}-#{code_date[6...8]}"
    end
    render :layout => false
  end

  #项目投资排名
  def rank
    pid = params[:id].to_i
    @project = Project.where("id=?",pid).first
    sql = "select sum(j.total_price) as investment, j.user_id, j.paied_at as invest_time,u.real_name,u.tel from d5c_project_joins j,d5c_users u where j.user_id=u.id and j.project_id = #{pid} and j.state = 1 and j.is_refunded = 0 and j.is_deleted = 0 group by j.user_id order by invest_time asc, j.created_at asc";
    @items = ProjectJoin.find_by_sql(sql)
  end

end