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

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class WXInterFaceController : Controller
    {

        //工单信息
        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_Call_CallRecords recordBLL = new BLL.T_Call_CallRecords();
        //
        // GET: /WXInterFace/

        /// <summary>
        /// 微信创建工单
        /// </summary>
        /// <param name="workOrderType">1、微信号/2、微博号</param>
        /// <param name="webChartNum">微信号/微博号</param>
        /// <param name="recordId">工单id</param>
        /// <returns></returns>
        public ActionResult WXWorkFlow(int workOrderType, string userCode, string webChartNum, string recordId)
        {
            #region 创建工单
            int workOrderNum = 0;
            bool wobl = false;
            wobl = CreateWXWorkOrder(workOrderType, userCode, webChartNum, recordId, out workOrderNum);
            //实例化基础信息表
            CallScreenModel callScreenModel = new CallScreenModel();
            if (wobl && workOrderNum > 0)
            {
                callScreenModel.WorkOrderBaseModel = orderBLL.GetModel(workOrderNum);
                if (!string.IsNullOrEmpty(webChartNum))
                {
                    //如果电话号码不为空
                    if (callScreenModel.WorkOrderBaseModel != null)
                    {
                        T_Cus_CustomerBase baseCusModel = cusBLL.GetCusInfoModel(workOrderType, webChartNum);

                        //微信号或者微博号
                        callScreenModel.WorkOrderBaseModel.F_EMPCODE = webChartNum;

                        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();
                    }
                }
                else
                {
                    callScreenModel.WorkOrderBaseModel.CustomerBaseModel = new T_Cus_CustomerBase();
                }

                callScreenModel.WorkOrderBaseModel.CustomerBaseModel.F_Kind = (workOrderType + 1).ToString();
                if (workOrderType == 1)
                {
                    callScreenModel.WorkOrderBaseModel.CustomerBaseModel.F_CustomerNature = "微信";
                }
                else
                {
                    callScreenModel.WorkOrderBaseModel.CustomerBaseModel.F_CustomerNature = "微博";
                }
                //业务类型字典表
                callScreenModel.WorkOrderTypeModelList = GetWorkTypeModelList();
                //获取处理方式字典表
                callScreenModel.GoTypeModelList = GetCodeType("CLFS");
                //获取处理年龄段字典表
                callScreenModel.AgeModelList = GetCodeType("NLD");
                //获取部门列表
                callScreenModel.DepartmentList = deptBll.GetModelList("F_ParentId=0");


                return View(callScreenModel);

            }

            #endregion

            return View();
        }

        #region
        ///// <summary>
        ///// 保存来电弹屏信息
        ///// </summary>
        ///// <param name="workOrderBaseModel"></param>
        ///// <returns></returns>
        //[AcceptVerbs(HttpVerbs.Post)]
        //public bool workOrderAddData(T_Wo_WorkOrderBase workOrderBaseModel)
        //{
        //    //工单来源
        //    //workOrderBaseModel.F_REPAIRMANID = 1;
        //    //workOrderBaseModel.F_WORKORDERFROM = "语音";


        //    ////当前用户信息
        //    //workOrderBaseModel.F_USERID = F_UserID;//id
        //    //workOrderBaseModel.F_LINKMAN = F_UserCode;//工号
        //    //workOrderBaseModel.F_REPAIRMANNAME = "12";//姓名

        //    if (workOrderBaseModel.CustomerBaseModel != null)
        //    { 
        //        //最近联络方式
        //        if (workOrderBaseModel.F_REPAIRMANID.ToString() == "4")
        //        { workOrderBaseModel.CustomerBaseModel.F_Kind = "3"; }
        //        else
        //        {
        //            workOrderBaseModel.CustomerBaseModel.F_Kind = workOrderBaseModel.F_REPAIRMANID.ToString();
        //        } 
        //        workOrderBaseModel.CustomerBaseModel.F_CustomerNature = workOrderBaseModel.F_WORKORDERFROM;
        //        //业务类型
        //        workOrderBaseModel.CustomerBaseModel.F_IndustryId = workOrderBaseModel.F_REPAIRLEVEL;
        //        workOrderBaseModel.CustomerBaseModel.F_Url = workOrderBaseModel.F_ADSLACCOUNT;

        //        ////客户等级
        //        //workOrderBaseModel.CustomerBaseModel.F_ScaleResume = "1";
        //        //workOrderBaseModel.CustomerBaseModel.F_CustomerClass = "一般客户";

        //        //修改客户基础信息
        //        int cusid = cusBLL.EditCusInfoModel(workOrderBaseModel.CustomerBaseModel);
        //        if (cusid > 0)
        //        {
        //            workOrderBaseModel.F_CUSTOMERID = cusid;
        //        }
        //    }



        //    return orderBLL.UpdateWorkOrderInfoModel(workOrderBaseModel);
        //}
        #endregion


        /// <summary>
        /// 获取业务术语模板信息
        /// </summary>
        /// <param name="bussnessType">业务类型</param>
        /// <returns></returns>
        public string GetTypeContenet(int bussnessType, string creatDateTime)
        {

            try
            {
                //模板内容
                string bussContent = "";
                if (bussnessType > 0)
                {
                    if (bussnessType > 0)
                    {
                        BLL.T_Wo_WorkOrderType bll = new BLL.T_Wo_WorkOrderType();
                        Model.T_Wo_WorkOrderType orderTypeModel = new T_Wo_WorkOrderType();

                        orderTypeModel = bll.GetModel(bussnessType);
                        if (orderTypeModel != null && !string.IsNullOrEmpty(orderTypeModel.F_Remark))
                        {
                            bussContent += string.Format(orderTypeModel.F_Remark, DateTime.Parse(creatDateTime).ToString("f") + "分");
                        }
                    }
                }
                return bussContent;
            }
            catch (Exception ex)
            {

            }
            return null;

        }

        #region 创建微信/微博工单

        public bool CreateWXWorkOrder(int workOrderType,string userCode ,string webChartNum, string recordId, out int workorderid)
        {
            bool bl = false;
            workorderid = 0;
            try
            {
                Model.T_Wo_WorkOrderBase model = new Model.T_Wo_WorkOrderBase();
                BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
                Model.T_Sys_UserAccount userModel = userBLL.GetModelByUserCode(userCode);
                #region 工单信息
                model.F_EMPCODE = webChartNum;//微信微博号
                model.F_SERVICEWAY = recordId;//微信微博聊天记录
                //model.F_WORKORDERTYPEID = Convert.ToInt32(typeid);//类型ID
                model.F_WORKORDERSTATEID = 0;//状态ID
                model.F_REPAIRREQUEST = "未处理";
                model.F_CODE = "";//工单编号
                model.F_WORKORDERNAME = "";//工单名称
                model.F_NUMBER = model.F_CODE;
                //model.F_CUSTOMERNAME = txtCustomerName.Text.Trim();//客户姓名
                // model.F_CUSTOMERTELEPHONE = phone.Trim();//客户电话
                model.F_USERID = userModel.F_UserId;//坐席id
                model.F_LINKMAN = userModel.F_UserCode;//坐席工号
                model.F_REPAIRMANNAME = userModel.F_UserName;//坐席名称
                //  model.F_LINKMANTELEPHONE = phone.Trim();//联系人电话
                //model.F_ADSLACCOUNT = hfCallId.Value.Trim();
                model.F_BANDWIDTH = "";
                //model.F_STANDARDADDRESS = locationaddress.Trim();
                //model.F_INSTALLADDRESS = F_INSTALLADDRESS;
                // model.F_KSHADDRESSCODE = callid;
                // model.F_RANGEID = Convert.ToInt32(recordid);
                model.F_PROVINCE = "";
                model.F_CITY = "";
                model.F_AREA = "";
                model.F_ROAD = "";
                model.F_HOUSING = "";
                if (workOrderType == 1)
                {
                    model.F_REPAIRMANID = 3;
                    model.F_WORKORDERFROM = "微信工单";
                }
                else
                {
                    model.F_REPAIRMANID = 4;
                    model.F_WORKORDERFROM = "微博工单";

                }
                //model.F_WORKORDERLEVELID = F_WORKORDERLEVELID;
                model.F_FILEFLAG = 0;
                model.F_CREATEBY = userModel.F_UserId;
                model.F_CREATEDATE = DateTime.Now;
                model.F_RETURNVISITFLAG = 0;
                model.F_RETURNVISITSTATE = 0;
                model.F_RETURNVISITRESULT = 0;
                model.F_RETURNVISITCONTENT = "";
                model.F_DELETEFLAG = 0;
                model.F_ALLUSETIMES = 0;
                model.F_STARTTIME = DateTime.Now;
                model.F_OVERTIMES = 0;
                model.F_FORMID = 0;
                model.F_HASTENCOUNTS = 0;
                model.F_RANGEX = 0;
                model.F_RANGEY = 0;
                model.F_REPAIRCLOSEMANID = 0;
                model.F_REPAIRCLOSEMANNAME = "";
                model.F_REPAIRCLOSEREASONS = "";
                model.F_REPAIRCLOSEREPTID = 0;
                model.F_REPAIRCLOSEREPT = "";


                #endregion

                int resorderbase = new BLL.T_Wo_WorkOrderBase().Add(model);//添加基本信息  
                workorderid = resorderbase;
                if (resorderbase > 0)
                {
                    bl = true;

                    #region 调用接口回调
                    try
                    {
                        StringBuilder returnStr = new StringBuilder();
                        string wxAddUrl = string.Format("http://rexian.zzmetro.com/api/customer/close/{0}/{1}", recordId, resorderbase);
                        HttpHelper.HttpGet(wxAddUrl);

                    }
                    catch (Exception ex)
                    {

                    }
                    #endregion

                }
                else
                {
                    bl = false;
                } 

            }
            catch (Exception ex)
            {

            }
            return bl;
        }

        #endregion
        //字典表
        BLL.T_Sys_DictionaryValue dicValueBll = new BLL.T_Sys_DictionaryValue();
        /// <summary>
        /// 获取员工信息
        /// </summary>
        /// <param name="deptId">部门id</param>
        /// <returns></returns>
        public string GetDeptUsers(string deptId)
        {

            try
            {
                return JsonConvert.SerializeObject(userBll.GetModelList("F_DeptId=" + deptId));
            }
            catch (Exception ex)
            {

            }
            return null;

        }

        #region 获取处理类型字典
     
        /// <summary>
        /// 获取处理类型字典
        /// </summary>
        /// <returns></returns>
        public List<T_Sys_DictionaryValue> GetCodeType(string dicCodeType)
        {

            return dicValueBll.GetModelList("F_DictionaryFlag='" + dicCodeType + "'");

        }
        #endregion

        #region 获取业务类型
        /// <summary>
        /// 获取业务类型
        /// </summary>
        public List<Model.T_Wo_WorkOrderType> GetWorkTypeModelList()
        {
            //最终需要返回的菜单
            List<Model.T_Wo_WorkOrderType> newModelList = new List<T_Wo_WorkOrderType>();

            Model.PageData<Model.T_Wo_WorkOrderType> pageModel = new Model.PageData<Model.T_Wo_WorkOrderType>();
            BLL.T_Wo_WorkOrderType bll = new BLL.T_Wo_WorkOrderType();
            //获取业务类型列表
            List<Model.T_Wo_WorkOrderType> WorkTypeModelList = bll.GetModelList("");

            if (WorkTypeModelList != null && WorkTypeModelList.Count > 0)
            {
                //获取默认根节点
                Model.T_Wo_WorkOrderType rootNode = WorkTypeModelList.FirstOrDefault(m => m.F_ParentId == 0);
                //获取一级节点
                List<Model.T_Wo_WorkOrderType> rootModelList = WorkTypeModelList.Where(t => t.F_ParentId == rootNode.F_WorkOrderTypeId).ToList();
                if (rootModelList != null && rootModelList.Count > 0)
                {
                    foreach (T_Wo_WorkOrderType WorkOrderTypeModel in rootModelList)
                    {
                        List<Model.T_Wo_WorkOrderType> nullModelList = new List<T_Wo_WorkOrderType>();
                        WorkOrderTypeModel.WorkTypeModelList = nullModelList;
                        newModelList.Add(GetWorkTypeModelChild(WorkOrderTypeModel, WorkOrderTypeModel, WorkTypeModelList));
                    }
                }
            }
            return newModelList;
        }

        //获取所有子集
        public Model.T_Wo_WorkOrderType GetWorkTypeModelChild(T_Wo_WorkOrderType rootNode, T_Wo_WorkOrderType WorkOrderTypeModel, List<Model.T_Wo_WorkOrderType> WorkTypeModelList)
        {
            //获取子集节点
            List<Model.T_Wo_WorkOrderType> sonModelList = WorkTypeModelList.Where(t => t.F_ParentId == WorkOrderTypeModel.F_WorkOrderTypeId).ToList();

            if (sonModelList != null && sonModelList.Count > 0)
            {
                foreach (T_Wo_WorkOrderType workOrderTypeModel in sonModelList)
                {
                    //获取子子集
                    List<Model.T_Wo_WorkOrderType> childModelList = WorkTypeModelList.Where(t => t.F_ParentId == workOrderTypeModel.F_WorkOrderTypeId).ToList();

                    if (childModelList != null && childModelList.Count > 0)
                    {
                        GetWorkTypeModelChild(rootNode, workOrderTypeModel, WorkTypeModelList);
                    }
                    else
                    {
                        rootNode.WorkTypeModelList.Add(workOrderTypeModel);
                    }


                }

            }

            return rootNode;
        }
        #endregion


        /// <summary>
        /// 获取字典信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDicSelect(string diccode, string code1, string name1, string code2, string name2, string code3, string name3)
        {
            SelectInfoTypeModel modelRes = new SelectInfoTypeModel();
            modelRes.diccode = diccode;
            modelRes.code1 = code1;
            modelRes.name1 = string.IsNullOrEmpty(name1) ? "请选择" : name1;
            modelRes.code2 = code2;
            modelRes.name2 = string.IsNullOrEmpty(name2) ? "请选择" : name2;
            modelRes.code3 = code3;
            modelRes.name3 = string.IsNullOrEmpty(name3) ? "请选择" : name3;

            List<T_Sys_DictionaryValue> modelList = new List<T_Sys_DictionaryValue>();
            BLL.T_Sys_DictionaryValue dicValueBll = new BLL.T_Sys_DictionaryValue();

            modelList = dicValueBll.GetModelList("F_DictionaryFlag='ZDXX'");

            if (modelList != null && modelList.Count > 0)
            {
                foreach (T_Sys_DictionaryValue model in modelList)
                {
                    model.DictionaryValueModelList = dicValueBll.GetModelList("F_DictionaryFlag='" + model.F_ValueCode + "'");
                }
            }
            modelRes.modelList = modelList;
            return View(modelRes);
        }

        public ActionResult GetDicSelect2(string diccode, string code1, string name1, string code2, string name2, string code3, string name3)
        {
            SelectInfoTypeModel modelRes = new SelectInfoTypeModel();
            modelRes.diccode = diccode;
            modelRes.code1 = code1;
            modelRes.name1 = string.IsNullOrEmpty(name1) ? "请选择" : name1;
            modelRes.code2 = code2;
            modelRes.name2 = string.IsNullOrEmpty(name2) ? "请选择" : name2;
            modelRes.code3 = code3;
            modelRes.name3 = string.IsNullOrEmpty(name3) ? "请选择" : name3;

            List<T_Sys_DictionaryValue> modelList = new List<T_Sys_DictionaryValue>();
            BLL.T_Sys_DictionaryValue dicValueBll = new BLL.T_Sys_DictionaryValue();

            modelList = dicValueBll.GetModelList("F_DictionaryFlag='ZDXX'");

            if (modelList != null && modelList.Count > 0)
            {
                foreach (T_Sys_DictionaryValue model in modelList)
                {
                    model.DictionaryValueModelList = dicValueBll.GetModelList("F_DictionaryFlag='" + model.F_ValueCode + "'");
                }
            }
            modelRes.modelList = modelList;
            return View(modelRes);
        }



#region
        ///// <summary>
        ///// 编辑手工工单信息
        ///// </summary>
        ///// <param name="workOrderBaseModel"></param>
        ///// <returns></returns>
        //[AcceptVerbs(HttpVerbs.Post)]
        //public bool workOrderAddDatanew(T_Wo_WorkOrderBase workOrderBaseModel)
        //{
        //    bool bl = false;
        //    try
        //    {
        //        //工单来源
        //        workOrderBaseModel.F_REPAIRMANID = 4;
        //        workOrderBaseModel.F_WORKORDERFROM = "微博工单";
        //        string wbh = workOrderBaseModel.CustomerBaseModel.F_CustomerEName.Trim();
        //        if (!string.IsNullOrEmpty(wbh))
        //        {
        //            workOrderBaseModel.F_EMPCODE = wbh;
        //            workOrderBaseModel.CustomerBaseModel.F_PostCode = wbh;
        //        } 

        //        //当前用户信息
        //        //workOrderBaseModel.F_USERID = F_UserID;//id
        //        //workOrderBaseModel.F_LINKMAN = F_UserCode;//工号
        //        //workOrderBaseModel.F_REPAIRMANNAME = F_UserName;//姓名
        //        workOrderBaseModel.F_LINKMANTELEPHONE = workOrderBaseModel.CustomerBaseModel.F_Mobile;
        //        //if (F_UserID > 0)
        //        //{
        //        //    T_Sys_UserAccount usermodel = new T_Sys_UserAccount();
        //        //    usermodel = userBll.GetModel(F_UserID);
        //        //    workOrderBaseModel.F_REPAIRMANNAME = usermodel.F_UserName;//姓名
        //        //}
        //        if (workOrderBaseModel.CustomerBaseModel != null)
        //        {
        //            //最近联络方式
        //            if (workOrderBaseModel.F_REPAIRMANID.ToString()=="4")
        //            { workOrderBaseModel.CustomerBaseModel.F_Kind = "3"; }
        //            else {
        //                workOrderBaseModel.CustomerBaseModel.F_Kind = workOrderBaseModel.F_REPAIRMANID.ToString();
        //            } 
        //            workOrderBaseModel.CustomerBaseModel.F_CustomerNature = workOrderBaseModel.F_WORKORDERFROM;
        //            //业务类型
        //            workOrderBaseModel.CustomerBaseModel.F_IndustryId = workOrderBaseModel.F_REPAIRLEVEL;
        //            workOrderBaseModel.CustomerBaseModel.F_Url = workOrderBaseModel.F_ADSLACCOUNT;
        //            workOrderBaseModel.CustomerBaseModel.F_CustomerId = Convert.ToInt32(workOrderBaseModel.F_CUSTOMERID);
        //            ////客户等级
        //            workOrderBaseModel.CustomerBaseModel.F_ScaleResume = "1";
        //            workOrderBaseModel.CustomerBaseModel.F_CustomerClass = "一般客户";
        //            //保存工单基础信息

        //            //修改客户基础信息
        //            int cusid = cusBLL.EditCusInfoModel(workOrderBaseModel.CustomerBaseModel);
        //            if (cusid > 0)
        //            {
        //                workOrderBaseModel.F_CUSTOMERID = cusid;
        //            }

        //        }
        //        if (workOrderBaseModel.F_WORKORDERID == 0)
        //        { 
        //            workOrderBaseModel.F_CREATEDATE = DateTime.Now;
        //            bl =orderBLL.AddWbgd(workOrderBaseModel)>0; 
        //        }
        //        else
        //        {
        //            try
        //            {
        //                ZXDT.CallCenter.Model.T_Wo_WorkOrderBase m = new ZXDT.CallCenter.BLL.T_Wo_WorkOrderBase().GetModel(workOrderBaseModel.F_WORKORDERID);
        //                workOrderBaseModel.F_LINKMANTELEPHONE = m.F_LINKMANTELEPHONE;
        //                workOrderBaseModel.F_CUSTOMERTELEPHONE = m.F_CUSTOMERTELEPHONE;
        //                workOrderBaseModel.F_NUMBER = m.F_CODE;
        //            }
        //            catch
        //            { }
        //            if (workOrderBaseModel.F_SERVICETYPE == "待处理")
        //            {
        //                workOrderBaseModel.F_WORKORDERNAME = "手工工单";
        //                return orderBLL.UpdateWorkOrderInfoModel(workOrderBaseModel);
        //            }
        //            else
        //            {
        //                workOrderBaseModel.F_WORKORDERNAME = "手工工单";
        //                return orderBLL.UpdateWorkOrderInfoModel(workOrderBaseModel);
        //            }

        //        }



        //    }
        //    catch (Exception ex) { }
        //    return bl;
        //}
#endregion
    }
}
