using MESClassLibrary.BLL;
using MESClassLibrary.BLL.Inspection;
using MESClassLibrary.EFModel;
using MESWebSite.CommonClass;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace MESWebSite.HttpHandlers
{
    /// <summary>
    /// CommonlyInspectionVersionHandler 的摘要说明
    /// </summary>
    public class CommonlyInspectionVersionHandler : IHttpHandler
    {



        HttpRequest Request = null;
        HttpResponse Response = null;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            Request = context.Request;
            Response = context.Response;

            string method = Request.Params["method"];
            switch (method)
            {

                case "QueryList":
                    QueryList();
                    break;
                case "SaveInfo":
                    SaveInfo();
                    break;
                case "DelInfo":
                    DelInfo();
                    break;
                case "OpenInfo":
                    OpenInfo();
                    break;
                case "CloseInfo":
                    CloseInfo();
                    break;
                case "QueryRecord":
                    QueryRecord();
                    break;
                case "QueryCheckList":
                    QueryCheckList();
                    break;
                case "QueryForCombobox":
                    QueryForCombobox();
                    break;

            }
        }
        void QueryList()
        {
            string page = Request.Params["page"];
            string pagesize = Request.Params["rows"];
            string DeviceID = Request.Params["DeviceID"];//查询条件
            if (string.IsNullOrEmpty(page))
            {
                page = "0";
            }
            if (string.IsNullOrEmpty(pagesize))
            {
                pagesize = "15";
            }
            if (string.IsNullOrEmpty(DeviceID))
            {
                DeviceID = "";
            }

            CommonlyInspectionVersionBLL bll = new CommonlyInspectionVersionBLL();
            Response.Write(bll.SearchInfo(page, pagesize, DeviceID));
            Response.End();


        }
        public class parmQuery
        {
            public string VersionID { get; set; }

        }
        void QueryCheckList()
        {
            parmQuery queryparms = new parmQuery();
            queryparms.VersionID = Request.Params["Cid"].ToString();

            string sql = @"select InspectionContentID as id from tb_CommonlyInspection where VersionID= @VersionID ";

            DataSet ds = SQLHelper.QueryDataSet(CommandType.Text, sql, SQLHelper.ModelToParameterList(queryparms).ToArray());
            string jsonStr = JSONHelper.DataTableToJSON(ds.Tables[0]);
            Response.Write(jsonStr);
            Response.End();
        }
        public class pamRecord
        {
            public string Version { get; set; }
        }
        void QueryRecord()
        {
            pamRecord model = new pamRecord();
            model.Version = Request.Params["Version"];
            string sql = @"select a.CommonlyInspectionID from tb_CommonlyInspectionRecord a left join tb_CommonlyInspection b on a.CommonlyInspectionID= b.ID left 

                           join tb_CommonlyInspectionVersion c on c.Version = b.VersionID where c.Version=@Version";
            DataSet ds = SQLHelper.QueryDataSet(CommandType.Text, sql, SQLHelper.ModelToParameterList(model).ToArray());
            if (ds.Tables[0].Rows.Count > 0)
            {
                Response.Write("true");

            }
            else
            {
                Response.Write("false");

            }
            Response.End();
        }
        public class parmSave
        {
            public string ID { get; set; }
            public string CommonlyInspectionID { get; set; }
            public string DeviceID { get; set; }
            public string InspectionId { get; set; }
            public string Version { get; set; }
            public string Description { get; set; }
            public string UserID { get; set; }
            public string arrid { get; set; }
        }
        void SaveInfo()
        {
            string ID = Request.Params["ID"];
            string InspectionId = Request.Params["InspectionId"];
            string DeviceID = Request.Params["DeviceID"];
            string Version = Request.Params["Version"];
            string Description = Request.Params["Description"];
            string UserID = Request.Params["UserID"];

            CommonlyInspectionVersionBLL bll = new CommonlyInspectionVersionBLL();
            tb_CommonlyInspectionVersion md = new tb_CommonlyInspectionVersion();
            md.DeviceID = DeviceID;
            md.Version = Version;
            md.Description = Description;

            if (ID == "")
            {
                md.ID = Guid.NewGuid().ToString();
                md.CreateUserID = UserID;
                md.CreateTime = DateTime.Now;
                md.IsUseing = 0;
                Response.Write(bll.AddInfo(md, InspectionId) == true ? "true" : "false");
            }
            else
            {
                md.ID = ID;
                parmSave model = new parmSave();
                string sql_qrecord = @"select a.CommonlyInspectionID from tb_CommonlyInspectionRecord a left join tb_CommonlyInspection b on a.CommonlyInspectionID= b.ID left 

                           join tb_CommonlyInspectionVersion c on c.ID = b.VersionID where c.ID=@ID ";


                string sql_insert1 = @"INSERT INTO [tb_CommonlyInspection] ([ID],[VersionID],[InspectionContentID],[CreateUserID]) VALUES (@CommonlyInspectionID,@ID,@arrid,@UserID)";

                string sql_update = @"
                                    UPDATE tb_CommonlyInspectionVersion
                                       SET [DeviceID] = @DeviceID,
                                           [Version] = @Version,
                                           [Description] = @Description,
                                           [UpdateUserID] = @UserID,
                                           [UpdateTime] = getdate()
                                     WHERE [ID] = @ID";
                string sqldel = @"delete from [tb_CommonlyInspection]  where VersionID=@ID";


                model.ID = Request.Params["ID"];
                model.DeviceID = Request.Params["DeviceID"];
                model.Description = Request.Params["Description"];
                model.InspectionId = Request.Params["InspectionId"];
                model.Version = Request.Params["Version"];
                model.UserID = HttpContext.Current.Request.Cookies["LoginUserInfo"]["UserID"].ToString();
                try
                {
                    DataSet ds = SQLHelper.QueryDataSet(CommandType.Text, sql_qrecord, SQLHelper.ModelToParameterList(model).ToArray());
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Response.Write("false");

                    }
                    else
                    {
                        BasicBLL<tb_CommonlyInspectionVersion> db = new BasicBLL<tb_CommonlyInspectionVersion>();
                        var list = db.SearchInfoByKey("DeviceID", md.DeviceID).Where(p => p.Version == md.Version && p.ID != md.ID).ToList();//判断是否有重复数据
                        if (list.Count > 0)
                        {
                            Response.Write("false");
                        }
                        else
                        {
                            SQLHelper.ExecuteNonQuery(CommandType.Text, sql_update, SQLHelper.ModelToParameterList(model).ToArray());

                            SQLHelper.ExecuteNonQuery(CommandType.Text, sqldel, SQLHelper.ModelToParameterList(model).ToArray());
                            if (!string.IsNullOrEmpty(model.InspectionId))
                            {
                                string[] idArr = model.InspectionId.Split(',');
                                foreach (string id in idArr)
                                {
                                    if (id == "")
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        model.CommonlyInspectionID = Guid.NewGuid().ToString();
                                        model.arrid = id;
                                        SQLHelper.ExecuteNonQuery(CommandType.Text, sql_insert1, SQLHelper.ModelToParameterList(model).ToArray());
                                    }
                                }
                            }
                            Response.Write("true");
                        }
                    }
                }
                catch (Exception e)
                {
                    Response.Write("false");
                }
                Response.End();
            }
            Response.End();





        }
        public class parmDel
        {
            public string ID { get; set; }
            public string Version { get; set; }

        }
        void DelInfo()
        {
            parmDel model = new parmDel();
            string sqldel1 = @"delete from [tb_CommonlyInspectionVersion]  where ID=@ID";
            string sqldel2 = @"delete from [tb_CommonlyInspection]  where VersionID=@ID";
            model.ID = Request.Params["ID"].ToString();
            try
            {
                SQLHelper.ExecuteNonQuery(CommandType.Text, sqldel1, SQLHelper.ModelToParameterList(model).ToArray());

                SQLHelper.ExecuteNonQuery(CommandType.Text, sqldel2, SQLHelper.ModelToParameterList(model).ToArray());

                Response.Write("true");
            }
            catch
            {
                Response.Write("false");
            }
            Response.End();
        }
        public class parmOpen
        {
            public string ID { get; set; }
            public string UserID { get; set; }
        }
        void OpenInfo()
        {
            parmOpen model = new parmOpen();
            string sqlopen = @"update tb_CommonlyInspectionVersion
                                  SET  IsUseing=1,
                                       [UpdateUserID] = @UserID,
                                       [UpdateTime] = getdate()
                                       where ID=@ID";
            model.ID = Request.Params["ID"] == null ? "0" : Request.Params["ID"].ToString();
            model.UserID = HttpContext.Current.Request.Cookies["LoginUserInfo"]["UserID"].ToString();
            try
            {
                SQLHelper.ExecuteNonQuery(CommandType.Text, sqlopen, SQLHelper.ModelToParameterList(model).ToArray());
                Response.Write("true");
            }
            catch
            {
                Response.Write("false");
            }
            Response.End();

        }
        public class parmClose
        {
            public string ID { get; set; }
            public string UserID { get; set; }
        }
        void CloseInfo()
        {
            parmClose model = new parmClose();
            string sqlclose = @"update tb_CommonlyInspectionVersion
                                  SET  IsUseing=0,
                                       [UpdateUserID] = @UserID,
                                       [UpdateTime] = getdate()
                                       where ID=@ID";
            model.ID = Request.Params["ID"].ToString();
            model.UserID = HttpContext.Current.Request.Cookies["LoginUserInfo"]["UserID"].ToString();
            try
            {
                SQLHelper.ExecuteNonQuery(CommandType.Text, sqlclose, SQLHelper.ModelToParameterList(model).ToArray());
                Response.Write("true");
            }
            catch
            {
                Response.Write("false");
            }
            Response.End();

        }
        void QueryForCombobox()
        {
            string d_id = Request.Params["d_id"];
            CommonlyInspectionVersionBLL bll = new CommonlyInspectionVersionBLL();
            Response.Write(bll.GetComboboxData(d_id));
            Response.End();

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}