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

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

        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;
                default:
                    break;

            }

            string page = Request.Params["page"];

            string urlPic = string.Empty;
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                if ("photo".Equals(files.AllKeys[0].ToString()))
                {
                    HttpPostedFile _upfile = context.Request.Files["photo"];
                    if (_upfile == null)
                    {
                        ResponseWriteEnd(context, "4");//请选择要上传的文件  
                    }
                    else
                    {
                        string fileName = _upfile.FileName;/*获取文件名: C:\Documents and Settings\Administrator\桌面\123.jpg*/
                        string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();/*获取后缀名并转为小写: jpg*/
                        int bytes = _upfile.ContentLength;//获取文件的字节大小  

                        //if (suffix != "jpg")
                        //    ResponseWriteEnd(context, "2"); //只能上传JPG格式图片  
                        if (bytes > 1024 * 1024)
                            ResponseWriteEnd(context, "3"); //图片不能大于1M  
                        string newFilename = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + suffix;
                        _upfile.SaveAs(HttpContext.Current.Server.MapPath("~/images/" + newFilename));//保存图片  
                        urlPic += "../images/" + newFilename;

                        ResponseWriteEnd(context, urlPic); //上传成功  
                    }
                }

                if ("video".Equals(files.AllKeys[0].ToString()))
                {
                    HttpPostedFile _upfile = context.Request.Files["video"];
                    if (_upfile == null)
                    {
                        ResponseWriteEnd(context, "4");//请选择要上传的文件  
                    }
                    else
                    {
                        string fileName = _upfile.FileName;/*获取文件名: C:\Documents and Settings\Administrator\桌面\123.jpg*/
                        string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();/*获取后缀名并转为小写: jpg*/
                        int bytes = _upfile.ContentLength;//获取文件的字节大小  

                        //if (suffix != "jpg")
                        //    ResponseWriteEnd(context, "2"); //只能上传JPG格式图片  
                        if (bytes > 1024 * 1024)
                            ResponseWriteEnd(context, "3"); //图片不能大于1M  
                        string newFilename = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + suffix;
                        _upfile.SaveAs(HttpContext.Current.Server.MapPath("~/video/" + newFilename));//保存图片  
                        urlPic += "../video/" + newFilename;

                        ResponseWriteEnd(context, urlPic); //上传成功  
                    }
                }

            }

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

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

            if (string.IsNullOrEmpty(page))
            {
                page = "0";
            }
            if (string.IsNullOrEmpty(pagesize))
            {
                pagesize = "15";
            }

            ImgVideoBLL bll = new ImgVideoBLL();
            Response.Write(bll.SearchInfoAll(page, pagesize, StartTime + " 00:00:00", EndTime + " 23:59:59"));
            Response.End();

        }

        void SaveInfo()
        {
            string Name = Request.Params["Name"];
            string fileUrl = Request.Params["fileUrl"];
            string fileType = Request.Params["fileType"];
            tb_ImgVideo imgVideo = new tb_ImgVideo();
            imgVideo.ID = Guid.NewGuid().ToString();
            imgVideo.fileName = Name;
            imgVideo.fileUrl = fileUrl;
            imgVideo.fileType = fileType;
            imgVideo.CreateTime = DateTime.Now;
            imgVideo.UpdateTime = DateTime.Now;

            ImgVideoBLL bll = new ImgVideoBLL();
            Response.Write(bll.AddInfo(imgVideo) == true ? "true" : "false");
            Response.End();

        }

        private void ResponseWriteEnd(HttpContext context, string msg)
        {
            context.Response.Write(msg);
            context.Response.End();
        }


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

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

        }

    }
}