using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Transactions;

namespace HySoft.BaseCallCenter.Web.customermanage
{
    public partial class customerSYNC : System.Web.UI.Page
    {
        public static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringMetaCRM6"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            GetCustomerInfo();
        }
        private void GetCustomerInfo()
        {
            string sql = "SELECT [khbm],[khmc],[xtktdate],[zbjsrq],[sskfb],[khsx],[khlxr],[khlxrdh],[sssf],[cpxl],[dqver],[ptlx],[khdawszt],[khdj],[sscs],[khzt],[khdz],[bzxx] FROM [MetaCRM6].[dbo].[同步到呼叫中心的客户信息]";
            DataTable dt = Query(sql).Tables[0];
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                try
                {
                    Dictionary<string, int> dicDept = new Dictionary<string, int>();
                    Dictionary<string, int> dicKHSX = new Dictionary<string, int>();//客户属性
                    Dictionary<string, int> dicCPXL = new Dictionary<string, int>();//产品系列
                    Dictionary<string, int> dicXTLX = new Dictionary<string, int>();//系统类型
                    Dictionary<string, int> dicKHDJ = new Dictionary<string, int>();//客户等级
                    Dictionary<string, int> dicSheng = new Dictionary<string, int>();
                    Dictionary<string, int> dicShi = new Dictionary<string, int>();
                    DataTable dtdic = new DataTable();
                    DataTable dtcity = new DataTable();
                    DataTable dtdept = new DataTable();
                    #region 取出字典值
                    dtdic = new HySoft.BaseCallCenter.BLL.T_Sys_DictionaryValue().GetAllList().Tables[0];
                    foreach (DataRow dr in dtdic.Select("F_DictionaryFlag='KHSX'"))
                    {
                        try
                        {
                            dicKHSX.Add(dr["F_Name"].ToString(), Convert.ToInt32(dr["F_DictionaryValueId"].ToString()));
                        }
                        catch
                        {
                            //this.AddLogsList(Loglevel.Normal, "CRM客户属性[" + dr["F_Name"].ToString() + "]有重复", 0);
                        }
                    }
                    foreach (DataRow dr in dtdic.Select("F_DictionaryFlag='CPXL'"))
                    {
                        //dicCPXL.Add(dr["F_Name"].ToString(), Convert.ToInt32(dr["F_DictionaryValueId"].ToString()));
                        try
                        {
                            dicCPXL.Add(dr["F_Name"].ToString(), Convert.ToInt32(dr["F_DictionaryValueId"].ToString()));
                        }
                        catch
                        {
                            //this.AddLogsList(Loglevel.Normal, "CRM产品系列[" + dr["F_Name"].ToString() + "]有重复", 0);
                        }
                    }
                    foreach (DataRow dr in dtdic.Select("F_DictionaryFlag='XTLX'"))
                    {
                        try
                        {
                            dicXTLX.Add(dr["F_Name"].ToString(), Convert.ToInt32(dr["F_DictionaryValueId"].ToString()));
                        }
                        catch
                        {
                            //this.AddLogsList(Loglevel.Normal, "CRM系统类型[" + dr["F_Name"].ToString() + "]有重复", 0);
                        }
                    }
                    foreach (DataRow dr in dtdic.Select("F_DictionaryFlag='KHDJ'"))
                    {
                        try
                        {
                            dicKHDJ.Add(dr["F_Name"].ToString(), Convert.ToInt32(dr["F_DictionaryValueId"].ToString()));
                        }
                        catch
                        {
                            //this.AddLogsList(Loglevel.Normal, "CRM客户等级[" + dr["F_Name"].ToString() + "]有重复", 0);
                        }
                    }
                    dtdic.Clear();
                    dtdic.Dispose();

                    dtcity = new HySoft.BaseCallCenter.BLL.T_RegionCategory().GetAllList().Tables[0];
                    foreach (DataRow dr in dtcity.Select("F_ParentId=0"))
                    {
                        try
                        {
                            dicSheng.Add(dr["F_RegionName"].ToString(), Convert.ToInt32(dr["F_RegionId"].ToString()));
                        }
                        catch
                        {
                            //this.AddLogsList(Loglevel.Normal, "CRM客户所在省份[" + dr["F_RegionName"].ToString() + "]有重复", 0);
                        }
                    }
                    foreach (DataRow dr in dtcity.Select("F_ParentId<>0"))
                    {
                        try
                        {
                            dicShi.Add(dr["F_RegionName"].ToString(), Convert.ToInt32(dr["F_RegionId"].ToString()));
                        }
                        catch
                        {
                            //this.AddLogsList(Loglevel.Normal, "CRM客户所在县市[" + dr["F_RegionName"].ToString() + "]有重复", 0);
                        }
                    }
                    dtcity.Clear();
                    dtcity.Dispose();

                    dtdept = new HySoft.BaseCallCenter.BLL.T_Sys_Department().GetAllList().Tables[0];
                    foreach (DataRow dr in dtcity.Rows)
                    {
                        try
                        {
                            dicDept.Add(dr["F_DeptName"].ToString(), Convert.ToInt32(dr["F_DeptId"].ToString()));
                        }
                        catch
                        {
                            //this.AddLogsList(Loglevel.Normal, "CRM客户所属部门[" + dr["F_DeptName"].ToString() + "]有重复", 0);
                        }
                    }
                    dtdept.Clear();
                    dtdept.Dispose();
                    #endregion
                    for (int n = 0; n < rowsCount; n++)
                    {
                        List<Model.T_Cus_CustomerBase> tccblist = new BLL.T_Cus_CustomerBase().GetModelList("F_CustomerCode='" + dt.Rows[n]["khbm"].ToString() + "'");
                        //存在就更新
                        #region 更新信息赋值
                        #region 不转换字段
                        //客户名称
                        if (dt.Rows[n]["khmc"] != null && dt.Rows[n]["khmc"].ToString() != "")
                        {
                            tccblist[0].F_CustomerName = dt.Rows[n]["khmc"].ToString();
                        }
                        //系统开通日期
                        if (dt.Rows[n]["xtktdate"] != null && dt.Rows[n]["xtktdate"].ToString() != "")
                        {
                            tccblist[0].F_SystemStartTime = Convert.ToDateTime(dt.Rows[n]["xtktdate"].ToString());
                        }
                        //质保结束日期
                        if (dt.Rows[n]["zbjsrq"] != null && dt.Rows[n]["zbjsrq"].ToString() != "")
                        {
                            tccblist[0].F_QualityGuaranteeEndTime = Convert.ToDateTime(dt.Rows[n]["zbjsrq"].ToString());
                        }
                        //所属客服部
                        if (dt.Rows[n]["sskfb"] != null && dt.Rows[n]["sskfb"].ToString() != "")
                        {
                            tccblist[0].F_ServiceDept = dt.Rows[n]["sskfb"].ToString();
                        }
                        //客户属性
                        if (dt.Rows[n]["khsx"] != null && dt.Rows[n]["khsx"].ToString() != "")
                        {
                            tccblist[0].F_CustomerNature = dt.Rows[n]["khsx"].ToString();
                        }

                        //客户联系人
                        if (dt.Rows[n]["khlxr"] != null && dt.Rows[n]["khlxr"].ToString() != "")
                        {
                            tccblist[0].F_ChargeName = dt.Rows[n]["khlxr"].ToString();
                        }
                        //客户联系人电话
                        if (dt.Rows[n]["khlxrdh"] != null && dt.Rows[n]["khlxrdh"].ToString() != "")
                        {
                            tccblist[0].F_ChargeTelephone = dt.Rows[n]["khlxrdh"].ToString();
                        }
                        //所属省份
                        if (dt.Rows[n]["sssf"] != null && dt.Rows[n]["sssf"].ToString() != "")
                        {
                            tccblist[0].F_Province = dt.Rows[n]["sssf"].ToString();
                        }
                        //产品类型
                        if (dt.Rows[n]["cpxl"] != null && dt.Rows[n]["cpxl"].ToString() != "")
                        {
                            tccblist[0].F_ProductLine = dt.Rows[n]["cpxl"].ToString();
                        }
                        //当前版本
                        if (dt.Rows[n]["dqver"] != null && dt.Rows[n]["dqver"].ToString() != "")
                        {
                            tccblist[0].F_CurrentVersion = dt.Rows[n]["dqver"].ToString();
                        }
                        //客户档案完善状态
                        if (dt.Rows[n]["khdawszt"] != null && dt.Rows[n]["khdawszt"].ToString() != "")
                        {
                            if (dt.Rows[n]["khdawszt"].ToString().Trim() == "已完善")
                            {
                                tccblist[0].F_StatusCode = 1;
                            }
                            else
                            {
                                tccblist[0].F_StatusCode = 0;
                            }
                            //model. = dt.Rows[n]["khdawszt"].ToString();
                        }
                        //客户等级
                        if (dt.Rows[n]["khdj"] != null && dt.Rows[n]["khdj"].ToString() != "")
                        {
                            tccblist[0].F_CustomerClass = dt.Rows[n]["khdj"].ToString();
                        }
                        // 所属城市
                        if (dt.Rows[n]["sscs"] != null && dt.Rows[n]["sscs"].ToString() != "")
                        {
                            tccblist[0].F_City = dt.Rows[n]["sscs"].ToString();
                        }
                        //客户状态
                        if (dt.Rows[n]["khzt"] != null && dt.Rows[n]["khzt"].ToString() != "")
                        {
                            tccblist[0].F_CustomerStatus = dt.Rows[n]["khzt"].ToString();
                        }
                        //客户地址
                        if (dt.Rows[n]["khdz"] != null && dt.Rows[n]["khdz"].ToString() != "")
                        {
                            tccblist[0].F_Address = dt.Rows[n]["khdz"].ToString();
                        }
                        //备注信息
                        if (dt.Rows[n]["bzxx"] != null && dt.Rows[n]["bzxx"].ToString() != "")
                        {
                            tccblist[0].F_Notes = dt.Rows[n]["bzxx"].ToString();
                        }
                        #endregion
                        if (dt.Rows[n]["sskfb"] != null && dt.Rows[n]["sskfb"].ToString() != "")
                        {
                            bool IsContain = dicDept.ContainsKey(dt.Rows[n]["sskfb"].ToString());
                            if (IsContain)
                                tccblist[0].F_ServiceDeptID = dicDept[dt.Rows[n]["sskfb"].ToString()];
                        }
                        if (dt.Rows[n]["khsx"] != null && dt.Rows[n]["khsx"].ToString() != "")
                        {
                            bool IsContain = dicKHSX.ContainsKey(dt.Rows[n]["khsx"].ToString());
                            if (IsContain)
                                tccblist[0].F_CustomerNatureID = dicKHSX[dt.Rows[n]["khsx"].ToString()];
                        }
                        if (dt.Rows[n]["sssf"] != null && dt.Rows[n]["sssf"].ToString() != "")
                        {
                            bool IsContain = dicSheng.ContainsKey(dt.Rows[n]["sssf"].ToString());
                            if (IsContain)
                                tccblist[0].F_RegionId = dicSheng[dt.Rows[n]["sssf"].ToString()];
                        }
                        if (dt.Rows[n]["cpxl"] != null && dt.Rows[n]["cpxl"].ToString() != "")
                        {
                            bool IsContain = dicCPXL.ContainsKey(dt.Rows[n]["cpxl"].ToString());
                            if (IsContain)
                                tccblist[0].F_ProductLineID = dicCPXL[dt.Rows[n]["cpxl"].ToString()];
                        }
                        //平台类型
                        if (dt.Rows[n]["ptlx"] != null && dt.Rows[n]["ptlx"].ToString() != "")
                        {
                            //F_CustomerType
                            tccblist[0].F_SystemType = dt.Rows[n]["ptlx"].ToString();
                            bool IsContain = dicXTLX.ContainsKey(dt.Rows[n]["ptlx"].ToString());
                            if (IsContain)
                                tccblist[0].F_CustomerType = dicXTLX[dt.Rows[n]["ptlx"].ToString()];
                        }
                        //客户等级
                        if (dt.Rows[n]["khdj"] != null && dt.Rows[n]["khdj"].ToString() != "")
                        {
                            bool IsContain = dicKHDJ.ContainsKey(dt.Rows[n]["khdj"].ToString());
                            if (IsContain)
                                tccblist[0].F_CustomerClassID = dicKHDJ[dt.Rows[n]["khdj"].ToString()];
                        }
                        if (dt.Rows[n]["sscs"] != null && dt.Rows[n]["sscs"].ToString() != "")
                        {
                            bool IsContain = dicShi.ContainsKey(dt.Rows[n]["sscs"].ToString());
                            if (IsContain)
                                tccblist[0].F_CityID = dicShi[dt.Rows[n]["sscs"].ToString()];
                        }
                        #endregion
                        Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
                        //不存在就新加
                        #region 新增信息赋值
                        #region 不转换字段
                        
                        //客户编号
                        if (dt.Rows[n]["khbm"] != null && dt.Rows[n]["khbm"].ToString() != "")
                        {
                            model.F_CustomerCode = dt.Rows[n]["khbm"].ToString();
                        }
                        //客户名称
                        if (dt.Rows[n]["khmc"] != null && dt.Rows[n]["khmc"].ToString() != "")
                        {
                            model.F_CustomerName = dt.Rows[n]["khmc"].ToString();
                        }
                        //系统开通日期
                        if (dt.Rows[n]["xtktdate"] != null && dt.Rows[n]["xtktdate"].ToString() != "")
                        {
                            model.F_SystemStartTime = Convert.ToDateTime(dt.Rows[n]["xtktdate"].ToString());
                        }
                        //质保结束日期
                        if (dt.Rows[n]["zbjsrq"] != null && dt.Rows[n]["zbjsrq"].ToString() != "")
                        {
                            model.F_QualityGuaranteeEndTime = Convert.ToDateTime(dt.Rows[n]["zbjsrq"].ToString());
                        }
                        //所属客服部
                        if (dt.Rows[n]["sskfb"] != null && dt.Rows[n]["sskfb"].ToString() != "")
                        {
                            model.F_ServiceDept = dt.Rows[n]["sskfb"].ToString();
                        }
                        //客户属性
                        if (dt.Rows[n]["khsx"] != null && dt.Rows[n]["khsx"].ToString() != "")
                        {
                            model.F_CustomerNature = dt.Rows[n]["khsx"].ToString();
                        }

                        //客户联系人
                        if (dt.Rows[n]["khlxr"] != null && dt.Rows[n]["khlxr"].ToString() != "")
                        {
                            model.F_ChargeName = dt.Rows[n]["khlxr"].ToString();
                        }
                        //客户联系人电话
                        if (dt.Rows[n]["khlxrdh"] != null && dt.Rows[n]["khlxrdh"].ToString() != "")
                        {
                            model.F_ChargeTelephone = dt.Rows[n]["khlxrdh"].ToString();
                        }
                        //所属省份
                        if (dt.Rows[n]["sssf"] != null && dt.Rows[n]["sssf"].ToString() != "")
                        {
                            model.F_Province = dt.Rows[n]["sssf"].ToString();
                        }
                        //产品类型
                        if (dt.Rows[n]["cpxl"] != null && dt.Rows[n]["cpxl"].ToString() != "")
                        {
                            model.F_ProductLine = dt.Rows[n]["cpxl"].ToString();
                        }
                        //当前版本
                        if (dt.Rows[n]["dqver"] != null && dt.Rows[n]["dqver"].ToString() != "")
                        {
                            model.F_CurrentVersion = dt.Rows[n]["dqver"].ToString();
                        }
                        //客户档案完善状态
                        if (dt.Rows[n]["khdawszt"] != null && dt.Rows[n]["khdawszt"].ToString() != "")
                        {
                            if (dt.Rows[n]["khdawszt"].ToString().Trim() == "已完善")
                            {
                                model.F_StatusCode = 1;
                            }
                            else
                            {
                                model.F_StatusCode = 0;
                            }
                            //model. = dt.Rows[n]["khdawszt"].ToString();
                        }
                        //客户等级
                        if (dt.Rows[n]["khdj"] != null && dt.Rows[n]["khdj"].ToString() != "")
                        {
                            model.F_CustomerClass = dt.Rows[n]["khdj"].ToString();
                        }
                        // 所属城市
                        if (dt.Rows[n]["sscs"] != null && dt.Rows[n]["sscs"].ToString() != "")
                        {
                            model.F_City = dt.Rows[n]["sscs"].ToString();
                        }
                        //客户状态
                        if (dt.Rows[n]["khzt"] != null && dt.Rows[n]["khzt"].ToString() != "")
                        {
                            model.F_CustomerStatus = dt.Rows[n]["khzt"].ToString();
                        }
                        //客户地址
                        if (dt.Rows[n]["khdz"] != null && dt.Rows[n]["khdz"].ToString() != "")
                        {
                            model.F_Address = dt.Rows[n]["khdz"].ToString();
                        }
                        //备注信息
                        if (dt.Rows[n]["bzxx"] != null && dt.Rows[n]["bzxx"].ToString() != "")
                        {
                            model.F_Notes = dt.Rows[n]["bzxx"].ToString();
                        }
                        #endregion
                        if (dt.Rows[n]["sskfb"] != null && dt.Rows[n]["sskfb"].ToString() != "")
                        {
                            bool IsContain = dicDept.ContainsKey(dt.Rows[n]["sskfb"].ToString());
                            if (IsContain)
                                model.F_ServiceDeptID = dicDept[dt.Rows[n]["sskfb"].ToString()];
                        }
                        if (dt.Rows[n]["khsx"] != null && dt.Rows[n]["khsx"].ToString() != "")
                        {
                            bool IsContain = dicKHSX.ContainsKey(dt.Rows[n]["khsx"].ToString());
                            if (IsContain)
                                model.F_CustomerNatureID = dicKHSX[dt.Rows[n]["khsx"].ToString()];
                        }
                        if (dt.Rows[n]["sssf"] != null && dt.Rows[n]["sssf"].ToString() != "")
                        {
                            bool IsContain = dicSheng.ContainsKey(dt.Rows[n]["sssf"].ToString());
                            if (IsContain)
                                model.F_RegionId = dicSheng[dt.Rows[n]["sssf"].ToString()];
                        }
                        if (dt.Rows[n]["cpxl"] != null && dt.Rows[n]["cpxl"].ToString() != "")
                        {
                            bool IsContain = dicCPXL.ContainsKey(dt.Rows[n]["cpxl"].ToString());
                            if (IsContain)
                                model.F_ProductLineID = dicCPXL[dt.Rows[n]["cpxl"].ToString()];
                        }
                        //平台类型
                        if (dt.Rows[n]["ptlx"] != null && dt.Rows[n]["ptlx"].ToString() != "")
                        {
                            //F_CustomerType
                            model.F_SystemType = dt.Rows[n]["ptlx"].ToString();
                            bool IsContain = dicXTLX.ContainsKey(dt.Rows[n]["ptlx"].ToString());
                            if (IsContain)
                                model.F_CustomerType = dicXTLX[dt.Rows[n]["ptlx"].ToString()];
                        }
                        //客户等级
                        if (dt.Rows[n]["khdj"] != null && dt.Rows[n]["khdj"].ToString() != "")
                        {
                            bool IsContain = dicKHDJ.ContainsKey(dt.Rows[n]["khdj"].ToString());
                            if (IsContain)
                                model.F_CustomerClassID = dicKHDJ[dt.Rows[n]["khdj"].ToString()];
                        }
                        if (dt.Rows[n]["sscs"] != null && dt.Rows[n]["sscs"].ToString() != "")
                        {
                            bool IsContain = dicShi.ContainsKey(dt.Rows[n]["sscs"].ToString());
                            if (IsContain)
                                model.F_CityID = dicShi[dt.Rows[n]["sscs"].ToString()];
                        }
                        #endregion
                    }
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('success')</script>");
                }
                catch
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>SaveInfo('warn')</script>");
                }
            }
        }
        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        private DataSet Query(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }
    }
}