BaseService底层

143 阅读1分钟
public  class BaseService<T> where T:BaseEntity
{

    protected Model DB { get; set; }

    public BaseService(Model db) {
        DB = db;
    }

    #region 添加

    public long Add(T model) {
        DB.Entry<T>(model).State = EntityState.Added;
        DB.SaveChanges();
        return model.Id;
    }
    public bool AddBool(T model)
    {
        DB.Entry<T>(model).State = EntityState.Added;  
        return DB.SaveChanges()>0;
    }

    #endregion

    #region 删除

    /// <summary>
    /// 删除(软删除)
    /// </summary>
    /// <param name="model">实体对象</param>
    /// <returns></returns>
    public bool Delete(T model) {
        DB.Set<T>().Attach(model);//根据Id附加保证数据库有这条数据
        model.IsDeleted = true;
        DB.Entry<T>(model).State = EntityState.Modified;
        return DB.SaveChanges() > 0;
    }

    public bool Delete(long id)
    {
        var t = DB.Set<T>().Find(id);
        if (t == null || t.IsDeleted)
        {
            return true;
        }
        t.IsDeleted = true;
        return DB.SaveChanges() > 0;
    }

    #endregion

    #region 修改
    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model">实体对象</param>
    /// <returns></returns>
    public bool Update(T model) {
        DB.Set<T>().Attach(model);//根据Id附加保证数据库有这条数据
        DB.Entry<T>(model).State = EntityState.Modified;
        return DB.SaveChanges() > 0;
    }
    #endregion

    #region 查询单个数据
    /// <summary>
    /// 查询单个数据
    /// </summary>
    /// <param name="whereLambda"></param>
    /// <returns></returns>
    public T Get(Expression<Func<T, bool>> whereLambda) {
       return DB.Set<T>().FirstOrDefault(whereLambda);
    }
    #endregion

    #region 查询列表
    public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda) {
        return DB.Set<T>().Where(p => p.IsDeleted == false).Where(whereLambda);
    }

    /// <summary>
    /// 查询排序
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <param name="whereLambda">查询条件</param>
    /// <param name="OrderLamda">排序条件</param>
    /// <param name="isASC">是否正序</param>
    /// <returns>排序后的列表</returns>
    public IQueryable<T> GetList<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> OrderLamda,bool isASC=true)
    {
        var list= GetList(whereLambda);
        if (isASC == true)
        {
            return list.OrderBy(OrderLamda);
        }
        else {
            return list.OrderByDescending(OrderLamda);
        }
    }

    /// <summary>
    /// 查询分页
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <param name="start">查询的开始位置</param>
    /// <param name="pageSize">分页大小</param>
    /// <param name="rowCount">查询结果总行数</param>
    /// <param name="whereLambda">排序条件</param>
    /// <param name="orderBy">是否正序</param>
    /// <param name="isAsc"></param>
    /// <returns></returns>
    public IQueryable<T> GetPagedList<TKey>(int start, int pageSize, ref long rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
    {
        var list = GetList(whereLambda, orderBy, isAsc);
        rowCount = list.LongCount();
        return list.Skip(start).Take(pageSize);
    }
    #endregion
}