温控系统-DAL数据层(2)BaseDAL和ProductDAL功能代码

ops/2024/10/18 14:17:18/

BaseDAL是数据层中非常重要的基类,业务DAL使用到其中一些共用方法;

ProductDAL.cs结构如下参考:

using Common;
using STMS.DbUtility;
using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace STMS.DAL
{public class ProductDAL:BaseDAL<ProductInfo>{/// <summary>/// 查询产品列表/// </summary>/// <param name="keywords"></param>/// <param name="isDeleted"></param>/// <returns></returns>public List<ProductInfo> GetProductList(string keywords,int isDeleted){string cols = CreateSql.GetColsString<ProductInfo>("IsDeleted");string strWhere = $"IsDeleted={isDeleted}";SqlParameter para = null;if(!string.IsNullOrEmpty(keywords)){strWhere += " and (ProductName like @keywords or ProductNo like @keywords)";para = new SqlParameter("@keywords", $"%{keywords}%");return GetModelList(strWhere, cols, para);}return GetModelList(strWhere, cols);}/// <summary>/// 获取指定的产品信息/// </summary>/// <param name="productId"></param>/// <returns></returns>public ProductInfo GetProductInfo(int productId){string cols= CreateSql.GetColsString<ProductInfo>("IsDeleted");return GetById(productId, cols);}/// <summary>/// 获取所有产品列表  绑定下拉框/// </summary>/// <returns></returns>public List<ProductInfo> GetAllProducts(){return  GetModelList("ProductId,ProductName", 0);}/// <summary>/// 获取指定产品的库存数/// </summary>/// <param name="productId"></param>/// <returns></returns>public int GetProductCount(int productId){string sql = "select sum(ProductCount) from ProductStoreInfos where ProductId=" + productId;object oCount= SqlHelper.ExecuteScalar(sql, 1);if (oCount != null && oCount.ToString() != "")return oCount.GetInt();return 0;}/// <summary>/// 检查编码或名称是否已存在/// </summary>/// <param name="proName"></param>/// <param name="proNo"></param>/// <returns></returns>public bool[] ExistsProduct(string proName, string proNo){bool blName = false;if (!string.IsNullOrEmpty(proName))blName = ExistsByName("ProductName", proName);bool blNo = false;if (!string.IsNullOrEmpty(proNo))blNo = ExistsByName("ProductNo", proNo);return new bool[] { blName, blNo };}/// <summary>/// 添加产品信息/// </summary>/// <param name="proInfo"></param>/// <param name="isGetId"></param>/// <returns></returns>public int AddProductInfo(ProductInfo proInfo,int isGetId){string cols = CreateSql.GetColsString<ProductInfo>("ProductId,IsDeleted");return Add(proInfo, cols, isGetId);}/// <summary>/// 修改产品信息/// </summary>/// <param name="proInfo"></param>/// <returns></returns>public bool UpdateProductInfo(ProductInfo proInfo){string cols = CreateSql.GetColsString<ProductInfo>("IsDeleted");return Update(proInfo, cols);}}
}

BaseDAL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Common;
using System.Data.SqlTypes;
using STMS.DbUtility;namespace STMS.DAL
{public class BaseDAL<T> : BQuery<T> where T : class{#region 添加/// <summary>/// 添加实体信息/// </summary>/// <param name="t"></param>/// <param name="strCols">插入列名字符串,若为空,则全插入</param>/// <returns></returns>public int Add(T t, string strCols, int isReturn){if (t == null)return 0;//获取生成的sql和参数列表  sql   Paras 参数数组 SqlParameter[] SqlModel insert = CreateSql.GetInsertSqlAndParas<T>(t, strCols, isReturn);//执行sql命令if (isReturn == 0)  //受影响  1  失败  0return SqlHelper.ExecuteNonQuery(insert.Sql, 1, insert.SqlParaArray);else{//sql   insert  select @@identity  主键值object oId = SqlHelper.ExecuteScalar(insert.Sql, 1, insert.SqlParaArray);if (oId != null && oId.ToString() != "")return oId.GetInt();elsereturn 0;}}/// <summary>/// 批量插入/// </summary>/// <param name="list"></param>/// <param name="strCols"></param>/// <returns></returns>public bool AddList(List<T> list, string strCols){if (list == null || list.Count == 0)return false;List<CommandInfo> comList = new List<CommandInfo>();foreach (T t in list){SqlModel insert = CreateSql.GetInsertSqlAndParas<T>(t, strCols, 0);//sql parasCommandInfo com = new CommandInfo(insert.Sql, false, insert.SqlParaArray);comList.Add(com);}return SqlHelper.ExecuteTrans(comList);}#endregion#region 修改/// <summary>/// 修改实体  以主键为条件定位/// </summary>/// <param name="t"></param>/// <param name="strCols">也包括Id列</param>/// <returns></returns>public bool Update(T t, string strCols){if (t == null)return false;elsereturn Update(t, strCols, "");}/// <summary>/// 修改信息实体/// </summary>/// <param name="t"></param>/// <param name="strCols">要修改的列  标识列名</param>/// <param name="strWhere">另外附加条件 </param>/// <returns></returns>public bool Update(T t, string strCols, string strWhere, params SqlParameter[] paras){if (t == null)return false;//获取生成的sql和参数列表SqlModel update = CreateSql.GetUpdateSqlAndParas<T>(t, strCols, strWhere);List<SqlParameter> listParas = update.SqlParaArray.ToList();if (paras != null && paras.Length > 0)listParas.AddRange(paras);//执行sql命令return SqlHelper.ExecuteNonQuery(update.Sql, 1, listParas.ToArray()) > 0;}/// <summary>/// 批量修改/// </summary>/// <param name="list"></param>/// <param name="strCols"></param>/// <returns></returns>public bool UpdateList(List<T> list, string strCols){if (list == null || list.Count == 0)return false;List<CommandInfo> comList = new List<CommandInfo>();foreach (T t in list){SqlModel update = CreateSql.GetUpdateSqlAndParas<T>(t, strCols, "");CommandInfo com = new CommandInfo(update.Sql, false, update.SqlParaArray);comList.Add(com);}return SqlHelper.ExecuteTrans(comList);}#endregion#region 删除/// <summary>/// 根据Id删除  这里id是主键 delType=1 真删除  0 假删除/// </summary>/// <param name="id"></param>/// <returns></returns>public bool Delete(int id, int delType, int isDeleted){Type type = typeof(T);string strWhere = $"[{type.GetPrimary()}]=@Id";SqlParameter[] paras ={new SqlParameter("@Id",id)};return Delete(delType, strWhere, isDeleted, paras);}/// <summary>/// 按条件删除数据(假删除,包含可以恢复)  真删除  ---Delete /// </summary>///  <param name="actType">删除类型 0 假  1 真</param>/// <param name="strWhere">条件</param>/// <param name="isDeleted">删除标识值  0 1  2</param>///  <param name="paras">参数列表</param>/// <returns></returns>public bool Delete(int actType, string strWhere, int isDeleted, SqlParameter[] paras){Type type = typeof(T);string delSql = "";//删除语句的生成if (actType == 1)delSql = CreateSql.CreateDeleteSql<T>(strWhere);elsedelSql = CreateSql.CreateLogicDeleteSql<T>(strWhere, isDeleted);List<CommandInfo> list = new List<CommandInfo>();//可能会批量的删除或修改  ----启用事务   ---一致性提交list.Add(new CommandInfo(){CommandText = delSql,IsProc = false,Paras = paras});return SqlHelper.ExecuteTrans(list);}/// <summary>/// 批量删除/// </summary>/// <param name="idList"></param>/// <returns></returns>public bool DeleteList(List<int> idList, int actType, int isDeleted){Type type = typeof(T);List<CommandInfo> comList = new List<CommandInfo>();foreach (int id in idList){string strWhere = $"[{type.GetPrimary()}]=@Id";string delSql = "";if (actType == 1)delSql = CreateSql.CreateDeleteSql<T>(strWhere);elsedelSql = CreateSql.CreateLogicDeleteSql<T>(strWhere, isDeleted);SqlParameter[] paras ={new SqlParameter("@Id",id)};CommandInfo com = new CommandInfo(delSql, false, paras);comList.Add(com);}return SqlHelper.ExecuteTrans(comList);}#endregion}
}


http://www.ppmy.cn/ops/52288.html

相关文章

vue 利用element的Table 表格实现自制的穿梭框(可以高度自定义)_vue 穿梭框

2. 其实可以从最后的效果图看出这个自制的穿梭框&#xff0c;只是由两个table表格和两个按钮组成&#xff0c;只需要写其中逻辑事件即可完成穿梭框的效果&#xff0c;其中的事件主要分为“选中”&#xff0c;“穿梭”和“删除”&#xff0c;其实也只是关于数组的增&#xff0c;…

汇编快速入门

一.基础知识 1.数据类型 DB&#xff08;Define Byte&#xff0c;字节类型 占位8位bit 1字节&#xff09; 范围&#xff1a;DB可以用来定义&#xff08;无符号、有符号&#xff09;整数&#xff08;包含二、十、十六进制&#xff09;和字符 语法&#xff1a;a DB 数据个数…

关于小程序内嵌H5页面交互的问题?

有木有遇到&#xff1f;有木有遇到。 小程序内嵌了H5&#xff0c;然后H5某个按钮&#xff0c;需要打开小程序某个页面进行信息完善或登记&#xff0c;登记后要返回H5页面&#xff0c;而H5页面要动态显示刚才在小程序页面登记的信息。 操作流程是这样&#xff1a; 方案1&#…

ssh在本地虚拟机中的应用——解决虚拟机中编写和阅读代码不方便问题的一个小技巧

虚拟机中编程小技巧分享——ssh的使用 事情的起因是这样的&#xff1a;前几天一位工程师过来我这边&#xff0c;他看到我在主机和虚拟机运行了两个vscode环境&#xff0c;不经意间提了句&#xff1a;“这么艰苦的环境写代码啊”。 后来我一想&#xff1a;确实。 我长时间以来…

GPT3.5的PPO目标函数怎么来的:From PPO to PPO-ptx

给定当前优化的大模型 π \pi π&#xff0c;以及SFT模型 π S F T \pi_{SFT} πSFT​ 原始优化目标为: max ⁡ E ( s , a ) ∼ R L [ π ( s , a ) π S F T ( s , a ) A π S F T ( s , a ) ] \max E_{(s,a)\sim RL}[\frac{\pi(s,a)}{\pi_{SFT}(s,a)}A^{\pi_{SFT}}(s,a)] m…

QML 中宽度、高度与隐式宽度/高度的区别及其应用场景

在 QML 中&#xff0c;width、height 与 implicitWidth、implicitHeight 这几个属性常常令开发者感到困惑。本文将详细介绍它们之间的区别&#xff0c;并说明在何种情况下应使用隐式尺寸以及普通尺寸。 基本定义 width 和 height&#xff1a;表示组件/item 的实际尺寸。impli…

鸿蒙HarmonyOS实战:渲染控制、路由案例

条件渲染 简单来说&#xff0c;就是动态控制组件的显示与隐藏&#xff0c;类似于vue中的v-if 但是这里写法就是用if、else、else if看起来更像是原生的感觉 效果 循环渲染 我们实际开发中&#xff0c;数据一般是后端返回来的对象格式&#xff0c;对此我们需要进行遍历&#…

Word2Vec基本实践

系列文章目录 提示&#xff1a;这里可以添加系列文章的所有文章的目录&#xff0c;目录需要自己手动添加 例如&#xff1a;第一章 Python 机器学习入门之pandas的使用 提示&#xff1a;写完文章后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目…