using CallCenter.Utility;
using CallCenter.Utility.Linq;
using CallCenter.Utility.Time;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Dto;
using CallCenterApi.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.report
{
    [Authority]
    public class WorkOrderReportController : BaseController
    {
        private readonly BLL.T_Sys_Department departmentBLL = new BLL.T_Sys_Department();
        private readonly BLL.T_Bus_WorkOrder workOrderBLL = new BLL.T_Bus_WorkOrder();
        private readonly BLL.T_Bus_AssignedInfo assignedInfoBLL = new BLL.T_Bus_AssignedInfo();
        private readonly BLL.T_Bus_Feedback feedbackBLL = new BLL.T_Bus_Feedback();
        private readonly BLL.T_Sys_DictionaryValue dictValueBLL = new BLL.T_Sys_DictionaryValue();
        private readonly BLL.T_Sys_Area areaBLL = new BLL.T_Sys_Area();
        private readonly BLL.T_Bus_VisitResult visitResultBLL = new BLL.T_Bus_VisitResult();
        private readonly BLL.T_Bus_DelayTime delayTimeBLL = new BLL.T_Bus_DelayTime();
        private readonly BLL.T_Sys_UserAccount userAccount = new BLL.T_Sys_UserAccount();
        private readonly BLL.T_Bus_SubmitSuper submitsuperBLL = new BLL.T_Bus_SubmitSuper();

        ///// <summary>
        ///// 处理数量统计
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult GetDeptCount(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTools.GetWeekFirstDayMon(DateTime.Now);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTools.GetWeekLastDaySun(DateTime.Now);
        //    }

        //    //部门
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_DeptNameSpell=3 ").Tables[0]);
        //    var distinctQuery = feedbackBLL.DataTableToList(feedbackBLL.GetList($"  F_IsFeedEnd=1 and F_Type in (1,2) and  F_State=1 and F_IsDelete not in (1)  and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]).DistinctBy(x => x.F_WorkOrderId).ToList();
        //    List<DeptCountDto> list = new List<DeptCountDto>();
        //    foreach (var item in deptList)
        //    {
        //        var count = distinctQuery.Where(y => y.F_CreateDeptId == item.F_DeptId).Count();
        //        list.Add(new DeptCountDto
        //        {
        //            deptname = item.F_DeptName ?? "",
        //            count = count
        //        });
        //    }

        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "deptname", "count" });
        //        new NPOIHelper().ExportToExcel($"处理数量统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "单位名称", "数量" });
        //        return Success("导出excel");
        //    }
        //    var obj = new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    };

        //    return Content(obj.ToJson());
        //}

        /// <summary>
        /// 处理数量统计
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptCount(DateTime? start, DateTime? end, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }

            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_DeptDealReport", paras, "DeptDealReport");
            if (isExport)
            {
                var cols = new string[] { "单位名称", "数量" };
                new NPOIHelper().ExportToExcel($"处理数量统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", obj.Tables[0], cols);
                return Success("导出excel");
            }
            return Success("成功", obj);

        }

        ///// <summary>
        /////  处理效率统计
        ///// </summary>
        ///// <returns></returns> 
        //public ActionResult GetDeptEfficiency(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }

        //    //部门
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_DeptNameSpell=3 ").Tables[0]);
        //    var workOrderList = new BLL.T_Bus_WorkOrderReport().DataTableToEfficiencyList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,C.F_CreateDeptId,B.F_IsOverdue,B.F_IsSatisfie FROM T_Bus_WorkOrder A LEFT JOIN T_Bus_AssignedInfo B ON A.F_WorkOrderId = B.F_WorkOrderId LEFT JOIN T_Bus_Feedback C ON A.F_WorkOrderId = C.F_WorkOrderId WHERE  datediff(s, '{start}', A.F_CreateTime) > 0  and   datediff(s, '{end}', A.F_CreateTime) < 0  and A.F_IsDelete = 0 AND A.F_WorkOrderId IS NOT NULL AND  B.F_IsDelete = 0 and B.F_IsSure in (0, 1, 2) and  B.F_State in (0, 1) AND C.F_IsFeedEnd = 1 and C.F_Type in (1,2) and C.F_State = 1 and C.F_IsDelete not in (1) ").Tables[0]).DistinctBy(x => x.WorkOrderId).ToList();

        //    //var feedbackList = feedbackBLL.DataTableToList(feedbackBLL.GetList($" F_IsFeedEnd=1 and F_Type in (1,2) and  F_State=1 and F_IsDelete not in (1)  and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]).DistinctBy(x => x.F_WorkOrderId).ToList();
        //    //var assignedList = assignedInfoBLL.DataTableToList(assignedInfoBLL.GetList($" F_IsDelete=0 and F_IsSure in (0,1,2) and  F_State in (0,1)   and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]).DistinctBy(x => x.F_WorkOrderId);

        //    //var deptJoinAssignedQ = deptList.Join(
        //    //                        assignedList,
        //    //                        a => a.F_DeptId,
        //    //                        b => b.F_MainDeptId,
        //    //                        (a, b) => new { a.F_DeptName, b.F_WorkOrderId, b.F_IsOverdue });
        //    //var feedbackS = feedbackList.Select(x => x.F_WorkOrderId).ToList();
        //    //var deptJoinAssignedQW = deptJoinAssignedQ.Where(x => feedbackS.Contains(x.F_WorkOrderId)).ToList();

        //    List<DeptEfficiencyDto> list = new List<DeptEfficiencyDto>();
        //    deptList.ForEach(x =>
        //    {
        //        var deptWorkOrderList = workOrderList.Where(y => x.F_DeptId == y.DeptId).ToList();

        //        var deptname = x.F_DeptName ?? "";
        //        var count = deptWorkOrderList.Count();
        //        var intimeCount = deptWorkOrderList.Where(y => y.IsOverdue).Count();
        //        var outtimeCount = deptWorkOrderList.Where(y => y.IsSatisfie).Count();
        //        list.Add(new DeptEfficiencyDto
        //        {
        //            deptname = x.F_DeptName ?? "",
        //            count = count,
        //            intimecount = intimeCount,
        //            outtimecount = outtimeCount,
        //            efficrate = count == 0 ? "0%" : (intimeCount == 0 ? "0%" : ((double)intimeCount / count * 100).ToString("f2") + "%")
        //        });
        //    });

        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "deptname", "count", "intimecount", "outtimecount", "efficrate" });
        //        new NPOIHelper().ExportToExcel($"处理效率统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "单位名称", "处理总量", "及时量", "超时量", "处理及时率" });
        //        return Success("导出excel");
        //    }
        //    var obj = new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    };

        //    return Content(obj.ToJson());
        //}

        ///// <summary>
        ///// 市民满意度评价
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult GetDeptSatisfied(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }

        //    //部门
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_DeptNameSpell=3 ").Tables[0]);
        //    var feedbackList = feedbackBLL.DataTableToList(feedbackBLL.GetList($"  F_IsFeedEnd=1 and F_Type in (1,2) and  F_State=1 and F_IsDelete not in (1)  and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]).DistinctBy(x => x.F_WorkOrderId);
        //    var assignedList = assignedInfoBLL.DataTableToList(assignedInfoBLL.GetList($" F_IsDelete=0 and F_IsSure in (0,1,2) and  F_State in (0,1)   and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]).DistinctBy(x => x.F_WorkOrderId);
        //    var deptJoinAssignedQ = deptList.Join(
        //                            assignedList,
        //                            a => a.F_DeptId,
        //                            b => b.F_MainDeptId,
        //                            (a, b) => new { a.F_DeptName, b.F_WorkOrderId, b.F_IsSatisfie });
        //    var feedbackS = feedbackList.Select(x => x.F_WorkOrderId).ToList();
        //    var deptJoinAssignedQW = deptJoinAssignedQ.Where(x => feedbackS.Contains(x.F_WorkOrderId)).ToList();
        //    List<DeptSatisfiedDto> list = new List<DeptSatisfiedDto>();
        //    deptList.ForEach(x =>
        //    {
        //        var deptJoinAssignedQWW = deptJoinAssignedQW.Where(y => y.F_DeptName == x.F_DeptName).ToList();

        //        var deptname = x.F_DeptName ?? "";
        //        var count = deptJoinAssignedQWW.Count();
        //        var satisfiedCount = deptJoinAssignedQWW.Where(y => y.F_IsSatisfie == 0).Count();
        //        var notsatisfiedCount = deptJoinAssignedQWW.Where(y => y.F_IsSatisfie == 1).Count();
        //        list.Add(new DeptSatisfiedDto
        //        {
        //            deptname = x.F_DeptName ?? "",
        //            count = count,
        //            satisfiedcount = satisfiedCount,
        //            notsatisfiedcount = notsatisfiedCount,
        //            satisfiedrate = count == 0 ? "0%" : (satisfiedCount == 0 ? "0%" : ((double)satisfiedCount / count * 100).ToString("f2") + "%")
        //        });
        //    });
        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "deptname", "count", "satisfiedcount", "notsatisfiedcount", "satisfiedrate" });
        //        new NPOIHelper().ExportToExcel($"市民满意度评价报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "单位名称", "处理总量", "满意总量", "处理总量", "满意度" });
        //        return Success("导出excel");
        //    }
        //    var obj = new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    };

        //    return Content(obj.ToJson());
        //}

        /// <summary>
        ///  处理效率统计
        /// </summary>
        /// <returns></returns> 
        public ActionResult GetDeptEfficiency(DateTime? start, DateTime? end, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_DeptEfficiencyReport", paras, "DeptEfficiencyReport");
            if (isExport)
            {
                string[] cols = new string[] { "单位名称", "处理总量", "及时量", "超时量", "处理及时率" };
                NPOIHelper npoi = new NPOIHelper();
                if (npoi.ExportToExcel("处理效率统计报表", obj.Tables[0], cols) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return Success("成功", obj);
        }

        /// <summary>
        /// 市民满意度评价
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptSatisfied(DateTime? start, DateTime? end, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            //string sql = $"select * from dbo.GetDeptSatisfied('{start.Value.ToString("yyyy-MM-dd")}','{end.Value.ToString("yyyy-MM-dd")}') ORDER BY rate DESC";
            //var dtall = DbHelperSQL.Query(sql).Tables[0];
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_DeptSatisfiedReport", paras, "DeptSatisfiedReport");

            if (isExport)
            {
                string[] cols = new string[] {  "单位名称", "处理总量", "满意总量", "不满意总量", "满意度" };
                new NPOIHelper().ExportToExcel($"满意度统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", obj.Tables[0], cols);
                return Success("导出excel");
            }
            return Success("成功", obj);
            //int recordCount = 0;
            //var dt = BLL.PagerBLL.GetListPager(
            //   "("+sql+") as v",
            //    "deptname",
            //    "*",
            //    "",
            //    "ORDER BY rate DESC",
            //    pageSize,
            //    pageIndex,
            //    true,
            //    out recordCount);

            //var obj = new
            //{
            //    state = "success",
            //    message = "成功",
            //    rows = dt,
            //    total = recordCount
            //};

            //return Content(obj.ToJson());
        }

        ///// <summary>
        ///// 单位绩效考核
        ///// </summary>
        ///// <param name="start"></param>
        ///// <param name="end"></param>
        ///// <returns></returns>
        //public ActionResult GetDeptAssessment(DateTime? start, DateTime? end, int deptId = -1, bool isExport = false)
        //{
        //    StringBuilder sb1 = new StringBuilder();
        //    StringBuilder sb2 = new StringBuilder();
        //    if (start == null)
        //    {
        //        start = DateTime.Now.AddDays(-7);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.AddDays(1).AddSeconds(-1);
        //    }
        //    //部门
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_DeptNameSpell=3 ").Tables[0]);
        //    //var feedbackList = feedbackBLL.DataTableToList(feedbackBLL.GetList($"  F_IsFeedEnd=1 and F_Type in (1,2) and  F_State=1 and F_IsDelete not in (1)  and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0 ").Tables[0]).DistinctBy(y => y.F_WorkOrderId).ToList();
        //    var assignedList = assignedInfoBLL.DataTableToList(assignedInfoBLL.GetList($" F_IsDelete = 0 and F_IsSure in (0, 1, 2) and  F_State in (0, 1)   and datediff(s,'{start}', F_CreateTime)> 0  and datediff(s,'{end}', F_CreateTime)< 0  ").Tables[0]);
        //    var visitList = visitResultBLL.DataTableToList(visitResultBLL.GetList($"   datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0   ").Tables[0]);
        //    var delayList = delayTimeBLL.DataTableToList(delayTimeBLL.GetList($"  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]);
        //    List<DeptAssessmentDto> list = new List<DeptAssessmentDto>();
        //    double satisfiedsum = 0;
        //    double notsatisfiedsum = 0;
        //    double intimsum = 0;

        //    deptList.ForEach(x =>
        //    {
        //        var deptname = x.F_DeptName ?? "";
        //        if (deptname == "市公安局")
        //        {
        //            var t = 1;
        //        }
        //        //交办总量
        //        var query = assignedList.Where(y => y.F_MainDeptId == x.F_DeptId).DistinctBy(y => y.F_WorkOrderId).ToList();
        //        var workOrderIdQuery = query.Select(y => y.F_WorkOrderId).ToList();
        //        var assignedcount = query.Count();
        //        //已办理总量
        //        //var completedcount = feedbackList.Where(y => y.F_CreateDeptId == x.F_DeptId ).Count();
        //        var completedcount = query.Where(y => y.F_FeedbackTime != null).Count();
        //        //及时量
        //        double intimecount = query.Where(y =>y.F_FeedbackTime != null && y.F_LimitTime>=y.F_FeedbackTime).Count();
        //        intimsum += intimecount;
        //        //超时量
        //        var outtimeQuery = query.Where(y =>y.F_FeedbackTime != null && y.F_LimitTime >= y.F_FeedbackTime).ToList();
        //        //超时均长
        //        var outtimecount = outtimeQuery.Count();
        //        var avgouttime = (from o in outtimeQuery
        //                          select new
        //                          {
        //                              differ = (DateTime)o.F_FeedbackTime - (DateTime)o.F_LimitTime
        //                          }).Where(y => y.differ.Ticks > 0);

        //        var hours = 0;
        //        if (avgouttime.Count() > 0)
        //        {
        //            var avg = avgouttime.Average(y => y.differ.Ticks);
        //            hours = (DateTime.MinValue.AddTicks((long)avg) - DateTime.MinValue).Hours;
        //        }

        //        //延时量
        //        var delayedcount = delayList.Where(y => workOrderIdQuery.Contains(y.F_WorkOrderId)).Count();
        //        //处理均长
        //        var completedtimeQuery = (from o in query
        //                                  select new
        //                                  {
        //                                      differ = (o.F_FeedbackTime == null || o.F_SureTime == null) ? 0 : ((o.F_FeedbackTime ?? DateTime.MinValue) - (o.F_SureTime ?? DateTime.MinValue)).Ticks
        //                                  }).Where(y => y.differ > 0).ToList();

        //        var avgcompletedtime = (double)0;
        //        var maxcompletedtime = (double)0;
        //        if (completedtimeQuery.Count() > 0)
        //        {
        //            avgcompletedtime = new TimeSpan((long)completedtimeQuery.Average(y => y.differ)).Hours;
        //            //最大处理时长
        //            maxcompletedtime = new TimeSpan(completedtimeQuery.Max(y => y.differ)).Hours;

        //        }


        //        //回访量
        //        var visitCount = visitList.Where(y => workOrderIdQuery.Contains(y.F_WorkOrderId)).Count();
        //        //满意量(满意率)
        //        double satisfiedcount = query.Where(y => y.F_IsSatisfie == 1).Count();
        //        //不满意量(不满意率)
        //        double notsatisfiedcount = query.Where(y => y.F_IsSatisfie == 0).Count();
        //        satisfiedsum += satisfiedcount;
        //        notsatisfiedsum += notsatisfiedcount;
        //        list.Add(new DeptAssessmentDto
        //        {
        //            deptname = x.F_DeptName ?? "",
        //            assignedcount = assignedcount,
        //            completedcount = completedcount,
        //            intime = completedcount == 0 ? "0(0%)" : $"{intimecount}({(intimecount == 0 ? "0%" : ((intimecount / completedcount * 100).ToString("f2")) + "%")})",
        //            outtimecount = outtimecount,
        //            avgouttime = hours,
        //            delayedcount = delayedcount,
        //            avghandletime = avgcompletedtime,
        //            maxhandletime = maxcompletedtime,
        //            visitcount = visitCount,
        //            satisfiedrate = assignedcount == 0 ? "0(0%)" : $"{satisfiedcount}({(satisfiedcount == 0 ? "0%" : (satisfiedcount / assignedcount * 100).ToString("f2"))}%)",
        //            notsatisfiedrate = assignedcount == 0 ? "0(0%)" : $"{notsatisfiedcount}({(notsatisfiedcount == 0 ? "0%" : (notsatisfiedcount / assignedcount * 100).ToString("f2") + "%")})",
        //        });
        //    });
        //    var assignedsum = list.Sum(x => x.assignedcount);
        //    var completedsum = list.Sum(x => x.completedcount);
        //    var count = list.Count();
        //    var total = new
        //    {
        //        assignedcount = list.Sum(x => x.assignedcount),
        //        completedcount = list.Sum(x => x.completedcount),
        //        intime = completedsum == 0 ? $"0(0%)" : $"{intimsum}({(intimsum == 0 ? "0%" : (intimsum / completedsum * 100).ToString("f2"))}%)",
        //        outtimecount = list.Sum(x => x.outtimecount),
        //        avgouttime = list.Sum(x => x.outtimecount) / list.Count(),
        //        delayedcount = list.Sum(x => x.delayedcount),
        //        avghandletime = count > 0 ? Math.Floor((double)list.Sum(x => x.avghandletime) / count) : 0,
        //        maxhandletime = count > 0 ? list.Max(x => x.maxhandletime) : 0,
        //        visitcount = list.Sum(x => x.visitcount),
        //        satisfiedrate = assignedsum == 0 ? $"0(0%)" : $"{satisfiedsum}({(satisfiedsum == 0 ? "0%" : (satisfiedsum / assignedsum * 100).ToString("f2"))}%)",
        //        notsatisfiedrate = assignedsum == 0 ? $"0(0%)" : $"{notsatisfiedsum}({(notsatisfiedsum == 0 ? "0%" : (notsatisfiedsum / assignedsum * 100).ToString("f2") + "%")})",
        //    };
        //    if (isExport)
        //    {
        //        list.Add(new DeptAssessmentDto
        //        {
        //            deptname = "总计",
        //            assignedcount = total.assignedcount,
        //            completedcount = total.completedcount,
        //            intime = total.intime,
        //            outtimecount = total.outtimecount,
        //            avgouttime = total.avgouttime,
        //            delayedcount = total.delayedcount,
        //            avghandletime = total.avghandletime,
        //            maxhandletime = total.maxhandletime,
        //            visitcount = total.visitcount,
        //            satisfiedrate = total.satisfiedrate,
        //            notsatisfiedrate = total.notsatisfiedrate
        //        });
        //        var dt = list.ToDataTable(new string[] { "deptname", "completedcount", "intime", "outtimecount", "avgouttime", "delayedcount", "avghandletime", "maxhandletime", "visitcount", "satisfiedrate", "notsatisfiedrate" });
        //        new NPOIHelper().ExportToExcel($"单位绩效考核报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "单位名称", "坐席", "手工录单数", "通话成单数", "当即办理数", "交办量", "已办理", "回访量", "市民满意", "市民不满意" });
        //        return Success("导出excel");
        //    }
        //    return Success("", new
        //    {
        //        list = list,
        //        total = total
        //    });
        //}

        /// <summary>
        /// 单位绩效考核
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public ActionResult GetDeptAssessment(DateTime? start, DateTime? end, bool isExport = false)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_DeptAssessmentReport", paras, "DeptAssessmentReport");
            if (isExport)
            {
                string[] cols = new string[] { "单位名称", "已办总量", "及时量", "超时量", "超时时长(小时)", "延时量", "处理时长(小时)", "平均处理时长(小时)", "最大处理时长(小时)", "回访量", "市民满意", "市民不满意" };
                NPOIHelper npoi = new NPOIHelper();
                if (npoi.ExportToExcel("单位绩效考核报表", obj.Tables[0], cols) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return Success("成功", obj);
        }

        ///// <summary>
        ///// 坐席工作统计
        ///// </summary>
        ///// <param name="start"></param>
        ///// <param name="end"></param>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public ActionResult GetUserAccountAssessment(DateTime? start, DateTime? end, int deptId = -1, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }
        //    string where = "";
        //    if (deptId > 0)
        //    {
        //        where = " AND  F_DeptId=" + deptId;
        //    }
        //    var userList = userAccount.DataTableToList(userAccount.GetList(" F_DeleteFlag=0 " + where).Tables[0]);
        //    var workOrderList = workOrderBLL.DataTableToList(workOrderBLL.GetList($" F_IsDelete=0 and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]);
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList($"  F_DeptNameSpell=3  ").Tables[0]);
        //    var assignedList = assignedInfoBLL.DataTableToList(assignedInfoBLL.GetList($" F_IsDelete=0 and F_IsSure in (0,1,2) and  F_State in (0,1)   and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]).DistinctBy(x => x.F_WorkOrderId);
        //    var assignedIdList = assignedList.Select(x => x.F_WorkOrderId);
        //    var assignedSatisfiedIdList = assignedList.Where(y => y.F_IsSatisfie == 1).Select(x => x.F_WorkOrderId);
        //    var assignedNotSatisfiedIdList = assignedList.Where(y => y.F_IsSatisfie == 0).Select(x => x.F_WorkOrderId);
        //    var feedbackList = feedbackBLL.DataTableToList(feedbackBLL.GetList($"  F_IsFeedEnd=1 and F_Type in (1,2) and  F_State=1 and F_IsDelete not in (1)  and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0   ").Tables[0]).DistinctBy(x => x.F_WorkOrderId).Select(x => x.F_WorkOrderId);
        //    var visitResultList = visitResultBLL.DataTableToList(visitResultBLL.GetList($"   datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0   ").Tables[0]).Select(x => x.F_WorkOrderId);
        //    List<UserAccountAssessmentDto> list = new List<UserAccountAssessmentDto>();
        //    double satisfiedsum = 0;
        //    double notsatisfiedsum = 0;
        //    foreach (var u in userList)
        //    {
        //        var dept = deptList.SingleOrDefault(y => y.F_DeptId == u.F_DeptId);
        //        if (dept == null)
        //            continue;
        //        var deptName = dept.F_DeptName ?? "";
        //        var workOrderQuery = workOrderList.Where(x => x.F_CreateUser == u.F_UserCode);
        //        var workOrderCount = workOrderQuery.Count();
        //        var manualcount = workOrderQuery.Where(x => x.F_CallRecordId != null).Count();
        //        var assignedcount = workOrderQuery.Where(x => assignedIdList.Contains(x.F_WorkOrderId)).Count();
        //        double satisfiedcount = workOrderQuery.Where(x => assignedSatisfiedIdList.Contains(x.F_WorkOrderId)).Count();
        //        double notsatisfiedcount = workOrderQuery.Where(x => assignedNotSatisfiedIdList.Contains(x.F_WorkOrderId)).Count();
        //        satisfiedsum += satisfiedcount;
        //        notsatisfiedsum += notsatisfiedcount;
        //        list.Add(new UserAccountAssessmentDto
        //        {
        //            detpname = deptName,
        //            useraccount = $"{u.F_UserName}({u.F_UserCode})",
        //            manualcount = manualcount,
        //            talkcount = workOrderCount - manualcount,
        //            immecount = workOrderQuery.Where(x => x.F_IsResult == 1).Count(),
        //            assignedcount = assignedcount,
        //            completedcount = workOrderQuery.Where(x => feedbackList.Contains(x.F_WorkOrderId)).Count(),
        //            visitcount = workOrderQuery.Where(x => visitResultList.Contains(x.F_WorkOrderId)).Count(),
        //            satisfiedrate = assignedcount == 0 ? $"0(0%)" : $"{satisfiedcount}({(satisfiedcount == 0 ? "0%" : ((satisfiedcount / assignedcount * 100).ToString("f2") + "%"))})",
        //            notsatisfiedrate = assignedcount == 0 ? $"0(0%)" : $"{notsatisfiedcount}({(notsatisfiedcount == 0 ? "0%" : ((notsatisfiedcount / assignedcount * 100).ToString("f2") + "%"))})",

        //        });
        //    }
        //    var assignedsum = list.Sum(x => x.assignedcount);
        //    var total = new
        //    {
        //        manualcount = list.Sum(x => x.manualcount),
        //        talkcount = list.Sum(x => x.talkcount),
        //        immecount = list.Sum(x => x.immecount),
        //        assignedcount = assignedsum,
        //        completedcount = list.Sum(x => x.completedcount),
        //        visitcount = list.Sum(x => x.visitcount),
        //        satisfiedrate = assignedsum == 0 ? $"0(0%)" : $"{satisfiedsum}({(satisfiedsum == 0 ? "0%" : ((satisfiedsum / assignedsum * 100).ToString("f2") + "%"))})",
        //        notsatisfiedrate = assignedsum == 0 ? $"0(0%)" : $"{notsatisfiedsum}({(notsatisfiedsum == 0 ? "0%" : ((notsatisfiedsum / assignedsum * 100).ToString("f2") + "%"))})",

        //    };
        //    if (isExport)
        //    {
        //        list.Add(new UserAccountAssessmentDto
        //        {
        //            detpname = "总",
        //            useraccount = "计",
        //            manualcount = total.manualcount,
        //            talkcount = total.talkcount,
        //            immecount = total.immecount,
        //            assignedcount = total.assignedcount,
        //            completedcount = total.completedcount,
        //            visitcount = total.visitcount,
        //            satisfiedrate = total.satisfiedrate,
        //            notsatisfiedrate = total.notsatisfiedrate
        //        });
        //        var dt = list.ToDataTable(new string[] { "detpname", "useraccount", "manualcount", "talkcount", "immecount", "assignedcount", "completedcount", "visitcount", "satisfiedrate", "notsatisfiedrate", });
        //        new NPOIHelper().ExportToExcel($"坐席工作统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "单位名称", "交办总量", "已办理总量", "及时量", "超时量", "超市均长(小时)", "延时量", "处理均长(小时)", "最大处理时间(小时)", "回访量", "市民满意", "市民不满意", });
        //        return Success("导出excel");
        //    }
        //    return Success("", new
        //    {
        //        list = list,
        //        total = total
        //    });
        //}

        ///// <summary>
        ///// 逾期未回复统计
        ///// </summary>
        ///// <param name="date"></param>
        ///// <param name="deptId"></param>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public ActionResult GetOutTimeNotApply(DateTime? start, DateTime? end, int deptId = -1, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }
        //    var where = "";
        //    if (deptId > 0)
        //    {
        //        where = " AND  F_MainDeptId=" + deptId;
        //    }
        //    var workOrderList = workOrderBLL.DataTableToList(workOrderBLL.GetList($" F_IsDelete=0 and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]);
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList($" F_DeptNameSpell=3   ").Tables[0]);
        //    var assignedList = assignedInfoBLL.DataTableToList(assignedInfoBLL.GetList($" F_IsDelete=0 and F_IsSure in (0,1,2) and  F_State in (0,1)   and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  " + where).Tables[0]).DistinctBy(x => x.F_WorkOrderId);

        //    List<OutTimeNotReplyDto> list = new List<OutTimeNotReplyDto>();
        //    foreach (var item in assignedList)
        //    {
        //        var deptName = deptList.SingleOrDefault(x => x.F_DeptId == item.F_MainDeptId)?.F_DeptName ?? "";
        //        if (string.IsNullOrWhiteSpace(deptName))
        //            continue;
        //        var workorder = workOrderList.SingleOrDefault(x => x.F_WorkOrderId == item.F_WorkOrderId);
        //        if (workorder == null)
        //            continue;
        //        list.Add(new OutTimeNotReplyDto
        //        {
        //            deptname = deptName,
        //            content = workorder?.F_ComContent ?? "",
        //            time = item.F_CreateTime == null ? "" : Convert.ToDateTime(item.F_CreateTime).ToString("yyyy-MM-dd HH:mm:ss"),
        //            limit = item.F_LimitTime == null ? "" : Convert.ToDateTime(item.F_LimitTime).ToString("yyyy-MM-dd HH:mm:ss"),
        //            reply = item.F_FeedbackTime == null ? "" : Convert.ToDateTime(item.F_FeedbackTime).ToString("yyyy-MM-dd HH:mm:ss")
        //        });
        //    }
        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "deptname", "content", "time", "limit", "reply" });
        //        new NPOIHelper().ExportToExcel($"逾期未回复统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "承办单位", "诉求内容", "转交时间", "回复时限", "回复时间" });
        //        return Success("导出excel");
        //    }
        //    return Content(new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    }.ToJson());

        //}

        /// <summary>
        /// 坐席工作统计
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public ActionResult GetUserAccountAssessment(DateTime? start, DateTime? end, bool isExport = false)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_UserAssessmentReport", paras, "UserAssessmentReport");
            if (isExport)
            {
                string[] cols = new string[] { "姓名", "接听量", "通话成单量", "手动录单量", "当即办理量", "网络转办量", "回访量"};
                NPOIHelper npoi = new NPOIHelper();
                if (npoi.ExportToExcel("坐席工作统计报表", obj.Tables[0], cols) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return Success("成功", obj);
        }

        /// <summary>
        /// 逾期未回复统计
        /// </summary>
        /// <param name="date"></param>
        /// <param name="deptId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult GetOutTimeNotApply(DateTime? start, DateTime? end, int deptId = -1, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            var where = "";
            if (deptId > 0)
            {
                where = " AND  F_MainDeptId=" + deptId;
            }

            string sql = $" and datediff(day,F_CreateTime,'{start.Value.ToString("yyyy-MM-dd")}')<=0 and datediff(day,F_CreateTime,'{end.Value.ToString("yyyy-MM-dd")}')>=0 "+ where;
            

            if (isExport)
            {
                string[] cols = new string[] { "工单编号", "承办单位", "工单内容", "转派时间", "回复时限", "超时时间" };
                var dtall = DbHelperSQL.Query("select * from V_Bus_OverNoBack where 1=1 "+sql).Tables[0];
                dtall.Columns.Remove("F_MainDeptId");
                dtall.Columns.Remove("F_CreateTime");
                new NPOIHelper().ExportToExcel($"逾期未回复统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dtall, cols);
                return Success("导出excel");
            }

            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "V_Bus_OverNoBack",
                "F_WorkOrderId",
                "*",
                sql,
                "ORDER BY F_CreateTime DESC",
                pageSize,
                pageIndex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());

        }

        ///// <summary>
        ///// 不满意统计
        ///// </summary>
        ///// <param name="date"></param>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public ActionResult GetNotSatisfied(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }

        //    var workOrderList = workOrderBLL.DataTableToList(workOrderBLL.GetList($" F_IsDelete=0 and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]);
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList($"  ").Tables[0]);
        //    var assignedList = assignedInfoBLL.DataTableToList(assignedInfoBLL.GetList($" F_IsDelete=0 and F_IsSure in (0,1,2) and  F_State in (0,1)   and  datediff(s,'{start}',F_CreateTime)>0  and   datediff(s,'{end}',F_CreateTime)<0  ").Tables[0]).DistinctBy(x => x.F_WorkOrderId);
        //    var userAccountList = userAccount.DataTableToList(userAccount.GetList(" F_DeleteFlag=0 ").Tables[0]);
        //    List<OutNotSatisfiedDto> list = new List<OutNotSatisfiedDto>();
        //    foreach (var item in assignedList)
        //    {
        //        var deptName = deptList.SingleOrDefault(x => x.F_DeptId == item.F_MainDeptId)?.F_DeptName ?? "";
        //        if (string.IsNullOrWhiteSpace(deptName))
        //            continue;
        //        var workorder = workOrderList.SingleOrDefault(x => x.F_WorkOrderId == item.F_WorkOrderId);
        //        if (workorder == null)
        //            continue;
        //        var userAccount = userAccountList.SingleOrDefault(x => x.F_UserCode == workorder.F_CreateUser);
        //        if (userAccount == null)
        //            continue;
        //        list.Add(new OutNotSatisfiedDto
        //        {
        //            workorder = item.F_WorkOrderId,
        //            deptname = deptName,
        //            title = workorder.F_ComTitle,
        //            creattime = Convert.ToDateTime(workorder.F_CreateTime).ToString("yyyy-MM-dd HH:mm:ss"),
        //            useraccount = userAccount.F_UserName + $"({userAccount.F_UserCode})",

        //        });
        //    }
        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "workorder", "detpname", "title", "creattime", "useraccount" });
        //        new NPOIHelper().ExportToExcel($"不满意统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "诉求编号", "承办单位", "诉求标题", "诉求时间", "受理人" });
        //        return Success("导出excel");
        //    }
        //    return Content(new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    }.ToJson());

        //}

        /// <summary>
        /// 重办不满意统计
        /// </summary>
        /// <param name="date"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult GetNotSatisfied(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }

            string sql = $" and datediff(day,cjTime,'{start.Value.ToString("yyyy-MM-dd")}')<=0 and datediff(day,cjTime,'{end.Value.ToString("yyyy-MM-dd")}')>=0 ";


            if (isExport)
            {
                string[] cols = new string[] { "工单编号", "承办单位", "工单内容", "创建时间", "处理结果", "处理时间", "回访结果", "回访时间" };
                var dtall = DbHelperSQL.Query("select * from V_Bus_ReloadNoSatisfie where 1=1 " + sql + " order by cjTime DESC ").Tables[0];
                dtall.Columns.Remove("F_Id");
                new NPOIHelper().ExportToExcel($"重办不满意统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dtall, cols);
                return Success("导出excel");
            }

            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "V_Bus_ReloadNoSatisfie",
                "F_Id",
                "*",
                sql,
                "ORDER BY cjTime DESC",
                pageSize,
                pageIndex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());


        }

        ///// <summary>
        ///// 延期统计
        ///// </summary>
        ///// <param name="start"></param>
        ///// <param name="end"></param>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <param name="isExport"></param>
        ///// <returns></returns>
        //public ActionResult GetDelay(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_State=0 AND  F_DeptNameSpell=3 ").Tables[0]);
        //    var delayList = new BLL.T_Bus_WorkOrderReport().DataTableToDelayList(DbHelperSQL.Query($"SELECT A.F_WorkOrderId,B.F_MainDeptId, C.F_ID FROM T_Bus_WorkOrder A LEFT JOIN T_Bus_AssignedInfo B ON A.F_WorkOrderId = B.F_WorkOrderId LEFT JOIN T_Bus_DelayTime C ON B.F_Id = C.F_AssignedId where datediff(s, '{start}', A.F_CreateTime) > 0  and datediff(s, '{end}', A.F_CreateTime) < 0  and A.F_IsDelete = 0 AND A.F_WorkOrderId IS NOT NULL AND B.F_ID IS NOT NULL AND B.F_MainDeptId IS NOT NULL AND  B.F_IsDelete=0 and B.F_IsSure in (0,1,2) and  B.F_State in (0,1)  ").Tables[0]);
        //    List<DelayDto> list = new List<DelayDto>();
        //    deptList.ForEach(x =>
        //    {
        //        var assignQuery = delayList.Where(y => x.F_DeptId == y.DeptId).ToList();
        //        var delayQuery = assignQuery.Where(y => y.DelayId > 0).ToList();
        //        var count = assignQuery.Count();
        //        var delayCount = delayQuery.Count();
        //        list.Add(new DelayDto
        //        {
        //            deptname = x.F_DeptName,
        //            count = count,
        //            delaycount = delayCount,
        //            delayrate = count == 0 ? "0%" : delayCount == 0 ? "0%" : ((double)delayCount / count * 100).ToString("f2") + "%"
        //        });
        //    });
        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "name", "count", "delaycount", "delayrate" });
        //        new NPOIHelper().ExportToExcel($"延时分析统计{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "承办单位", "受理数量", "延时数量", "延时率" });
        //        return Success("导出excel");
        //    }
        //    return Content(new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    }.ToJson());

        //}

        ///// <summary>
        ///// 通报批评
        ///// </summary>
        ///// <param name="start"></param>
        ///// <param name="end"></param>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <param name="isExport"></param>
        ///// <returns></returns>
        //public ActionResult GetNotice(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_State=0 AND  F_DeptNameSpell=3 ").Tables[0]);
        //    var noticeList = new BLL.T_Bus_WorkOrderReport().DataTableToNoticeList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,B.F_MainDeptId,B.F_IsNotice  FROM T_Bus_WorkOrder A LEFT JOIN T_Bus_AssignedInfo B ON A.F_WorkOrderId = B.F_WorkOrderId where datediff(s, '{start}', A.F_CreateTime) > 0  and   datediff(s, '{end}', A.F_CreateTime) < 0  and A.F_IsDelete = 0 AND A.F_WorkOrderId IS NOT NULL AND B.F_ID IS NOT NULL AND B.F_MainDeptId IS NOT NULL AND  B.F_IsDelete = 0 and B.F_IsSure in (0, 1, 2) and  B.F_State in (0, 1)  ").Tables[0]);
        //    List<NoticeDto> list = new List<NoticeDto>();
        //    deptList.ForEach(x =>
        //    {
        //        var assignQuery = noticeList.Where(y => x.F_DeptId == y.DeptId).ToList();
        //        var noticeQuery = assignQuery.Where(y => y.IsNotice).ToList();
        //        var count = assignQuery.Count();
        //        var noticeCount = noticeQuery.Count();
        //        list.Add(new NoticeDto
        //        {
        //            deptname = x.F_DeptName,
        //            count = count,
        //            noticecount = noticeCount,
        //            noticerate = count == 0 ? "0%" : noticeCount == 0 ? "0%" : ((double)noticeCount / count * 100).ToString("f2") + "%"
        //        });
        //    });
        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "name", "count", "noticecount", "noticerate" });
        //        new NPOIHelper().ExportToExcel($"通报批评{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "承办单位", "受理数量", "通报批评数量", "通报批评率" });
        //        return Success("导出excel");
        //    }
        //    return Content(new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    }.ToJson());
        //}

        /// <summary>
        /// 延期统计
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isExport"></param>
        /// <returns></returns>
        public ActionResult GetDelay(DateTime? start, DateTime? end, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_DeptDelayReport", paras, "DeptDelayReport");
            if (isExport)
            {
                string[] cols = new string[] { "单位名称", "办理数量", "延时数量", "延时率" };
                NPOIHelper npoi = new NPOIHelper();
                if (npoi.ExportToExcel("延时分析统计报表", obj.Tables[0], cols) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return Success("成功", obj);
        }

        /// <summary>
        /// 通报批评
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isExport"></param>
        /// <returns></returns>
        public ActionResult GetNotice(DateTime? start, DateTime? end, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            //string sql = "select * from dbo.GetNoticeReport('"+ start.Value.ToString("yyyy-MM-dd") + "','" + end.Value.ToString("yyyy-MM-dd") + "') ORDER BY zbcount+xbcount DESC";
            //var dt = DbHelperSQL.Query(sql).Tables[0];
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_DeptNoticeReport", paras, "DeptNoticeReport");
            if (isExport)
            {
                var cols = new string[] { "承办单位", "主办受理数量", "主办通报批评数量", "主办通报批评率", "协办受理数量", "协办通报批评数量", "协办通报批评率" };
                new NPOIHelper().ExportToExcel($"通报批评{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", obj.Tables[0], cols);
                return Success("导出excel");
            }
            return Success("成功", obj);
        }

        ///// <summary>
        ///// 诉求受理对比
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult GetAppeal(DateTime? start, DateTime? end, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTools.FirstDayOfMonth(DateTime.Now.Date);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTools.LastDayOfMonth(DateTime.Now.Date);
        //    }
        //    var lastStart = DateTools.FirstDayOfPreviousMonth(DateTime.Now.Date);
        //    var lastEnd = DateTools.LastDayOfPrdviousMonth(DateTime.Now.Date);
        //    var lastYearStart = DateTools.FirstDayOfMonth(DateTime.Now.AddYears(-1).Date);
        //    var lastYearEnd = DateTools.LastDayOfMonth(DateTime.Now.AddYears(-1).Date);
        //    // var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_State=0 AND  F_DeptNameSpell=3 ").Tables[0]);
        //    var userList = userAccount.DataTableToList(userAccount.GetList(" F_DeleteFlag=0  AND F_SeatFlag=1 ").Tables[0]);
        //    var workOrderListNow = new BLL.T_Bus_WorkOrderReport().DataTableAppealList(DbHelperSQL.Query($"SELECT F_WorkOrderId,F_CreateUser FROM T_Bus_WorkOrder WHERE  datediff(s, '{start}', F_CreateTime) > 0  and   datediff(s, '{end}', F_CreateTime) < 0  and F_IsDelete = 0 AND F_WorkOrderId IS NOT NULL").Tables[0]);
        //    var workOrderListLast = new BLL.T_Bus_WorkOrderReport().DataTableAppealList(DbHelperSQL.Query($"SELECT F_WorkOrderId,F_CreateUser FROM T_Bus_WorkOrder WHERE  datediff(s, '{lastStart}', F_CreateTime) > 0  and   datediff(s, '{lastEnd}', F_CreateTime) < 0  and F_IsDelete = 0 AND F_WorkOrderId IS NOT NULL").Tables[0]);
        //    var workOrderListLastYear = new BLL.T_Bus_WorkOrderReport().DataTableAppealList(DbHelperSQL.Query($"SELECT F_WorkOrderId,F_CreateUser FROM T_Bus_WorkOrder WHERE  datediff(s, '{lastYearStart}', F_CreateTime) > 0  and   datediff(s, '{lastYearEnd}', F_CreateTime) < 0  and F_IsDelete = 0 AND F_WorkOrderId IS NOT NULL").Tables[0]);
        //    List<AppealDto> list = new List<AppealDto>();
        //    userList.ForEach(x =>
        //    {
        //        var count = workOrderListNow.Where(y => x.F_UserCode == y.UserCode).ToList().Count();
        //        var lastCount = workOrderListLast.Where(y => x.F_UserCode == y.UserCode).ToList().Count();
        //        var lastYearCount = workOrderListLastYear.Where(y => x.F_UserCode == y.UserCode).ToList().Count();
        //        list.Add(new AppealDto
        //        {
        //            name = x.F_UserName,
        //            count = count,
        //            mom = lastCount == 0 ? "0%" : count == 0 ? "0%" : ((double)count / lastCount * 100).ToString("f2") + "%",
        //            yoy = lastYearCount == 0 ? "0%" : count == 0 ? "0%" : ((double)count / lastYearCount * 100).ToString("f2") + "%"
        //        });
        //    });
        //    if (isExport)
        //    {
        //        var dt = list.ToDataTable(new string[] { "name", "count", "mom", "yoy" });
        //        new NPOIHelper().ExportToExcel($"诉求受理对比{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "坐席名称", "受理数量", "上月环比", "去年同期同比" });
        //        return Success("导出excel");
        //    }
        //    return Content(new
        //    {
        //        rows = list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = list.Count
        //    }.ToJson());
        //}

        /// <summary>
        /// 诉求受理对比
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAppeal(DateTime? start, DateTime? end, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }

            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_UserAppealReport", paras, "UserAppealReport");
            if (isExport)
            {
                var cols = new string[] { "坐席名称", "受理数量", "上月同期受理数量", "上月同期环比", "去年同期受理数量", "去年同期同比" };
                new NPOIHelper().ExportToExcel($"诉求受理对比{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", obj.Tables[0], cols);
                return Success("导出excel");
            }
            return Success("成功", obj);
        }

        /// <summary>
        /// 工单周报
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult GetWeaklyReport(DateTime? start, DateTime? end, int type = 0, int dateType = 0, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sb3 = new StringBuilder();
            var startDate = new DateTime();
            var endDate = new DateTime();
            var lastStartDate = new DateTime();
            var lastEndDate = new DateTime();
            var differ = 0;
            var str = "";
            switch (dateType)
            {
                //日
                case 1:
                    if (start == null)
                    {
                        start = DateTime.Now.Date;
                    }
                    if (end == null)
                    {
                        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
                    }
                    startDate = Convert.ToDateTime(start);
                    endDate = Convert.ToDateTime(end).AddDays(1).AddSeconds(-1);
                    differ = (endDate - startDate).Days + 1;
                    lastStartDate = startDate.AddDays(-differ);
                    lastEndDate = endDate.AddDays(-differ);
                    str = "日";
                    break;
                //周
                case 2:
                    if (start == null)
                    {
                        start = DateTime.Now.Date;
                    }
                    if (end == null)
                    {
                        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
                    }
                    startDate = Convert.ToDateTime(start);
                    endDate = Convert.ToDateTime(end);
                    differ = 7 - (int)startDate.DayOfWeek;
                    startDate = DateTools.GetWeekFirstDayMon((DateTime)start);
                    endDate = DateTools.GetWeekLastDaySun((DateTime)end).AddDays(1).AddSeconds(-1);
                    differ = (endDate - startDate).Days + 1;
                    lastStartDate = DateTools.GetWeekFirstDayMon(startDate.AddDays(-1));
                    lastEndDate = DateTools.GetWeekLastDaySun(lastStartDate).AddDays(1).AddSeconds(-1);
                    str = "周";
                    break;
                //月
                case 3:
                    if (start == null)
                    {
                        start = DateTime.Now.Date;
                    }
                    if (end == null)
                    {
                        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
                    }
                    startDate = Convert.ToDateTime(start);
                    endDate = Convert.ToDateTime(end);
                    differ = startDate.Day;
                    startDate = startDate.AddDays(-differ + 1);
                    endDate = endDate.AddDays(DateTime.DaysInMonth(startDate.Year, startDate.Month) - differ).AddDays(1).AddSeconds(-1);
                    //differ = (endDate - startDate). + 1;
                    lastStartDate = startDate.AddMonths(-1);
                    lastEndDate = lastStartDate.AddDays(DateTime.DaysInMonth(lastStartDate.Year, lastStartDate.Month) - 1).AddDays(1).AddSeconds(-1);
                    str = "月";
                    break;
                default:
                    break;
            }
            //type 查询类型 type=1 按区域;type=2 按部门; type=3 按工单类型
            switch (type)
            {
                case 1:
                    var areaList = areaBLL.DataTableToList(areaBLL.GetList(" F_State=0 ").Tables[0]);
                    var areaList1 = new BLL.T_Bus_WorkOrderReport().DataTableToAreaList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,A.F_SourceArea,B.F_AreaName, row_number() over(partition by B.F_AreaId order by A.F_Id desc) FROM T_Bus_WorkOrder A LEFT JOIN T_Sys_Area B ON A.F_SourceArea = B.F_AreaId WHERE  datediff(s, '{startDate}', A.F_CreateTime) > 0  and  datediff(s, '{endDate}', A.F_CreateTime) < 0  and A.F_IsDelete = 0 AND A.F_WorkOrderId IS NOT NULL AND A.F_SourceArea > 0").Tables[0]);
                    var areaList2 = new BLL.T_Bus_WorkOrderReport().DataTableToAreaList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,A.F_SourceArea,B.F_AreaName, row_number() over(partition by B.F_AreaId order by A.F_Id desc) FROM T_Bus_WorkOrder A LEFT JOIN T_Sys_Area B ON A.F_SourceArea = B.F_AreaId WHERE  datediff(s, '{lastStartDate}', A.F_CreateTime) > 0  and  datediff(s, '{lastEndDate}', A.F_CreateTime) < 0  and A.F_IsDelete = 0 AND A.F_WorkOrderId IS NOT NULL AND A.F_SourceArea > 0").Tables[0]);
                    List<DeptWeaklyReportDto> areaWeaklyReportDtoList = new List<DeptWeaklyReportDto>();
                    areaList.ForEach(x =>
                    {
                        var query = areaList1.Where(y => x.F_AreaId == y.FieldId).ToList();
                        var assignedCount = query.Count();
                        var lastAssignedcount = areaList2.Where(y => x.F_AreaId == y.FieldId).Count();
                        var countRate = lastAssignedcount == 0 ? "0%" : assignedCount == 0 ? "0%" : ((double)assignedCount / lastAssignedcount * 100).ToString("f2") + "%";
                        var completedCount = query.Where(y => y.WorkOrderState == 9).Count();
                        var completedRate = assignedCount == 0 ? "0%" : completedCount == 0 ? "0%" : ((double)completedCount / assignedCount * 100).ToString("f2") + "%";
                        areaWeaklyReportDtoList.Add(new DeptWeaklyReportDto
                        {
                            name = x.F_AreaName,
                            assignedcount = assignedCount,
                            countrate = countRate,
                            completedcount = completedCount,
                            completedrate = completedRate
                        });
                    });
                    if (isExport)
                    {
                        var dt = areaWeaklyReportDtoList.ToDataTable(new string[] { "name", "assignedcount", "countrate", "completedcount", "completedrate" });
                        new NPOIHelper().ExportToExcel($"按区域{str}报表{Convert.ToDateTime(startDate).ToString("yyyyMMdd")}-{Convert.ToDateTime(endDate).ToString("yyyyMMdd")}", dt, new string[] { "区域", "受理数量", "数量同比", "办结数量", "办结率" });
                        return Success("导出excel");
                    }
                    return Content(new
                    {
                        rows = areaWeaklyReportDtoList.Skip((pageIndex - 1) * pageSize).Take(pageSize),
                        total = areaWeaklyReportDtoList.Count
                    }.ToJson());
                case 2:
                    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList(" F_State=0 AND F_DeptNameSpell=3 ").Tables[0]);
                    var deptList1 = new BLL.T_Bus_WorkOrderReport().DataTableToDeptList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,B.F_MainDeptId,C.F_DeptName , row_number() over(partition by B.F_MainDeptId order by A.F_Id desc)FROM T_Bus_WorkOrder A LEFT JOIN T_Bus_AssignedInfo B ON A.F_WorkOrderId=B.F_WorkOrderId LEFT JOIN T_Sys_Department C ON B.F_MainDeptId=C.F_DeptId AND C.F_DeptNameSpell=3 WHERE  datediff(s,'{startDate}',A.F_CreateTime)>0  and   datediff(s,'{endDate}',A.F_CreateTime)<0  and A.F_IsDelete=0 AND A.F_WorkOrderId IS NOT NULL AND  B.F_IsDelete=0 and B.F_IsSure in (0,1,2) and  B.F_State in (0,1)").Tables[0]).DistinctBy(x => x.WorkOrderId).ToList();
                    var deptList2 = new BLL.T_Bus_WorkOrderReport().DataTableToDeptList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,B.F_MainDeptId,C.F_DeptName , row_number() over(partition by B.F_MainDeptId order by A.F_Id desc)FROM T_Bus_WorkOrder A LEFT JOIN T_Bus_AssignedInfo B ON A.F_WorkOrderId=B.F_WorkOrderId LEFT JOIN T_Sys_Department C ON B.F_MainDeptId=C.F_DeptId AND C.F_DeptNameSpell=3 WHERE  datediff(s,'{lastStartDate}',A.F_CreateTime)>0  and   datediff(s,'{lastEndDate}',A.F_CreateTime)<0  and A.F_IsDelete=0 AND A.F_WorkOrderId IS NOT NULL AND  B.F_IsDelete=0 and B.F_IsSure in (0,1,2) and  B.F_State in (0,1)").Tables[0]).DistinctBy(x => x.WorkOrderId).ToList();
                    List<DeptWeaklyReportDto> deptWeaklyReportDtoList = new List<DeptWeaklyReportDto>();
                    deptList.ForEach(x =>
                     {
                         var query = deptList1.Where(y => y.FieldId == x.F_DeptId).ToList();
                         var assignedCount = query.Count();
                         var lastAssignedcount = deptList2.Where(y => y.FieldId == x.F_DeptId).Count();
                         var countRate = lastAssignedcount == 0 ? "0%" : assignedCount == 0 ? "0%" : ((double)assignedCount / lastAssignedcount * 100).ToString("f2") + "%";
                         var completedCount = query.Where(y => y.WorkOrderState == 9).Count();
                         var completedRate = assignedCount == 0 ? "0%" : completedCount == 0 ? "0%" : ((double)completedCount / assignedCount * 100).ToString("f2") + "%";
                         deptWeaklyReportDtoList.Add(new DeptWeaklyReportDto
                         {
                             name = x.F_DeptName,
                             assignedcount = assignedCount,
                             countrate = countRate,
                             completedcount = completedCount,
                             completedrate = completedRate
                         });
                     });
                    if (isExport)
                    {
                        var dt = deptWeaklyReportDtoList.ToDataTable(new string[] { "name", "assignedcount", "countrate", "completedcount", "completedrate" });
                        new NPOIHelper().ExportToExcel($"按单位{str}报表{Convert.ToDateTime(startDate).ToString("yyyyMMdd")}-{Convert.ToDateTime(endDate).ToString("yyyyMMdd")}", dt, new string[] { "单位", "受理数量", "数量同比", "办结数量", "办结率" });
                        return Success("导出excel");
                    }
                    return Content(new
                    {
                        rows = deptWeaklyReportDtoList.Skip((pageIndex - 1) * pageSize).Take(pageSize),
                        total = deptWeaklyReportDtoList.Count
                    }.ToJson());
                case 3:
                    var typeList = dictValueBLL.DataTableToList(dictValueBLL.GetList(" F_State=0 and F_ItemId=2 ").Tables[0]);
                    var typeList1 = new BLL.T_Bus_WorkOrderReport().DataTableToTypeList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,A.F_InfoType,B.F_Value  , row_number() over(partition by A.F_InfoType order by A.F_Id desc) FROM T_Bus_WorkOrder A  LEFT JOIN T_Sys_DictionaryValue B ON A.F_InfoType=B.F_ValueId  WHERE  datediff(s,'{startDate}',A.F_CreateTime)>0  and   datediff(s,'{endDate}',A.F_CreateTime)<0  and A.F_IsDelete=0 AND A.F_WorkOrderId IS NOT NULL AND    B.F_State=0 and B.F_ItemId=2 ").Tables[0]);
                    var typeList2 = new BLL.T_Bus_WorkOrderReport().DataTableToTypeList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,A.F_InfoType,B.F_Value  , row_number() over(partition by A.F_InfoType order by A.F_Id desc) FROM T_Bus_WorkOrder A  LEFT JOIN T_Sys_DictionaryValue B ON A.F_InfoType=B.F_ValueId  WHERE  datediff(s,'{lastStartDate}',A.F_CreateTime)>0  and   datediff(s,'{lastEndDate}',A.F_CreateTime)<0  and A.F_IsDelete=0 AND A.F_WorkOrderId IS NOT NULL AND    B.F_State=0 and B.F_ItemId=2 ").Tables[0]);
                    List<DeptWeaklyReportDto> typeWeaklyReportDtoList = new List<DeptWeaklyReportDto>();
                    typeList.ForEach(x =>
                    {
                        var query = typeList1.Where(y => y.FieldId == x.F_ValueId);
                        var assignedCount = query.Count();
                        var lastAssignedcount = typeList2.Where(y => y.FieldId == x.F_ValueId).Count();
                        var countRate = lastAssignedcount == 0 ? "0%" : assignedCount == 0 ? "0%" : ((double)assignedCount / lastAssignedcount * 100).ToString("f2") + "%";
                        var completedCount = query.Where(y => y.WorkOrderState == 9).Count();
                        var completedRate = assignedCount == 0 ? "0%" : completedCount == 0 ? "0%" : ((double)completedCount / assignedCount * 100).ToString("f2") + "%";
                        typeWeaklyReportDtoList.Add(new DeptWeaklyReportDto
                        {
                            name = x.F_Value,
                            assignedcount = assignedCount,
                            countrate = countRate,
                            completedcount = completedCount,
                            completedrate = completedRate
                        });
                    });
                    if (isExport)
                    {
                        var dt = typeWeaklyReportDtoList.ToDataTable(new string[] { "name", "assignedcount", "countrate", "completedcount", "completedrate" });
                        new NPOIHelper().ExportToExcel($"按工单类型{str}报表{Convert.ToDateTime(startDate).ToString("yyyyMMdd")}-{Convert.ToDateTime(endDate).ToString("yyyyMMdd")}", dt, new string[] { "工单类型", "受理数量", "数量同比", "办结数量", "办结率" });
                        return Success("导出excel");
                    }
                    return Content(new
                    {
                        rows = typeWeaklyReportDtoList.Skip((pageIndex - 1) * pageSize).Take(pageSize),
                        total = typeWeaklyReportDtoList.Count
                    }.ToJson());
                case 4:
                    var sourceList = dictValueBLL.DataTableToList(dictValueBLL.GetList(" F_State = 0 and F_ItemId = 1").Tables[0]);
                    var sourceList1 = new BLL.T_Bus_WorkOrderReport().DataTableToSourceList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,A.F_InfoSource,B.F_Value  , row_number() over(partition by A.F_InfoSource order by A.F_Id desc) FROM T_Bus_WorkOrder A LEFT JOIN T_Sys_DictionaryValue B ON A.F_InfoSource = B.F_ValueId WHERE  datediff(s, '{startDate}', A.F_CreateTime) > 0  and   datediff(s, '{endDate}', A.F_CreateTime) < 0  and A.F_IsDelete = 0 AND A.F_WorkOrderId IS NOT NULL AND  B.F_State = 0 and B.F_ItemId = 1").Tables[0]);
                    var sourceList2 = new BLL.T_Bus_WorkOrderReport().DataTableToSourceList(DbHelperSQL.Query($"SELECT  A.F_WorkOrderId,A.F_WorkState,A.F_InfoSource,B.F_Value  , row_number() over(partition by A.F_InfoSource order by A.F_Id desc) FROM T_Bus_WorkOrder A LEFT JOIN T_Sys_DictionaryValue B ON A.F_InfoSource = B.F_ValueId WHERE  datediff(s, '{lastStartDate}', A.F_CreateTime) > 0  and   datediff(s, '{lastEndDate}', A.F_CreateTime) < 0  and A.F_IsDelete = 0 AND A.F_WorkOrderId IS NOT NULL AND  B.F_State = 0 and B.F_ItemId = 1").Tables[0]);
                    List<DeptWeaklyReportDto> sourceWeaklyReportDtoList = new List<DeptWeaklyReportDto>();
                    sourceList.ForEach(x =>
                    {
                        var query = sourceList1.Where(y => y.FieldId == x.F_ValueId).ToList();
                        var assignedCount = query.Count();
                        var lastAssignedcount = sourceList2.Where(y => y.FieldId == x.F_ValueId).Count();
                        var countRate = lastAssignedcount == 0 ? "0%" : assignedCount == 0 ? "0%" : ((double)assignedCount / lastAssignedcount * 100).ToString("f2") + "%";
                        var completedCount = query.Where(y => y.WorkOrderState == 9).Count();
                        var completedRate = assignedCount == 0 ? "0%" : completedCount == 0 ? "0%" : ((double)completedCount / assignedCount * 100).ToString("f2") + "%";
                        sourceWeaklyReportDtoList.Add(new DeptWeaklyReportDto
                        {
                            name = x.F_Value,
                            assignedcount = assignedCount,
                            countrate = countRate,
                            completedcount = completedCount,
                            completedrate = completedRate
                        });
                    });
                    if (isExport)
                    {
                        var dt = sourceWeaklyReportDtoList.ToDataTable(new string[] { "name", "assignedcount", "countrate", "completedcount", "completedrate" });
                        new NPOIHelper().ExportToExcel($"按工单来源{str}报表{Convert.ToDateTime(startDate).ToString("yyyyMMdd")}-{Convert.ToDateTime(endDate).ToString("yyyyMMdd")}", dt, new string[] { "工单来源", "受理数量", "数量同比", "办结数量", "办结率" });
                        return Success("导出excel");
                    }
                    return Content(new
                    {
                        rows = sourceWeaklyReportDtoList.Skip((pageIndex - 1) * pageSize).Take(pageSize),
                        total = sourceWeaklyReportDtoList.Count
                    }.ToJson());
                default:
                    return Error("参数错误");
            }
        }

        /// <summary>
        /// 工单日周月报
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult GetDayWeekMonthReport(DateTime? date, int type = 0, int dateType = 0, bool isExport = false)
        {
            if (date == null)
            {
                date = DateTime.Now.Date;
            }
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@date", date.Value.ToString("yyyy-MM-dd"));
            paras.Add("@datatype", type.ToString());
            paras.Add("@datetype", dateType.ToString());
            var obj = DbHelperSQL.RunProcedure("P_DayWeekMonthReport", paras, "DayWeekMonthReport");
            if (isExport)
            {
                var cols = new string[] { "名称", "受理数量", "数量同比", "结案数量", "结案率" };
                var start = obj.Tables[1].Rows[0]["startdate"].ToString();
                var end = obj.Tables[1].Rows[0]["enddate"].ToString();
                new NPOIHelper().ExportToExcel($"工单报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", obj.Tables[0], cols);
                return Success("导出excel");
            }
            return Success("成功", obj);
        }

        ///// <summary>
        ///// 调度工作量统计
        ///// </summary>
        ///// <param name="start"></param>
        ///// <param name="end"></param>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <param name="isExport"></param>
        ///// <returns></returns>
        //public ActionResult GetWorkLoad(DateTime? start, DateTime? end, int deptId = -1, int pageIndex = 1, int pageSize = 10, bool isExport = false)
        //{
        //    if (start == null)
        //    {
        //        start = DateTime.Now.Date.AddDays(-6);
        //    }
        //    if (end == null)
        //    {
        //        end = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
        //    }
        //    string where = "";
        //    if (deptId > 0)
        //    {
        //        where = " AND F_DeptId=" + deptId;
        //    }
        //    var userList = userAccount.DataTableToList(userAccount.GetList(" F_DeleteFlag=0 and F_RoleId=6 " + where).Tables[0]);
        //    var deptList = departmentBLL.DataTableToList(departmentBLL.GetList($" F_State=0 AND F_DeptNameSpell=3  ").Tables[0]);
        //    var assignedList = assignedInfoBLL.DataTableToList(assignedInfoBLL.GetList($" F_IsDelete = 0 and F_IsSure in (0, 1) and  F_State in (1) and datediff(s,'{start}', F_CreateTime)> 0  and datediff(s,'{end}', F_CreateTime)< 0  ").Tables[0]);
        //    var submitsuperList= submitsuperBLL.DataTableToList(submitsuperBLL.GetList($" F_IsDelete = 0 and F_Type in (3) and  F_State in (1) and datediff(s,'{start}', F_CreateTime)> 0  and datediff(s,'{end}', F_CreateTime)< 0  ").Tables[0]);
        //    List<UserAccountWorkLoad> list = new List<UserAccountWorkLoad>();
        //    foreach (var u in userList)
        //    {
        //        var dept = deptList.SingleOrDefault(x => x.F_DeptId == u.F_DeptId);
        //        if (dept == null)
        //            continue;
        //        var deptName = dept.F_DeptName ?? "";
        //        //转派次数
        //        var assignedCount = assignedList.Where(x => x.F_CreateUser == u.F_UserCode).Count();
        //        //退回坐席次数
        //        var returnedcount = submitsuperList.Where(x => x.F_CreateUser == u.F_UserCode).Count();
        //        //调度工作量
        //        var workcount = assignedCount + returnedcount;
        //        list.Add(new UserAccountWorkLoad
        //        {
        //            detpname = deptName,
        //            useraccount = $"{u.F_UserName}({u.F_UserCode})",
        //            fordeliverycount = assignedCount,   
        //            returnedcount= returnedcount,
        //            workcount = workcount
        //        });
        //    }
        //    var total = new
        //    {
        //        fordeliverycount = list.Sum(x => x.fordeliverycount),
        //        returnedcount = list.Sum(x => x.returnedcount),
        //        workcount = list.Sum(x => x.workcount)
        //    };
        //    //导出
        //    if (isExport)
        //    {
        //        list.Add(new UserAccountWorkLoad
        //        {
        //            detpname = "总",
        //            useraccount = "计",
        //            fordeliverycount = total.fordeliverycount,
        //            returnedcount = total.returnedcount,
        //            workcount = total.workcount
        //        });
        //        var dt = list.ToDataTable(new string[] { "detpname", "useraccount", "fordeliverycount", "returnedcount", "workcount", });
        //        new NPOIHelper().ExportToExcel($"调度工作量统计报表{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", dt, new string[] { "部门名称", "坐席", "转派次数", "退回坐席次数", "调度工作量", });
        //        return Success("导出excel");
        //    }
        //    var obj = new
        //    {
        //        rows = list,    //list.Skip((pageIndex - 1) * pageSize).Take(pageSize),
        //        total = total
        //    };
        //    return Content(obj.ToJson());
        //}

        /// <summary>
        /// 调度工作量统计
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isExport"></param>
        /// <returns></returns>
        public ActionResult GetWorkLoad(DateTime? start, DateTime? end, bool isExport = false)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1); 
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", start.Value.ToString("yyyy-MM-dd"));
            paras.Add("@edate", end.Value.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_LeaderAssessmentReport", paras, "LeaderAssessmentReport");
            if (isExport)
            {
                var cols = new string[] { "姓名", "回退量", "交办量",  "退回审核量", "延时审核量", "重办量", "结案量" };
                new NPOIHelper().ExportToExcel($"调度工作量统计{Convert.ToDateTime(start).ToString("yyyyMMdd")}-{Convert.ToDateTime(end).ToString("yyyyMMdd")}", obj.Tables[0], cols);
                return Success("导出excel");
            }
            return Success("成功", obj);
        }


        /// <summary>
        /// 获取未处理报表
        /// </summary>
        /// <param name="date"></param>
        /// <param name="isdc"></param>
        /// <returns></returns>
        public ActionResult GetNoDealReport(DateTime? start, DateTime? end, int deptid = 0, int pageIndex = 1, int pageSize = 10, int isdc = 0)
        {
            if (start == null)
            {
                start = DateTime.Now;
                start = new DateTime(start.Value.Year, start.Value.Month, 1);
            }
            if (end == null)
            {
                end = DateTime.Now;
            }
            var where = "";
            if (deptid > 0)
            {
                where = " AND  F_MainDeptId=" + deptid;
            }

            string sql = $" and datediff(day,F_CreateTime,'{start.Value.ToString("yyyy-MM-dd")}')<=0 and datediff(day,F_CreateTime,'{end.Value.ToString("yyyy-MM-dd")}')>=0 " + where;


            if (isdc > 0)
            {
                string[] cols = new string[] { "工单编号", "承办单位", "工单内容", "转派时间", "回复时限", "超时/剩余", "工单状态" };
                var dtall = DbHelperSQL.Query("select * from V_Bus_NoDeal where 1=1 " + sql).Tables[0];
                dtall.Columns.Remove("F_MainDeptId");
                dtall.Columns.Remove("F_CreateTime");
                dtall.Columns.Remove("F_WorkState");
                new NPOIHelper().ExportToExcel($"单位未处理统计报表{start.Value.ToString("yyyyMMdd")}-{end.Value.ToString("yyyyMMdd")}", dtall, cols);
                return Success("导出excel");
            }

            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "V_Bus_NoDeal",
                "F_WorkOrderId",
                "*",
                sql,
                "ORDER BY F_CreateTime DESC",
                pageSize,
                pageIndex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());
        }

    }
}