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;
using ZXDT.DBUtility;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class WorkOrderController : BaseController
    {

        //工单信息
        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_Sys_UserAccount userBll = new BLL.T_Sys_UserAccount();
        //历史工单表
        BLL.T_Wo_WorkOrderHistory historyBll = new BLL.T_Wo_WorkOrderHistory();

        #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" + "'";

            }
            string tableNew = "";
            if (F_RoleID == 17)
            {
                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) as t");

            }
            else
            {
               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 处理回复内容"
                                               + ",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);
        }



        #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);
            return View(callScreenModel);
        }
        #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)
        {

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

            string sql = "";

            //工单编号
            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;
            }
            //内容
            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" + "'";
                }
                //sql += " and F_WORKORDERID not in (select top 1 F_INSTANCEID from T_Wo_WorkOrderHistory  where F_INSTANCEID = t.F_WORKORDERID and F_TASKFLAG = 2 and F_HISTORYID = (select top 1  F_HISTORYID from T_Wo_WorkOrderHistory where  F_INSTANCEID = t.F_WORKORDERID order by F_OPTDATE desc))";
            }//一周按钮
            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);
            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 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,string datatype)
        {

            //数据结果集
            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
            datatype = "可撤回";
            string sql = "";
            if (datatype != "可撤回")
            {
                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);
            }
            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);
            }

            //工单编号
            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 F_HISTORYID from T_Wo_WorkOrderHistory where F_INSTANCEID=F_WORKORDERID and F_WORKORDERSTATEID=0 and F_OPTUSERID={0}) AS F_HISTORYID", F_UserID)
              + @" , replace(replace(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,'\','/'),'" + 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)
        {

            return orderBLL.SubmitOrder(workOrderBaseModel, workOrderBaseModel.F_INSTANCEID, F_UserName);
        }


        #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)
        {
            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_WORKORDERTYPEID=3 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 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,'\','/'),'" + 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 WorkOrderMyList(int dtype)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dtype;
            //业务类型字典表
            
            model.WorkOrderTypeModelList = GetWorkTypeModelList();
        
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }


        /// <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();
                }

            }
            //业务类型字典表
            callScreenModel.WorkOrderTypeModelList = GetWorkTypeModelList();
            //获取处理方式字典表
            callScreenModel.GoTypeModelList = GetCodeType("CLFS");
            //回访满意度
            callScreenModel.MYdModelList = GetCodeType("khyjfl");
            //获取部门列表
            callScreenModel.DepartmentList = deptBll.GetModelList("F_ParentId=0");

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

        /// <summary>
        /// 保存转办信息
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool workMyOrderAddData(T_Wo_WorkOrderBase workOrderBaseModel)
        {
            return orderBLL.EditMyOrderInfo(workOrderBaseModel);
        }



        /// <summary>
        /// 关闭转办订单
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool workMyOrderCloseData(T_Wo_WorkOrderBase workOrderBaseModel)
        {
            workOrderBaseModel.F_WORKORDERSTATEID = 2;
            workOrderBaseModel.F_REPAIRREQUEST = "已办结";

            return orderBLL.CloseMyOrderInfo(workOrderBaseModel);
        }


        /// <summary>
        /// 我的待办工单
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderMyData")]
        [HttpGet]
        public string WorkOrderMyData(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);



            //工单编号
            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 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,'\','/'),'" + 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>

        /// <returns></returns>
        public ActionResult WorkOrderNoList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }

        /// <summary>
        /// 补录工单管理
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderNoData")]
        [HttpGet]
        public string WorkOrderNoData(DateTime? NowDateTime, 
            int page, int limit, int? types, string dateParty,
            string F_CONTENT,string F_HOUSING,int? F_WORKORDERTYPEID
            )
        {

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

            //工单类型：转单 工单状态：处理中 处理人员：当前用户id
            string sql = " and F_WORKORDERSTATEID=0  and F_USERID=" + F_UserID;
            //查询按钮
            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 + "'";

            }
            //内容类型 模糊查询
            if (F_CONTENT!=null) {
                sql += " and F_CONTENT   like '%" + F_CONTENT + "%'";

            }
            // 工单类型 查询F_WORKORDERTYPEID
            if (F_WORKORDERTYPEID >0)
            {
                sql += " and F_WORKORDERTYPEID ="+ F_WORKORDERTYPEID;
            }
            if (F_HOUSING!=null) {
                sql += " and F_HOUSING like  '%" + F_HOUSING + "%'";
            }

            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"
                //+ ",(SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE) AS FilePath",
                + @" , 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) ELSE NULL END,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as FilePath",
                       //   + " ,(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) 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>

        /// <returns></returns>
        public ActionResult WorkOrderAllList_Wb()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            //话务标志
            model.SeatFlag = F_SeatFlag;
            return View(model);
        }
        /// <summary>
        /// 微博工单管理
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderWebData")]
        [HttpGet]
        public string WorkOrderWebData(DateTime? NowDateTime, 
            int page, int limit, int? types, string dateParty, string F_CONTENT, string F_HOUSING)
        {

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

            //工单类型：微博 工单状态：处理中 处理人员：当前用户id
            string sql = " and F_WORKORDERSTATEID=0 and F_REPAIRMANID=4 and F_USERID=" + F_UserID;
            //查询按钮
            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 + "'";

            }
            else
            {

                sql += " and F_CREATEDATE>= '" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00" + "'";
                sql += " and F_CREATEDATE<= '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59" + "'";
            }
            //内容类型 模糊查询
            if (F_CONTENT != null)
            {
                sql += " and F_CONTENT   like '%" + F_CONTENT + "%'";

            }
            if (F_HOUSING != null)
            {
                sql += " and F_HOUSING like  '%" + F_HOUSING + "%'";
            }

            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"
                          //+ ",(SELECT FilePath FROM dbo.T_Call_CallRecords WHERE CallId=F_KSHADDRESSCODE) 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) 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="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderHistoryData")]
        [HttpGet]
        public string WorkOrderHistoryData(DateTime? NowDateTime, string phoneNumber)
        {

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

            string sql = " and F_CUSTOMERTELEPHONE ='" + phoneNumber.Trim() + "'";


            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",
                sql,
                "ORDER BY F_CREATEDATE desc",
                10,
                1,
                true,
                out recordCount);


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

            return JsonConvert.SerializeObject(dataModel);
        }


      
        #endregion

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

            return View(callScreenModel);
        }
        /// <summary>
        /// 获取工单信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderAllListDataReport")]
        [HttpGet]
        public string WorkOrderAllListDataReport(DateTime? NowDateTime, int page, int limit, string dateParty,
             int? bussType, int? detailType, string linkman,string clfs,string bblb, string Qdyw ,string ywlx,
             string hfmyd)
        { 

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

            string sql = ""; 
            #region
            string startDate = "";
            string endDate = "";
            
            if (bussType == null && detailType !=0 && detailType!=null)
            {
                bussType = detailType;
                detailType = null;
                sql += " and t.F_FILEFLAG <>0 ";
            }
            else if (bussType ==null && detailType==0  )
            {
                sql += " and t.F_FILEFLAG <>0 " ;
            }
            
            if (!string.IsNullOrEmpty(dateParty))
            { 
                if (dateParty.Length==23)
                {
                    startDate = dateParty.Substring(0, 10) + " 00:00:00";
                    endDate = dateParty.Substring(12) + " 23:59:59";
                }
                else { 
                startDate = dateParty.Split('~')[0].Trim();
                endDate = dateParty.Split('~')[1].Trim();
                }
            }
            else
            {
                startDate = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
                endDate = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
            }
            sql += " and t.F_CREATEDATE>= '" + startDate + "'";
            sql += " and t.F_CREATEDATE<= '" + endDate + "'";
           
            //业务类别
            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(linkman))
            {
                sql += " and t.F_LINKMAN =" + linkman;
            }
            //处理方式 
            if (!string.IsNullOrEmpty(clfs))
            {
                sql += " and t.F_WORKORDERTYPEID =" + clfs;
            }
            //报表类别，综合报表为空(语音工单：1、2；微信工单：3；微博工单：4）
            if (bblb != "1" && !string.IsNullOrEmpty(bblb))
            {
                sql = sql + " and t.F_REPAIRMANID<=2 ";
            }
            //渠道业务报表
            if (!String.IsNullOrEmpty(Qdyw))
            {
                switch (Qdyw)
                {
                    case "1": sql = sql + " and t.F_REPAIRMANID in (1,2) and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break;
                    case "3": sql = sql + "and t.F_REPAIRMANID =3 and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break;
                    case "4": sql = sql + "and t.F_REPAIRMANID =4 and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break;
                    default: break;
                }
            }
            else
            { sql = sql + "and  t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; }
            //坐席业务量报表：业务类型0全部业务，1语音业务数量，2在线业务数量，3微信业务数量，4微博业务数量
            if (!String.IsNullOrEmpty(ywlx))
            {
                switch (ywlx)
                {
                    case "0": sql = sql + " and t.F_REPAIRMANID in (1,2,3,4) and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break;
                    case "1": sql = sql + " and t.F_REPAIRMANID in (1,2) and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break;
                    case "2": sql = sql + "and t.F_REPAIRMANID in (3,4) and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break; 
                    case "3": sql = sql + "and t.F_REPAIRMANID =3 and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break;
                    case "4": sql = sql + "and t.F_REPAIRMANID =4 and t.F_REPAIRLEVEL>0 and t.F_FILEFLAG>0"; break;
                    default: break;
                }
            }
            //hfmyd 回访满意度:1-满意，2-一般，3-不满意
            if (!string.IsNullOrEmpty(hfmyd))
            {
                switch (hfmyd)
                {
                    case "1":sql = sql + "and t.F_FILEFLAG=3 and t.F_WORKORDERTYPEID = 1 and ISNULL(t.F_RETURNVISITRESULT, 2) =1"; break;
                    case "2": sql = sql + "and t.F_FILEFLAG=3 and t.F_WORKORDERTYPEID = 1 and ISNULL(t.F_RETURNVISITRESULT, 2) =2"; break;
                    case "3": sql = sql + "and t.F_FILEFLAG=3 and t.F_WORKORDERTYPEID = 1 and ISNULL(t.F_RETURNVISITRESULT, 2) =3"; break;
                    default: break;
                }
            }
            #endregion
            string tableNew = "";
            if (F_RoleID == 17)
            {
                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) as t");
            }
            else
            {
                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.*,CONVERT(varchar,t.F_CREATEDATE, 120 ) as F_CREATEDATENew"
                  + " ,(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 ) 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);
        } 
        /// <summary>
        /// 导出报表数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public string ExToExcelReport(ExToExcel paramModel)
        {

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

            string sql = "";
            #region
            string startDate = "";
            string endDate = "";
            if (paramModel.bussType == null && paramModel.detailType != 0)
            {
                paramModel.bussType = paramModel.detailType;
                paramModel.detailType = null;
            }
            else if (paramModel.bussType == null && paramModel.detailType == 0)
            {
                sql += " and t.F_FILEFLAG <>0 ";
            }
            if (!string.IsNullOrEmpty(paramModel.dateParty))
            {
                startDate = paramModel.dateParty.Split('~')[0].Trim();
                endDate = paramModel.dateParty.Split('~')[1].Trim();
            }
            else
            {
                startDate = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
                endDate = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
            }
            sql += " and t.F_CREATEDATE>= '" + startDate + "'";
            sql += " and t.F_CREATEDATE<= '" + endDate + "'";
            #endregion
            //业务类别
            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;
            }

            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 处理回复内容"
                                               + ",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);
        }

        #endregion
        #region 获取来电时间工单列表
        /// <summary>
        /// 获取来电时间工单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult WorkOrderAllListShow(int? orderType)
        {
            CallScreenModel callScreenModel = new CallScreenModel();
             
            return View(callScreenModel);
        }

        #endregion
          
        /// <summary>
        /// 驳回待处理即处理中的工单
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool OverruleWorkOrder(int workOrderId,string opinion)
        {
            string F_UserCode = "";
            if (Request.Cookies["BaseCallCenter_T_User"] != null)
            {
                F_UserCode = Request.Cookies["BaseCallCenter_T_User"]["F_UserCode"];
            }
            
            List<T_Wo_WorkOrderHistory> lists = new List<T_Wo_WorkOrderHistory>();
            List<T_Wo_WorkOrderHistory> dealedlists = new List<T_Wo_WorkOrderHistory>();
            //如果处理人历史中已处理 就不能驳回到补录工单了
            try
            {
                BLL.T_Wo_WorkOrderHistory his = new BLL.T_Wo_WorkOrderHistory();
               lists=  his.GetModelList("F_StateName = '未处理' AND  F_INSTANCEID = '" +workOrderId + "'");

                dealedlists = his.GetModelList("F_StateName = '已处理' AND  F_INSTANCEID = '" + workOrderId + "'");

       
                //linkman = DbHelperSQL.GetSingle("  SELECT F_UserName FROM dbo.T_Wo_WorkOrderHistory WHERE  F_StateName='未处理' AND  F_INSTANCEID='" +workOrderId + "'").ToString();
            }
            catch (Exception)
            {
                return false;

            }
            if (dealedlists!=null && dealedlists.Count>0)
            {
                return false;
            }


            if(lists!=null&& lists.Count()>0&& lists.Where(x=>x.F_UserName== F_UserName).Count()>0)          
            {
                bool result = false; ;
              
                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.OverruleWorkOrder(workOrderId, F_UserID, F_UserName, F_DeptId, bmmc,opinion);
                    }
                }
                return result;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 驳回待回访列表 即状态是待回访的工单
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool OverruleDHFWorkOrder(int workOrderId,string opinion)
        {
            string F_UserCode = "";
            if (Request.Cookies["BaseCallCenter_T_User"] != null)
            {
                F_UserCode = Request.Cookies["BaseCallCenter_T_User"]["F_UserCode"];
            }
            //string linkman = DbHelperSQL.GetSingle("select F_LINKMAN from T_Wo_WorkOrderBase where F_WORKORDERID='" +
            //   workOrderId + "'").ToString();
            //if (linkman.Equals(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.OverruleDHFWorkOrder(workOrderId, F_UserID, F_UserName, F_DeptId, bmmc, opinion);
                    }
                }
                return result;
            //}
            //else
            //{
            //    return false;
            //}
        }

        /// <summary>
        /// 驳回待定责列表 即状态是待定责的工单
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool OverruleDDZWorkOrder(int workOrderId,string opinion)
        {
            string F_UserCode = "";
            if (Request.Cookies["BaseCallCenter_T_User"] != null)
            {
                F_UserCode = Request.Cookies["BaseCallCenter_T_User"]["F_UserCode"];
            }
     
            Model.T_Wo_WorkOrderBase workordermodel=   orderBLL.GetModel(workOrderId);
            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.OverruleDDZWorkOrder(workOrderId, F_UserID, F_UserName, F_DeptId, bmmc, opinion);
                }
            }
 



            return result;
            //}
            //else
            //{
            //    return false;
            //}
        }


        //工单可撤回列表
        //处理人可以撤回的工单列表（工单状态是待回访，history表有处理记录）
        [ActionName("WorkOrderMyKCH")]
        [HttpGet]
        public string WorkOrderMyKCH(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  3 待回访
            //F_WORKORDERTYPEID 处理方式  1转单 3客服处理 GoType
            //F_REPAIRMANID 接入渠道 1语音 2留言  3微信 4微博 FromType
            string 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);
            
            //工单编号
            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 F_HISTORYID from T_Wo_WorkOrderHistory where F_INSTANCEID=F_WORKORDERID and F_WORKORDERSTATEID=0 and F_OPTUSERID={0}) AS F_HISTORYID", F_UserID)
              + @" , replace(replace(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,'\','/'),'" + 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.Get)]
        //public bool CheHuiYCLWorkOrder(int workOrderId)
        //{
        //    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);
        //        }
        //    }
        //    return result;
      
        //}



    }
}
