/// <summary>
        /// 获取指定拜访路线上的门店列表
        /// </summary>
        /// <param name="User"></param>
        /// <param name="TDP"></param>
        /// <param name="VisitRoute"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Counts"></param>
        /// <returns></returns>
        public static List<ClientInfo> GetRetailerListByVisitRoute(UserInfo User, int TDP, string FindKey, int VisitRoute, int PageSize, int PageIndex, out int Counts)
        {
            Counts = 0;
            LogWriter.WriteLog("PBMIFService.GetRetailerListByVisitRoute:UserName="******",TDP=" + TDP.ToString()
                + ",FindKey=" + FindKey + ",VisitRoute=" + VisitRoute + ",PageSize=" + PageSize.ToString() + ",PageIndex=" + PageIndex.ToString());
            if (User.OwnerType == 3) TDP = User.ClientID;

            #region 设定查询条件
            string ExtConditionStr = "";
            if (FindKey.Trim() != "")
                ExtConditionStr = " (CM_Client.FullName LIKE '%" + FindKey.Trim() + "%'  OR CM_Client.ShortName LIKE '%" + FindKey.Trim()
                    + "%' OR CM_Client.Address LIKE '%" + FindKey.Trim() + "%')";

            //根据线路筛选
            if (VisitRoute != 0)
            {
                if (User.OwnerType == 3)
                {
                    ExtConditionStr += " AND CM_Client.ID IN (SELECT Client FROM CM_ClientSupplierInfo WHERE Supplier=" + TDP.ToString() + " AND State = 1 "
                        + " AND VisitRoute=" + VisitRoute.ToString() + ")";
                }
                else if (User.OwnerType == 2)
                {
                    ExtConditionStr += " AND CM_Client.ID IN (SELECT Client FROM CM_ClientManufactInfo WHERE Manufacturer=" + User.ClientID.ToString()
                        + " AND VisitRoute=" + VisitRoute.ToString() + ")";

                    ExtConditionStr += " AND CM_Client.ID IN (SELECT Client FROM CM_ClientSupplierInfo WHERE Supplier=" + TDP.ToString() + " AND State = 1 )";
                }
            }

            IList<CM_Client> clients = CM_ClientBLL.GetModelList(ExtConditionStr);
            #endregion

            Counts = clients.Count;

            #region 分页返回
            if (PageSize == 0) PageSize = 10;
            List<ClientInfo> list = new List<ClientInfo>();
            int fromindex = PageSize * PageIndex;
            int endindex = PageSize * (PageIndex + 1);

            if (fromindex < clients.Count)
            {
                for (int i = fromindex; (i < endindex && i < clients.Count); i++)
                {
                    ClientInfo c = new ClientInfo(clients[i], User.ClientID);
                    list.Add(c);
                }
            }
            return list;
            #endregion
        }
        /// <summary>
        /// 获取所有管辖的门店列表
        /// </summary>
        /// <returns></returns>
        public static List<ClientInfo> GetRetailerList(UserInfo User, int TDP, string FindKey, int PageSize, int PageIndex, out int Counts)
        {
            Counts = 0;
            LogWriter.WriteLog("PBMIFService.GetRetailerList:UserName="******",TDP=" + TDP.ToString()
                + ",FindKey=" + FindKey + ",PageSize=" + PageSize.ToString() + ",PageIndex=" + PageIndex.ToString());
            if (User.OwnerType == 3) TDP = User.ClientID;

            #region 设定查询条件
            IList<CM_Client> clients = CM_ClientBLL.GetRetailerListBySalesMan(TDP, User.StaffID);
            FindKey = FindKey.Trim();
            if (FindKey != "")
                clients = clients.Where(p => p.FullName.Contains(FindKey) || p.ShortName.Contains(FindKey) || p.Address.Contains(FindKey)).ToList();
            #endregion

            Counts = clients.Count;

            #region 分页返回
            if (PageSize == 0) PageSize = 99999;
            List<ClientInfo> list = new List<ClientInfo>();
            int fromindex = PageSize * PageIndex;
            int endindex = PageSize * (PageIndex + 1);

            if (fromindex < clients.Count)
            {
                for (int i = fromindex; (i < endindex && i < clients.Count); i++)
                {
                    ClientInfo c = new ClientInfo(clients[i], TDP);
                    list.Add(c);
                }
            }
            return list;
            #endregion
        }
        /// <summary>
        /// 新增门店资料
        /// </summary>
        /// <param name="User">用户</param>
        /// <param name="TDP"></param>
        /// <param name="ClientJson">门店资料Json</param>
        /// <returns>大于0:新增的门店ID -1:Json字符串无法解析 -2:所属经销商无效 -3:更新门店基本资料失败</returns>
        public static int RetailerUpdate(UserInfo User, int TDP, ClientInfo Client, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.RetailerUpdate:UserName="******",TDP=" + TDP.ToString()
                + ",ClientInfo=" + JsonConvert.SerializeObject(Client));
            if (User.OwnerType == 3) TDP = User.ClientID;

            try
            {
                ClientInfo c = Client;
                if (c == null) { ErrorInfo = "Json字符串无法解析"; return -1; }        //Json字符串无法解析

                CM_Client TDPClient = new CM_ClientBLL(TDP).Model;
                if (TDPClient == null) { ErrorInfo = "所属经销商无效"; return -2; }   //所属经销商无效

                #region 判断必填与重复检测
                if (c.FullName == "") { ErrorInfo = "门店名称必填"; return -10; }
                if (c.Mobile == "") { ErrorInfo = "联系电话必填"; return -10; }
                if (c.Address == "") { ErrorInfo = "门店地址必填"; return -10; }

                if (c.Mobile.StartsWith("01")) c.Mobile = c.Mobile.Substring(1);
                if (c.TeleNum.Contains("-")) c.TeleNum = c.TeleNum.Replace("-", "");
                if (c.Mobile.Contains("-")) c.Mobile = c.Mobile.Replace("-", "");

                //检查门店资料重复
                int rptclientid = CM_ClientBLL.CheckRepeat(TDP, Client.ID, c.Mobile, c.TeleNum, c.FullName, c.Address);
                if (rptclientid > 0)
                {
                    CM_Client _rptclient = new CM_ClientBLL(rptclientid).Model;
                    ErrorInfo = "门店资料与[" + _rptclient.FullName + "]资料重复,请勿重复新增!";
                    return -11;
                }
                #endregion

                CM_ClientBLL bll = new CM_ClientBLL(c.ID);

                #region 门店基本资料
                bll.Model.Code = c.Code;
                bll.Model.FullName = c.FullName != "" ? c.FullName : c.ShortName;
                bll.Model.ShortName = c.ShortName == "" ? c.FullName : c.ShortName;

                if (c.OfficialCity > 0)
                    bll.Model.OfficialCity = c.OfficialCity;
                else
                    bll.Model.OfficialCity = TDPClient.OfficialCity;

                if (bll.Model.DeliveryAddress == bll.Model.Address)
                    bll.Model.DeliveryAddress = c.Address;
                bll.Model.Address = c.Address;

                bll.Model.LinkManName = c.LinkManName;
                bll.Model.TeleNum = c.TeleNum == "" ? c.Mobile : c.TeleNum;
                if (c.Mobile.StartsWith("1") && c.Mobile.Length == 11) bll.Model.Mobile = c.Mobile;
                bll.Model.UpdateStaff = User.StaffID;
                #endregion

                int retailerid = bll.Update();
                if (retailerid < 0) return -3;      //更新门店基本资料失败

                #region 设置供货商信息
                CM_ClientSupplierInfo supplierinfo = bll.GetSupplierInfo(TDP);
                supplierinfo.Code = c.CodeBySupplier;
                supplierinfo.StandardPrice = c.StandardPrice;
                supplierinfo.TDPChannel = c.TDPChannel;
                supplierinfo.TDPSalesArea = c.TDPSalesArea;
                supplierinfo.VisitRoute = c.VisitRoute;
                supplierinfo.VisitSequence = c.VisitSequence;
                supplierinfo.VisitTemplate = c.VisitTemplate;
                supplierinfo.UpdateStaff = User.StaffID;
                bll.SetSupplierInfo(supplierinfo);
                #endregion

                #region 设置厂商管理信息
                #endregion

                return 0;
            }
            catch (System.Exception err)
            {
                LogWriter.WriteLog("PBMIFService.RetailerAdd Exception Error!", err);
                return -100;
            }
        }
        /// <summary>
        /// 获取附近门店列表
        /// </summary>
        /// <param name="User"></param>
        /// <param name="TDP"></param>
        /// <param name="FindKey">门店关键字</param>
        /// <param name="Latitude">纬度</param>
        /// <param name="Longitude">经度</param>
        /// <param name="MaxDistance">最大距离范围(米)</param>
        /// <param name="PageSize">分页大小</param>
        /// <param name="PageIndex">页码</param>
        /// <param name="Counts">输出:总记录数</param>
        /// <returns></returns>
        public static List<ClientInfo> GetNearRetailerList(UserInfo User, int TDP, string FindKey, decimal Latitude, decimal Longitude, int MaxDistance, int PageSize, int PageIndex, out int Counts)
        {
            Counts = 0;
            LogWriter.WriteLog("PBMIFService.GetNearRetailerList:UserName="******",TDP=" + TDP.ToString()
                + ",FindKey=" + FindKey + ",Latitude=" + Latitude.ToString() + ",Longitude=" + Longitude.ToString() + ",MaxDistance=" + MaxDistance
                + ",PageSize=" + PageSize.ToString() + ",PageIndex=" + PageIndex.ToString());
            if (User.OwnerType == 3) TDP = User.ClientID;

            #region 设定查询条件
            IList<CM_Client> clients = CM_ClientBLL.GetRetailerListBySalesMan(TDP, User.StaffID);
            FindKey = FindKey.Trim();
            if (FindKey != "")
                clients = clients.Where(p => p.FullName.Contains(FindKey) || p.ShortName.Contains(FindKey) || p.Address.Contains(FindKey)).ToList();
            #endregion

            Counts = clients.Count;

            #region 如果有传入经、纬度,则计算距离,并排序
            Dictionary<int, int> dic_Distance = new Dictionary<int, int>(clients.Count);
            if (Latitude != 0 && Longitude != 0)
            {
                foreach (CM_Client item in clients)
                {
                    //计算距离
                    CM_ClientGeoInfo geo = CM_ClientGeoInfoBLL.GetGeoInfoByClient(item.ID);
                    if (geo != null)
                    {
                        int _distance = (int)GIS_OfficialCityGeoBLL.DistanceByLatLong((double)Latitude, (double)Longitude, (double)geo.Latitude, (double)geo.Longitude);
                        dic_Distance.Add(item.ID, _distance);
                    }
                    else
                    {
                        dic_Distance.Add(item.ID, int.MaxValue);
                    }
                }
                dic_Distance = dic_Distance.OrderBy(p => p.Value).ToDictionary(p => p.Key, p => p.Value);

                if (MaxDistance == 0) MaxDistance = int.MaxValue;

                clients = clients.Where(p => dic_Distance[p.ID] < MaxDistance).OrderBy(p => dic_Distance[p.ID]).ToList();
            }
            #endregion

            #region 分页返回
            if (PageSize == 0) PageSize = 99999;
            List<ClientInfo> list = new List<ClientInfo>();
            int fromindex = PageSize * PageIndex;
            int endindex = PageSize * (PageIndex + 1);

            if (fromindex < clients.Count)
            {
                for (int i = fromindex; (i < endindex && i < clients.Count); i++)
                {
                    ClientInfo c = new ClientInfo(clients[i], User.ClientID);
                    if (dic_Distance != null && dic_Distance.ContainsKey(c.ID)) c.Distance = dic_Distance[c.ID];
                    list.Add(c);
                }
            }
            return list;
            #endregion
        }
        /// <summary>
        /// 新增门店资料
        /// </summary>
        /// <param name="User">用户</param>
        /// <param name="TDP"></param>
        /// <param name="ClientJson">门店资料Json</param>
        /// <returns>大于0:新增的门店ID -1:Json字符串无法解析 -2:所属经销商无效 -3:新增门店基本资料失败</returns>
        public static int RetailerAdd(UserInfo User, int TDP, ClientInfo Client, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.RetailerAdd:UserName="******",TDP=" + TDP.ToString()
                + ",ClientInfo=" + JsonConvert.SerializeObject(Client));
            if (User.OwnerType == 3) TDP = User.ClientID;
            try
            {
                ClientInfo c = Client;
                if (c == null) { ErrorInfo = "Json字符串无法解析"; return -1; }        //Json字符串无法解析

                CM_Client TDPClient = new CM_ClientBLL(TDP).Model;
                if (TDPClient == null) { ErrorInfo = "所属经销商无效"; return -2; }   //所属经销商无效

                #region 判断必填与重复检测
                if (c.FullName == "") { ErrorInfo = "门店名称必填"; return -10; }
                if (c.Mobile == "") { ErrorInfo = "联系电话必填"; return -10; }
                if (c.Address == "") { ErrorInfo = "门店地址必填"; return -10; }

                if (c.Mobile.StartsWith("01")) c.Mobile = c.Mobile.Substring(1);
                if (c.TeleNum.Contains("-")) c.TeleNum = c.TeleNum.Replace("-", "");
                if (c.Mobile.Contains("-")) c.Mobile = c.Mobile.Replace("-", "");

                //检查门店资料重复
                int rptclientid = CM_ClientBLL.CheckRepeat(TDP, 0, c.Mobile, c.TeleNum, c.FullName, c.Address);
                if (rptclientid > 0)
                {
                    CM_Client _rptclient = new CM_ClientBLL(rptclientid).Model;
                    ErrorInfo = "门店资料与[" + _rptclient.FullName + "]资料重复,请勿重复新增!";
                    return -11;
                }
                #endregion

                CM_ClientBLL bll = new CM_ClientBLL();

                #region 门店基本资料
                bll.Model.Code = c.Code;
                bll.Model.FullName = c.FullName != "" ? c.FullName : c.ShortName;
                bll.Model.ShortName = c.ShortName == "" ? c.FullName : c.ShortName;

                if (c.OfficialCity > 0)
                    bll.Model.OfficialCity = c.OfficialCity;
                else
                    bll.Model.OfficialCity = TDPClient.OfficialCity;

                bll.Model.Address = c.Address;
                bll.Model.DeliveryAddress = c.Address;
                bll.Model.LinkManName = c.LinkManName;
                bll.Model.TeleNum = c.TeleNum == "" ? c.Mobile : c.TeleNum;
                if (c.Mobile.StartsWith("1") && c.Mobile.Length == 11) bll.Model.Mobile = c.Mobile;

                bll.Model.OpenTime = DateTime.Parse("1900-01-01 08:00");
                bll.Model.CloseTime = DateTime.Parse("1900-01-01 20:00");
                bll.Model.ClientType = 3;
                bll.Model.InsertStaff = User.StaffID;
                bll.Model.OwnerType = 3;
                bll.Model.OwnerClient = TDPClient.ID;
                bll.Model.ApproveFlag = 2;      //默认为未审核
                #endregion

                int retailerid = bll.Add();
                if (retailerid < 0) { ErrorInfo = "新增门店基本资料失败"; return -3; }    //新增门店基本资料失败

                #region 设置供货商信息
                CM_ClientSupplierInfo supplierinfo = new CM_ClientSupplierInfo();
                supplierinfo.Code = c.CodeBySupplier;
                supplierinfo.Client = retailerid;
                supplierinfo.Supplier = TDP;
                if (User.OwnerType == 3)
                {
                    supplierinfo.Salesman = User.StaffID;
                    IList<VST_Route> routes = VST_RouteBLL.GetRouteListByStaff(User.StaffID);
                    if (routes.Count > 0) supplierinfo.VisitRoute = routes[0].ID;
                }
                supplierinfo.StandardPrice = c.StandardPrice;
                supplierinfo.TDPChannel = c.TDPChannel;
                supplierinfo.TDPSalesArea = c.TDPSalesArea;

                supplierinfo.VisitSequence = c.VisitSequence;
                supplierinfo.VisitTemplate = c.VisitTemplate;
                supplierinfo.InsertStaff = User.StaffID;
                bll.SetSupplierInfo(supplierinfo);
                #endregion

                #region 设置厂商管理信息
                CM_ClientManufactInfo manufactinfo = new CM_ClientManufactInfo();
                manufactinfo.Client = retailerid;
                manufactinfo.Code = c.CodeByManufaturer;
                manufactinfo.Manufacturer = TDPClient.OwnerClient;      //门店归属厂商默认为经销商资料的所属厂商

                if (User.OwnerType == 2)
                {
                    manufactinfo.ClientManager = User.StaffID;

                    IList<VST_Route> routes = VST_RouteBLL.GetRouteListByStaff(User.StaffID);
                    if (routes.Count > 0) manufactinfo.VisitRoute = routes[0].ID;

                }

                //门店所属区域为经销商对应区域
                CM_ClientBLL s = new CM_ClientBLL(TDP);
                manufactinfo.OrganizeCity = s.GetManufactInfo().OrganizeCity;

                bll.SetManufactInfo(manufactinfo);
                #endregion

                #region 保存

                #endregion
                return retailerid;
            }
            catch (System.Exception err)
            {
                LogWriter.WriteLog("PBMIFService.RetailerAdd Exception Error!", err);
                return -100;
            }
        }