//这个是分页数据和总页数类

public class SummaryBase<TModel>
{
public SummaryBase();

public IList<TModel> Collection { get; set; }
public int TotalCount { get; set; }
}

//查询学校数据分页  BM.Criteria.SchoolDataCriteria 这个是条件类,根据自己的需求定义
public static SummaryBase<EF.SchoolData> QuerySchoolData(BM.Criteria.SchoolDataCriteria schoolDataCriteria)
{
string cityCode = schoolDataCriteria.CityCode;
using (EF.EstateCoreEntities context = DBContextFactory.Create<EF.EstateCoreEntities>(cityCode))
{
IQueryable<EF.SchoolData> estatequery = context.SchoolDatas.Where(d => d.IsActived == true);

#region 房源搜索条件
if (schoolDataCriteria != null)
{
if (!string.IsNullOrEmpty(schoolDataCriteria.FullName)) //全名称
{
estatequery = estatequery.Where(d => d.FullName.Contains(schoolDataCriteria.FullName));
}

if (!string.IsNullOrEmpty(schoolDataCriteria.ZoneCode)) //行政区划
{
estatequery = estatequery.Where(d => d.ZoneCode.Equals(schoolDataCriteria.ZoneCode));
}
if (schoolDataCriteria.SchoolType.HasValue) //学校类型
{
estatequery = estatequery.Where(d => d.SchoolType == schoolDataCriteria.SchoolType.Value);
}
if (schoolDataCriteria.Nature.HasValue) //性质
{
estatequery = estatequery.Where(d => d.Nature == schoolDataCriteria.Nature.Value);
}
if (schoolDataCriteria.Grade.HasValue) //级别
{
estatequery = estatequery.Where(d => d.Grade == schoolDataCriteria.Grade.Value);
}
}
#endregion

//联合列表图片
var resultquery = estatequery;

//排序
resultquery = resultquery.OrderByDescending(s => s.CreatedDateTime);

//计算总页数
int totalcount = resultquery.Count();

//分页
if (schoolDataCriteria.PageSize.HasValue && schoolDataCriteria.PageSize.GetValueOrDefault(0) > 0)
{
//一页数量
int size = schoolDataCriteria.PageSize.HasValue ? schoolDataCriteria.PageSize.Value : 0;
//当前第几页
int pn = schoolDataCriteria.CurrentPage.HasValue ? schoolDataCriteria.CurrentPage.Value : 0;
if (size > 0 && pn > 1 && (size * (pn - 1) + 1 > totalcount))
schoolDataCriteria.CurrentPage = (totalcount - 1) / size + 1;
//如果选的页面有值且大于1,就把所选页面之前的数据从list中排除
if (schoolDataCriteria.CurrentPage.HasValue && schoolDataCriteria.CurrentPage.GetValueOrDefault(0) > 1)
{
int skipCount = schoolDataCriteria.CurrentPage.Value - 1;
resultquery = resultquery.Skip(skipCount * schoolDataCriteria.PageSize.Value).Take(schoolDataCriteria.PageSize.Value);
}
else
{
resultquery = resultquery.Take(schoolDataCriteria.PageSize.Value);
}
}

//数据转换
SummaryBase<EF.SchoolData> resultlist = new SummaryBase<EF.SchoolData>();
resultlist.Collection = resultquery.ToList();
resultlist.TotalCount = totalcount;

return resultlist;
}
}

/// <summary>
/// 修改学校信息
/// </summary>
/// <param name="siList"></param>
/// <param name="cityCode"></param>
/// <returns></returns>
public static bool EditSchoolData(EF.SchoolData sdItem, string cityCode)
{
using (EF.EstateCoreEntities context = DBContextFactory.Create<EF.EstateCoreEntities>(cityCode))
{
EF.SchoolData entity = context.SchoolDatas.Where(b => b.Id == sdItem.Id).FirstOrDefault();
if (entity == null)
{
return false;
}
entity.Name = sdItem.Name;
entity.FullName = sdItem.Name;
entity.SchoolType = sdItem.SchoolType;
entity.Nature = sdItem.Nature;
entity.Grade = sdItem.Grade;
entity.ZoneCode = sdItem.ZoneCode;
entity.SchoolAddress = sdItem.SchoolAddress;
entity.SchoolRemark = sdItem.SchoolRemark;
entity.AreaId = SubWaysManager.GetRegionIdByPoints((double)sdItem.Lat, (double)sdItem.Lng, cityCode);
entity.Lat = sdItem.Lat;
entity.Lng = sdItem.Lng;
entity.ModifiedDateTime = DateTime.Now;

return context.SaveChanges() > 0;
}

}

/// <summary>
/// 添加学校信息
/// </summary>
/// <param name="siList"></param>
/// <param name="cityCode"></param>
/// <returns></returns>
public static bool AddSchoolData(EF.SchoolData sdItem, string cityCode)
{
using (EF.EstateCoreEntities context = DBContextFactory.Create<EF.EstateCoreEntities>(cityCode))
{
EF.SchoolData entity = new SchoolData();

entity.Id = Guid.NewGuid();
entity.Name = sdItem.Name;
entity.FullName = sdItem.Name;
entity.SchoolType = sdItem.SchoolType;
entity.Nature = sdItem.Nature;
entity.Grade = sdItem.Grade;
entity.ZoneCode = sdItem.ZoneCode;
entity.SchoolAddress = sdItem.SchoolAddress;
entity.SchoolRemark = sdItem.SchoolRemark;
entity.AreaId = SubWaysManager.GetRegionIdByPoints((double)sdItem.Lat, (double)sdItem.Lng, cityCode);
entity.Lat = sdItem.Lat;
entity.Lng = sdItem.Lng;
entity.CreatedDateTime = DateTime.Now;

context.SchoolDatas.Add(entity);
return context.SaveChanges() > 0;
}
}

/// <summary>
/// 删除学校信息
/// </summary>
/// <param name="siList"></param>
/// <param name="cityCode"></param>
/// <returns></returns>
public static bool DelSchoolData(Guid sdId, string cityCode)
{
using (EF.EstateCoreEntities context = DBContextFactory.Create<EF.EstateCoreEntities>(cityCode))
{
EF.SchoolData entity = context.SchoolDatas.Where(b => b.Id == sdId).FirstOrDefault();
context.SchoolDatas.Remove(entity);
return context.SaveChanges() > 0;
}
}

04-09 12:24