using MESClassLibrary.BLL.BasicInfo;
using MESClassLibrary.EFModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MESWebSite.HttpHandlers
{
    /// <summary>
    /// DeviceHandler 的摘要说明
    /// </summary>
    public class DeviceHandler : 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 "QueryOne":
                    QueryOne();
                    break;
                case "SaveInfo":
                    SaveInfo();
                    break;
                case "DelInfo":
                    DelInfo();
                    break;
                case "QueryForCombobox":
                    QueryForCombobox();
                    break;
                case "QueryForComboboxByLineID":
                    QueryForComboboxByLineID();
                    break;
                case "QueryForComboboxByStationID":
                    QueryForComboboxByStationID();
                    break;
                default:
                    break;

            }

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

        void QueryList()
        {
            string page = Request.Params["page"];
            string pagesize = Request.Params["rows"];
            string DeviceNo = Request.Params["DeviceNo"];
            string StationID = Request.Params["StationID"];


            if (string.IsNullOrEmpty(page))
            {
                page = "0";
            }
            if (string.IsNullOrEmpty(pagesize))
            {
                pagesize = "15";
            }
            DeviceBLL bll = new DeviceBLL();
            Response.Write(bll.SearchInfo(page, pagesize, DeviceNo, StationID));
            Response.End();


        }

        void QueryOne()
        {
            string DeviceID = Request.Params["DeviceID"];

            DeviceBLL bll = new DeviceBLL();
            Response.Write(bll.SearchInfoByID(DeviceID));
            Response.End();


        }


        void SaveInfo()
        {
            string DeviceID = Request.Params["DeviceID"];
            string StationID = Request.Params["StationID"];
            string DeviceNo = Request.Params["DeviceNo"];
            string DeviceName = Request.Params["DeviceName"];
            string DeviceModel = Request.Params["DeviceModel"];
            string FixNo = Request.Params["FixNo"];
            string Des = Request.Params["Des"];

            DeviceBLL bll = new DeviceBLL();
            tb_Device md = new tb_Device();

            md.StationID = StationID;
            md.DeviceNo = DeviceNo;
            md.DeviceName = DeviceName;
            md.DeviceModel = DeviceModel;
            md.FixNo = FixNo;
            md.Des = Des;
            var info = Request.Cookies.Get("LoginUserInfo");
            if (info != null)
            {
                md.UserID = info["UserID"].ToUpper();
            }
            if (DeviceID == "0")
            {
                //新增
                md.DeviceID = Guid.NewGuid().ToString();
                Response.Write(bll.AddInfo(md) == true ? "true" : "false");
            }
            else
            {
                //修改
                md.DeviceID = DeviceID;
                Response.Write(bll.UpdateInfo(md) == true ? "true" : "false");
            }
            Response.End();
        }
        void DelInfo()
        {
            string DeviceID = Request.Params["DeviceID"];

            DeviceBLL bll = new DeviceBLL();
            tb_Device md = new tb_Device();
            md.DeviceID = DeviceID;
            var info = Request.Cookies.Get("LoginUserInfo");
            if (info != null)
            {
                md.UserID = info["UserID"].ToUpper();
            }
            Response.Write(bll.DelInfo(md) == true ? "true" : "false");
            Response.End();

        }

        void QueryForCombobox()
        {
            DeviceBLL bll = new DeviceBLL();
            Response.Write(bll.GetComboboxData());
            Response.End();

        }

        void QueryForComboboxByLineID()
        {
            string fl_id = Request.Params["fl_id"];
            DeviceBLL bll = new DeviceBLL();
            Response.Write(bll.QueryForComboboxByLineID(fl_id));
            Response.End();
        }

        void QueryForComboboxByStationID()
        {
            string fl_id = Request.Params["fl_id"];
            DeviceBLL bll = new DeviceBLL();
            Response.Write(bll.QueryForComboboxByStationID(fl_id));
            Response.End();
        }
    }
}