using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Models;
using ZXDT.Common;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{

    public class OrderController : BaseController
    {
        BLL.T_Call_CallRecords recordBLL = new BLL.T_Call_CallRecords();
        //工单信息
        BLL.T_Wo_WorkOrderBase orderBLL = new BLL.T_Wo_WorkOrderBase();
        //客户信息
        BLL.T_Cus_CustomerBase cusBLL = new BLL.T_Cus_CustomerBase();
        //部门表
        BLL.T_Sys_Department deptBll = new BLL.T_Sys_Department();
        //转单通知
        BLL.T_Wo_WorkOrderNotice noticeBLL = new BLL.T_Wo_WorkOrderNotice();
        //用户表
        BLL.T_Sys_UserAccount userBll = new BLL.T_Sys_UserAccount();
        //历史工单表
        BLL.T_Wo_WorkOrderHistory historyBll = new BLL.T_Wo_WorkOrderHistory();
        int rxzhj = 0;
        int wxzhj = 0;
        int wbzhj = 0;
        #region 待处理

        /// <summary>
        /// 获取待办工单  新
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult WorkOrderWaitList(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        /// <summary>
        /// 我的待办工单
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyWaitData")]
        [HttpGet]
        public string WorkOrderMyWaitData(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber,
            string userInfo, int? bussType, int? detailType, string Content)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结 status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //  string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_OPTUSERID={0} AND F_OPTDATE= (select TOP 1 F_OPTDATE   from T_Wo_WorkOrderHistory     WHERE F_INSTANCEID = A.F_INSTANCEID   ORDER BY F_OPTDATE DESC)  )", F_UserID);

            string sql = "";

            //9.29注释
            //sql = " and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and ( F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_OPTUSERID='" + F_UserID + "' AND  CONVERT(CHAR(20), F_OPTDATE, 120)= (select TOP 1  CONVERT(CHAR(20), F_OPTDATE, 120)   from T_Wo_WorkOrderHistory     WHERE F_INSTANCEID = A.F_INSTANCEID   ORDER BY F_OPTDATE DESC)  OR  F_WORKORDERID in  (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID = 1 and F_OPTUSERID = '" + F_UserID + "' AND ReplayTime IS NOT NULL )) )";
            //sql = " and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and ( F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_StateName='未处理' and F_OPTUSERID='" + F_UserID + "'  OR  F_WORKORDERID in  (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID = 1 and F_OPTUSERID = '" + F_UserID + "' AND ReplayTime IS NOT NULL )) )";

            sql = " and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and  F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_StateName='未处理' and F_OPTUSERID='" + F_UserID + "' )";

            //datatype = "可撤回";

            //if (datatype != "可撤回")
            //{
            //9.2号注释   sql = " and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and ( F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_OPTUSERID='" + F_UserID + "' AND F_OPTDATE= (select TOP 1 F_OPTDATE   from T_Wo_WorkOrderHistory     WHERE F_INSTANCEID = A.F_INSTANCEID   ORDER BY F_OPTDATE DESC)  OR  F_WORKORDERID in  (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID = 1 and F_OPTUSERID = '" + F_UserID + "' AND ReplayTime IS NOT NULL )) )";
            //}
            //else
            //{
            //    sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=3 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=1 and F_OPTUSERID={0})", F_UserID);
            //}

            //  string sql = " and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and ( F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_OPTUSERID='"+F_UserID+ "' AND F_OPTDATE= (select TOP 1 F_OPTDATE   from T_Wo_WorkOrderHistory     WHERE F_INSTANCEID = A.F_INSTANCEID   ORDER BY F_OPTDATE DESC)  OR  F_WORKORDERID in  (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID = 1 and F_OPTUSERID = '"+F_UserID+"' AND ReplayTime IS NOT NULL )) )";

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                "F_WORKORDERID",
              "*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew"
              + string.Format(",(select top 1 F_HISTORYID from T_Wo_WorkOrderHistory where F_INSTANCEID=F_WORKORDERID and F_WORKORDERSTATEID=0 and F_OPTUSERID={0} Order by F_OPTDATE desc) AS F_HISTORYID", F_UserID)
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                "ORDER BY F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }


        /// <summary>
        /// 二级单位处理完成提交处理信息
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SubmitOrder(T_Wo_WorkOrderHistory workOrderBaseModel )
        {
            AddAction("t_wo_workorderhistory", workOrderBaseModel.F_HISTORYID.ToMyString(), "提交工单", "数据：" + JsonConvert.SerializeObject(workOrderBaseModel), "关键信息");
            AddAction("t_wo_workorderhistory", workOrderBaseModel.F_HISTORYID.ToMyString(), "部门提交工单", workOrderBaseModel.F_INSTANCEID.ToMyString());
            return orderBLL.SubmitOrder(workOrderBaseModel);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>

        public int WorkOrderMyWaitDataCount()
        {
            string sql = string.Format(" F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=0 and F_OPTUSERID={0})", F_UserID);

            return orderBLL.GetRecordCount(sql);
        }

        #endregion

        #region 待回访



        /// <summary>
        /// 获取待回访工单
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult WorkOrderMyListHF(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        /// <summary>
        /// 全媒体客服处理回访
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SubmitOrderHF(T_Wo_WorkOrderBase workOrderBaseModel)
        {
            AddAction("t_wo_workorderbase", workOrderBaseModel.F_WORKORDERID.ToMyString(), "回访提交工单", workOrderBaseModel.F_CODE);
            AddAction("t_wo_workorderbase", workOrderBaseModel.F_WORKORDERID.ToMyString(), "回访提交工单", "数据：" + JsonConvert.SerializeObject(workOrderBaseModel), "关键信息");
            return orderBLL.SubmitOrderHF(workOrderBaseModel);
        }


        /// <summary>
        /// 待回访工单
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyDataHF")]
        [HttpGet]
        public string WorkOrderMyDataHF(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber,
            string userInfo, int? bussType, int? detailType, string Content)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结 status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=0 and F_OPTUSERID={0})", F_UserID);

            string sql = string.Format(" and F_WORKORDERSTATEID=3 and F_UserId={0}", F_UserID);

            
            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                "F_WORKORDERID",
              "*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew"
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                "ORDER BY F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }



        public int WorkOrderMyDataHFCount()
        {
            string sql = string.Format(" F_WORKORDERSTATEID=3 and F_UserId={0}", F_UserID);
            return orderBLL.GetRecordCount(sql);
        }
        #endregion

        #region 待定责

        public string DZDeptList(int workid)
        {
            DataTable dt = null;
            DataSet ds = historyBll.GetListDZ(string.Format(" F_INSTANCEID={0} ", workid));
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dt = ds.Tables[0];
            }
            ResponseData dataModel = new ResponseData();
            dataModel.code = 0;
            dataModel.count = 0;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        /// <summary>
        /// 定责视图
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult WorkOrderMyListDZ(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        /// <summary>
        /// 录入定责结果
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SubmitOrderDZ(T_Wo_WorkOrderNoticeInput modelinput)
        {
            if (modelinput.orderid == 0)
            {
                return false;
            }
            #region 处理工单事件分类
            Model.T_Wo_WorkOrderBase modelorder =orderBLL.GetModel(modelinput.orderid);
            modelorder.F_PROVINCE = modelinput.ClassName1;
            modelorder.F_CITY = modelinput.ClassName2;
            modelorder.F_AREA = modelinput.ClassName3;
            modelorder.F_SERVICENATURE = modelinput.F_SERVICENATURE;
            modelorder.F_SERVICETYPE = modelinput.F_SERVICETYPE;
            #endregion
            int DZNumber=noticeBLL.updatenum(modelinput.orderid);

            if (string.IsNullOrEmpty(modelinput.DeptIds)&&false)
            {
                #region 通知  定责相关处理部门
                if (modelorder.F_WORKORDERTYPEID == 1)
                {
                    List<T_Wo_WorkOrderHistory> modelhisList = historyBll.GetModelList(string.Format(" F_INSTANCEID={0}", modelinput.orderid));
                    if (modelhisList.Count > 0)
                    {

                        Dictionary<int, string> dict = new Dictionary<int, string>() { };

                        foreach (T_Wo_WorkOrderHistory modelhis in modelhisList)
                        {
                            if (dict.ContainsKey(modelhis.F_OPTBTNID.ToInt32()))
                            {
                                continue;
                            }
                            T_Wo_WorkOrderNotice modeladd = new T_Wo_WorkOrderNotice();
                            modeladd.OrderId = modelinput.orderid;
                            modeladd.DealAdvise = modelinput.F_SERVICETYPE;
                            modeladd.DealResult = modelinput.F_SERVICENATURE;

                            modeladd.Class1 = modelorder.F_PROVINCE;
                            modeladd.Class2 = modelorder.F_CITY;
                            modeladd.Class3 = modelorder.F_AREA;

                            modeladd.DZNumber = DZNumber;
                            modeladd.NoticeState = 1;
                            modeladd.NoticeType = 1;
                            modeladd.Userid = F_UserID;
                            modeladd.Username = F_UserName;

                            modeladd.NoticeUserid = modelhis.F_OPTUSERID.ToInt32();
                            modeladd.NoticeUsername = modelhis.F_UserName;
                            modeladd.NoticeDeptId = modelhis.F_OPTBTNID.ToInt32();
                            modeladd.NoticeDeptName = modelhis.F_NEXTOWNERARRID;
                            modeladd.NoticeDeptContent = modelhis.F_NEXTOWNERARRID + " 调查情况：\n";
                            modeladd.NoticeDeptAdvise = modelhis.F_NEXTOWNERARRID + " 处理意见和整改措施：\n";
                            foreach (T_Wo_WorkOrderHistory modelhistemp in modelhisList)
                            {
                                if (string.IsNullOrEmpty(modelhistemp.F_REMARK))
                                {
                                    continue;
                                }
                                int tempint = modelhistemp.F_REMARK.IndexOf("|");
                                if (tempint == -1)
                                {
                                    modeladd.NoticeDeptContent += modelhistemp.F_REMARK;
                                    modeladd.NoticeDeptAdvise += modelhistemp.F_REMARK;
                                }
                                else
                                {
                                    string[] arrystr = modelhistemp.F_REMARK.Split('|');

                                    modeladd.NoticeDeptContent += arrystr[0];
                                    modeladd.NoticeDeptContent += "\n";
                                    modeladd.NoticeDeptAdvise += arrystr[1];
                                    modeladd.NoticeDeptAdvise += "\n";
                                }
                            }
                            dict.Add(modelhis.F_OPTBTNID.ToInt32(), "1");
                            noticeBLL.Add(modeladd);
                        }

                    }
                }
                else
                {
                    T_Wo_WorkOrderNotice modeladd = new T_Wo_WorkOrderNotice();
                    modeladd.OrderId = modelinput.orderid;
                    modeladd.DealAdvise = modelinput.F_SERVICETYPE;
                    modeladd.DealResult = modelinput.F_SERVICENATURE;

                    modeladd.Class1 = modelorder.F_PROVINCE;
                    modeladd.Class2 = modelorder.F_CITY;
                    modeladd.Class3 = modelorder.F_AREA;

                    modeladd.DZNumber = DZNumber;
                    modeladd.NoticeState = 1;
                    modeladd.NoticeType = 1;
                    modeladd.Userid = F_UserID;
                    modeladd.Username = F_UserName;

                    modeladd.NoticeUserid = modelorder.F_USERID.ToInt32();
                    modeladd.NoticeUsername = modelorder.F_USERNAME;
                    modeladd.NoticeDeptId = modelorder.F_DEPTID.ToInt32();
                    modeladd.NoticeDeptName = modelorder.F_DEPTCODE;

                    modeladd.NoticeDeptContent = modelorder.F_DEPTCODE + " 调查情况：\n";
                    modeladd.NoticeDeptAdvise = modelorder.F_DEPTCODE + " 处理意见和整改措施：\n";
                    if (!string.IsNullOrEmpty(modelorder.F_RETURNVISITCONTENT))
                    {
                        int tempint = modelorder.F_RETURNVISITCONTENT.IndexOf("|");
                        if (tempint == -1)
                        {
                            modeladd.NoticeDeptContent += modelorder.F_RETURNVISITCONTENT;
                            modeladd.NoticeDeptAdvise += modelorder.F_RETURNVISITCONTENT;
                        }
                        else
                        {
                            string[] arrystr = modelorder.F_RETURNVISITCONTENT.Split('|');

                            modeladd.NoticeDeptContent += arrystr[0];
                            modeladd.NoticeDeptContent += "\n";
                            modeladd.NoticeDeptAdvise += arrystr[1];
                            modeladd.NoticeDeptAdvise += "\n";
                        }
                    }
                    noticeBLL.Add(modeladd);
                }

                #endregion
            }
            #region 通知 自选部门
            if (!string.IsNullOrEmpty(modelinput.DeptIds))
            {
                List<T_Sys_Department> modelhisList = deptBll.GetModelList(string.Format(" F_DeptId in ({0})", modelinput.DeptIds));
                if (modelhisList.Count > 0)
                {
                    foreach (T_Sys_Department modeldept in modelhisList)
                    {
                        T_Wo_WorkOrderNotice modeladd = new T_Wo_WorkOrderNotice();
                        modeladd.OrderId = modelinput.orderid;
                        modeladd.DealAdvise = modelinput.F_SERVICETYPE;
                        modeladd.DealResult = modelinput.F_SERVICENATURE;

                        modeladd.Class1 = modelorder.F_PROVINCE;
                        modeladd.Class2 = modelorder.F_CITY;
                        modeladd.Class3 = modelorder.F_AREA;

                        modeladd.DZNumber = DZNumber + 1;
                        modeladd.NoticeState = 1;
                        modeladd.NoticeType = 1;
                        modeladd.Userid = F_UserID;
                        modeladd.Username = F_UserName;

                        modeladd.NoticeUserid = 0;
                        modeladd.NoticeUsername = "";
                        modeladd.NoticeDeptId = modeldept.F_DeptId;
                        modeladd.NoticeDeptName = modeldept.F_DeptName;
                        modeladd.NoticeDeptContent = "";
                        modeladd.NoticeDeptAdvise = "";
                        //modeladd.NoticeDeptContent = modelhis.F_NEXTOWNERARRID + " 调查情况：\n";
                        //modeladd.NoticeDeptAdvise = modelhis.F_NEXTOWNERARRID + " 处理意见和整改措施：\n";
                        noticeBLL.Add(modeladd);
                    }

                }
            }

            #endregion

            AddAction("t_wo_workorderbase", modelorder.F_WORKORDERID.ToMyString(), "提交定责工单", modelorder.F_CODE);
            AddAction("t_wo_workorderbase", modelorder.F_WORKORDERID.ToMyString(), "提交定责工单", "数据：" + JsonConvert.SerializeObject(modelorder), "关键信息");
            return orderBLL.UpdateDZClass(modelorder);
        }


        /// <summary>
        /// 待定责列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyDataDZ")]
        [HttpGet]
        public string WorkOrderMyDataDZ(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber,
            string userInfo, int? bussType, int? detailType, string Content, int? F_RETURNVISITRESULT)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结 status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=0 and F_OPTUSERID={0})", F_UserID);

            //因为yanyanfei和马岭娜调整权限了，所以待定责和已定责改成 展示所有的
           // string sql = string.Format(" and F_WORKORDERSTATEID=4 and (F_HASTENCOUNTS=0 OR F_HASTENCOUNTS IS NULL) and (F_ROAD='{0}' Or F_ROAD='' Or F_ROAD IS NULL)", F_UserID);

            string sql =   string.Format(" and F_WORKORDERSTATEID=4 and (F_HASTENCOUNTS=0 OR F_HASTENCOUNTS IS NULL)");
            if (F_RETURNVISITRESULT != null && F_RETURNVISITRESULT > 0)
            {
                sql += " and F_RETURNVISITRESULT =" + F_RETURNVISITRESULT;
              
            }

            //if (F_UserID == 8000)
            //{
            //    sql = string.Format(" and F_WORKORDERSTATEID=4 and (F_HASTENCOUNTS=0 OR F_HASTENCOUNTS IS NULL)");
            //}

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                "F_WORKORDERID",
              "*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew"
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                "ORDER BY F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public bool canDZ(int workid)
        {
            int n = orderBLL.GetRecordCount(string.Format("F_ROAD='{0}' AND F_WORKORDERID={1}", F_UserID, workid));
            if (n > 0)
                return true;
            else
            {
                Model.T_Wo_WorkOrderBase model = new T_Wo_WorkOrderBase();
                model.F_WORKORDERID = workid;
                model.F_ROAD = F_UserID.ToMyString();
                return orderBLL.UpdateDZuser(model);
            }
        }


        public int WorkOrderMyDataDZCount()
        {
           // string sql = string.Format("  F_WORKORDERSTATEID=4 and (F_HASTENCOUNTS=0 OR F_HASTENCOUNTS IS NULL) and (F_ROAD='{0}' Or F_ROAD='' Or F_ROAD IS NULL)", F_UserID);
            string sql = " F_WORKORDERSTATEID=4 and (F_HASTENCOUNTS=0 OR F_HASTENCOUNTS IS NULL)";
            return orderBLL.GetRecordCount(sql);
        }
        #endregion

        #region 已定责

        /// <summary>
        /// 已定责视图
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult WorkOrderMyListDZ1(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }


        #region
        ///// <summary>
        ///// 录入定责结果
        ///// </summary>
        ///// <param name="workOrderBaseModel"></param>
        ///// <returns></returns>
        //[AcceptVerbs(HttpVerbs.Post)]
        //public bool SubmitOrderDZ1(T_Wo_WorkOrderNoticeInput modelinput)
        //{
        //    if (modelinput.orderid == 0)
        //    {
        //        return false;
        //    }
        //    #region 处理工单事件分类
        //    Model.T_Wo_WorkOrderBase modelorder = new T_Wo_WorkOrderBase();
        //    modelorder.F_WORKORDERID = modelinput.orderid;
        //    modelorder.F_PROVINCE = modelinput.ClassName1;
        //    modelorder.F_CITY = modelinput.ClassName2;
        //    modelorder.F_AREA = modelinput.ClassName3;
        //    #endregion
        //    if (modelinput.Noticelist != null)
        //    {
        //        noticeBLL.updatenum(modelinput.orderid);
        //        foreach (T_Wo_WorkOrderNotice model in modelinput.Noticelist)
        //        {
        //            T_Wo_WorkOrderNotice modeladd = new T_Wo_WorkOrderNotice();

        //            #region 前端传入参数
        //            modeladd.OrderId = modelinput.orderid;
        //            modeladd.DealAdvise = model.DealAdvise;
        //            modeladd.DealResult = model.DealResult;
        //            modeladd.NoticeDeptId = model.NoticeDeptId;
        //            modeladd.NoticeDeptName = model.NoticeDeptName;

        //            #endregion

        //            modeladd.NoticeState = 1;
        //            modeladd.NoticeType = 1;
        //            modeladd.Userid = F_UserID;
        //            modeladd.Username = F_UserName;
        //            List<T_Wo_WorkOrderHistory> modelhisList = historyBll.GetModelList(string.Format(" F_INSTANCEID={0} AND F_OPTBTNID={1}", model.OrderId, model.NoticeDeptId));
        //            foreach (T_Wo_WorkOrderHistory modelhis in modelhisList)
        //            {
        //                modeladd.NoticeDeptContent += modelhis.F_REMARK;
        //                modeladd.NoticeDeptAdvise += modelhis.F_REMARK;
        //                modeladd.NoticeUserid = modelhis.F_OPTUSERID.ToInt32();
        //                modeladd.NoticeUsername = modelhis.F_UserName;
        //            }
        //            noticeBLL.Add(modeladd);

        //        }
        //    }
        //    return orderBLL.UpdateDZClass(modelorder);
        //}
        #endregion

        /// <summary>
        /// 已定责列表
        /// </summary>
        /// <param name="F_RETURNVISITRESULT">满意度</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyDataDZ1")]
        [HttpGet]
        public string WorkOrderMyDataDZ1(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber,
            string userInfo, int? bussType, int? detailType, string Content,int? F_RETURNVISITRESULT)
        {
            bussType = 3;
            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结 status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=0 and F_OPTUSERID={0})", F_UserID);
            string sql = "";
            //if (F_RoleID != 17)
            //{
            //    sql = string.Format(" and F_WORKORDERSTATEID=5 and F_HOUSING='投诉' and F_REPAIRREQUEST='已定责' and F_ROAD='{0}' ", F_UserID);
            //}
            //else
            //{
                sql = string.Format(" and F_WORKORDERSTATEID=5 and F_HOUSING='投诉' and F_REPAIRREQUEST='已定责'");
            //}

            //满意度
            if (F_RETURNVISITRESULT>0)
            {
                sql += " and F_RETURNVISITRESULT ="+ F_RETURNVISITRESULT ;
            }

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                "F_WORKORDERID",
              "*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew"
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                "ORDER BY F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


             dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        #endregion 

        #region 已处理建议

        /// <summary>
        /// 已处理建议视图
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult WorkOrderMyListJY(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }


        /// <summary>
        /// 获取建议工单信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyDataJY")]
        [HttpGet]
        public string WorkOrderMyDataJY(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber, string userInfo, int? bussType, int? detailType, string Content)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = " and F_WORKORDERSTATEID=5 and F_HOUSING='建议' ";

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and t.F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and t.F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }

            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and t.F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and t.F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            #region
            //状态  F_WORKORDERSTATEID   0未处理 1处理中  2已办结（结束） 3待回访 4待定责 5已定责
            //if (status != null)
            //{
            //    sql += " and t.F_WORKORDERSTATEID =" + status;
            //}
            #endregion
            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and t.F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and t.F_REPAIRLEVEL =" + detailType;
            }

            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and t.F_CONTENT like '%" + Content.Trim() + "%'";
            }

            #region
            ////处理方式  F_WORKORDERTYPEID  处理方式  1转单 3客服处理
            //if (GoType != null && GoType > 0)
            //{
            //    sql += " and t.F_WORKORDERTYPEID =" + GoType;
            //}
            ////接入渠道 1语音 2留言  3微信 4微博
            //if (FromType != null && FromType > 0)
            //{
            //    sql += " and t.F_REPAIRMANID =" + FromType;
            //}
            #endregion

            //查询按钮
            if (types == 0 || types == null)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {

                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
                else
                {
                    //string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                    //string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                    //sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    //sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                    string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string startDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            //一天按钮
            else if (types == 4)
            {
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            string tableNew = "(select DISTINCT t.*  from T_Wo_WorkOrderBase t LEFT JOIN T_Wo_WorkOrderHistory h ON t.F_WORKORDERID=h.F_INSTANCEID LEFT JOIN T_Sys_UserAccount u ON  t.F_USERID= u.F_UserId ) as t";
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                tableNew,
                "F_WORKORDERID",
                " t.*,CONVERT(varchar,t.F_CREATEDATE, 120 ) as F_CREATEDATENew"
                  + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1  F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=t.F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT  top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=t.F_KSHADDRESSCODE) ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath"
,
                sql,
                "ORDER BY t.F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }


        #endregion 

        #region 相关通知

        /// <summary>
        /// 已定责视图
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult WorkOrderMyListNotice(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }


        /// <summary>
        /// 相关表扬
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyDataNotice")]
        [HttpGet]
        public string WorkOrderMyDataNotice(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber,
            string userInfo, int? bussType, int? detailType, string Content, int NoticeType)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结 status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=0 and F_OPTUSERID={0})", F_UserID);

            string sql = string.Format(" and F_WORKORDERTYPEID=3 and F_WORKORDERSTATEID in (2,5) and T2.NoticeDeptId={0} AND NoticeType={1} AND  NoticeState=1", F_DeptId, NoticeType);
            if (NoticeType == 1)
            {
                sql = string.Format(" and F_HOUSING='投诉'  and F_WORKORDERSTATEID=5 and T2.NoticeDeptId={0} AND NoticeType={1} AND  NoticeState=1", F_DeptId, NoticeType);
            }


            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {

                if (!string.IsNullOrEmpty(dateParty))
                {
                    //if (F_UserName == "闫艳飞")
                    //{
                    //    sql += " and  F_CREATEDATE>=' 2021-06-21 00:00:00'";
                    //}
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {
                //if (F_UserName == "闫艳飞")
                //{
                //    sql += " and  F_CREATEDATE>=' 2021-06-21 00:00:00'";
                //}

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {
                //if (F_UserName == "闫艳飞")
                //{
                //    sql += " and  F_CREATEDATE>=' 2021-06-21 00:00:00'";
                //}

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {
                //if (F_UserName == "闫艳飞")
                //{
                //    sql += " and  F_CREATEDATE>=' 2021-06-21 00:00:00'";
                //}
                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetTableList(
                " T_Wo_WorkOrderBase T1 Inner join T_Wo_WorkOrderNotice T2 On T1.F_WORKORDERID=T2.OrderId  ",
                "F_WORKORDERID",
              "T1.*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew,NoticeDeptId,NoticeDeptName"
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                " F_CREATEDATE desc",
                limit,
                page,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        #endregion

        #region 超时工单

        /// <summary>
        /// 视图
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult OverOrderView(int dtype=0)
        {
            CallScreenModel callScreenModel = new CallScreenModel();
            //业务类型字典表
            callScreenModel.WorkOrderTypeModelList = GetWorkTypeModelList();

            //业务类型字典表
            callScreenModel.GoTypeModelList = GetCodeType("CLFS");
            //话务标志
            callScreenModel.SeatFlag = F_SeatFlag;

            //1、所有工单页面2、工单编辑页面
              callScreenModel.OrderType = 1;



            return View(callScreenModel);
        }


        /// <summary>
        /// 超时工单列表 获取工单信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("OverOrderData")]
        [HttpGet]
        public string OverOrderData(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber, string userInfo, int? status, int? bussType, int? detailType, string Content, int? GoType, int? FromType,int? overtime)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string  sql = " AND  t.F_REMAINDERENDTIME>t.F_RETURNVISITTIME  AND t.F_HOUSING IN ('投诉','建议','咨询')";

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and t.F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and t.F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }

            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and t.F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and t.F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }
            //状态  F_WORKORDERSTATEID   0未处理 1处理中  2已办结
            if (status != null)
            {
                sql += " and t.F_WORKORDERSTATEID =" + status;
            }
            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and t.F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and t.F_REPAIRLEVEL =" + detailType;
            }

            #region
            //定责次数
            //if (F_HASTENCOUNTS != null)
            //{
            //    if (F_HASTENCOUNTS == 0)
            //    {
            //        sql += " AND (t.F_HASTENCOUNTS is null OR  t.F_HASTENCOUNTS =0) ";
            //    }
            //    else
            //    {
            //        sql += " AND  t.F_HASTENCOUNTS >0 ";
            //    }
            //}
            #endregion

            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and t.F_CONTENT like '%" + Content.Trim() + "%'";
            }
            //处理方式  F_WORKORDERTYPEID  处理方式  1转单 3客服处理
            if (GoType != null && GoType > 0)
            {
                sql += " and t.F_WORKORDERTYPEID =" + GoType;
            }
            //接入渠道 1语音 2留言  3微信 4微博
            if (FromType != null && FromType > 0)
            {
                sql += " and t.F_REPAIRMANID =" + FromType;
            }


            //查询按钮
            if (types == 0 || types == null)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {

                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
                else
                {
                    string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            //一天按钮
            else if (types == 4)
            {
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            // string tableNew = string.Format("(select DISTINCT t.*  from T_Wo_WorkOrderBase t LEFT JOIN T_Wo_WorkOrderHistory h ON t.F_WORKORDERID=h.F_INSTANCEID LEFT JOIN T_Sys_UserAccount u ON  t.F_USERID= u.F_UserId  where  (u.F_DeptId={0} OR  h.F_OPTBTNID={0})) as t", F_DeptId);

            //CONVERT(VARCHAR(10), F_OVERTIMES / 3600) + '时' + CONVERT(VARCHAR(10), F_OVERTIMES % 3600 / 60) + '分' + CONVERT(VARCHAR(10), F_OVERTIMES % 3600 % 60) + '秒' AS F_OVERTIME
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
             "T_Wo_WorkOrderBase as t",
              // tableNew,
                "F_WORKORDERID",
                " t.*,CONVERT(varchar,t.F_CREATEDATE, 120 ) as F_CREATEDATENew, dbo.GetConvertSecond(DATEDIFF(SECOND,F_RETURNVISITTIME,F_REMAINDERENDTIME)) AS F_OVERTIME"
                  + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1  F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=t.F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT  top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=t.F_KSHADDRESSCODE) ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath"
,
                sql,
                "ORDER BY t.F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        #endregion

        #region 全部工单

        /// <summary>
        /// 获取所有工单信息
        /// </summary>
        /// <returns></returns>
        public ActionResult WorkOrderAllList(int? orderType)
        {
            CallScreenModel callScreenModel = new CallScreenModel();
            //业务类型字典表
            callScreenModel.WorkOrderTypeModelList = GetWorkTypeModelList();

            //业务类型字典表
            callScreenModel.GoTypeModelList = GetCodeType("CLFS");
            //话务标志
            callScreenModel.SeatFlag = F_SeatFlag;

            //1、所有工单页面2、工单编辑页面
            if (orderType != null && orderType > 0)
            {
                callScreenModel.OrderType = int.Parse(orderType.ToString());
            }
            else
            {
                callScreenModel.OrderType = 1;
            }


            return View(callScreenModel);
        }


        /// <summary>
        /// 获取工单信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderAllData")]
        [HttpGet]
        public string WorkOrderAllList(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber, string userInfo, int? status, int? bussType, int? detailType, string Content, int? GoType, int? FromType, int? F_HASTENCOUNTS,int? turnout)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = "";

            //未按时转出
            if (turnout.ToInt32() > 0)
            {
                sql += " and t.turntime =0 ";
            }

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and t.F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and t.F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }

            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and t.F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and t.F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }
            //状态  F_WORKORDERSTATEID   0未处理 1处理中  2已办结（结束） 3待回访 4待定责 5已定责
            if (status != null)
            {
                if (status == 6)
                {
                    sql += " and t.F_WORKORDERSTATEID =5 and t.F_REPAIRREQUEST='已定责'";

                }
                else if (status == 5)
                {
                    sql += " and t.F_WORKORDERSTATEID =5 and t.F_REPAIRREQUEST='已处理'";
                }
                else
                {
                    sql += " and t.F_WORKORDERSTATEID =" + status;

                }

            }
            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and t.F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and t.F_REPAIRLEVEL =" + detailType;
            }

            #region
            //定责次数
            //if (F_HASTENCOUNTS != null)
            //{
            //    if (F_HASTENCOUNTS == 0)
            //    { 
            //        sql += " AND (t.F_HASTENCOUNTS is null OR  t.F_HASTENCOUNTS =0) ";
            //    }
            //    else
            //    {
            //        sql += " AND  t.F_HASTENCOUNTS >0 ";
            //    }
            //}
            #endregion


            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and t.F_CONTENT like '%" + Content.Trim() + "%'";
            }
            //处理方式  F_WORKORDERTYPEID  处理方式  1转单 3客服处理
            if (GoType != null && GoType > 0)
            {
                sql += " and t.F_WORKORDERTYPEID =" + GoType;
            }
            //接入渠道 1语音 2留言  3微信 4微博
            if (FromType != null && FromType > 0)
            {
                sql += " and t.F_REPAIRMANID =" + FromType;
            }


            //查询按钮
            if (types == 0 || types == null)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {

                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
                else
                {
                    string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一年按钮
            else if (types == 3)
            {
 
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            //一天按钮
            else if (types == 4)
            {
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }



            //string tableNew = "(select DISTINCT  t.*  ";
            //tableNew += ", case WHEN F_SENTORDERTIME IS NULL AND DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE),  GETDATE())> 0  THEN 0 WHEN F_SENTORDERTIME IS NULL  THEN 1 WHEN DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE), F_SENTORDERTIME)> 0 THEN 0 ELSE 1 END AS turntime ";
            //tableNew += " from T_Wo_WorkOrderBase t LEFT JOIN T_Wo_WorkOrderHistory h ON t.F_WORKORDERID=h.F_INSTANCEID LEFT JOIN T_Sys_UserAccount u ON  t.F_USERID= u.F_UserId";
            //tableNew += " ) as t";

            //string tableNew = "(select    t.F_WORKORDERID ,t.F_RETURNVISITCONTENT as oldF_RETURNVISITCONTENT,t. F_WORKORDERTYPEID,t. F_WORKORDERSTATEID,t. F_CODE,t. F_WORKORDERNAME,t. F_NUMBER,t. F_CUSTOMERNAME,t. F_CUSTOMERTELEPHONE,t. F_LINKMAN,t. F_LINKMANTELEPHONE,t. F_ADSLACCOUNT,t. F_BANDWIDTH,t. F_STANDARDADDRESS,t. F_INSTALLADDRESS,t. F_KSHADDRESSCODE,t. F_PROVINCE,t. F_CITY,t. F_AREA,t. F_ROAD,t. F_HOUSING,t. F_WORKORDERFROM,t. F_WORKORDERLEVELID,t. F_FILEFLAG,t. F_CONTENT,t. F_CREATEBY,t. F_CREATEDATE,t. F_DEPTCODE,t. F_EMPCODE,t. F_DEPTID,t. F_USERID,t. F_USERNAME,t. F_RETURNVISITFLAG,t. F_RETURNVISITSTATE,t. F_RETURNVISITRESULT ,t. F_DELETEFLAG,t. F_ALLUSETIMES,t. F_STARTTIME,t. F_ENDTIME,t. F_REMAINDERTIME,t. F_REMAINDERENDTIME,t. F_SUBSCRIBETIME1,t. F_SUBSCRIBETIME2,t. F_ACCEPTTIMELIMIT,t. F_ASKFINISHTIME,t. F_OVERTIMES,t. F_FORMID,t. F_HASTENCOUNTS,t. F_RANGEID,t. F_RANGEX,t. F_RANGEY,t. F_INSTANCEID,t. F_CUSTOMERID,t. F_REPAIRMANID,t. F_REPAIRMANNAME,t. F_REPAIRMANPHONE,t. F_REPAIRREQUEST,t. F_REPAIRCLOSEMANID,t. F_REPAIRCLOSEMANNAME,t. F_REPAIRCLOSEREASONS,t. F_REPAIRCLOSEREPTID,t. F_REPAIRCLOSEREPT,t. F_REPAIRLEVEL,t. F_REPAIRLEVELNAME,t. F_SENTORDERTIME,t. F_USERPHONE,t. F_RETURNVISITTIME,t. F_REQUESTSERVICETIME,t. F_SERVICETYPE,t. F_DECLARATIONTIME,t. F_SERVICENATURE,t. F_SERVICEWAY,t. F_RETURNVISITMAN,t. F_RETURNVISITID,t. F_RETURNVISITOPINION,t. F_RETURNVISITPROBLEM,t. F_CallId,t. F_CREATEBYids ";
            //tableNew += ",(case WHEN F_SENTORDERTIME IS NULL AND DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE),  GETDATE())> 0  THEN 0 WHEN F_SENTORDERTIME IS NULL  THEN 1 WHEN DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE), F_SENTORDERTIME)> 0 THEN 0 ELSE 1 END) AS turntime ";
            //tableNew += " from T_Wo_WorkOrderBase t ";
            //tableNew += " ) as t";

            string tableNew = "(select    t.F_WORKORDERID ,t.F_RETURNVISITCONTENT ,t. F_WORKORDERTYPEID,t. F_WORKORDERSTATEID,t. F_CODE,t. F_WORKORDERNAME,t. F_NUMBER,ISNULL(t.F_CUSTOMERNAME,[dbo].GetCusNameByUserPhone(t.F_CUSTOMERTELEPHONE)) F_CUSTOMERNAME,t. F_CUSTOMERTELEPHONE,t. F_LINKMAN,t. F_LINKMANTELEPHONE,t. F_ADSLACCOUNT,t. F_BANDWIDTH,t. F_STANDARDADDRESS,t. F_INSTALLADDRESS,t. F_KSHADDRESSCODE,t. F_PROVINCE,t. F_CITY,t. F_AREA,t. F_ROAD,t. F_HOUSING,t. F_WORKORDERFROM,t. F_WORKORDERLEVELID,t. F_FILEFLAG,t. F_CONTENT,t. F_CREATEBY,t. F_CREATEDATE,t. F_DEPTCODE,t. F_EMPCODE,t. F_DEPTID,t. F_USERID,t. F_USERNAME,t. F_RETURNVISITFLAG,t. F_RETURNVISITSTATE,t. F_RETURNVISITRESULT ,t. F_DELETEFLAG,t. F_ALLUSETIMES,t. F_STARTTIME,t. F_ENDTIME,t. F_REMAINDERTIME,t. F_REMAINDERENDTIME,t. F_SUBSCRIBETIME1,t. F_SUBSCRIBETIME2,t. F_ACCEPTTIMELIMIT,t. F_ASKFINISHTIME,t. F_OVERTIMES,t. F_FORMID,t. F_HASTENCOUNTS,t. F_RANGEID,t. F_RANGEX,t. F_RANGEY,t. F_INSTANCEID,t. F_CUSTOMERID,t. F_REPAIRMANID,t. F_REPAIRMANNAME,t. F_REPAIRMANPHONE,t. F_REPAIRREQUEST,t. F_REPAIRCLOSEMANID,t. F_REPAIRCLOSEMANNAME,t. F_REPAIRCLOSEREASONS,t. F_REPAIRCLOSEREPTID,t. F_REPAIRCLOSEREPT,t. F_REPAIRLEVEL,t. F_REPAIRLEVELNAME,t. F_SENTORDERTIME,t. F_USERPHONE,t. F_RETURNVISITTIME,t. F_REQUESTSERVICETIME,t. F_SERVICETYPE,t. F_DECLARATIONTIME,t. F_SERVICENATURE,t. F_SERVICEWAY,t. F_RETURNVISITMAN,t. F_RETURNVISITID,t. F_RETURNVISITOPINION,t. F_RETURNVISITPROBLEM,t. F_CallId,t. F_CREATEBYids ";
          tableNew += ",(case WHEN F_SENTORDERTIME IS NULL AND DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE),  GETDATE())> 0  THEN 0 WHEN F_SENTORDERTIME IS NULL  THEN 1 WHEN DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE), F_SENTORDERTIME)> 0 THEN 0 ELSE 1 END) AS turntime ";
            tableNew += " from T_Wo_WorkOrderBase t ";
            tableNew += " ) as t";



            #region
            //if (F_RoleID == 17)
            //{
            //    tableNew += " ) as t";
            //}
            //else
            //{
            //    tableNew += string.Format(" where (u.F_DeptId ={0}     OR h.F_OPTBTNID ={0})) as t", F_DeptId);
            //}
            #endregion
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                tableNew,
                "F_WORKORDERID",
                " t.*,CONVERT(varchar,t.F_CREATEDATE, 120 ) as F_CREATEDATENew"
               
                  //  + @" ( case WHEN t.F_WORKORDERTYPEID =1  then(select top 1 T_Wo_WorkOrderHistory.F_REMARK  from T_Wo_WorkOrderHistory where T_Wo_WorkOrderHistory.F_StateName = '已处理' and T_Wo_WorkOrderHistory.F_INSTANCEID = t.F_workorderid order by T_Wo_WorkOrderHistory.F_optdate desc)    WHEN t.F_WORKORDERTYPEID = 3 then t.oldF_RETURNVISITCONTENT ELSE '' end  ) as F_RETURNVISITCONTENT"
                  + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1  F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=t.F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT  top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=t.F_KSHADDRESSCODE) ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath"
,
                sql,
                "ORDER BY t.F_CREATEDATE desc",
                 limit,
                page,
                true,
                out recordCount);

            
            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        #endregion


      #region 公共

        /// <summary>
        /// 获取二级业务类型
        /// </summary>
        /// <param name="deptId">业务类别</param>
        /// <returns></returns>
        public string GetBussType(int? bussnessType)
        {

            try
            {
                if (bussnessType != null && bussnessType > 0)
                {
                    List<Model.T_Wo_WorkOrderType> modelList = GetWorkTypeModelList();
                    return JsonConvert.SerializeObject(modelList.Where(t => t.F_WorkOrderTypeId == bussnessType).ToList()[0].WorkTypeModelList);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {

            }
            return null;

        }



        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public string ExToExcel(ExToExcel paramModel)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = "";

            //工单编号
            if (!string.IsNullOrEmpty(paramModel.workorderNum))
            {
                sql += " and t.F_CODE like '%" + paramModel.workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(paramModel.cusName))
            {
                sql += " and t.F_CUSTOMERNAME like '%" + paramModel.cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(paramModel.phoneNumber))
            {
                sql += " and t.F_CUSTOMERTELEPHONE like '%" + paramModel.phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(paramModel.userInfo))
            {
                sql += " and t.F_LINKMAN like '%" + paramModel.userInfo.Trim() + "%'";
            }
            //状态
            if (paramModel.status != null)
            {
                sql += " and t.F_WORKORDERSTATEID =" + paramModel.status;
            }
            //业务类别
            if (paramModel.bussType != null && paramModel.bussType > 0)
            {
                sql += " and t.F_FILEFLAG =" + paramModel.bussType;
            }
            //业务类型
            if (paramModel.detailType != null && paramModel.detailType > 0)
            {
                sql += " and t.F_REPAIRLEVEL =" + paramModel.detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(paramModel.Content))
            {
                sql += " and t.F_CONTENT like '%" + paramModel.Content.Trim() + "%'";
            }
            //处理方式
            if (paramModel.GoType != null && paramModel.GoType > 0)
            {
                sql += " and t.F_WORKORDERTYPEID =" + paramModel.GoType;
            }
            //渠道
            if (paramModel.FromType != null && paramModel.FromType > 0)
            {
                sql += " and t.F_REPAIRMANID =" + paramModel.FromType;
            }



            //查询按钮

            if (!string.IsNullOrEmpty(paramModel.dateParty))
            {

                string startDate = paramModel.dateParty.Substring(0, 10);
                string endDate = paramModel.dateParty.Substring(12);
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            else
            {
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
            }
            //string tableNew = "(select DISTINCT  t.*  ";
            //tableNew += ", case WHEN F_SENTORDERTIME IS NULL AND DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE),  GETDATE())> 0  THEN 0 WHEN F_SENTORDERTIME IS NULL  THEN 1 WHEN DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE), F_SENTORDERTIME)> 0 THEN 0 ELSE 1 END AS turntime ";
            //tableNew += " from T_Wo_WorkOrderBase t LEFT JOIN T_Wo_WorkOrderHistory h ON t.F_WORKORDERID=h.F_INSTANCEID LEFT JOIN T_Sys_UserAccount u ON  t.F_USERID= u.F_UserId";
            //tableNew += " ) as t";

            string tableNew = "(select DISTINCT  F_WORKORDERID ,F_RETURNVISITCONTENT , F_WORKORDERTYPEID  , F_WORKORDERSTATEID  , F_CODE  , F_WORKORDERNAME  , F_NUMBER  ,ISNULL(F_CUSTOMERNAME, [dbo].GetCusNameByUserPhone(F_CUSTOMERTELEPHONE)) F_CUSTOMERNAME  , F_CUSTOMERTELEPHONE  , F_LINKMAN  , F_LINKMANTELEPHONE  , F_ADSLACCOUNT  , F_BANDWIDTH  , F_STANDARDADDRESS  , F_INSTALLADDRESS  , F_KSHADDRESSCODE  , F_PROVINCE  , F_CITY  , F_AREA  , F_ROAD  , F_HOUSING  , F_WORKORDERFROM  , F_WORKORDERLEVELID  , F_FILEFLAG  , F_CONTENT  , F_CREATEBY  , F_CREATEDATE  , F_DEPTCODE  , F_EMPCODE  , F_DEPTID  , F_USERID  , F_USERNAME  , F_RETURNVISITFLAG  , F_RETURNVISITSTATE  , F_RETURNVISITRESULT     , F_DELETEFLAG  , F_ALLUSETIMES  , F_STARTTIME  , F_ENDTIME  , F_REMAINDERTIME  , F_REMAINDERENDTIME  , F_SUBSCRIBETIME1  , F_SUBSCRIBETIME2  , F_ACCEPTTIMELIMIT  , F_ASKFINISHTIME  , F_OVERTIMES  , F_FORMID  , F_HASTENCOUNTS  , F_RANGEID  , F_RANGEX  , F_RANGEY  , F_INSTANCEID  , F_CUSTOMERID  , F_REPAIRMANID  , F_REPAIRMANNAME  , F_REPAIRMANPHONE  , F_REPAIRREQUEST  , F_REPAIRCLOSEMANID  , F_REPAIRCLOSEMANNAME  , F_REPAIRCLOSEREASONS  , F_REPAIRCLOSEREPTID  , F_REPAIRCLOSEREPT  , F_REPAIRLEVEL  , F_REPAIRLEVELNAME  , F_SENTORDERTIME  , F_USERPHONE  , F_RETURNVISITTIME  , F_REQUESTSERVICETIME  , F_SERVICETYPE  , F_DECLARATIONTIME  , F_SERVICENATURE  , F_SERVICEWAY  , F_RETURNVISITMAN  , F_RETURNVISITID  , F_RETURNVISITOPINION  , F_RETURNVISITPROBLEM  , F_CallId  , F_CREATEBYids    ";
            tableNew += ", case WHEN F_SENTORDERTIME IS NULL AND DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE),  GETDATE())> 0  THEN 0 WHEN F_SENTORDERTIME IS NULL  THEN 1 WHEN DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE), F_SENTORDERTIME)> 0 THEN 0 ELSE 1 END AS turntime ";
            tableNew += " from T_Wo_WorkOrderBase t ";
            tableNew += " ) as t";




            //     string tableNew = string.Format("(select DISTINCT t.*  from T_Wo_WorkOrderBase t LEFT JOIN T_Wo_WorkOrderHistory h ON t.F_WORKORDERID=h.F_INSTANCEID LEFT JOIN T_Sys_UserAccount u ON  t.F_USERID= u.F_UserId  where  (u.F_DeptId={0} OR  h.F_OPTBTNID={0})) as t", F_DeptId);
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                tableNew,
                "F_WORKORDERID",
                " t.F_CODE as 工单编号"
                 + ",F_CUSTOMERNAME as 客户姓名"
                   + ",F_CUSTOMERTELEPHONE as 客户电话"
                     + ",F_LINKMAN as 坐席工号"
                          + ",F_REPAIRMANNAME as 坐席姓名"
                               + ",F_WORKORDERFROM as 接入渠道"
                                  + " , CONVERT(varchar, t.F_CREATEDATE, 120) as 受理时间"
                                    + ",F_REPAIRREQUEST as 业务状态"
                                         + ",F_CONTENT as 内容"
                                          + ",F_RETURNVISITCONTENT as 处理回复内容"
                                               //  + ",( case WHEN t.F_WORKORDERTYPEID =1  then(select top 1 T_Wo_WorkOrderHistory.F_REMARK  from T_Wo_WorkOrderHistory where T_Wo_WorkOrderHistory.F_StateName = '已处理' and T_Wo_WorkOrderHistory.F_INSTANCEID = t.F_workorderid order by T_Wo_WorkOrderHistory.F_optdate desc)    WHEN t.F_WORKORDERTYPEID = 3 then t.oldF_RETURNVISITCONTENT ELSE '' end  ) as     处理回复内容"
                                               + ",F_HOUSING as 业务类型"
                                                 + ",F_ADSLACCOUNT as 业务明细"
                ,
                sql,
                "ORDER BY t.F_CREATEDATE desc",
                100000,
                1,
                true,
                out recordCount);

            ResponseDataModel NewData = new ResponseDataModel();

            ExcelReadWrite erw = new ExcelReadWrite();
            byte[] Filebyte = erw.GetExcelByte(dt, "sheet1", true);

            int officeVersion = erw.OfficeType();

            if (officeVersion == 1 || officeVersion == 2)
            {
                NewData.Message = DateTime.Now.ToString("yyyy-MM-dd") + "工单数据.xls";
            }
            else
            {
                NewData.Message = DateTime.Now.ToString("yyyy-MM-dd") + "工单数据.xlsx";
            }
            NewData.Code = "0";
            NewData.Data = JsonConvert.SerializeObject(Filebyte);

            return JsonConvert.SerializeObject(NewData);
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public string ExToExcelDZ(ExToExcel paramModel)
        {
            string sql = " and F_WORKORDERSTATEID=5 and F_HOUSING='投诉'  and F_REPAIRREQUEST='已定责' ";

            //工单编号
            if (!string.IsNullOrEmpty(paramModel.workorderNum))
            {
                sql += " and F_CODE like '%" + paramModel.workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(paramModel.cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + paramModel.cusName.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(paramModel.cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + paramModel.cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(paramModel.phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + paramModel.phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(paramModel.userInfo))
            {
                sql += " and F_LINKMAN like '%" + paramModel.userInfo.Trim() + "%'";
            }
            //状态
            if (paramModel.status != null)
            {
                sql += " and F_WORKORDERSTATEID =" + paramModel.status;
            }
            //业务类别
            if (paramModel.bussType != null && paramModel.bussType > 0)
            {
                sql += " and F_FILEFLAG =" + paramModel.bussType;
            }
            //业务类型
            if (paramModel.detailType != null && paramModel.detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + paramModel.detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(paramModel.Content))
            {
                sql += " and F_CONTENT like '%" + paramModel.Content.Trim() + "%'";
            }
            //处理方式
            if (paramModel.GoType != null && paramModel.GoType > 0)
            {
                sql += " and F_WORKORDERTYPEID =" + paramModel.GoType;
            }
            
            //渠道
            if (paramModel.FromType != null && paramModel.FromType > 0)
            {
                sql += " and F_REPAIRMANID =" + paramModel.FromType;
            }
            if (paramModel.F_RETURNVISITRESULT != null && paramModel.F_RETURNVISITRESULT > 0)
            {
                sql += " and F_RETURNVISITRESULT =" + paramModel.F_RETURNVISITRESULT;
            }
            //处理方式
            if (paramModel.F_HASTENCOUNTS != null && paramModel.F_HASTENCOUNTS > -1)
            {
                if (paramModel.F_HASTENCOUNTS == 0)
                {
                    sql += " and F_HASTENCOUNTS =0";
                }
                else
                {
                    sql += " and F_HASTENCOUNTS >0";
                }

            }
            //查询按钮

            if (!string.IsNullOrEmpty(paramModel.dateParty))
            {

                string startDate = paramModel.dateParty.Substring(0, 10);
                string endDate = paramModel.dateParty.Substring(12);
                sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            else
            {
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
            }
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                " F_WORKORDERID",
                " F_WORKORDERID as 序号"
                + ", F_CODE as 工单编号"
                + ", F_UserName as 受理人"
                + ",CONVERT(varchar,  F_CREATEDATE, 120) as 受理时间"
                + ", F_WORKORDERFROM as 事件来源"
                + ", F_RETURNVISITPROBLEM as 事件概况"
                  + ", F_CONTENT as 事件内容"
                + ", F_PROVINCE as 一级内容分类"
                + " ,  F_CITY as 二级内容分类"
                + ",  F_AREA as 三级内容分类"
                + ", '' as 责任部门"
                + ", '' as 责任部门调查情况"
                + ", '' as 责任部门处理意见和整改措施"
                + ", F_SERVICETYPE as 定性定责处理意见"
                + ", F_SERVICENATURE as 定性定责结果"
                + ",case when F_WORKORDERSTATEID=0 then '未处理' when F_WORKORDERSTATEID=1 then '处理中' else '已处理' END AS 办理状态"
                + " ,'-' as 风险点"
                + ",'-' as 风险等级"
                + ",'-' as 风险概率"
                + ",'-' as 风险评估"
                + ",case when F_WORKORDERTYPEID=1 then '转单处理' else '客服处理' END AS 处理方式"
               + ",F_RETURNVISITCONTENT"
                + ",case when F_RETURNVISITRESULT=1 then '满意' when F_RETURNVISITRESULT=2 then '一般' when F_RETURNVISITRESULT=3 then '不满意' else '空' END  AS 满意度"
                ,
                sql,
                "ORDER BY F_CREATEDATE desc",
                100000,
                1,
                true,
                out recordCount);
            if (recordCount > 0)
            {
                int tempn = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    string temp1 = "";
                    string temp2 = "";
                    string temp3 = "";
                    #region 责任部门

                    List<T_Wo_WorkOrderNotice> modellistnotice = noticeBLL.GetModelList(string.Format(" orderid={0} and NoticeType=1 AND  NoticeState=1", dr["序号"].ToInt32()));
                    if (modellistnotice.Count > 0)
                    {
                        foreach (T_Wo_WorkOrderNotice modelnotice in modellistnotice)
                        {
                            temp1 += modelnotice.NoticeDeptName + "\n";
                        }
                    }
                    dr["责任部门"] = temp1;
                    #endregion

                    if (dr["处理方式"].ToMyString() == "转单处理")
                    {
                        List<T_Wo_WorkOrderHistory> modelhisList = historyBll.GetModelList(string.Format(" F_INSTANCEID={0} and F_StateName='已处理'   AND NeedReplayTime IS NOT NULL ", dr["序号"].ToInt32()));
                        if (modelhisList.Count > 0)
                        {
                            foreach (T_Wo_WorkOrderHistory modelhistemp in modelhisList)
                            {
                                temp2 += modelhistemp.F_NEXTOWNERARRID + " 调查情况：\n";
                                temp3 += modelhistemp.F_NEXTOWNERARRID + " 处理意见和整改措施：\n";
                          
                                if (string.IsNullOrEmpty(modelhistemp.F_REMARK))
                                {
                                    continue;
                                }
                                int tempint = modelhistemp.F_REMARK.IndexOf("|");
                                if (tempint == -1)
                                {
                                    temp2 += modelhistemp.F_REMARK;
                                    temp3 += modelhistemp.F_REMARK;
                                }
                                else
                                {
                                    string[] arrystr = modelhistemp.F_REMARK.Split('|');

                                    temp2 += arrystr[0];
                                    temp2 += "\n";
                                    temp3 += arrystr[1];
                                    temp3 += "\n";
                                }                                
                            }
                        }

                    }
                    else
                    {
                        temp2 += dr["F_RETURNVISITCONTENT"].ToMyString();
                        temp3 += "客服即时处理工单";
                    }

                    dr["责任部门调查情况"] = temp2;
                    dr["责任部门处理意见和整改措施"] = temp3;

                    tempn += 1;
                    dr["序号"] = tempn;
                    //tempCode = dr["工单编号"].ToMyString();
                }
            }

            DataTable dataTable = dt.Copy();
            foreach (DataColumn dc in dataTable.Columns)
            {
                if (dc.ColumnName.Equals("F_RETURNVISITCONTENT"))
                    dt.Columns.Remove(dc.ColumnName);
                if (dc.ColumnName.Equals("F_WORKORDERTYPEID"))
                    dt.Columns.Remove(dc.ColumnName);
            }
            ResponseDataModel NewData = new ResponseDataModel();

            ExcelReadWrite erw = new ExcelReadWrite();
            byte[] Filebyte = erw.GetExcelByte(dt, "sheet1", true);

            int officeVersion = erw.OfficeType();

            if (officeVersion == 1 || officeVersion == 2)
            {
                NewData.Message = DateTime.Now.ToString("yyyy-MM-dd") + "全媒体客服转办及化解投诉定性定责情况汇总表.xls";
            }
            else
            {
                NewData.Message = DateTime.Now.ToString("yyyy-MM-dd") + "全媒体客服转办及化解投诉定性定责情况汇总表.xlsx";
            }
            NewData.Code = "0";
            NewData.Data = JsonConvert.SerializeObject(Filebyte);

            return JsonConvert.SerializeObject(NewData);
        }

        #endregion

        #region 工单详情
        public ActionResult ViewMyOrder(int orderKeyId)
        {
            //实例化基础信息表
            CallScreenModel callScreenModel = new CallScreenModel();

            //获取业务工单信息
            if (orderKeyId > 0)
            {
                callScreenModel.WorkOrderBaseModel = orderBLL.GetModel(orderKeyId);
                //如果电话号码不为空
                if (callScreenModel.WorkOrderBaseModel != null && !string.IsNullOrEmpty(callScreenModel.WorkOrderBaseModel.F_CUSTOMERTELEPHONE))
                {
                    T_Cus_CustomerBase baseCusModel = cusBLL.GetCusInfoModel(callScreenModel.WorkOrderBaseModel.F_CUSTOMERTELEPHONE);


                    if (baseCusModel != null && baseCusModel.F_CustomerId > 0)
                    {
                        callScreenModel.WorkOrderBaseModel.CustomerBaseModel = baseCusModel;
                    }
                    else
                    {
                        callScreenModel.WorkOrderBaseModel.CustomerBaseModel = new T_Cus_CustomerBase();
                    }
                }
                else
                {
                    callScreenModel.WorkOrderBaseModel.CustomerBaseModel = new T_Cus_CustomerBase();
                }
                //获取客户基本信息
                if (!string.IsNullOrEmpty(callScreenModel.WorkOrderBaseModel.F_EMPCODE))
                {
                    T_Cus_CustomerBase baseCusModel = cusBLL.GetCusInfoModel(1, callScreenModel.WorkOrderBaseModel.F_EMPCODE);


                    if (baseCusModel != null && baseCusModel.F_CustomerId > 0)
                    {
                        callScreenModel.WorkOrderBaseModel.CustomerBaseModel = baseCusModel;
                    }
                    else
                    {
                        callScreenModel.WorkOrderBaseModel.CustomerBaseModel = new T_Cus_CustomerBase();
                    }
                }
            }
            //业务类型字典表
            callScreenModel.WorkOrderTypeModelList = GetWorkTypeModelList();
            //获取处理方式字典表
            callScreenModel.GoTypeModelList = GetCodeType("CLFS");
            //获取部门列表
            callScreenModel.DepartmentList = deptBll.GetModelList("F_ParentId=0");

            callScreenModel.HistoryOrderList = historyBll.GetModelList("F_INSTANCEID=" + orderKeyId);

            callScreenModel.Noticelist = noticeBLL.GetModelList(" Orderid=" + orderKeyId + " Order by DZNumber ASC");

            callScreenModel.F_RoleID = F_RoleID;
            return View(callScreenModel);
        }


        /// <summary>
        /// 处理历史工单
        /// </summary>
        /// <param name="orderKeyId">主键</param>
        /// <returns></returns>
        public ActionResult EditMyOrder(int dtype = 0, int orderKeyId = 0)
        {
            //实例化基础信息表
            CallScreenModel callScreenModel = new CallScreenModel();
            //工单类型
            callScreenModel.DType = dtype;
            //话务标志
            callScreenModel.SeatFlag = F_SeatFlag;
            //获取业务工单信息
            if (orderKeyId > 0)
            {
                callScreenModel.WorkOrderBaseModel = orderBLL.GetModel(orderKeyId);
                //如果电话号码不为空
                if (callScreenModel.WorkOrderBaseModel != null && callScreenModel.WorkOrderBaseModel.F_CUSTOMERID > 1)
                {

                    T_Cus_CustomerBase baseCusModel = cusBLL.GetModel((int)callScreenModel.WorkOrderBaseModel.F_CUSTOMERID);
                    if (baseCusModel != null && baseCusModel.F_CustomerId > 1)
                    {
                        callScreenModel.WorkOrderBaseModel.CustomerBaseModel = baseCusModel;
                    }
                    else
                    {
                        callScreenModel.WorkOrderBaseModel.CustomerBaseModel = new T_Cus_CustomerBase();
                    }

                }
                else
                {
                    callScreenModel.WorkOrderBaseModel.CustomerBaseModel = new T_Cus_CustomerBase();
                }
                if (callScreenModel.WorkOrderBaseModel.F_WORKORDERTYPEID == 1)
                { callScreenModel.HistoryOrderList = historyBll.GetModelList("F_INSTANCEID=" + orderKeyId); }
                else
                { callScreenModel.HistoryOrderList = null; }


            }
            //业务类型字典表
            callScreenModel.WorkOrderTypeModelList = GetWorkTypeModelList();
            //获取处理方式字典表
            callScreenModel.GoTypeModelList = GetCodeType("CLFS");
            //回访满意度
            callScreenModel.MYdModelList = GetCodeType("khyjfl");
            //获取部门列表
            callScreenModel.DepartmentList = deptBll.GetModelList("F_ParentId=0");
 
            callScreenModel.Noticelist = noticeBLL.GetModelList(" Orderid=" + orderKeyId + " Order by DZNumber ASC");
            return View(callScreenModel);
        }

        #endregion

        #region 已定责

        /// <summary>
        /// 超时答复视图
        /// </summary>
        /// <param name="dtype">工单类型 1、所有待办工单 2、代办结工单</param>
        /// <returns></returns>
        public ActionResult WorkOrderMyDataOverAnswer(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        /// <summary>
        /// 超时答复数据
        /// </summary>
        /// <param name="F_RETURNVISITRESULT">满意度</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyDataOverAnswerData")]
        [HttpGet]
        public string WorkOrderMyDataOverAnswerData(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber,
            string userInfo, int? bussType, int? detailType, string Content, int? F_RETURNVISITRESULT, int? turnout)
        {
            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = " and t.F_WORKORDERSTATEID>0  and t.F_WORKORDERTYPEID=1 ";

            //未按时转出
            if (turnout.ToInt32() > 0)
            {
                sql += " and t.turntime =0 ";
            }

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and t.F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and t.F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }

            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and t.F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and t.F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and t.F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and t.F_REPAIRLEVEL =" + detailType;
            }
            #region
            //定责次数
            //if (F_HASTENCOUNTS != null)
            //{
            //    if (F_HASTENCOUNTS == 0)
            //    { 
            //        sql += " AND (t.F_HASTENCOUNTS is null OR  t.F_HASTENCOUNTS =0) ";
            //    }
            //    else
            //    {
            //        sql += " AND  t.F_HASTENCOUNTS >0 ";
            //    }
            //}
            #endregion
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and t.F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0 || types == null)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {

                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
                else
                {
                    string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            //一天按钮
            else if (types == 4)
            {
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }


            string tableNew = "(select DISTINCT  t.*  ";
            tableNew += ", case WHEN F_SENTORDERTIME IS NULL AND DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE),  GETDATE())> 0  THEN 0 WHEN F_SENTORDERTIME IS NULL  THEN 1 WHEN DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE), F_SENTORDERTIME)> 0 THEN 0 ELSE 1 END AS turntime ";
            tableNew += " from T_Wo_WorkOrderBase t LEFT JOIN T_Wo_WorkOrderHistory h ON t.F_WORKORDERID=h.F_INSTANCEID LEFT JOIN T_Sys_UserAccount u ON  t.F_USERID= u.F_UserId";
            tableNew += " where  h.NeedReplayTime is not null AND F_StateName NOT LIKE '%撤回%' AND F_StateName NOT LIKE '%驳回%' and ((h.ReplayTime IS NULL AND DATEDIFF(second,  GETDATE(), h.NeedReplayTime)<0) OR (h.ReplayTime IS NOT NULL AND DATEDIFF(second,  h.ReplayTime, h.NeedReplayTime)<0))) as t";
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                tableNew,
                "F_WORKORDERID",
                " t.*,CONVERT(varchar,t.F_CREATEDATE, 120 ) as F_CREATEDATENew"
                + ",dbo.GetOverReplayTime(t.F_WORKORDERID) as F_OVERTIME"
            //                 + ", CONVERT(VARCHAR(10),ISNULL(CASE WHEN F_REMAINDERTIME IS NULL THEN DATEDIFF(SECOND,F_REQUESTSERVICETIME,GETDATE()) "
            //+ " ELSE DATEDIFF(SECOND,F_REQUESTSERVICETIME,F_REMAINDERTIME) END ,0)/3600)+'时'+CONVERT(VARCHAR(10),ISNULL(CASE WHEN F_REMAINDERTIME IS NULL THEN DATEDIFF(SECOND,F_REQUESTSERVICETIME,GETDATE()) "
            // + " ELSE DATEDIFF(SECOND,F_REQUESTSERVICETIME,F_REMAINDERTIME) END ,0)%3600/60)+'分'+CONVERT(VARCHAR(10),ISNULL(CASE WHEN F_REMAINDERTIME IS NULL THEN DATEDIFF(SECOND,F_REQUESTSERVICETIME,GETDATE()) "
            // + " ELSE DATEDIFF(SECOND,F_REQUESTSERVICETIME,F_REMAINDERTIME) END ,0)%3600%60)+'秒' AS F_OVERTIME"
            + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1  F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=t.F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT  top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=t.F_KSHADDRESSCODE) ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath"
,
                sql,
                "ORDER BY t.F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);

            //string tableNew = "(     SELECT * FROM dbo.T_Wo_WorkOrderBase WHERE F_WORKORDERID IN ( SELECT DISTINCT a.F_WORKORDERID FROM dbo.T_Wo_WorkOrderBase a    LEFT JOIN dbo.T_Wo_WorkOrderHistory h ON a.F_WORKORDERID = h.F_INSTANCEID    WHERE F_WORKORDERTYPEID = 1  AND      h.NeedReplayTime is not null AND F_StateName NOT LIKE '%撤回%' AND F_StateName NOT LIKE '%驳回%'     and((h.ReplayTime IS NULL AND DATEDIFF(second, GETDATE(), h.NeedReplayTime) < 0) OR(h.ReplayTime IS NOT NULL AND DATEDIFF(second, h.ReplayTime, h.NeedReplayTime) < 0)) ) )as t";
            //DataTable dt = new DataTable();
            //int recordCount = 0;
            //Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            //dt = BLL.PagerBLL.GetListPager(
            //               tableNew,
            //               "F_WORKORDERID",
            //               "t.*,dbo.GetOverReplayTime(t.F_WORKORDERID) as F_OVERTIME,CONVERT(varchar,t.F_CREATEDATE, 120 ) as F_CREATEDATENew"
            //               + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1  F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=t.F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT  top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=t.F_KSHADDRESSCODE) ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath"
            //             ,  sql,
            //                "ORDER BY t.F_CREATEDATE desc",
            //                 limit,
            //                  page,
            //                true,
            //                out recordCount);
            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        #endregion


 

        public ActionResult WorkOrderDealedList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();        
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }
        //已处理可撤回的列表
        [ActionName("WorkOrderDealedData")]
        [HttpGet]
        public string WorkOrderDealedData(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
    string workorderNum, string cusName, string phoneNumber,
    string userInfo, int? bussType, int? detailType, string Content)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结  ,4 待定责status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //  string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_OPTUSERID={0} AND F_OPTDATE= (select TOP 1 F_OPTDATE   from T_Wo_WorkOrderHistory     WHERE F_INSTANCEID = A.F_INSTANCEID   ORDER BY F_OPTDATE DESC)  )", F_UserID);

            string sql = " and F_WORKORDERTYPEID=1 and ( F_WORKORDERSTATEID=3 ";

            //处理中的工单，多单位时已处理
            sql += " or F_WORKORDERSTATEID=1";
            //不回访的工单，是待定责状态也可以撤回到已处理
            sql += " or ( F_WORKORDERSTATEID=4  and F_RETURNVISITFLAG=0 ))";

            sql += " and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=1 and F_OPTUSERID=" + F_UserID + " and ReplayTime is not null)";

            //客服处理的数据
            //sql += " or (F_WORKORDERTYPEID=3  and F_WORKORDERSTATEID=3 and F_LINKMAN='"+F_UserCode+"' ) ";


            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                "F_WORKORDERID",
              "*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew"
              + string.Format(",(select top 1 F_HISTORYID from T_Wo_WorkOrderHistory where F_INSTANCEID=F_WORKORDERID and F_WORKORDERSTATEID=0 and F_OPTUSERID={0} Order by F_OPTDATE desc) AS F_HISTORYID", F_UserID)
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                "ORDER BY F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        /// <summary>
        /// 待处理工单总数
        /// </summary>
        /// <returns></returns>
        public int WorkOrderMyDealedDataCount()
        {
            //string sql = string.Format("   (F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=3 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=1 and F_OPTUSERID={0}))", F_UserID);
            //sql += string.Format(" or ( F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=4  and F_RETURNVISITFLAG=0 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=1 and F_OPTUSERID={0}))", F_UserID);

            string sql = "  F_WORKORDERTYPEID=1 and ( F_WORKORDERSTATEID=3 ";

            //处理中的工单，多单位时已处理
            sql += " or F_WORKORDERSTATEID=1";
            //不回访的工单，是待定责状态也可以撤回到已处理
            sql += " or ( F_WORKORDERSTATEID=4  and F_RETURNVISITFLAG=0 ))";

            sql += " and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory where F_WORKORDERSTATEID=1 and F_OPTUSERID=" + F_UserID + " and ReplayTime is not null)";

            return orderBLL.GetRecordCount(sql);
        }





        public ActionResult WorkOrderVisitedList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        //已回访 即待定责的列表（只有登录人的数据）待定责可以撤回
       
        [ActionName("WorkOrderVisitedData")]
        [HttpGet]
        public string WorkOrderVisitedData(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
    string workorderNum, string cusName, string phoneNumber,
    string userInfo, int? bussType, int? detailType, string Content)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结 status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //F_RETURNVISITFLAG 1 是要回访 0 是不回访
            //  string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_WORKORDERID in (select F_INSTANCEID from T_Wo_WorkOrderHistory A where F_WORKORDERSTATEID=0 and F_OPTUSERID={0} AND F_OPTDATE= (select TOP 1 F_OPTDATE   from T_Wo_WorkOrderHistory     WHERE F_INSTANCEID = A.F_INSTANCEID   ORDER BY F_OPTDATE DESC)  )", F_UserID);
            string sql = string.Format(" and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=4 and F_RETURNVISITFLAG=1 and F_LINKMAN={0}", F_UserCode);



            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                "F_WORKORDERID",
              "*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew"
              + string.Format(",(select top 1 F_HISTORYID from T_Wo_WorkOrderHistory where F_INSTANCEID=F_WORKORDERID and F_WORKORDERSTATEID=0 and F_OPTUSERID={0} Order by F_OPTDATE desc) AS F_HISTORYID", F_UserID)
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                "ORDER BY F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        public int WorkOrderMyVisitedDataCount()
        {
            string sql = string.Format("   F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=4  and F_RETURNVISITFLAG=1 and F_LINKMAN={0}", F_UserCode);

            return orderBLL.GetRecordCount(sql);
        }




        /// <summary>
        /// 处理人撤回自己处理的，工单状态是待回访的工单 
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool ResetDealedWorkOrder(int workOrderId,string reason)
        {
            string F_UserCode = "";
            if (Request.Cookies["BaseCallCenter_T_User"] != null)
            {
                F_UserCode = Request.Cookies["BaseCallCenter_T_User"]["F_UserCode"];
            }
         
                bool result = false; 
                string bycode = F_UserCode;        
                string byname = F_UserName;
                int bmid = F_DeptId;
                string bmmc = "";
                if (F_UserID > 0)
                {
                    ZXDT.CallCenter.BLL.T_Sys_Department depBll = new BLL.T_Sys_Department();
                    List<Model.T_Sys_Department> list = new List<T_Sys_Department>();
                    list = depBll.GetModelList("F_DeptId=" + bmid);
                    if (list.Count > 0)
                    {
                        bmmc = list[0].F_DeptName.ToString();
                        result = orderBLL.CheHuiYCLWorkOrder(workOrderId, F_UserID, F_UserName, F_DeptId, bmmc,reason);
                    }
                }
                return result;
        
        }

        /// 回访人撤回自己回访的，工单状态是待定责的工单 ;客服角色的回访列表里
        [AcceptVerbs(HttpVerbs.Get)]
        public bool ResetVisitedWorkOrder(int workOrderId,string reason)
        {
            string F_UserCode = "";
            if (Request.Cookies["BaseCallCenter_T_User"] != null)
            {
                F_UserCode = Request.Cookies["BaseCallCenter_T_User"]["F_UserCode"];
            }

            bool result = false; ;
            string bycode = F_UserCode;
            string byname = F_UserName;
            int bmid = F_DeptId;
            string bmmc = "";
            if (F_UserID > 0)
            {
                ZXDT.CallCenter.BLL.T_Sys_Department depBll = new BLL.T_Sys_Department();
                List<Model.T_Sys_Department> list = new List<T_Sys_Department>();
                list = depBll.GetModelList("F_DeptId=" + bmid);
                if (list.Count > 0)
                {
                    bmmc = list[0].F_DeptName.ToString();
                    result = orderBLL.CheHuiDDZWorkOrder(workOrderId, F_UserID, F_UserName, F_DeptId, bmmc,reason);
                }
            }
            return result;

        }



        public ActionResult DepartmentWorkOrderList()
        {
            CallScreenModel callScreenModel = new CallScreenModel();
            //业务类型字典表
            callScreenModel.WorkOrderTypeModelList = GetWorkTypeModelList();

            //业务类型字典表
            callScreenModel.GoTypeModelList = GetCodeType("CLFS");
            //话务标志
            callScreenModel.SeatFlag = F_SeatFlag;         


            return View(callScreenModel);
        }


        /// <summary>
        /// 获取工单信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("ReassignmentDeparmentWorkOrderAllData")]
        [HttpGet]
        public string DepartmentWorkOrderList(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
            string workorderNum, string cusName, string phoneNumber, string userInfo, int? status, int? bussType, int? detailType, string Content, int? GoType, int? FromType, int? F_HASTENCOUNTS, int? turnout)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = "";

            //未按时转出
            if (turnout.ToInt32() > 0)
            {
                sql += " and t.turntime =0 ";
            }

            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and t.F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and t.F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }

            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and t.F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and t.F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }
            //状态  F_WORKORDERSTATEID   0未处理 1处理中  2已办结（结束） 3待回访 4待定责 5已定责
            if (status != null)
            {
                sql += " and t.F_WORKORDERSTATEID =" + status;

            }
            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and t.F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and t.F_REPAIRLEVEL =" + detailType;
            }

        


            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and t.F_CONTENT like '%" + Content.Trim() + "%'";
            }
            //处理方式  F_WORKORDERTYPEID  处理方式  1转单 3客服处理
            if (GoType != null && GoType > 0)
            {
                sql += " and t.F_WORKORDERTYPEID =" + GoType;
            }
            //接入渠道 1语音 2留言  3微信 4微博
            if (FromType != null && FromType > 0)
            {
                sql += " and t.F_REPAIRMANID =" + FromType;
            }


            //查询按钮
            if (types == 0 || types == null)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {

                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
                else
                {
                    string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                    sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";
                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }
            //一天按钮
            else if (types == 4)
            {
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                sql += " and t.F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                sql += " and t.F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

            }



            //string tableNew = "(select DISTINCT  t.*  ";
            //tableNew += ", case WHEN F_SENTORDERTIME IS NULL AND DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE),  GETDATE())> 0  THEN 0 WHEN F_SENTORDERTIME IS NULL  THEN 1 WHEN DATEDIFF(minute, DATEADD(day,1,F_CREATEDATE), F_SENTORDERTIME)> 0 THEN 0 ELSE 1 END AS turntime ";
            //tableNew += " from T_Wo_WorkOrderBase t LEFT JOIN T_Wo_WorkOrderHistory h ON t.F_WORKORDERID=h.F_INSTANCEID LEFT JOIN T_Sys_UserAccount u ON  t.F_USERID= u.F_UserId";
            //tableNew += " ) as t";

            string tableNew = " (select * from    T_Wo_WorkOrderBase WHERE  F_WORKORDERID in (SELECT  DISTINCT F_WORKORDERID    FROM dbo.T_Wo_WorkOrderBase a LEFT JOIN  t_wo_workorderhistory b ON a.F_WORKORDERID = b.F_INSTANCEID WHERE a.F_WORKORDERTYPEID = 1  AND b.F_StateName != '已撤回' AND b.F_StateName != '已驳回' AND b.F_OPTUSERID IN(  SELECT F_UserId FROM t_sys_useraccount WHERE F_DeptId = " + F_DeptId + "))) as t";





            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                tableNew,
                "F_WORKORDERID",
                " t.*,CONVERT(varchar,t.F_CREATEDATE, 120 ) as F_CREATEDATENew"
                  + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1  F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=t.F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT  top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=t.F_KSHADDRESSCODE) ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath"
,
                sql,
                "ORDER BY t.F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }



        //审批撤回的工单的页面
        public ActionResult WorkOrderShenPiList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        //审批撤回的工单的列表
        [ActionName("WorkOrderShenPiData")]
        [HttpGet]
        public string WorkOrderShenPiData(DateTime? NowDateTime, int page, int limit, int? types, string dateParty,
      string workorderNum, string cusName, string phoneNumber,
      string userInfo, int? bussType, int? detailType, string Content)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            //F_FILEFLAG 业务类型ID
            //F_WORKORDERSTATEID   0未处理 1处理中  2已办结 status
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            //F_RETURNVISITFLAG 1 是要回访 0 是不回访
     
         //   撤回审批的工单状态是8,9,10
            string sql = " and F_WORKORDERSTATEID in(8,9,10)";



            //工单编号
            if (!string.IsNullOrEmpty(workorderNum))
            {
                sql += " and F_CODE like '%" + workorderNum.Trim() + "%'";
            }
            //客户姓名
            if (!string.IsNullOrEmpty(cusName))
            {
                sql += " and F_CUSTOMERNAME like '%" + cusName.Trim() + "%'";
            }
            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and F_CUSTOMERTELEPHONE like '%" + phoneNumber.Trim() + "%'";
            }
            //坐席工号
            if (!string.IsNullOrEmpty(userInfo))
            {
                sql += " and F_LINKMAN like '%" + userInfo.Trim() + "%'";
            }

            //业务类别
            if (bussType != null && bussType > 0)
            {
                sql += " and F_FILEFLAG =" + bussType;
            }
            //业务类型
            if (detailType != null && detailType > 0)
            {
                sql += " and F_REPAIRLEVEL =" + detailType;
            }
            //内容
            if (!string.IsNullOrEmpty(Content))
            {
                sql += " and F_CONTENT like '%" + Content.Trim() + "%'";
            }



            //查询按钮
            if (types == 0)
            {
                if (!string.IsNullOrEmpty(dateParty))
                {
                    string startDate = dateParty.Substring(0, 10);
                    string endDate = dateParty.Substring(12);
                    sql += " and F_CREATEDATE>= '" + startDate + " 00:00:00" + "'";
                    sql += " and F_CREATEDATE<= '" + endDate + " 23:59:59" + "'";

                }
            }//一周按钮
            else if (types == 1)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddDays(-7).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一月按钮
            else if (types == 2)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddMonths(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }//一年按钮
            else if (types == 3)
            {

                string endDate = DateTime.Now.ToString();
                string startDate = DateTime.Now.AddYears(-1).ToString();
                sql += " and F_CREATEDATE>= '" + startDate + "'";
                sql += " and F_CREATEDATE<= '" + endDate + "'";

            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderBase",
                "F_WORKORDERID",
              "*,CONVERT(varchar,F_CREATEDATE, 120 ) as F_CREATEDATENew"
              + string.Format(",(select top 1 F_HISTORYID from T_Wo_WorkOrderHistory where F_INSTANCEID=F_WORKORDERID and F_WORKORDERSTATEID=0 and F_OPTUSERID={0} Order by F_OPTDATE desc) AS F_HISTORYID", F_UserID)
              + @" , replace(replace(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT top 1 F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT top 1 FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                //+ " ,(CASE F_WORKORDERFROM WHEN '留言工单' THEN (SELECT F_RecFileUrl FROM dbo.T_Call_LeaveRecord  WHERE F_CallId=F_KSHADDRESSCODE) WHEN '语音工单' THEN (SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE AND FilePath!='') ELSE NULL END ) as FilePath",
                sql,
                "ORDER BY F_CREATEDATE desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public bool SubmiShenPiYJ(int workorderid, int shenpiresult, string shenpiopinion)
        {
            //int  workorderid, int shenpiresult,string shenpiopinion,int byid,string byname,int bmid )
            //AddAction("t_wo_workorderhistory", workOrderBaseModel.F_HISTORYID.ToMyString(), "提交工单", "数据：" + JsonConvert.SerializeObject(workOrderBaseModel), "关键信息");
            //AddAction("t_wo_workorderhistory", workOrderBaseModel.F_HISTORYID.ToMyString(), "部门提交工单", workOrderBaseModel.F_INSTANCEID.ToMyString());
       
            return orderBLL.SubmitShenPiYj(workorderid, shenpiresult, shenpiopinion,F_UserID,F_UserName,F_DeptId); 
        }


        //审批工单弹框 页面
        public ActionResult OrderShenPiTanKuangList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            //业务类型字典表
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        public int WorkOrderShenPiCount()
        {
            // string sql = string.Format("  F_WORKORDERSTATEID=4 and (F_HASTENCOUNTS=0 OR F_HASTENCOUNTS IS NULL) and (F_ROAD='{0}' Or F_ROAD='' Or F_ROAD IS NULL)", F_UserID);
            string sql = "   F_WORKORDERSTATEID in(8,9,10) ";
            return orderBLL.GetRecordCount(sql);
        }
    }
}
