using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EKETEAM.Data;
using EKETEAM.FrameWork;
using System.Text;

public partial class Application_Check_Contrast : System.Web.UI.Page
{
    public string UserArea = "Application";
    public string AppItem = eParameters.QueryString("AppItem");
    string PlanID = eParameters.QueryString("PlanID");
    public DataRow row;
    string sql = "";
    public string act = eParameters.QueryString("act");

    protected void Page_Load(object sender, EventArgs e)
    {

        eUser user = new eUser(eBase.getUserArea(UserArea));
        sql = "select * from CheckPlans where delTag=0 and CheckPlanID='" + PlanID + "'";
        DataTable dt = eOleDB.getDataTable(sql);
        if (dt.Rows.Count == 0)
        {
            Response.Write("该批次不存在,或已被删除.");
            Response.End();
        }
        row = dt.Rows[0];
        string baseName = "_" + row["BaseName"].ToString();
        StringBuilder sb = new StringBuilder();
        sb.Append("<div style=\"padding:15px;line-height:26px;\">");
        if (act.Length == 0)
        {
            //计划采集人数
            string plancount = eOleDB.getValue("select count(*) from Check_plan where CheckPlanID='" + PlanID + "'");// where orgCode like '" + user["code"].ToString() + "%'");
            //导入结果人数
            string resultcount = eOleDB.getValue("select count(*) from Check_result where CheckPlanID='" + PlanID + "'");
            sb.Append("批次： " + row["mc"].ToString() + "<br>");
            sb.Append("本次计划采集人数： " + plancount + "<br>");
            sb.Append("导入结果数量： " + resultcount + "<br>");
            sb.Append("<a style=\"margin-top:10px;margin-left:20px;\" class=\"button\" href=\"?AppItem=" + AppItem + "&PlanID=" + PlanID + "&act=start\"><span><i>开始对比</i></span></a>");
        }
        else
        {
            //已采集更新为:1，未采集更新为:0
            //eOleDB.Execute("update a set a.result=(case when b.id is null then 0 else 1 end) from [plan_" + baseName + "] a left join [result_" + baseName + "] b on a.sfzh=b.sfzh");
            eOleDB.Execute("update a set a.result=(case when b.id is null then 0 else 1 end) from Check_plan a left join Check_result b on a.CheckPlanID=b.CheckPlanID and a.sfzh=b.sfzh where a.CheckPlanID='" + PlanID + "' and a.result=0"); //只更新未采集部份
            //计划外

            //sql = "truncate table [other_" + baseName + "]";
            //eOleDB.Execute(sql);

            sql = "insert into Check_other (CheckPlanID,Code,orgCode,SFZH,XM,addUser)";
            sql += " select '" + PlanID +   "','" + user["code"] + "',a.OrgCode,a.sfzh,a.xm,'" + user.ID + "' from T_ryxx a";
            sql += " left join Check_plan b on a.SFZH=b.sfzh";
            sql += " inner join Check_result c on b.CheckPlanID=c.CheckPlanID and a.SFZH=c.sfzh";
            sql += " where b.CheckPlanID=b.CheckPlanID and a.delTag=0 and a.Multiple=0 and b.id is null";

            //eOleDB.Execute(sql);

            sb.Append("比对完成!<br>");
        }
        sb.Append("</div>");

        LitBody.Text = sb.ToString();
       
       


    }

}