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

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class APIController : Controller
    {
        BLL.T_Call_CallRecords recordBLL = new BLL.T_Call_CallRecords();
        //
        // GET: /API/

        public ActionResult Index()
        {
            return View();
        }

        #region 接口数据
        /// <summary>
        /// 接口数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderTypeFromData")]
        [HttpGet]
        public string WorkOrderTypeFromData(DateTime? NowDateTime, string dateParty)
        {
            //数据结果集
            ResponseData dataModel = new ResponseData();

            try
            {
                string startDate = "";
                string endDate = "";
                if (!string.IsNullOrEmpty(dateParty))
                {
                    startDate = dateParty.Substring(0, 10);
                    endDate = dateParty.Substring(12);

                }
                else
                {
                    startDate = endDate = DateTime.Now.ToString("yyyy-MM-dd");
                }

                DataTable datas = recordBLL.GetWorkOrderBussTypeFromData(startDate, endDate);
                DataTable newTable = new DataTable();
                newTable.Columns.Add("业务类型");
                newTable.Columns.Add("语音工单");
                newTable.Columns.Add("微信工单");
                newTable.Columns.Add("微博工单");

                if (datas != null && datas.Rows.Count > 0)
                {

                    DataRow dataRow;

                    if (datas != null && datas.Rows.Count > 0)
                    {

                        #region 咨询工单
                        int callInCount = 0;
                        int wxCount = 0;
                        int wbCount = 0;
                        //来电工单数量
                        DataRow[] CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }


                        //微信工单数量
                        CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wxCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wbCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["业务类型"] = "咨询";
                        dataRow["语音工单"] = callInCount;
                        dataRow["微信工单"] = wxCount;
                        dataRow["微博工单"] = wbCount;
                        newTable.Rows.Add(dataRow);

                        #endregion


                        #region 建议工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }


                        //微信工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wxCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wbCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["业务类型"] = "建议";
                        dataRow["语音工单"] = callInCount;
                        dataRow["微信工单"] = wxCount;
                        dataRow["微博工单"] = wbCount;
                        newTable.Rows.Add(dataRow);

                        #endregion

                        #region 表扬工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }


                        //微信工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wxCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wbCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["业务类型"] = "表扬";
                        dataRow["语音工单"] = callInCount;
                        dataRow["微信工单"] = wxCount;
                        dataRow["微博工单"] = wbCount;
                        newTable.Rows.Add(dataRow);

                        #endregion


                        #region 挂失工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }


                        //微信工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wxCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wbCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["业务类型"] = "挂失";
                        dataRow["语音工单"] = callInCount;
                        dataRow["微信工单"] = wxCount;
                        dataRow["微博工单"] = wbCount;
                        newTable.Rows.Add(dataRow);

                        #endregion

                        #region 投诉工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                callInCount += int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }


                        //微信工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wxCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            if (CallRows[0]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[0]["callInCount"].ToString()))
                            {
                                wbCount = int.Parse(CallRows[0]["callInCount"].ToString());
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["业务类型"] = "投诉";
                        dataRow["语音工单"] = callInCount;
                        dataRow["微信工单"] = wxCount;
                        dataRow["微博工单"] = wbCount;
                        newTable.Rows.Add(dataRow);

                        #endregion



                    }


                }
                dataModel.code = 0;
                dataModel.data = newTable;

            }
            catch (Exception ex)
            {
                dataModel.code = 200;
                dataModel.msg = ex.Message;
            }


            return JsonConvert.SerializeObject(dataModel);
        }

        /// <summary>
        /// 接口数据2
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("WorkOrderTypeFromDataNew")]
        [HttpGet]
        public string WorkOrderTypeFromDataNew(DateTime? NowDateTime, string dateParty)
        {
            //数据结果集
            ResponseData dataModel = new ResponseData();
            try
            {
                string startDate = "";
                string endDate = "";
                if (!string.IsNullOrEmpty(dateParty))
                {
                    startDate = dateParty.Substring(0, 10);
                    endDate = dateParty.Substring(12);
                }
                else
                {
                    startDate = endDate = DateTime.Now.ToString("yyyy-MM-dd");
                }

                DataTable datas = recordBLL.GetWorkOrderBussTypeFromDataNew(startDate, endDate);
                DataTable newTable = new DataTable();
                newTable.Columns.Add("BusinessType");
                newTable.Columns.Add("allcount");
                newTable.Columns.Add("callcount");
                newTable.Columns.Add("wxcount");
                newTable.Columns.Add("wbcount");
                newTable.Columns.Add("Returnvisitproblem"); //概要：F_RETURNVISITPROBLEM 

                if (datas != null && datas.Rows.Count > 0)
                {
                    DataRow dataRow;
                    if (datas != null && datas.Rows.Count > 0)
                    {
                        #region 咨询工单
                        int callInCount = 0;
                        int wxCount = 0;
                        int wbCount = 0;
                        List<string> Returnvisitproblem_zx = new List<string>();
                        //string Returnvisitproblem_zx = "[";
                        //来电工单数量
                        DataRow[] CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_zx.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim());
                                }
                            }
                            //for (int i = 0; i < CallRows.Length; i++)
                            //{
                            //    if (i == 0)
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + "\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //    else
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + ",\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //}
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_zx.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim());
                                }
                            }
                            //for (int i = 0; i < CallRows.Length; i++)
                            //{
                            //    if (i == 0)
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + "\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //    else
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + ",\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //}
                        }
                        //微信工单数量
                        CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wxCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_zx.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim());
                                }
                            }
                            //for (int i = 0; i < CallRows.Length; i++)
                            //{
                            //    if (i == 0)
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + "\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //    else
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + ",\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //}
                        }
                        //微博工单数量
                        CallRows = datas.Select("BusinessType=6 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wbCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_zx.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim());
                                }
                            }
                            //for (int i = 0; i < CallRows.Length; i++)
                            //{
                            //    if (i == 0)
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + "\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //    else
                            //    {
                            //        Returnvisitproblem_zx = Returnvisitproblem_zx + ",\"" + CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() + "\"";
                            //    }
                            //}
                        }
                        //Returnvisitproblem_zx = Returnvisitproblem_zx + "]";
                        //微博接口
                        int wxxtCount = WXWorkOrderCount(3, startDate, endDate);
                        //微信接口
                        int wbxtCount = WXWorkOrderCount(4, startDate, endDate);
                        dataRow = newTable.NewRow();
                        dataRow["BusinessType"] = "咨询";
                        dataRow["allcount"] = callInCount + wxCount + wxxtCount + wbCount + wbxtCount;
                        dataRow["callcount"] = callInCount;
                        dataRow["wxcount"] = wxCount + wxxtCount;
                        dataRow["wbcount"] = wbCount + wbxtCount;
                        dataRow["Returnvisitproblem"] = JsonConvert.SerializeObject(Returnvisitproblem_zx);       //Returnvisitproblem_zx;Returnvisitproblem_zx.Replace("\\", "");   //JsonConvert.SerializeObject(Returnvisitproblem_zx); //string.Join(",", Returnvisitproblem_zx) ;
                        newTable.Rows.Add(dataRow);
                        #endregion
                        #region 建议工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //string Returnvisitproblem_jy = "";
                        List<string> Returnvisitproblem_jy = new List<string>();
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_jy.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_jy.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        //微信工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wxCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_jy.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=14 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wbCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_jy.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["BusinessType"] = "建议";
                        dataRow["allcount"] = callInCount + wxCount + wbCount;
                        dataRow["callcount"] = callInCount;
                        dataRow["wxcount"] = wxCount;
                        dataRow["wbcount"] = wbCount;
                        dataRow["Returnvisitproblem"] = JsonConvert.SerializeObject(Returnvisitproblem_jy);
                        newTable.Rows.Add(dataRow);

                        #endregion 
                        #region 表扬工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //string Returnvisitproblem_by = "";
                        List<string> Returnvisitproblem_by = new List<string>();
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_by.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_by.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        //微信工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wxCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_by.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=18 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wbCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_by.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }

                        dataRow = newTable.NewRow();
                        dataRow["BusinessType"] = "表扬";
                        dataRow["allcount"] = callInCount + wxCount + wbCount;
                        dataRow["callcount"] = callInCount;
                        dataRow["wxcount"] = wxCount;
                        dataRow["wbcount"] = wbCount;
                        dataRow["Returnvisitproblem"] = JsonConvert.SerializeObject(Returnvisitproblem_by); ;
                        newTable.Rows.Add(dataRow);

                        #endregion 
                        #region 挂失工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //string Returnvisitproblem_gs = "";
                        List<string> Returnvisitproblem_gs = new List<string>();
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_gs.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_gs.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }


                        //微信工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wxCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_gs.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=19 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wbCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_gs.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["BusinessType"] = "挂失";
                        dataRow["allcount"] = callInCount + wxCount + wbCount;
                        dataRow["callcount"] = callInCount;
                        dataRow["wxcount"] = wxCount;
                        dataRow["wbcount"] = wbCount;
                        dataRow["Returnvisitproblem"] = JsonConvert.SerializeObject(Returnvisitproblem_gs); ;
                        newTable.Rows.Add(dataRow);

                        #endregion 
                        #region 投诉工单
                        callInCount = 0;
                        wxCount = 0;
                        wbCount = 0;
                        //string Returnvisitproblem_ts="";
                        List<string> Returnvisitproblem_ts = new List<string>();
                        //来电工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=1");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_ts.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        //留言工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=2");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    callInCount += int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_ts.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }


                        //微信工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=3");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wxCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_ts.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }

                        //微博工单数量
                        CallRows = datas.Select("BusinessType=3 and F_REPAIRMANID=4");
                        if (CallRows != null && CallRows.Length > 0)
                        {
                            for (int i = 0; i < CallRows.Length; i++)
                            {
                                if (CallRows[i]["callInCount"] != null && !string.IsNullOrEmpty(CallRows[i]["callInCount"].ToString()))
                                {
                                    wbCount = int.Parse(CallRows[i]["callInCount"].ToString());
                                }
                                if (CallRows[i]["F_RETURNVISITPROBLEM"].ToString().Trim() != "")
                                {
                                    Returnvisitproblem_ts.Add(CallRows[i]["F_RETURNVISITPROBLEM"].ToString());
                                }
                            }
                        }
                        dataRow = newTable.NewRow();
                        dataRow["BusinessType"] = "投诉";
                        dataRow["allcount"] = callInCount + wxCount + wbCount;
                        dataRow["callcount"] = callInCount;
                        dataRow["wxcount"] = wxCount;
                        dataRow["wbcount"] = wbCount;
                        dataRow["Returnvisitproblem"] = JsonConvert.SerializeObject(Returnvisitproblem_ts); ;
                        newTable.Rows.Add(dataRow);

                        #endregion 
                    }
                }
                dataModel.code = 0;
                dataModel.data = newTable;
            }
            catch (Exception ex)
            {
                dataModel.code = 200;
                dataModel.msg = ex.Message;
            }
            return JsonConvert.SerializeObject(dataModel);
        }

        /// 获取 微信/微博接口数量--放置咨询类
        /// </summary>
        /// <param name="StartDate">开始日期</param>
        /// <param name="EndDate">结束日期</param>
        /// <returns></returns>
        public int WXWorkOrderCount(int? busType, string startDate, string endDate)
        {
            int totalCount = 0;
            string wxAddUrl = "";
            try
            {
                StringBuilder returnStr = new StringBuilder();
                if (busType == 4)
                {
                    wxAddUrl = "http://rexian.zzmetro.com/api/external/wbcount";
                }
                else
                {
                    wxAddUrl = "http://rexian.zzmetro.com/api/external/wxcount";
                }
                string paramsStr = "begin=" + startDate + "&end=" + endDate;
                string addWxResult = HttpHelper.HttpGet(wxAddUrl, paramsStr);
                WXResponseResult ResultModel = JsonConvert.DeserializeObject<WXResponseResult>(addWxResult);

                if (ResultModel != null)
                {
                    if (ResultModel.service != null && ResultModel.service > 0 && ResultModel.service > totalCount)
                    {
                        totalCount = ResultModel.service - totalCount;
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return totalCount;
        }
        #endregion

    }
}
