using MESClassLibrary.BLL.BasicInfo;
using MESClassLibrary.EFModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace MESWebSite.HttpHandlers
{
    /// <summary>
    /// PaintColorInfoHandler 的摘要说明
    /// </summary>
    public class PaintColorInfoHandler : 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 "QueryExcel":
                    QueryExcel();
                    break;

                default:
                    break;

            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        void QueryList()
        {
            string page = Request.Params["page"];
            string pagesize = Request.Params["rows"];
            string StockNo = Request.Params["StockNo"];
            string Paint_No = Request.Params["Paint_No"];
            
            if (string.IsNullOrEmpty(page))
            {
                page = "0";
            }
            if (string.IsNullOrEmpty(pagesize))
            {
                pagesize = "15";
            }
            PaintColorInfoBLL bll = new PaintColorInfoBLL();
            Response.Write(bll.SearchInfoAll(page, pagesize, StockNo, Paint_No));
            Response.End();


        }

        void SaveInfo()
        {
            string ID = Request.Params["ID"];
            string Color = Request.Params["Color"];
            string Paint_No = Request.Params["Paint_No"];
            string StockNo = Request.Params["StockNo"];

            PaintColorInfoBLL bll = new PaintColorInfoBLL();
            tb_PaintColorInfo md = new tb_PaintColorInfo();

            md.Color = Color;
            md.Paint_No = Paint_No;
            md.StockNo = StockNo;


            if (ID == "0")
            {
                //新增
                md.ID = Guid.NewGuid().ToString();
                Response.Write(bll.AddInfo(md) == true ? "true" : "false");
            }
            else
            {
                //修改
                md.ID = ID;
                Response.Write(bll.UpdateInfo(md) == true ? "true" : "false");
            }
            Response.End();
        }

        void DelInfo()
        {
            string ID = Request.Params["ID"];

            PaintColorInfoBLL bll = new PaintColorInfoBLL();
            tb_PaintColorInfo md = new tb_PaintColorInfo();
            md.ID = ID;
            Response.Write(bll.DeleteInfo(md) == true ? "true" : "false");
            Response.End();

        }

        void QueryExcel()
        {
            string StockNo = Request.Params["StockNo"];
            string PaintNo = Request.Params["PaintNo"];

            PaintColorInfoBLL bll=new PaintColorInfoBLL();

            List<List<string>> list = bll.SearchForExcel(PaintNo, StockNo);
            XSSFWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("Sheet1");

            for (int i = 0; i < list.Count; i++)
            {
                IRow row = sheet.CreateRow(i);

                for (int k = 0; k < list[i].Count; k++)
                {
                    row.CreateCell(k).SetCellValue(list[i][k].ToString());
                }

            }

            // 写入到客户端  
            MemoryStream ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
    }
}