首页 > 其他分享 >BaseDal.cs

BaseDal.cs

时间:2023-05-04 10:45:04浏览次数:35  
标签:return SqlSugarHelper Db BaseDal public cs IsDelete Id

using PT.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PT.DAL
{
    public class BaseDal<T, V> where T : class, new() where V : class, new()
    {

        #region 通用公共方法

        #region 查询

        /// <summary>
        /// 查询表所有数据
        /// </summary>
        /// <returns></returns>
        public virtual List<T> Queryable()
        {
            return SqlSugarHelper.Db.Queryable<T>().ToList();
        }


        /// <summary>
        /// 查询表未删除数据
        /// </summary>
        /// <returns></returns>
        public virtual List<T> QueryableNotDeleted()
        {
            return SqlSugarHelper.Db.Queryable<T>().Where("IsDelete=@IsDelete", new { IsDelete = 0 }).OrderBy("Id desc").ToList();
        }


        /// <summary>
        /// 查询表未删除数据  按条件查询
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual List<T> QueryableNotDeleted(T t)
        {
            return SqlSugarHelper.Db.Queryable<T>().Where("IsDelete=@IsDelete", new { IsDelete = 0 }).OrderBy("Id desc").ToList();
        }



        /// <summary>
        /// 单主键查询
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public virtual T QueryableInSingle(string Id)
        {
            //单主键查询
            return SqlSugarHelper.Db.Queryable<T>().InSingle(Id);
        }


        /// <summary>
        /// 查询单条  没有返回Null,如果结果大于1条会抛出错误 (根据业务重写)
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual UserList QueryableSingle(UserList t)
        {
            ////没有返回Null,如果结果大于1条会抛出错误
            return SqlSugarHelper.Db.Queryable<UserList>().Single(p => p.LoginID == t.LoginID && p.LoginPwd == t.LoginPwd);
        }


        /// <summary>
        /// 是否存在记录
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual bool IsExist(T t)
        {
            string whereString = "name=@name";
            object parameters = new { name = "xx" };
            return SqlSugarHelper.Db.Queryable<T>().Where(whereString, parameters).Any();
        }

        #endregion

        #region 插入



        /// <summary>
        /// 插入 忽略null列
        /// </summary>
        /// <param name="ts"></param>
        /// <returns>返回自增id</returns>
        public virtual int InsertableIgnoreColumns(List<T> ts)
        {
            int n = 0;
            int Index = 0;
            //ignoreNullColumn 不支持批量操作   需要打包提交
            foreach (var item in ts)
            {
                SqlSugarHelper.Db.Insertable(item).IgnoreColumns(ignoreNullColumn: true).AddQueue();

                if (Index % 200 == 0 && Index > 1)
                {
                    n += SqlSugarHelper.Db.SaveQueues();
                }

                Index += 1;
            }
            n += SqlSugarHelper.Db.SaveQueues();
            return n;
        }

        #endregion

        #region 更新


        /// <summary>
        /// 更新 根据主键
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual int Updateable(T t)
        {

            return SqlSugarHelper.Db.Updateable(t).ExecuteCommand();
        }



        /// <summary>
        /// 批量更新 根据主键 忽略null列
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public virtual int UpdateableIgnoreColumns(List<T> ts)
        {
            int n = 0;
            int Index = 0;
            //ignoreNullColumn 不支持批量操作   需要打包提交
            foreach (var item in ts)
            {
                SqlSugarHelper.Db.Updateable(item).IgnoreColumns(ignoreAllNullColumns: true).AddQueue();
                if (Index % 200 == 0 && Index > 1)
                {
                    n += SqlSugarHelper.Db.SaveQueues();
                }

                Index += 1;
            }
            n += SqlSugarHelper.Db.SaveQueues();
            return n;

        }


        /// <summary>
        /// 更新 根据自定义条件 忽略null列  (根据业务重写)
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual int UpdateableIgnoreColumnsWhere(T t)
        {

            //return SqlSugarHelper.Db.Updateable(t).Where(p => p.xx == t.xx && p.IsDelete == false).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
            return 0;


        }


        /// <summary>
        /// 主键等于默认值插入否则更新  id=0插入 id不等于0更新
        /// </summary>
        /// <param name="list"></param>
        /// <returns>返回影响行数</returns>
        public virtual int InsertableDefaultAddElseUpdate(List<T> list)
        {

            //会更新所有字段 有的字段不需要更新的设置不了
            return SqlSugarHelper.Db.Storageable(list).DefaultAddElseUpdate().ExecuteCommand();
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除 根据主键
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual int Deleteable(T t)
        {

            return SqlSugarHelper.Db.Deleteable<T>().Where(t).ExecuteCommand();
        }



        /// <summary>
        /// 逻辑删除 根据主键
        /// </summary>
        /// <param name="ModifyBy">修改人</param>
        /// <param name="Ids">删除id集合</param>
        /// <returns></returns>
        public virtual int DeleteableLogic(string ModifyBy, List<int> Ids)
        {

            return SqlSugarHelper.Db.Deleteable<T>().In(Ids)
    .IsLogic()
    .ExecuteCommand("IsDelete", 1, "ModifyDate", "ModifyBy", ModifyBy);
        }

        #endregion

        #endregion

        #region 业务查询,根据业务需要重写方法



        /// <summary>
        /// 视图 多表联查 查询表未删除数据 (根据业务重写)
        /// </summary>
        /// <returns></returns>
        public virtual List<UserListV> QueryableNotDeletedV()
        {
            //Type t = typeof(T);

            var list = SqlSugarHelper.Db.Queryable<UserList>()
               .LeftJoin<GroupList>((a, b) => (a.GroupId == b.Id && b.IsDelete == false))
               .Where((a, b) => a.IsDelete == false)
               .OrderBy(a => a.Id, OrderByType.Desc)
               .Select((a, b) => new UserListV()
               {
                   Id = a.Id,
               },
               true)//true表示 其余字段自动映射,根据字段名字
              .ToList();
            return list;
        }


        /// <summary>
        /// 视图 多表联查 查询表未删除数据 按条件查询 (根据业务重写)
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public virtual List<IPListV> QueryableNotDeletedV(IPListV v)
        {
            //Type t = typeof(T);

            var exp = Expressionable.Create<IPList, PurposeList, UserList>()
                .And((a, b, c) => a.IsDelete == false)
             .AndIF(!string.IsNullOrWhiteSpace(v.Floor), (a, b, c) => a.Floor == v.Floor)
             .AndIF(!string.IsNullOrWhiteSpace(v.UserName), (a, b, c) => a.UserName.Contains(v.UserName))
             .AndIF(!string.IsNullOrWhiteSpace(v.AssgnName), (a, b, c) => a.ModifyBy.Contains(v.AssgnName))
             .AndIF(!string.IsNullOrWhiteSpace(v.IP), (a, b, c) => a.IP.Contains(v.IP))
             .ToExpression();//注意 这一句 不能少


            var list = SqlSugarHelper.Db.Queryable<IPList>()
   .LeftJoin<PurposeList>((a, b) => (a.PurposeId == b.Id && b.IsDelete == false))
   .LeftJoin<UserList>((a, b, c) => (a.Assgn == c.Id && c.IsDelete == false))
   .Where(exp)
   .OrderBy(a => a.Id, OrderByType.Desc)
   .Select((a, b, c) => new IPListV()
   {
       Id = a.Id,
       UserName = a.UserName,
       PurposeName = b.Name,
       AssgnName = c.UserName
   },
   true)//true表示 其余字段自动映射,根据字段名字
  .ToList();
            return list;
        }


        /// <summary>
        /// 视图 多表联查 分页查询 查询表未删除数据 按条件查询 (根据业务重写)
        /// </summary>
        /// <param name="where"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public virtual List<IPListV> QueryablePageList(IPListV where, int pageIndex, int pageSize, out int totalCount)
        {
            //Type t = typeof(T);

            totalCount = 0;

            var exp = Expressionable.Create<IPList, PurposeList, UserList>()
                .And((a, b, c) => a.IsDelete == false)
             .AndIF(!string.IsNullOrWhiteSpace(where.Floor), (a, b, c) => a.Floor == where.Floor)
             .AndIF(!string.IsNullOrWhiteSpace(where.UserName), (a, b, c) => a.UserName.Contains(where.UserName))
             .AndIF(!string.IsNullOrWhiteSpace(where.AssgnName), (a, b, c) => a.ModifyBy.Contains(where.AssgnName))
             .AndIF(!string.IsNullOrWhiteSpace(where.IP), (a, b, c) => a.IP.Contains(where.IP))
             .ToExpression();//注意 这一句 不能少


            var list = SqlSugarHelper.Db.Queryable<IPList>()
   .LeftJoin<PurposeList>((a, b) => (a.PurposeId == b.Id && b.IsDelete == false))
   .LeftJoin<UserList>((a, b, c) => (a.Assgn == c.Id && c.IsDelete == false))
   .Where(exp)
   .OrderBy(a => a.Id, OrderByType.Desc)
   .Select((a, b, c) => new IPListV()
   {
       Id = a.Id,
       UserName = a.UserName,
       PurposeName = b.Name,
       AssgnName = c.UserName
   },
   true)//true表示 其余字段自动映射,根据字段名字
  .ToPageList(pageIndex, pageSize, ref totalCount);
            return list;
        }

        #endregion

    }
}

 

标签:return,SqlSugarHelper,Db,BaseDal,public,cs,IsDelete,Id
From: https://www.cnblogs.com/zyx321/p/17370378.html

相关文章

  • SqlSugarHelper.cs
    usingMicrosoft.Extensions.Configuration;usingMicrosoft.IdentityModel.Protocols;usingSqlSugar;usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.IO;namespacePT.DAL{publicclassSqlSugarHelper{......
  • vue学习 第十一天 CSS3新特性 ---- 新增选择器(1、属性选择器 2、结构伪类选择
    CSS3新特性1、CSS3现状1)新增的CSS3特性有兼容性问题,ie9+才支持2)移动端支持优于PC端3.)不断改进中,应用相对广泛 2、CSS3新增选择器CSS3给我们新增了选择器,可以更加便捷,更加自由的选择目标元素。1)属性选......
  • vue学习 第十天(1) css高级技巧 ----CSS用户界面样式 / vertical-align属性应用
    用户界面样式 1)鼠标样式cursorli{cursor:pointer;}设置或检索在对象上移动的鼠标指针采用何种系统预定义的光标形状。 2、轮廓线outline给表单添加outline:0;或者outline:none;样式之后,就可以去掉默认的蓝色边......
  • vue学习 第九天(2) css高级技巧. ---- 精灵图 / 字体图片 / CSS三角
    学习目标:1)精灵图2)字体图标3)CSS三角4)CSS用户界面样式5)vertical-align属性应用6)溢出的文字省略号显示7)常见的布局技巧 1、精灵图1)需要精灵图的原因  (减少网页多张小图片的多次服务器请求)网页中有很多小的背景图,过多的图片。......
  • Windows环境安装Elasticsearch和Kibana
    目录1Elasticsearch1.1下载1.2解压并添加环境变量1.3访问1.4cmd命令1.5中文分词器1.5.1下载1.5.2安装1.5.2.1命令安装1.5.2.2手动安装1.5.2.3验证分词1.6使用curl批量导入2安装kibana2.1下载kibana2.2中文界面2.3操作索引2.3.1增加索引2.3.1.1单条新增2.3.1.2......
  • CsvHelper简单使用
    发现一个比较好用的处理csv的C#库,CsvHelper:CsvHelper是一个用于读取和写入CSV文件的C#库,支持自动类型转换、自定义类型转换器和灵活的映射选项等功能,使得读写CSV文件变得非常方便。安装:Install-PackageCsvHelper读取csv使用CsvHelper读取CSV文件非常简单。首先,您需要创建......
  • multi-label问题的不同metrics评估指标对比
    【草稿】其中阴影方框代表分子,白色空白方框+阴影方框代表分子 其中Jaccard和F1比较容易出错。分析sklearn的jaccard_score如下:'''jaccard测试'''fromsklearn.metricsimportjaccard_score,f1_score,hamming_loss,accuracy_scoreimportnumpyasnpy_true=np.......
  • CS520 计算机架构
    CS520ComputerArchitectureProject4–Spring2023Duedate:5/8/20231.RULES(1)Youareallowedtoworkinagroupofuptotwostudentspergroup,wherebothmembersmusthaveanimportantroleinmakingsureallmembersareworkingtogether.Besides,you......
  • Docker 构建 elasticsearch + kibana
    elasticsearch环境准备修改系统配置cat>>/etc/sysctl.conf<<-'EOF'net.ipv4.ip_forward=1vm.max_map_count=655360EOF重新加载配置sysctl-p&&systemctlrestartnetwork下载ik分词器本次部署使用的是7.10.1版本,可在此处直接下载,其它版本请在github自行下载。将......
  • 数字asic流程实验(EX)VCS+Verdi前仿真&后仿真
    数字asic流程实验(EX)VCS+Verdi前仿真&后仿真1.前言写数字asic流程实验系列博客已经过去一年多了,现在也算结束了纯小白的状态,稍微有了一些数字前端开发经验。在老的系列教程里面用的前仿后仿工具还是modelsim,实际上业界主流工具还是功能更强大的VCS和Verdi。两个也都是synopsys家......