/// <summary>
 /// 我的客户信息
 /// </summary>
 /// <returns></returns>
 public ActionResult Index()
 {
     var model = new CustomerCompanySearchViewModel();
     ViewBag.Data_CustomerCompany_CustomerCateID = Utilities.GetSelectListData(
       CustomerCateService.GetALL()
       .Where(x => x.ID != 6),
       x => x.ID,
       x => x.CateName,
       true, true);
     return View(model);
 }
        /// <summary>
        /// 客户信息查询处理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private IQueryable<CustomerCompany> GetSearch(CustomerCompanySearchViewModel model)
        {
            var query = CustomerCompanyService.GetALL()
            .Include(x => x.AddMember)
            .Include(x => x.CustomerCate)
            .Include(x => x.Customer);

            if (!string.IsNullOrEmpty(model.CustomerCompany_SearchName))
            {
                query = query.Where(x => x.Name.Contains(model.CustomerCompany_SearchName));
            }
            if (!string.IsNullOrEmpty(model.CustomerCompany_BrandName))
            {
                query = query.Where(x => x.BrandName.Contains(model.CustomerCompany_BrandName));
            }
            if (!string.IsNullOrEmpty(model.CustomerCompany_Customer))
            {
                query = query.Where(x => x.Customer.Any(c => c.Name.Contains(model.CustomerCompany_Customer)));
            }
            if (!string.IsNullOrEmpty(model.CustomerCompany_UserName))
            {
                query = query.Where(x => x.AddMember.NickName.Contains(model.CustomerCompany_UserName));
            }
            if (model.CustomerCompany_CustomerCateID != 0)
            {
                query = query.Where(x => x.CustomerCateID == model.CustomerCompany_CustomerCateID);
            }
            if (!string.IsNullOrEmpty(model.CustomerCompany_Mobile))
            {
                query = query.Where(x =>
                    x.Customer.Any(c =>
                        c.Mobile.Contains(model.CustomerCompany_Mobile)
                        || c.Mobile1.Contains(model.CustomerCompany_Mobile)));
            }

            if (!string.IsNullOrEmpty(model.CustomerCompany_Phone))
            {
                query = query.Where(x =>
                   x.Customer.Any(c =>
                       c.Phone.Contains(model.CustomerCompany_Phone)) || x.Phone.Contains(model.CustomerCompany_Phone));

            }

            if (!string.IsNullOrEmpty(model.CustomerCompany_QQ))
            {
                query = query.Where(x => x.Customer.Any(c => c.QQ.Contains(model.CustomerCompany_QQ)));
            }

            if (!string.IsNullOrEmpty(model.CustomerCompany_Address))
            {
                query = query.Where(x =>
                    x.Customer.Any(c =>
                        c.Address.Contains(model.CustomerCompany_Address))
                        || x.Address.Contains(model.CustomerCompany_Address));
            }
            if (!string.IsNullOrEmpty(model.CustomerCompany_Fax))
            {
                query = query.Where(x => x.Fax.Contains(model.CustomerCompany_Fax));
            }

            //if (CookieHelper.CheckPermission("boss"))
            //{

            //}
            //else if (CookieHelper.CheckPermission("manager"))
            //{
            //    var memberIds = MemberService.GetMemberIDs(CookieHelper.GetDepartmentID());
            //    query = query.Where(x => memberIds.Contains(x.AddUser));
            //}
            //else
            //{
            //    query = query.Where(x => x.AddUser == CookieHelper.MemberID);
            //}
            if (model.CustomerCompany_IsInTime)
            {
                query = query.Where(x => x.AddTime < model.CustomerCompany_EndTime
                 && x.AddTime > model.CustomerCompany_StartTime);
            }
            query = query.Where(x => x.Status > (int)CustomerCompanyStatus.Delete
                && x.CustomerCateID != 6
                ).OrderByDescending(x => x.AddTime);
            return query;
        }
        /// <summary>
        /// 我的客户信息读取
        /// </summary>
        /// <param name="model"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public ActionResult getall(CustomerCompanySearchViewModel model, int page = 1, int rows = 10)
        {
            var query = GetSearch(model);

            var count = query.Count();

            var data = query.Select(x => new CustomerCompanyListViewModel()
            {
                ID = x.ID,

                Name = x.Name,

                BrandName = x.BrandName,

                CityID = x.CityID,

                CityValue = x.CityValue,

                IndustryID = x.IndustryID,

                IndustryValue = x.IndustryValue,

                CustomerCateID = x.CustomerCateID,

                CustomerCateName = x.CustomerCate.CateName,

                CoopCateID = x.CoopCateID,

                SourceCateID = x.SourceCateID,

                Fax = x.Fax,

                Phone = x.Phone,

                Address = x.Address,

                Description = x.Description,

                IsCommon = x.IsCommon,

                AddTime = x.AddTime,

                LastTime = x.LastTime,

                Status = x.Status,

                AddUser = x.AddUser,

                AddUserName = x.AddMember.NickName,

                LastUser = x.LastUser,

                Visits = x.Visits,

                Finance = x.Finance,

                FinancePhone = x.FinancePhone,

                ProxyName = x.ProxyName,

                ProxyAddress = x.ProxyAddress,

                ProxyPhone = x.ProxyPhone,

                RelationID = x.RelationID,

            })
            .OrderBy(x => x.ID)
            .Skip((page - 1) * rows)
            .Take(rows).ToList();

            foreach (var item in data)
            {
                item.CityValue = CityCateService.GetCascadingName(item.CityValue);
                item.IndustryValue = CityCateService.GetCascadingName(item.IndustryValue);
            }

            var obj = new
            {
                rows = data,
                total = count
            };
            return Json(obj);
        }