using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using SysCore;
using SysCore.Model;
using SysCore.DAL;
using SysCore.Control;
public partial class ARAP_Report_CustomerBalance_Grid : System.Web.UI.Page
{
    public Hashtable xview = new Hashtable();
    AWGridFun aw = new AWGridFun();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            CreateGrid();
        }
    }
    /// <summary>
    /// 绑定控件数据源
    /// </summary>
    public void CreateGrid()
    {
        //设置标题与样式
        ModuleFormatDesigner mfd = new ModuleFormatDesigner();
        string sql_select_field = "";//SQL所需选择列
        string module = "ARAP_Report_CustomerBalance";//模块
        string fields_index = "";//列索引
        mfd.CreateTitlesAndStyles(module, ref aw, ref sql_select_field, ref fields_index);
        xview.Add("fields_index", fields_index);
        //把样式传递到页面
        xview.Add("aw_column_style",aw.GetStyles());
        int page = 1;//当前页
        if (Request.QueryString["page"] != null)
            page = Convert.ToInt32(Request.QueryString["page"]);
        int page_size = aw.GetDefaultPageSize();//每页显示条数
        if (Request.QueryString["page_size"] != null)
            page_size = Convert.ToInt32(Request.QueryString["page_size"]);
        
        Hashtable ht = new Hashtable();
        //排序
        string order_field = "order by cus_code";
        ht.Add("order_field", order_field);
        ht.Add("page", page);
        ht.Add("page_size", page_size);
        //查询条件
        string sql_end = "";
        if (!Request["keyword"].Equals(""))
        {
            //要搜索关键字
            string keyword = Request["keyword"].ToString();
            //要搜索的字段
            string search_fields = "cus_code,cus_name";

            sql_end = FastSearh.MakeSearchSqlForWhere(keyword, search_fields);
        }
        if (Request["begin_date"].Equals("") || Request["end_date"].Equals(""))
        {
            Response.Write("请填写日期范围！");
            Response.End();
        }

        if (!Request["cus_name"].Equals(""))
        {
            sql_end += " and cus_name='" + Request["cus_name"] + "'";
        }


        ht.Add("sql_end", sql_end);
        //查询后返回DataTable
        DataTable dt = GetList(ht, sql_select_field);
        //把sql放入session中供打印用
        string sql_print_detail_session_id = "sql_print_detail_" + module;
        Session[sql_print_detail_session_id] = sql_print;
        //Response.Write(sql_print);
        xview.Add("sql_print_detail_session_id", sql_print_detail_session_id);
        //供导出Excel用
        string field_for_export = mfd.field_for_export;
        Session[sql_print_detail_session_id + "_export_field"] = field_for_export;
        //得到总记录数为分页提供依据
        int rows_count = GetList_TotalNumbers(ht);
        //开启合计行功能
        aw.to_sum_row = true;
        //从模板读出哪些列需要合计
        aw.last_row_info = mfd.last_row_info;
        string aw_grid_info = aw.MakeGridObject(dt, rows_count, page_size, page);
        //把控件参数与数据源传递到页面
        xview.Add("aw_grid_info", aw_grid_info);
    }
    //打印用的sql
    public string sql_print = "";
    //列表查询用的sql
    string sql_list = @"
                    select top 1000000000 t1.cus_id,cus.cus_code,cus.cus_name,
                    isnull(qc1.qc_ying_shou,0)-isnull(qc2.qc_shou_kuan,0) as qi_chu,
                    t2.ying_shou,t3.shou_kuan,
                    isnull(qc1.qc_ying_shou,0)-isnull(qc2.qc_shou_kuan,0)+
                    isnull(t2.ying_shou,0)-isnull(t3.shou_kuan,0) as yu_e
                    from
                    (
	                    select cus_id from
	                    (
		                    select cus_id from ARAP_APDetail where rp_flag='AR'
		                    union all
		                    select cus_id from ARAP_SFKuan where rp_flag='AR'
                            and isnull(verify_user,'')<>''
	                    ) t group by cus_id
                    )t1 
                    left join Base_Customer cus on t1.cus_id=cus.cus_id
                    left join
                    (
	                    select cus_id,sum(nat_tax_total_price) as ying_shou
	                    from ARAP_APDetail
	                    where rp_flag='AR'
	                    and new_date between @begin_date and @end_date
	                    group by cus_id
                    ) t2 on t1.cus_id=t2.cus_id
                    left join
                    (
	                    select cus_id,sum(nat_amount) as shou_kuan
	                    from ARAP_SFKuan
	                    where rp_flag='AR' and isnull(verify_user,'')<>''
	                    and new_date between @begin_date and @end_date
	                    group by cus_id
                    ) t3 on t1.cus_id=t3.cus_id
                    
                    left join
                    (
	                    select cus_id,sum(nat_tax_total_price) as qc_ying_shou
	                    from ARAP_APDetail
	                    where rp_flag='AR'
	                    and new_date < @begin_date
	                    group by cus_id
                    ) qc1 on t1.cus_id=qc1.cus_id
                    left join
                    (
	                    select cus_id,sum(nat_amount) as qc_shou_kuan
	                    from ARAP_SFKuan
	                    where rp_flag='AR' and isnull(verify_user,'')<>''
	                    and new_date < @begin_date
	                    group by cus_id
                    ) qc2 on t1.cus_id=qc2.cus_id
                    where 1=1 ";
    /// <summary>
    /// 得到列表
    /// </summary>
    /// <param name="sql_select_field">格式定义指定显示列</param>
    /// <returns>DataTable</returns>
    public DataTable GetList(Hashtable pg, string sql_select_field)
    {
        sql_list = sql_list.Replace("@begin_date", "'" + Request["begin_date"] + "'")
                           .Replace("@end_date", "'" + Request["end_date"] + "'");
        string order_field = pg["order_field"].ToString();
        string sql = @"select ROW_NUMBER() OVER(@order_field) AS row_num,@sql_select_field 
                from(@sql_list)t where 1=1 ";
        sql = sql.Replace("@sql_list", sql_list)
                 .Replace("@order_field", order_field)
                  .Replace("@sql_select_field", sql_select_field);
        sql_print = "select top 1000000000 * from (" + sql_list + ") t where 1=1 " + pg["sql_end"] + " " + pg["order_field"].ToString();
        //分页处理与查询条件
        PageHelper ph = new PageHelper();
        sql = ph.GetPageSQL(sql, pg, true);//得到分页后的SQL
        //Response.Write(sql);
        //Response.End();
        DataTable dt = new SqlUtil().GetDataTable(sql, null);
        return dt;
    }
    /// <summary>
    /// 得到总记录数
    /// </summary>
    /// <returns>int</returns>
    public int GetList_TotalNumbers(Hashtable pg)
    {

        string sql = "select count(1) from (" + sql_list + ")t where 1=1 ";
        //分页处理与查询条件
        PageHelper ph = new PageHelper();
        sql = ph.GetPageSQL(sql, pg, false);//得到分页SQL
        int total_numbers = (int)new SqlUtil().ExecuteScalar(sql, null);
        return total_numbers;
    }
}
