
using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Model;
using CallCenterAPI.WechatSDK;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace CallCenterApi.Interface.Controllers
{
    public class HomeController : BaseController
    {
        // GET: Home
        public ActionResult Index()
        {
            //string str = "羽羽(111111) 2022-07-19 10:52:51  外线结果";
            //string sql = str.Substring(str.IndexOf(":") + 6);
            return Success("成功");
        }
        [Authorize]
        public JsonResult json1()
        {
            var person = new
            {
                Name = "张三",
                Age = 22,
                Sex = "男"
            };
            return Json(person, JsonRequestBehavior.AllowGet);
        }

        public string json2()
        {
            var person = new
            {
                Name = "张三",
                Age = 22,
                Sex = "男",
                Date = DateTime.Now
            };
            return person.ToJson();
        }



        public string json2_()
        {
            var tt = new
            {
                exp = 20,
                money = 12
            };
            var person = new
            {
                Name = "张三",
                Age = 22,
                Sex = "男",
                other = tt,
                Date = DateTime.Now
            };
            return person.ToJson();
        }

        /// <summary>
        /// 演示 查询出来datatable的数据进行展示
        /// </summary>
        /// <returns></returns>
        public string json3()
        {
            DataTable tblDatas = new DataTable("Datas");
            DataColumn dc = null;


            dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));

            DataRow newRow;
            newRow = tblDatas.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "2.0";
            newRow["Description"] = "我很喜欢";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.0";
            newRow["Description"] = "比大话更幼稚";
            tblDatas.Rows.Add(newRow);

            return tblDatas.ToJson();
        }

        public ActionResult json3_()
        {
            DataTable tblDatas = new DataTable("Datas");
            DataColumn dc = null;


            dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));

            DataRow newRow;
            newRow = tblDatas.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "2.0";
            newRow["Description"] = "我很喜欢";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.0";
            newRow["Description"] = "比大话更幼稚";
            tblDatas.Rows.Add(newRow);

            return Success("成功", tblDatas);
        }

        public ActionResult excel()
        {
            DataTable tblDatas = new DataTable("Datas");
            DataColumn dc = null;


            dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));

            DataRow newRow;
            newRow = tblDatas.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "2.0";
            newRow["Description"] = "我很喜欢";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.0";
            newRow["Description"] = "比大话更幼稚";
            tblDatas.Rows.Add(newRow);

            NPOIHelper npoi = new NPOIHelper();
            string[] s = { "列1", "列2", "列3" };
            if (npoi.ToExcel(tblDatas, "test", null, "D:/2.xlsx", s))
            {
                return Success("成功", tblDatas);
            }
            else
            {
                return Error("导出失败");
            }

        }
        public ActionResult excel1()
        {
            DataTable tblDatas = new DataTable("Datas");
            DataColumn dc = null;


            dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));

            DataRow newRow;
            newRow = tblDatas.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "2.0";
            newRow["Description"] = "我很喜欢";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.0";
            newRow["Description"] = "比大话更幼稚";
            tblDatas.Rows.Add(newRow);

            NPOIHelper npoi = new NPOIHelper();

            if (npoi.ExportToExcel("test", tblDatas) == "")
            {
                return Success("成功", tblDatas);
            }
            else
            {
                return Error("导出失败");
            }

        }

        public ActionResult error()
        {
            return Error("不具备权限");
        }

        public string ss()
        {
            int ss = 22;
            int z = 0;

            return (ss/z).ToString();
        }

        public string tt(string name, string pass)
        {
            return "sss  " + name + pass;
        }

        /// <summary>
        /// 抛出HTTP 500
        /// </summary>
        /// <returns></returns>        
        public ActionResult ThrowHttp500()
        {
            throw new HttpException(500, "服务器错误");
        }
        /// <summary>
        /// 抛出HTTP 404
        /// </summary>
        /// <returns></returns>
        public ActionResult ThrowHttp404()
        {
            throw new HttpException(404, "页面未找到");
        }

        /// <summary>
        /// 抛出未引用对象异常
        /// ，此处单独使用HandleError特性
        /// ，并指定异常类型及响应视图
        /// </summary>
        /// <returns></returns>
        [HandleError(ExceptionType = typeof(NullReferenceException), View = "CustomError")]
        public ActionResult ThrowNullReferenceException()
        {
            throw new NullReferenceException();
        }
        /// <summary>
        /// 引发输入字符串的格式不正确异常
        /// ，此处指定了响应的错误页面
        /// ，由于是不同的控制器所以要完整的相对路径
        /// </summary>
        /// <returns></returns>
        [HandleError(View = "~/Views/Error/CustomHttpError.cshtml")]
        public ActionResult ThrowFormatException()
        {
            string str = "";
            int count = Convert.ToInt32(str);
            return View("Index");
        }


        public string  Testjson()
        {
            string jsonstr = "";
            List<Material> matmodels = new List<Material>();
            matmodels.Add(new Material { materialcode = "001", materialname = "物料名称", formula = "正常", returnum = 5 });
            matmodels.Add(new Material { materialcode = "002", materialname = "物料名称2", formula = "正常2", returnum = 50 });
            matmodels.Add(new Material { materialcode = "003", materialname = "物料名称3", formula = "正常3", returnum = 500 });
            return matmodels.ToJson();


        }

        public ActionResult TestjsonToObj(string jsonstr)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();   //实例化⼀个能够序列化数据的类
            List<Material> list = Serializer.Deserialize<List<Material>>(jsonstr);

            if (list != null && list.Count>0)
            {
                foreach (var item in list)
                {


                }
            }
           
            return Success("成功");
        }

        public class Material
        {
            /// <summary>
            /// 物料编号
            /// </summary>
            public string materialcode { get; set; }

            /// <summary>
            /// 产品名称
            /// </summary>
            public string materialname { get; set; }

            /// <summary>
            /// 规格
            /// </summary>
            public string formula { get; set; }

            /// <summary>
            /// 返厂数量
            /// </summary>
            public int returnum { get; set; }
        }

        public class copymessage
        {
            public string deptid { get; set; }
            public List<string> ucodelist { get; set; }
        }

        public string copyson()
        {
            string jsonstr = "";
            List<copymessage> matmodels = new List<copymessage>();

            matmodels.Add(new copymessage { deptid = "001", ucodelist = new List<string> { "8001", "8002", "8003" } });
            matmodels.Add(new copymessage { deptid = "002", ucodelist =new List<string> { "8001", "8002", "8003" } });

            return matmodels.ToJson();


        }

        public ActionResult mytest(string question2)
        {
            string sql = "";
            string squsdf = string.Format("'{0}'", question2.Replace(",", "','"));
            var QuestionManage = new BLL.T_Wo_QuestionManage().GetModelList("F_QuestionName in (" + squsdf + ")");
            if (QuestionManage != null)
            {
                if (QuestionManage.FirstOrDefault().F_Type == EnumWOType.Complaint.ToString())
                {
                    sql += $"  and T_Wo_WorkOrder.F_QuestionType  in (" + string.Join(",", QuestionManage.Select(T => T.F_Id)) + ")"; ;
                }
            }
            return Success(sql);
        }


    }
}