diff --git a/NX.ERP.Model/NX.ERP.Model.csproj b/NX.ERP.Model/NX.ERP.Model.csproj index c3c4118..03e3cc6 100644 --- a/NX.ERP.Model/NX.ERP.Model.csproj +++ b/NX.ERP.Model/NX.ERP.Model.csproj @@ -76,6 +76,7 @@ + diff --git a/NX.ERP.Model/Request/ProductOrderReportSn.cs b/NX.ERP.Model/Request/ProductOrderReportSn.cs new file mode 100644 index 0000000..50ddade --- /dev/null +++ b/NX.ERP.Model/Request/ProductOrderReportSn.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NX.ERP.Model.Request +{ + public class ProductOrderReportSn + { + /// + /// 生产单号 + /// + public string ProductOrder { get; set; } + + /// + /// 明细 + /// + public List OrderDetails { get; set; } + } + + + public class OrderDetail + { + /// + /// 明细行ID + /// + public string OrderLineId { get; set; } + + /// + /// 序列号列表 + /// + public List SnList { get; set; } + } +} diff --git a/NX.ERP.U8/MES/BaseLogic.cs b/NX.ERP.U8/MES/BaseLogic.cs new file mode 100644 index 0000000..95ec88b --- /dev/null +++ b/NX.ERP.U8/MES/BaseLogic.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NX.ERP.U8.MES +{ + public class BaseLogic: IDisposable + { + public static IDbHelper MesDb + { + get { return new MsSqlHelper("MES_SUBMIT"); } + } + + #region IDisposable Support + private bool disposedValue = false; // 要检测冗余调用 + + + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + // TODO: 释放托管状态(托管对象)。 + } + + // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。 + // TODO: 将大型字段设置为 null。 + + disposedValue = true; + } + } + + // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。 + // ~BaseLogic() { + // // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。 + // Dispose(false); + // } + + // 添加此代码以正确实现可处置模式。 + void IDisposable.Dispose() + { + // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。 + Dispose(true); + // TODO: 如果在以上内容中替代了终结器,则取消注释以下行。 + // GC.SuppressFinalize(this); + } + #endregion + } +} diff --git a/NX.ERP.U8/MES/MesLogic.cs b/NX.ERP.U8/MES/MesLogic.cs new file mode 100644 index 0000000..1822e8b --- /dev/null +++ b/NX.ERP.U8/MES/MesLogic.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NX.ERP.U8.MES +{ + public class MesLogic:BaseLogic + { + public bool ReportSnList(Model.Request.ProductOrderReportSn info) + { + var instertSql = "INSERT INTO [dbo].[MES_PRODUCT_SN_LIST] ([PRODUCT_ORDER_NO], [ORDER_LINE_ID], [SN]) VALUES (@PRODUCT_ORDER_NO, @ORDER_LINE_ID, @SN);"; + using (var conn = MesDb.GetSqlConnection()) + { + conn.Open(); + var tran = conn.BeginTransaction(); + var cmd = conn.CreateCommand(); + cmd.Transaction = tran; + foreach (var detail in info.OrderDetails) + { + foreach (var sn in detail.SnList) + { + cmd.Parameters.Clear(); + + cmd.CommandText = instertSql; + cmd.Parameters.Clear(); + cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PRODUCT_ORDER_NO", info.ProductOrder)); + cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ORDER_LINE_ID", detail.OrderLineId)); + cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SN", sn)); + var val = cmd.ExecuteNonQuery(); + if (val <= 0) + { + tran.Rollback(); + throw new Exception("保存失败"); + } + } + } + tran.Commit(); + cmd.Dispose(); + conn.Close(); + return true; + } + } + } +} diff --git a/NX.ERP.U8/Mom/MomOrderDAL.cs b/NX.ERP.U8/Mom/MomOrderDAL.cs index 77f0a9a..6e65d28 100644 --- a/NX.ERP.U8/Mom/MomOrderDAL.cs +++ b/NX.ERP.U8/Mom/MomOrderDAL.cs @@ -101,7 +101,7 @@ namespace NX.ERP.U8.Mom { where.Add(" b.InvCode like @ProductCode"); } - if (Params.ProductOrderNo!=null) + if (Params.ProductOrderNo != null) { where.Add(" a.moCode in @ProductOrderNo "); } @@ -132,7 +132,7 @@ namespace NX.ERP.U8.Mom return this.GetList(string.Join(" and ", where.ToArray()), new { ProductCode = $"%{Params.ProductCode}%", - ProductOrderNo =Params.ProductOrderNo, + ProductOrderNo = Params.ProductOrderNo, Params.ProductOrderendID, Params.ModifyDate, Params.Pubufts, @@ -160,7 +160,7 @@ namespace NX.ERP.U8.Mom { where.Add(" a.InvCode like @ProductCode"); } - if (Params.ProductOrderNo!=null) + if (Params.ProductOrderNo != null) { where.Add(" a.moCode in @ProductOrderNo "); } @@ -234,7 +234,7 @@ namespace NX.ERP.U8.Mom { where.Add(" b.InvCode like @ProductCode"); } - if (Params.ProductOrderNo!=null) + if (Params.ProductOrderNo != null) { where.Add(" b.moCode in @ProductOrderNo "); } @@ -245,7 +245,7 @@ namespace NX.ERP.U8.Mom return this.GetDetails(string.Join(" and ", where.ToArray()), new { ProductCode = $"%{Params.ProductCode}%", - ProductOrderNo =Params.ProductOrderNo, + ProductOrderNo = Params.ProductOrderNo, ProductOrderendID = $"{Params.ProductOrderendID}" }); } diff --git a/NX.ERP.U8/MsSqlHelper.cs b/NX.ERP.U8/MsSqlHelper.cs new file mode 100644 index 0000000..3d6ac11 --- /dev/null +++ b/NX.ERP.U8/MsSqlHelper.cs @@ -0,0 +1,1069 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; + +namespace NX.ERP.U8 +{ + public interface IDbHelper + { + + string GetConnectionString(); + + IDbConnection GetSqlConnection(); + /// + /// 执行 Transact-SQL 语句并返回受影响的行数。 + /// + int ExecuteNonQuery(string cmdText); + + /// + /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。 + /// + object ExecuteScalar(string cmdText); + + /// + /// 在事务中执行查询,返回DataSet + /// + DataSet ExecuteDataSet(string cmdText); + + DataTable ExecuteDataTable(string cmdText); + + DataRow ExecuteDataRow(string cmdText); + } + public sealed partial class MsSqlHelper: IDbHelper + { + /// + /// 批量操作每批次记录数 + /// + public static int BatchSize = 2000; + + /// + /// 超时时间 + /// + public static int CommandTimeOut = 600; + + /// + ///初始化SqlHelper实例 + /// + /// 数据库连接字符串 + public MsSqlHelper(string key) + { + this.ConnectionString = ConfigurationManager.ConnectionStrings[key].ConnectionString; + //var configHelper = new CfgCommon.ConfigHelper(); + //var config = configHelper.GetAppSettings("ConnectionStrings"); + //this.ConnectionString = config.MsSqlConnection; + } + + public string GetConnectionString() + { + return this.ConnectionString; + } + + /// + /// 数据库连接字符串 + /// + public string ConnectionString { get; set; } + + public IDbConnection GetSqlConnection() + { + return new SqlConnection(ConnectionString); + } + + #region 实例方法 + #region MyRegion + /// + /// 查询转换为List,仅可用实体类,不可用简单类型,如string int + /// + /// + /// + /// + public List QueryList(string sql) + { + IDataReader reader = this.ExecuteDataReader(sql); + //实例化一个List<>泛型集合 + List DataList = new List(); + while (reader.Read()) + { + T RowInstance = Activator.CreateInstance();//动态创建数据实体对象 + //通过反射取得对象所有的Property + foreach (PropertyInfo Property in typeof(T).GetProperties()) + { + try + { + //取得当前数据库字段的顺序 + int Ordinal = reader.GetOrdinal(Property.Name); + if (reader.GetValue(Ordinal) != DBNull.Value) + { + //将DataReader读取出来的数据填充到对象实体的属性里 + Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null); + } + } + catch (Exception ex) + { + //break; + continue; + } + } + DataList.Add(RowInstance); + } + return DataList; + } + #endregion + + #region ExecuteNonQuery + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// SQL语句 + /// 返回影响的行数 + public int ExecuteNonQuery(string commandText) + { + return ExecuteNonQuery(ConnectionString, CommandType.Text, commandText); + } + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// SQL语句 + /// 查询参数 + /// 返回影响的行数 + public int ExecuteNonQuery(string commandText, params SqlParameter[] parms) + { + return ExecuteNonQuery(ConnectionString, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回影响的行数 + public int ExecuteNonQuery(CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteNonQuery(ConnectionString, commandType, commandText, parms); + } + + #endregion ExecuteNonQuery + + #region ExecuteScalar + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// SQL语句 + /// 返回结果集中的第一行第一列 + public object ExecuteScalar(string commandText) + { + return ExecuteScalar(ConnectionString, CommandType.Text, commandText); + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 返回对象类型 + /// SQL语句 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public T ExecuteScalar(string commandText, params SqlParameter[] parms) + { + return ExecuteScalar(ConnectionString, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// SQL语句 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public object ExecuteScalar(string commandText, params SqlParameter[] parms) + { + return ExecuteScalar(ConnectionString, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public object ExecuteScalar(CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteScalar(ConnectionString, commandType, commandText, parms); + } + + #endregion ExecuteScalar + + #region ExecuteDataReader + + /// + /// 执行SQL语句,返回只读数据集 + /// + /// SQL语句 + /// 查询参数 + /// 返回只读数据集 + public SqlDataReader ExecuteDataReader(string commandText, params SqlParameter[] parms) + { + return ExecuteDataReader(ConnectionString, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回只读数据集 + /// + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回只读数据集 + public SqlDataReader ExecuteDataReader(CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataReader(ConnectionString, commandType, commandText, parms); + } + #endregion + + #region ExecuteDataRow + + /// + /// 执行SQL语句,返回结果集中的第一行 + /// + /// SQL语句或存储过程名称 + /// 返回结果集中的第一行 + public DataRow ExecuteDataRow(string commandText) + { + return ExecuteDataRow(ConnectionString, CommandType.Text, commandText); + } + + /// + /// 执行SQL语句,返回结果集中的第一行 + /// + /// SQL语句 + /// 查询参数 + /// 返回结果集中的第一行 + public DataRow ExecuteDataRow(string commandText, params SqlParameter[] parms) + { + return ExecuteDataRow(ConnectionString, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集中的第一行 + /// + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一行 + public DataRow ExecuteDataRow(CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataRow(ConnectionString, commandType, commandText, parms); + } + + #endregion ExecuteDataRow + + #region ExecuteDataTable + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// SQL语句 + /// 返回结果集中的第一个数据表 + public DataTable ExecuteDataTable(string commandText) + { + return ExecuteDataTable(ConnectionString, CommandType.Text, commandText); + } + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// SQL语句 + /// 查询参数 + /// 返回结果集中的第一个数据表 + public DataTable ExecuteDataTable(string commandText, params SqlParameter[] parms) + { + return ExecuteDataTable(ConnectionString, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一个数据表 + public DataTable ExecuteDataTable(CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(ConnectionString, commandType, commandText, parms).Tables[0]; + } + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// SQL语句 + /// 排序SQL,如"ORDER BY ID DESC" + /// 每页记录数 + /// 页索引 + /// 查询参数 + /// 查询SQL + /// + public DataTable ExecutePageDataTable(string sql, string order, int pageSize, int pageIndex, SqlParameter[] parms = null, string query = null, string cte = null) + { + return ExecutePageDataTable(sql, order, pageSize, pageIndex, parms, query, cte); + } + #endregion ExecuteDataTable + + #region ExecuteDataSet + + /// + /// 执行SQL语句,返回结果集 + /// + /// SQL语句 + /// 返回结果集 + public DataSet ExecuteDataSet(string commandText) + { + return ExecuteDataSet(ConnectionString, CommandType.Text, commandText); + } + + /// + /// 执行SQL语句,返回结果集 + /// + /// SQL语句 + /// 查询参数 + /// 返回结果集 + public DataSet ExecuteDataSet(string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(ConnectionString, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集 + /// + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集 + public DataSet ExecuteDataSet(CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(ConnectionString, commandType, commandText, parms); + } + + #endregion ExecuteDataSet + + #region 批量操作 + + /// + /// 大批量数据插入 + /// + /// 数据表 + public void BulkInsert(DataTable table) + { + BulkInsert(ConnectionString, table); + } + + /// + /// 使用MySqlDataAdapter批量更新数据 + /// + /// 数据表 + public void BatchUpdate(DataTable table) + { + BatchUpdate(ConnectionString, table); + } + + /// + /// 分批次批量删除数据 + /// + /// SQL语句 + /// 每批次删除记录行数 + /// 批次执行间隔(秒) + public void BatchDelete(string sql, int batchSize = 1000, int interval = 1) + { + BatchDelete(ConnectionString, sql, batchSize, interval); + } + + /// + /// 分批次批量更新数据 + /// + /// SQL语句 + /// 每批次更新记录行数 + /// 批次执行间隔(秒) + public void BatchUpdate(string sql, int batchSize = 1000, int interval = 1) + { + BatchUpdate(ConnectionString, sql, batchSize, interval); + } + + #endregion 批量操作 + + #endregion 实例方法 + + #region 静态方法 + + private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] parms) + { + if (connection.State != ConnectionState.Open) connection.Open(); + + command.Connection = connection; + command.CommandTimeout = CommandTimeOut; + // 设置命令文本(存储过程名或SQL语句) + command.CommandText = commandText; + // 分配事务 + if (transaction != null) + { + command.Transaction = transaction; + } + // 设置命令类型. + command.CommandType = commandType; + if (parms != null && parms.Length > 0) + { + //预处理SqlParameter参数数组,将为NULL的参数赋值为DBNull.Value; + foreach (SqlParameter parameter in parms) + { + if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) + { + parameter.Value = DBNull.Value; + } + } + command.Parameters.AddRange(parms); + } + } + + #region ExecuteNonQuery + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 查询参数 + /// 返回影响的行数 + public static int ExecuteNonQuery(string connectionString, string commandText, params SqlParameter[] parms) + { + using (SqlConnection connection = new SqlConnection(connectionString)) + { + return ExecuteNonQuery(connection, CommandType.Text, commandText, parms); + } + } + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// 数据库连接字符串 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回影响的行数 + public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] parms) + { + int result = 0; + using (SqlConnection connection = new SqlConnection(connectionString)) + { + result = ExecuteNonQuery(connection, commandType, commandText, parms); + if (connection.State == ConnectionState.Open) + { + connection.Close(); + } + } + return result; + } + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// 数据库连接 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回影响的行数 + public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteNonQuery(connection, null, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回影响的行数 + public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteNonQuery(transaction.Connection, transaction, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回影响的行数 + /// + /// 数据库连接 + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回影响的行数 + private static int ExecuteNonQuery(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + SqlCommand command = new SqlCommand(); + PrepareCommand(command, connection, transaction, commandType, commandText, parms); + int retval = command.ExecuteNonQuery(); + command.Parameters.Clear(); + return retval; + } + + #endregion ExecuteNonQuery + + #region ExecuteScalar + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 返回对象类型 + /// 数据库连接字符串 + /// SQL语句 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public static T ExecuteScalar(string connectionString, string commandText, params SqlParameter[] parms) + { + object result = ExecuteScalar(connectionString, commandText, parms); + if (result != null) + { + return (T)Convert.ChangeType(result, typeof(T)); ; + } + return default(T); + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public static object ExecuteScalar(string connectionString, string commandText, params SqlParameter[] parms) + { + using (SqlConnection connection = new SqlConnection(connectionString)) + { + return ExecuteScalar(connection, CommandType.Text, commandText, parms); + } + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 数据库连接字符串 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] parms) + { + using (SqlConnection connection = new SqlConnection(connectionString)) + { + return ExecuteScalar(connection, commandType, commandText, parms); + } + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 数据库连接 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteScalar(connection, null, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一行第一列 + public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteScalar(transaction.Connection, transaction, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集中的第一行第一列 + /// + /// 数据库连接 + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一行第一列 + private static object ExecuteScalar(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + SqlCommand command = new SqlCommand(); + PrepareCommand(command, connection, transaction, commandType, commandText, parms); + object retval = command.ExecuteScalar(); + command.Parameters.Clear(); + return retval; + } + + #endregion ExecuteScalar + + #region ExecuteDataReader + + /// + /// 执行SQL语句,返回只读数据集 + /// + /// 数据库连接 + /// SQL语句 + /// 查询参数 + /// 返回只读数据集 + private static SqlDataReader ExecuteDataReader(string connectionString, string commandText, params SqlParameter[] parms) + { + SqlConnection connection = new SqlConnection(connectionString); + return ExecuteDataReader(connection, null, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回只读数据集 + /// + /// 数据库连接 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回只读数据集 + private static SqlDataReader ExecuteDataReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] parms) + { + SqlConnection connection = new SqlConnection(connectionString); + return ExecuteDataReader(connection, null, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回只读数据集 + /// + /// 数据库连接 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回只读数据集 + private static SqlDataReader ExecuteDataReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataReader(connection, null, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回只读数据集 + /// + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回只读数据集 + private static SqlDataReader ExecuteDataReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataReader(transaction.Connection, transaction, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回只读数据集 + /// + /// 数据库连接 + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回只读数据集 + private static SqlDataReader ExecuteDataReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + SqlCommand command = new SqlCommand(); + PrepareCommand(command, connection, transaction, commandType, commandText, parms); + return command.ExecuteReader(CommandBehavior.CloseConnection); + } + + #endregion + + #region ExecuteDataRow + + /// + /// 执行SQL语句,返回结果集中的第一行 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 查询参数 + /// ,返回结果集中的第一行 + public static DataRow ExecuteDataRow(string connectionString, string commandText, params SqlParameter[] parms) + { + DataTable dt = ExecuteDataTable(connectionString, CommandType.Text, commandText, parms); + return dt.Rows.Count > 0 ? dt.Rows[0] : null; + } + + /// + /// 执行SQL语句,返回结果集中的第一行 + /// + /// 数据库连接字符串 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// ,返回结果集中的第一行 + public static DataRow ExecuteDataRow(string connectionString, CommandType commandType, string commandText, params SqlParameter[] parms) + { + DataTable dt = ExecuteDataTable(connectionString, commandType, commandText, parms); + return dt.Rows.Count > 0 ? dt.Rows[0] : null; + } + + /// + /// 执行SQL语句,返回结果集中的第一行 + /// + /// 数据库连接 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// ,返回结果集中的第一行 + public static DataRow ExecuteDataRow(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] parms) + { + DataTable dt = ExecuteDataTable(connection, commandType, commandText, parms); + return dt.Rows.Count > 0 ? dt.Rows[0] : null; + } + + /// + /// 执行SQL语句,返回结果集中的第一行 + /// + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// ,返回结果集中的第一行 + public static DataRow ExecuteDataRow(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + DataTable dt = ExecuteDataTable(transaction, commandType, commandText, parms); + return dt.Rows.Count > 0 ? dt.Rows[0] : null; + } + + #endregion ExecuteDataRow + + #region ExecuteDataTable + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 查询参数 + /// 返回结果集中的第一个数据表 + public static DataTable ExecuteDataTable(string connectionString, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(connectionString, CommandType.Text, commandText, parms).Tables[0]; + } + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// 数据库连接字符串 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一个数据表 + public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(connectionString, commandType, commandText, parms).Tables[0]; + } + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// 数据库连接 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一个数据表 + public static DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(connection, commandType, commandText, parms).Tables[0]; + } + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集中的第一个数据表 + public static DataTable ExecuteDataTable(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(transaction, commandType, commandText, parms).Tables[0]; + } + + /// + /// 获取空表结构 + /// + /// 数据库连接字符串 + /// 数据表名称 + /// 返回结果集中的第一个数据表 + public static DataTable ExecuteEmptyDataTable(string connectionString, string tableName) + { + return ExecuteDataSet(connectionString, CommandType.Text, string.Format("select * from {0} where 1=-1", tableName)).Tables[0]; + } + + /// + /// 执行SQL语句,返回结果集中的第一个数据表 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 排序SQL,如"ORDER BY ID DESC" + /// 每页记录数 + /// 页索引 + /// 查询参数 + /// 查询SQL + /// CTE表达式 + /// + public static DataTable ExecutePageDataTable(string connectionString, string sql, string order, int pageSize, int pageIndex, SqlParameter[] parms = null, string query = null, string cte = null) + { + string psql = string.Format(@" + {3} + SELECT * + FROM ( + SELECT ROW_NUMBER() OVER (ORDER BY {1}) RowNumber,* + FROM ( + {0} + ) t + WHERE 1 = 1 {2} + ) t + WHERE RowNumber BETWEEN @RowNumber_Begin + AND @RowNumber_End", sql, order, query, cte); + + List paramlist = new List() + { + new SqlParameter("@RowNumber_Begin", SqlDbType.Int){ Value = (pageIndex - 1) * pageSize + 1 }, + new SqlParameter("@RowNumber_End", SqlDbType.Int){ Value = pageIndex * pageSize } + }; + if (parms != null) paramlist.AddRange(parms); + return ExecuteDataTable(connectionString, psql, paramlist.ToArray()); + } + + #endregion ExecuteDataTable + + #region ExecuteDataSet + + /// + /// 执行SQL语句,返回结果集 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 查询参数 + /// 返回结果集 + public static DataSet ExecuteDataSet(string connectionString, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(connectionString, CommandType.Text, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集 + /// + /// 数据库连接字符串 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集 + public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params SqlParameter[] parms) + { + using (SqlConnection connection = new SqlConnection(connectionString)) + { + return ExecuteDataSet(connection, commandType, commandText, parms); + } + } + + /// + /// 执行SQL语句,返回结果集 + /// + /// 数据库连接 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集 + public static DataSet ExecuteDataSet(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(connection, null, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集 + /// + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集 + public static DataSet ExecuteDataSet(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + return ExecuteDataSet(transaction.Connection, transaction, commandType, commandText, parms); + } + + /// + /// 执行SQL语句,返回结果集 + /// + /// 数据库连接 + /// 事务 + /// 命令类型(存储过程,命令文本, 其它.) + /// SQL语句或存储过程名称 + /// 查询参数 + /// 返回结果集 + private static DataSet ExecuteDataSet(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] parms) + { + SqlCommand command = new SqlCommand(); + + PrepareCommand(command, connection, transaction, commandType, commandText, parms); + SqlDataAdapter adapter = new SqlDataAdapter(command); + + DataSet ds = new DataSet(); + adapter.Fill(ds); + if (commandText.IndexOf("@") > 0) + { + commandText = commandText.ToLower(); + int index = commandText.IndexOf("where "); + if (index < 0) + { + index = commandText.IndexOf("\nwhere"); + } + if (index > 0) + { + ds.ExtendedProperties.Add("SQL", commandText.Substring(0, index - 1)); //将获取的语句保存在表的一个附属数组里,方便更新时生成CommandBuilder + } + else + { + ds.ExtendedProperties.Add("SQL", commandText); //将获取的语句保存在表的一个附属数组里,方便更新时生成CommandBuilder + } + } + else + { + ds.ExtendedProperties.Add("SQL", commandText); //将获取的语句保存在表的一个附属数组里,方便更新时生成CommandBuilder + } + + foreach (DataTable dt in ds.Tables) + { + dt.ExtendedProperties.Add("SQL", ds.ExtendedProperties["SQL"]); + } + + command.Parameters.Clear(); + return ds; + } + + #endregion ExecuteDataSet + + #region 批量操作 + + /// + /// 大批量数据插入 + /// + /// 数据库连接字符串 + /// 数据表 + public static void BulkInsert(string connectionString, DataTable table) + { + if (string.IsNullOrEmpty(table.TableName)) throw new Exception("DataTable.TableName属性不能为空"); + using (SqlBulkCopy bulk = new SqlBulkCopy(connectionString)) + { + bulk.BatchSize = BatchSize; + bulk.BulkCopyTimeout = CommandTimeOut; + bulk.DestinationTableName = table.TableName; + foreach (DataColumn col in table.Columns) + { + bulk.ColumnMappings.Add(col.ColumnName, col.ColumnName); + } + bulk.WriteToServer(table); + bulk.Close(); + } + } + + /// + /// 使用MySqlDataAdapter批量更新数据 + /// + /// 数据库连接字符串 + /// 数据表 + public static void BatchUpdate(string connectionString, DataTable table) + { + SqlConnection connection = new SqlConnection(connectionString); + + SqlCommand command = connection.CreateCommand(); + command.CommandTimeout = CommandTimeOut; + command.CommandType = CommandType.Text; + SqlDataAdapter adapter = new SqlDataAdapter(command); + SqlCommandBuilder commandBulider = new SqlCommandBuilder(adapter); + commandBulider.ConflictOption = ConflictOption.OverwriteChanges; + + SqlTransaction transaction = null; + try + { + connection.Open(); + transaction = connection.BeginTransaction(); + //设置批量更新的每次处理条数 + adapter.UpdateBatchSize = BatchSize; + //设置事物 + adapter.SelectCommand.Transaction = transaction; + + if (table.ExtendedProperties["SQL"] != null) + { + adapter.SelectCommand.CommandText = table.ExtendedProperties["SQL"].ToString(); + } + adapter.Update(table); + transaction.Commit();/////提交事务 + } + catch (SqlException ex) + { + if (transaction != null) transaction.Rollback(); + throw ex; + } + finally + { + connection.Close(); + connection.Dispose(); + } + } + + /// + /// 分批次批量删除数据 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 每批次更新记录行数 + /// 批次执行间隔(秒) + public static void BatchDelete(string connectionString, string sql, int batchSize = 1000, int interval = 1) + { + sql = sql.ToLower(); + + if (batchSize < 1000) batchSize = 1000; + if (interval < 1) interval = 1; + while (ExecuteScalar(connectionString, sql.Replace("delete", "select top 1 1")) != null) + { + ExecuteNonQuery(connectionString, CommandType.Text, sql.Replace("delete", string.Format("delete top ({0})", batchSize))); + System.Threading.Thread.Sleep(interval * 1000); + } + } + + /// + /// 分批次批量更新数据 + /// + /// 数据库连接字符串 + /// SQL语句 + /// 每批次更新记录行数 + /// 批次执行间隔(秒) + public static void BatchUpdate(string connectionString, string sql, int batchSize = 1000, int interval = 1) + { + if (batchSize < 1000) batchSize = 1000; + if (interval < 1) interval = 1; + string existsSql = Regex.Replace(sql, @"[\w\s.=,']*from", "select top 1 1 from", RegexOptions.IgnoreCase); + existsSql = Regex.Replace(existsSql, @"set[\w\s.=,']* where", "where", RegexOptions.IgnoreCase); + existsSql = Regex.Replace(existsSql, @"update", "select top 1 1 from", RegexOptions.IgnoreCase); + while (ExecuteScalar(connectionString, existsSql) != 0) + { + ExecuteNonQuery(connectionString, CommandType.Text, Regex.Replace(sql, "update", string.Format("update top ({0})", batchSize), RegexOptions.IgnoreCase)); + System.Threading.Thread.Sleep(interval * 1000); + } + } + + #endregion 批量操作 + + #endregion 静态方法 + } +} diff --git a/NX.ERP.U8/NX.ERP.U8.csproj b/NX.ERP.U8/NX.ERP.U8.csproj index 1ab0460..079baa2 100644 --- a/NX.ERP.U8/NX.ERP.U8.csproj +++ b/NX.ERP.U8/NX.ERP.U8.csproj @@ -46,8 +46,11 @@ + + + diff --git a/NX.ERP.U8/Pmc/PmcQuery.cs b/NX.ERP.U8/Pmc/PmcQuery.cs index e6b8832..5c3b83c 100644 --- a/NX.ERP.U8/Pmc/PmcQuery.cs +++ b/NX.ERP.U8/Pmc/PmcQuery.cs @@ -106,13 +106,7 @@ left join ( from PO_Pomain a WITH (nolock), PO_Podetails b WITH (nolock) where a.poid=b.poid) as c where c.rownumber=1) a left join ( select cPsn_Num, cPsn_Name from hr_v_HR31_show WITH (nolock) where cDepCode ='52' and rEmployState ='在职') b on a.cMaker =b.cPsn_Name - ) cma on c.cInvCode = cma.cInvCode - - - - - - "; + ) cma on c.cInvCode = cma.cInvCode "; List where = new List(); diff --git a/NX.ERP.U8/Purchase/ArrivalDAL.cs b/NX.ERP.U8/Purchase/ArrivalDAL.cs index 230b1c6..cc2caaa 100644 --- a/NX.ERP.U8/Purchase/ArrivalDAL.cs +++ b/NX.ERP.U8/Purchase/ArrivalDAL.cs @@ -79,7 +79,7 @@ left join (SELECT cinvcode, STUFF(( SELECT ','+ cPosName FROM ( select b.cPosNam (isnull(cbustype,'')<>'委外加工') and isnull (b.cInvCode,'')=isnull(Pos.cInvCode,'') and isnull(b.cbcloser,'')='' And isnull(a.cverifier,'')<>'' And a.iBillType ='0' and isnull(b.bGsp,'')='0' and (isnull(b.iQuantity,0)-isnull(b.fRefuseQuantity,0)>isnull(b.fValidInQuan,0) or (d.igrouptype=2 - and isnull(b.inum,0)-isnull(b.frefusenum,0)>isnull(b.fvalidinnum,0))) and isnull(bStorageArrivalOrder,1)=1 "; + and isnull(b.inum,0)-isnull(b.frefusenum,0)>isnull(b.fvalidinnum,0))) and isnull(bStorageArrivalOrder,1)=1 "; if (!string.IsNullOrEmpty(where)) { strSQL += " and " + where; diff --git a/NX.ERP.WebAPi/Controllers/MES/MESController.cs b/NX.ERP.WebAPi/Controllers/MES/MESController.cs new file mode 100644 index 0000000..f02b297 --- /dev/null +++ b/NX.ERP.WebAPi/Controllers/MES/MESController.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; + +namespace NX.ERP.WebAPi.Controllers.MES +{ + [RoutePrefix("api/MES")] + public class MESController : ApiController + { + [Route("ReportSnList")] + [HttpPost] + public bool ReportSnList(Model.Request.ProductOrderReportSn info) + { + var logic = new NX.ERP.U8.MES.MesLogic(); + return logic.ReportSnList(info); + } + } +} diff --git a/NX.ERP.WebAPi/Controllers/MOM/MomOrderController.cs b/NX.ERP.WebAPi/Controllers/MOM/MomOrderController.cs index 1386036..19db3ae 100644 --- a/NX.ERP.WebAPi/Controllers/MOM/MomOrderController.cs +++ b/NX.ERP.WebAPi/Controllers/MOM/MomOrderController.cs @@ -8,8 +8,8 @@ namespace NX.ERP.WebAPi.Controllers.MOM { public class MomOrderController : ApiController { - - + + [Route("api/MomOrder/List")] [HttpGet] public IEnumerable List(long Pubufts = 0) @@ -62,8 +62,5 @@ namespace NX.ERP.WebAPi.Controllers.MOM var customerDAL = new NX.ERP.U8.Mom.MomOrderDAL(); return customerDAL.QueryDetails(momOrder); } - - - } } \ No newline at end of file diff --git a/NX.ERP.WebAPi/Controllers/Pmc/WhreportController.cs b/NX.ERP.WebAPi/Controllers/Pmc/WhreportController.cs index 81ca5aa..8103d9e 100644 --- a/NX.ERP.WebAPi/Controllers/Pmc/WhreportController.cs +++ b/NX.ERP.WebAPi/Controllers/Pmc/WhreportController.cs @@ -18,9 +18,5 @@ namespace NX.ERP.WebAPi.Controllers var FinanceQuery = new NX.ERP.U8.Finance.WarehouseQuery(); return FinanceQuery.ArrivalGet(info); } - - - - } } \ No newline at end of file diff --git a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj index e797bd1..75c6a81 100644 --- a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj +++ b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj @@ -158,6 +158,7 @@ + diff --git a/NX.ERP.WebAPi/Web.config b/NX.ERP.WebAPi/Web.config index 5526d76..bddf631 100644 --- a/NX.ERP.WebAPi/Web.config +++ b/NX.ERP.WebAPi/Web.config @@ -4,6 +4,9 @@ https://go.microsoft.com/fwlink/?LinkId=301879 --> + + +