diff --git a/API/TaskManager.EntityFramework/IRepository/IRepository.cs b/API/TaskManager.EntityFramework/IRepository/IRepository.cs index f83a6f3..f948a97 100644 --- a/API/TaskManager.EntityFramework/IRepository/IRepository.cs +++ b/API/TaskManager.EntityFramework/IRepository/IRepository.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using TaskManager.Entity; using TaskManager.EntityFramework.Repository; using Wood.Util.Filters; -using Z.BulkOperations; + namespace TaskManager.EntityFramework { @@ -27,9 +27,7 @@ namespace TaskManager.EntityFramework /// /// /// - Task BlukMergeAsync(List entities, Action> action); - - Task BlukInsertAsync(List entities, Action> action); + Task> GetPagedAsync(PagingParams pagingParams); diff --git a/API/TaskManager.EntityFramework/Repository/Repository.cs b/API/TaskManager.EntityFramework/Repository/Repository.cs index 07eab52..71f4865 100644 --- a/API/TaskManager.EntityFramework/Repository/Repository.cs +++ b/API/TaskManager.EntityFramework/Repository/Repository.cs @@ -1,8 +1,10 @@ using Microsoft.EntityFrameworkCore; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using Serilog; +using SkiaSharp; using System; using System.Collections.Generic; +using System.Data.Common; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -12,7 +14,7 @@ using TaskManager.Entity; using TaskManager.EntityFramework; using Wood.Util; using Wood.Util.Filters; -using Z.BulkOperations; + namespace TaskManager.EntityFramework.Repository { @@ -69,17 +71,6 @@ namespace TaskManager.EntityFramework.Repository } } - public async Task BlukMergeAsync(List entities,Action> action) - { - _context.BulkMerge(entities, action); - } - - - public async Task BlukInsertAsync(List entities, Action> action) - { - _context.BulkInsert(entities, action); - } - @@ -289,4 +280,330 @@ namespace TaskManager.EntityFramework.Repository } } + + + + + + + + + + + + + public static class EfCoreBulkExtensions + { + //private const int BatchSize = 1000; + + + private static int CalculateBatchSize(int propertyCount) + { + // 预留100个参数作为缓冲 + const int maxParameters = 2000; + return Math.Max(1, maxParameters / propertyCount); + } + + /// + /// EF Core 批量插入或更新扩展方法(支持复合主键) + /// + public static void BulkMerge(this DbContext context, + IEnumerable entities, + params Expression>[] keySelectors) + where TEntity : class + { + if (context == null || entities == null || !entities.Any()) + return; + using var transaction = context.Database.BeginTransaction(); + try + { + Type entityType = typeof(TEntity); + string tableName = context.Model.FindEntityType(entityType).GetTableName(); + string providerName = GetDatabaseProviderName(context); + + // 获取所有主键属性 + List keyPropNames = keySelectors.Select(GetPropertyName).ToList(); + List keyProps = keyPropNames.Select(entityType.GetProperty).ToList(); + + // 获取所有非主键属性 + PropertyInfo[] props = entityType.GetProperties() + .Where(p => !keyPropNames.Contains(p.Name)).ToArray(); + + // 计算实体的总属性数量 + int totalProperties = keyProps.Count + props.Length; + // 动态计算批处理大小 + int batchSize = CalculateBatchSize(totalProperties); + + // 分批处理 + var batches = entities.Chunk(batchSize); + + foreach (var batch in batches) + { + // 临时表名 + string tempTableName = $"#Temp_Bulk_{Guid.NewGuid().ToString("N").Substring(0, 8)}"; + + try + { + // 1. 创建临时表 + + + string createSql = $"SELECT * INTO {tempTableName} FROM {tableName} WHERE 1 = 0"; + context.Database.ExecuteSqlRaw(createSql); + + // 2. 插入数据到临时表 + InsertToTempTable(context, batch, tempTableName, keyProps, props); + + // 3. 执行MERGE操作 + ExecuteMerge(context, tableName, tempTableName, keyPropNames, props); + transaction.Commit(); + + } + finally + { + // 4. 清理临时表 + DropTempTable(context, tempTableName); + } + } + } + catch (Exception ex) + { + transaction.Rollback(); + Console.WriteLine($"批量操作失败: {ex.Message}"); + throw; + } + } + + + /// + /// EF Core 批量插入或更新扩展方法(支持复合主键) + /// + //public static void BulkMerge(this DbContext context, + // IEnumerable entities, + // params Expression>[] keySelectors) + // where TEntity : class + //{ + // if (context == null || entities == null || !entities.Any()) + // return; + + + // try + // { + // Type entityType = typeof(TEntity); + // string tableName = context.Model.FindEntityType(entityType).GetTableName(); + // string providerName = GetDatabaseProviderName(context); + + // // 获取所有主键属性 + // List keyPropNames = keySelectors.Select(GetPropertyName).ToList(); + // List keyProps = keyPropNames.Select(entityType.GetProperty).ToList(); + + // // 获取所有非主键属性 + // PropertyInfo[] props = entityType.GetProperties() + // .Where(p => !keyPropNames.Contains(p.Name)).ToArray(); + + // // 分批处理 + // var batches = entities.Chunk(BatchSize); + + // foreach (var batch in batches) + // { + // // 临时表名 + // string tempTableName = $"#Temp_Bulk_{Guid.NewGuid().ToString("N").Substring(0, 8)}"; + + // try + // { + // // 1. 创建临时表 + + // string createSql = $"SELECT * INTO {tempTableName} FROM {tableName} WHERE 1 = 0"; + + // context.Database.ExecuteSqlRaw(createSql); + + // //CreateTempTable(context, tempTableName, keyProps, props, providerName); + + // // 2. 插入数据到临时表 + // InsertToTempTable(context, batch, tempTableName, keyProps, props); + + // // 3. 执行MERGE操作 + // ExecuteMerge(context, tableName, tempTableName, keyPropNames, props); + // } + // finally + // { + // // 4. 清理临时表 + // DropTempTable(context, tempTableName); + // } + // } + + + // } + // catch (Exception ex) + // { + + // Console.WriteLine($"批量操作失败: {ex.Message}"); + // throw; + // } + + //} + + // 解析表达式获取属性名 + private static string GetPropertyName(Expression> expression) + { + if (expression.Body is UnaryExpression unary) + expression = (Expression>)unary.Operand; + + if (expression.Body is MemberExpression member) + return member.Member.Name; + + throw new ArgumentException("表达式必须指向实体属性", nameof(expression)); + } + + // 创建临时表 + private static void CreateTempTable(DbContext context, string tempTableName, + List keyProps, PropertyInfo[] props, string providerName) + { + string keyConstraints = string.Join(" AND ", keyProps.Select(p => $"[{p.Name}]")); + string sql = $"CREATE TABLE {tempTableName} (" + + string.Join(", ", keyProps.Select(p => $"[{p.Name}] {GetSqlType(p.PropertyType, providerName)}")) + + ", " + + string.Join(", ", props.Select(p => $"[{p.Name}] {GetSqlType(p.PropertyType, providerName)}")) + + ", PRIMARY KEY (" + keyConstraints + ")" + + ")"; + context.Database.ExecuteSqlRaw(sql); + } + + // 插入数据到临时表 + private static void InsertToTempTable(DbContext context, IEnumerable entities, + string tempTableName, List keyProps, PropertyInfo[] props) + { + // 构建参数化SQL + string paramPrefix = "@p"; + int paramIndex = 0; + string values = string.Join(",\n", entities.Select(e => + { + string row = "("; + // 添加主键参数 + foreach (var keyProp in keyProps) + row += $"{paramPrefix}{paramIndex++}, "; + // 添加其他属性参数 + foreach (var prop in props) + row += $"{paramPrefix}{paramIndex++}, "; + return row.TrimEnd(", ".ToCharArray()) + ")"; + })); + + string sql = $"INSERT INTO {tempTableName} ({GetColumnList(keyProps, props)}) VALUES {values}"; + DbCommand cmd = context.Database.GetDbConnection().CreateCommand(); + cmd.CommandText = sql; + + // 添加参数 + paramIndex = 0; + foreach (var entity in entities) + { + // 添加主键值 + foreach (var keyProp in keyProps) + AddParameter(cmd, $"{paramPrefix}{paramIndex++}", keyProp.GetValue(entity)); + + // 添加其他属性值 + foreach (var prop in props) + AddParameter(cmd, $"{paramPrefix}{paramIndex++}", prop.GetValue(entity)); + } + + context.Database.OpenConnection(); + try { cmd.ExecuteNonQuery(); } + finally { context.Database.CloseConnection(); } + } + + // 添加参数(防止SQL注入) + private static void AddParameter(DbCommand cmd, string paramName, object value) + { + DbParameter param = cmd.CreateParameter(); + param.ParameterName = paramName; + param.Value = value ?? DBNull.Value; + cmd.Parameters.Add(param); + } + + // 构建列名列表 + private static string GetColumnList(List keyProps, PropertyInfo[] props) + { + return string.Join(", ", keyProps.Select(p => $"[{p.Name}]")) + ", " + + string.Join(", ", props.Select(p => $"[{p.Name}]")); + } + + // 获取SQL类型 + private static string GetSqlType(Type type, string providerName) + { + type = Nullable.GetUnderlyingType(type) ?? type; + + if (providerName.Contains("SqlServer")) + { + if (type == typeof(int)) return "INT"; + if (type == typeof(long)) return "BIGINT"; + if (type == typeof(short)) return "SMALLINT"; + if (type == typeof(byte)) return "TINYINT"; + if (type == typeof(bool)) return "BIT"; + if (type == typeof(string)) return "NVARCHAR(MAX)"; + if (type == typeof(DateTime)) return "DATETIME2"; + if (type == typeof(DateTimeOffset)) return "DATETIMEOFFSET"; + if (type == typeof(decimal)) return "DECIMAL(18, 2)"; + if (type == typeof(float)) return "FLOAT"; + if (type == typeof(double)) return "FLOAT"; + if (type == typeof(Guid)) return "UNIQUEIDENTIFIER"; + if (type == typeof(byte[])) return "VARBINARY(MAX)"; + } + else if (providerName.Contains("PostgreSQL")) + { + if (type == typeof(int)) return "INTEGER"; + if (type == typeof(long)) return "BIGINT"; + if (type == typeof(short)) return "SMALLINT"; + if (type == typeof(bool)) return "BOOLEAN"; + if (type == typeof(string)) return "TEXT"; + if (type == typeof(DateTime)) return "TIMESTAMP"; + if (type == typeof(DateTimeOffset)) return "TIMESTAMPTZ"; + if (type == typeof(decimal)) return "DECIMAL(18, 2)"; + if (type == typeof(float)) return "REAL"; + if (type == typeof(double)) return "DOUBLE PRECISION"; + if (type == typeof(Guid)) return "UUID"; + if (type == typeof(byte[])) return "BYTEA"; + } + + throw new NotSupportedException($"类型 {type.Name} 或数据库提供者 {providerName} 不支持"); + } + + // 执行MERGE操作 + private static void ExecuteMerge(DbContext context, string tableName, string tempTableName, + List keyPropNames, PropertyInfo[] props) + { + string onClause = string.Join(" AND ", keyPropNames.Select(k => + $"target.[{k}] = source.[{k}]")); + + string updateFields = string.Join(", ", props.Select(p => + $"target.[{p.Name}] = source.[{p.Name}]")); + + string sql = $"MERGE {tableName} AS target " + + $"USING {tempTableName} AS source " + + $"ON ({onClause}) " + + $"WHEN MATCHED THEN UPDATE SET {updateFields} " + + $"WHEN NOT MATCHED THEN " + + $"INSERT ({GetColumnList(keyPropNames, props)}) " + + $"VALUES ({string.Join(", ", keyPropNames.Concat(props.Select(p => $"source.[{p.Name}]")))});"; + + context.Database.ExecuteSqlRaw(sql); + } + + // 删除临时表 + private static void DropTempTable(DbContext context, string tempTableName) + { + context.Database.ExecuteSqlRaw($"DROP TABLE IF EXISTS {tempTableName}"); + } + + // 获取数据库提供者名称 + private static string GetDatabaseProviderName(DbContext context) + { + return context.Database.ProviderName; + } + + // 构建列名列表(重载版本) + private static string GetColumnList(List keyPropNames, PropertyInfo[] props) + { + return string.Join(", ", keyPropNames.Select(k => $"[{k}]")) + ", " + + string.Join(", ", props.Select(p => $"[{p.Name}]")); + } + } + } diff --git a/API/TaskManager.EntityFramework/TaskManager.EntityFramework.csproj b/API/TaskManager.EntityFramework/TaskManager.EntityFramework.csproj index 9b1e421..9f567d5 100644 --- a/API/TaskManager.EntityFramework/TaskManager.EntityFramework.csproj +++ b/API/TaskManager.EntityFramework/TaskManager.EntityFramework.csproj @@ -8,6 +8,7 @@ + all @@ -18,7 +19,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/API/Wood.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml.user b/API/Wood.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml.user index 396b902..595ac54 100644 --- a/API/Wood.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/API/Wood.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -3,7 +3,7 @@ <_PublishTargetUrl>D:\QRAPI20250528\API\Wood.Admin.WebApi\bin\Release\net8.0\publish\ - True|2025-06-30T07:39:43.7918723Z||;True|2025-06-30T14:40:36.2678533+08:00||;True|2025-06-30T14:22:36.2631903+08:00||;True|2025-06-30T09:23:23.0780295+08:00||;True|2025-06-28T10:37:00.4525422+08:00||;True|2025-06-27T14:51:18.0006510+08:00||;True|2025-06-27T14:46:36.7189818+08:00||;True|2025-06-25T10:54:54.0406425+08:00||;True|2025-06-24T15:53:07.6141315+08:00||;True|2025-06-24T15:48:22.4116425+08:00||;True|2025-06-24T15:47:33.3794364+08:00||;True|2025-06-24T15:21:28.5600646+08:00||;True|2025-06-23T17:18:30.1990173+08:00||;True|2025-06-23T17:12:33.6485743+08:00||;True|2025-06-19T17:18:07.6086155+08:00||; + True|2025-07-01T10:51:45.0206266Z||;True|2025-06-30T15:39:43.7918723+08:00||;True|2025-06-30T14:40:36.2678533+08:00||;True|2025-06-30T14:22:36.2631903+08:00||;True|2025-06-30T09:23:23.0780295+08:00||;True|2025-06-28T10:37:00.4525422+08:00||;True|2025-06-27T14:51:18.0006510+08:00||;True|2025-06-27T14:46:36.7189818+08:00||;True|2025-06-25T10:54:54.0406425+08:00||;True|2025-06-24T15:53:07.6141315+08:00||;True|2025-06-24T15:48:22.4116425+08:00||;True|2025-06-24T15:47:33.3794364+08:00||;True|2025-06-24T15:21:28.5600646+08:00||;True|2025-06-23T17:18:30.1990173+08:00||;True|2025-06-23T17:12:33.6485743+08:00||;True|2025-06-19T17:18:07.6086155+08:00||; \ No newline at end of file diff --git a/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs b/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs index 91227c3..647f0e8 100644 --- a/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs +++ b/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs @@ -1,5 +1,6 @@ using Azure.Core; using Dapper; +using EFCore.BulkExtensions; using Hangfire; using Magicodes.ExporterAndImporter.Core; using Magicodes.ExporterAndImporter.Core.Extension; @@ -283,7 +284,7 @@ namespace TaskManager.Controllers if (result.code == 200) { await _logger.AddSuccess($"第 {i} 页奇瑞数据保存成功", TaskName, sub.TaskId, version); - _jobDbContext.BulkUpdate(records, options => options.UseTableLock = true); + await _jobDbContext.BulkUpdateAsync(records); List logs = new List(); foreach (var itm in records) @@ -298,7 +299,7 @@ namespace TaskManager.Controllers logs.Add(log); } - _jobDbContext.BulkInsert(logs, options => options.UseTableLock = true); + _jobDbContext.BulkInsert(logs); await UpdateAfter(records); sub.SyncedPageCount = i; diff --git a/API/Wood.Service/Controllers/CheryRecurringJobInputPageExtendController.cs b/API/Wood.Service/Controllers/CheryRecurringJobInputPageExtendController.cs index a31aaf7..8ec0555 100644 --- a/API/Wood.Service/Controllers/CheryRecurringJobInputPageExtendController.cs +++ b/API/Wood.Service/Controllers/CheryRecurringJobInputPageExtendController.cs @@ -1,5 +1,6 @@ using Azure.Core; using Dapper; +using EFCore.BulkExtensions; using Hangfire; using Magicodes.ExporterAndImporter.Core; using Magicodes.ExporterAndImporter.Core.Extension; @@ -350,7 +351,7 @@ namespace TaskManager.Controllers using (var transaction = await _jobDbContext.Database.BeginTransactionAsync()) { var tran = transaction.GetDbTransaction(); - await _jobDbContext.BulkUpdateAsync(records, options => { options.UseTableLock = false; }); + await _jobDbContext.BulkUpdateAsync(records); List logs = new List(); foreach (var itm in records) @@ -557,7 +558,7 @@ namespace TaskManager.Controllers using (var transaction = await _jobDbContext.Database.BeginTransactionAsync()) { var tran = transaction.GetDbTransaction(); - await _jobDbContext.BulkUpdateAsync(records, options => { options.UseTableLock = false; }); + await _jobDbContext.BulkUpdateAsync(records); List logs = new List(); foreach (var itm in records) { diff --git a/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs b/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs index 1d8167d..4c8bc5f 100644 --- a/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs +++ b/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs @@ -1,4 +1,6 @@ -using Magicodes.ExporterAndImporter.Core; +using EfCore.BulkOperations; +using EFCore.BulkExtensions; +using Magicodes.ExporterAndImporter.Core; using Magicodes.ExporterAndImporter.Core.Extension; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Builder; @@ -58,6 +60,7 @@ namespace TaskManager.Controllers { var tran = transaction.GetDbTransaction(); var taskId=list.First().TaskId; + var requestDate = list.First().RequestDate; var version = list.First().RequestDate.Replace("-", string.Empty); List logs = new List(); foreach (var itm in list) @@ -71,9 +74,73 @@ namespace TaskManager.Controllers } try { - - await _jobDbContext.BulkMergeAsync(list, options => { options.ColumnPrimaryKeyExpression = p => p.Id; options.Transaction = tran; }); - await _jobDbContext.BulkMergeAsync(logs, options => { options.ColumnPrimaryKeyExpression = p =>new { p.Id,p.RequestDate };options.Transaction = tran; }); + var ids=list.Select(p => p.Id); + + + //日志 + + var existListLogs = await _jobDbContext.Set().Where(p => ids.Contains(p.Id) && p.RequestDate==requestDate).ToListAsync(); + if (existListLogs.Any()) + { + foreach (var itm in existListLogs) + { + var first = logs.FirstOrDefault(p => p.Id == itm.Id && p.RequestDate == requestDate); + if (first != null) + { + itm.InjectFrom(first); + } + } + } + else + { + existListLogs = new List(); + } + var queryLogs = from item in logs + join existItem in existListLogs + on new { item.Id, item.RequestDate } equals new { existItem.Id, existItem.RequestDate } into gj + from subItem in gj.DefaultIfEmpty() + where subItem == null + select item; + + _jobDbContext.BulkUpdate(existListLogs.ToList()); + _jobDbContext.BulkInsert(queryLogs.ToList()); + //记录 + var existList = await _jobDbContext.Set().Where(p => ids.Contains(p.Id)).ToListAsync(); + if (existList.Any()) + { + foreach (var itm in existList) + { + var first = list.FirstOrDefault(p => p.Id == itm.Id); + if (first != null) + { + itm.InjectFrom(first); + } + } + } + else + { + existList = new List(); + } + + var query = from item in list + join existItem in existList + on item.Id equals existItem.Id into gj + from subItem in gj.DefaultIfEmpty() + where subItem == null + select item; + _jobDbContext.BulkUpdate(existList.ToList()); + _jobDbContext.BulkInsert(query.ToList()); + + + + + + + //_jobDbContext.BulkInsertOrUpdate + + + // await _jobDbContext.BulkMergeAsync(list, options => { options.ColumnPrimaryKeyExpression = p => p.Id; options.Transaction = tran; }); + // await _jobDbContext.BulkMergeAsync(logs, options => { options.ColumnPrimaryKeyExpression = p =>new { p.Id,p.RequestDate };options.Transaction = tran; }); //}); await ConfirmDataInsertAsync(list, _jobDbContext, tran); // 提交事务 @@ -130,37 +197,9 @@ namespace TaskManager.Controllers pagefirstList.Add(entity); allData.Add(itm); } - //if (readedcount > 0) - //{ - // var listrows = firstResponse.Data.Rows.Where(p => !ids.Contains(p.Id)); - // foreach (var itm in firstResponse.Data.Rows) - // { - // T entity = new T(); - // entity.InjectFrom(itm); - // entity.CreationTime = DateTime.Now; - // pagefirstList.Add(entity); - // allData.Add(itm); - // } - //} - //else - //{ - // foreach (var itm in firstResponse.Data.Rows) - // { - // T entity = new T(); - // entity.InjectFrom(itm); - // entity.CreationTime = DateTime.Now; - // pagefirstList.Add(entity); - // allData.Add(itm); - // } - //} - if (pagefirstList.Any()) { await InsertDataAsync(pagefirstList); - - - - } // 计算总页数 int totalPages = (int)Math.Ceiling((double)totalItems / pageSize); @@ -187,30 +226,6 @@ namespace TaskManager.Controllers allData.Add(itm); } - - //if (readedcount > 0) - //{ - // var listrows = pageResponse.Data.Rows.Where(p => !ids.Contains(p.Id)); - // foreach (var itm in pageResponse.Data.Rows) - // { - // T entity = new T(); - // entity.InjectFrom(itm); - // entity.CreationTime = DateTime.Now; - // pageList.Add(entity); - // allData.Add(itm); - // } - //} - //else - //{ - // foreach (var itm in pageResponse.Data.Rows) - // { - // T entity = new T(); - // entity.InjectFrom(itm); - // entity.CreationTime = DateTime.Now; - // pageList.Add(entity); - // allData.Add(itm); - // } - //} if (pageList.Any()) { await InsertDataAsync(pageList); @@ -260,31 +275,6 @@ namespace TaskManager.Controllers entity.RequestDate = date; allData.Add(itm); } - - - //if (readedcount > 0) - //{ - // var listrows = firstResponse.Data.Rows.Where(p => !ids.Contains(p.Id)); - // foreach (var itm in firstResponse.Data.Rows) - // { - // T entity = new T(); - // entity.InjectFrom(itm); - // entity.CreationTime = DateTime.Now; - // pagefirstList.Add(entity); - // allData.Add(itm); - // } - //} - //else - //{ - // foreach (var itm in firstResponse.Data.Rows) - // { - // T entity = new T(); - // entity.InjectFrom(itm); - // entity.CreationTime = DateTime.Now; - // pagefirstList.Add(entity); - // allData.Add(itm); - // } - //} if (pagefirstList.Any()) { await InsertDataAsync(pagefirstList); diff --git a/API/Wood.Service/Controllers/CherySupplierConDateService.cs b/API/Wood.Service/Controllers/CherySupplierConDateService.cs index 9917a7b..6c223f5 100644 --- a/API/Wood.Service/Controllers/CherySupplierConDateService.cs +++ b/API/Wood.Service/Controllers/CherySupplierConDateService.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using EFCore.BulkExtensions; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using System.Collections.Generic; @@ -82,7 +83,7 @@ namespace TaskManager.Controllers var tran = transaction.GetDbTransaction(); try { - await _jobDbContext.BulkUpdateAsync(entites, options => { options.Transaction = tran; }); + await _jobDbContext.BulkUpdateAsync(entites); await _jobDbContext.AddAsync(task); _jobDbContext.SaveChanges(); @@ -108,7 +109,7 @@ namespace TaskManager.Controllers [HttpPost] public async Task BatchUpdate([FromBody] List entites) { - await _jobDbContext.BulkUpdateAsync(entites, options => { options.ColumnPrimaryKeyExpression = p => p.UId; options.UseTableLock = false; }); + await _jobDbContext.BulkUpdateAsync(entites); return new JsonResult(new { Code = 200, Message = "批量修改成功!" }); diff --git a/API/Wood.Service/Controllers/CherySupplierConMmrpService.cs b/API/Wood.Service/Controllers/CherySupplierConMmrpService.cs index f459a3b..093b809 100644 --- a/API/Wood.Service/Controllers/CherySupplierConMmrpService.cs +++ b/API/Wood.Service/Controllers/CherySupplierConMmrpService.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using EFCore.BulkExtensions; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; @@ -84,7 +85,7 @@ namespace TaskManager.Controllers var tran = transaction.GetDbTransaction(); try { - await _jobDbContext.BulkUpdateAsync(entites, options => { options.Transaction = tran; }); + await _jobDbContext.BulkUpdateAsync(entites); await _jobDbContext.AddAsync(task); _jobDbContext.SaveChanges(); @@ -111,7 +112,9 @@ namespace TaskManager.Controllers public async Task BatchUpdate([FromBody] List entites) { - await _jobDbContext.BulkUpdateAsync(entites, options => { options.ColumnPrimaryKeyExpression = p => p.UId; options.UseTableLock = false; }); + + + await _jobDbContext.BulkUpdateAsync(entites); return new JsonResult(new { Code = 200, Message = "批量修改成功!" }); diff --git a/API/Wood.Service/Controllers/CherySupplierConPoService.cs b/API/Wood.Service/Controllers/CherySupplierConPoService.cs index cd242f4..a1d7437 100644 --- a/API/Wood.Service/Controllers/CherySupplierConPoService.cs +++ b/API/Wood.Service/Controllers/CherySupplierConPoService.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using EFCore.BulkExtensions; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using System.Collections.Generic; @@ -85,7 +86,7 @@ namespace TaskManager.Controllers var tran = transaction.GetDbTransaction(); try { - await _jobDbContext.BulkUpdateAsync(entites, options => { options.Transaction = tran; }); + await _jobDbContext.BulkUpdateAsync(entites); await _jobDbContext.AddAsync(task); _jobDbContext.SaveChanges(); @@ -108,7 +109,7 @@ namespace TaskManager.Controllers [HttpPost] public async Task BatchUpdate([FromBody] List entites) { - await _jobDbContext.BulkUpdateAsync(entites, options => { options.ColumnPrimaryKeyExpression = p => p.UId; options.UseTableLock = false; }); + await _jobDbContext.BulkUpdateAsync(entites); return new JsonResult(new { Code = 200, Message = "批量修改成功!" }); diff --git a/API/Wood.Service/Controllers/CherySupplierEmployeeService.cs b/API/Wood.Service/Controllers/CherySupplierEmployeeService.cs index 061b523..6626d85 100644 --- a/API/Wood.Service/Controllers/CherySupplierEmployeeService.cs +++ b/API/Wood.Service/Controllers/CherySupplierEmployeeService.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using EFCore.BulkExtensions; +using Microsoft.EntityFrameworkCore; using TaskManager.Contracts.Dtos; using TaskManager.Entity; using TaskManager.EntityFramework; @@ -53,7 +54,7 @@ namespace TaskManager.Controllers entity.ReadState = true; entity.WriteState = true; } - _jobDbContext.BulkUpdate(entities); + await _jobDbContext.BulkUpdateAsync(entities); } diff --git a/API/Wood.Service/Controllers/CherySupplierInfoService.cs b/API/Wood.Service/Controllers/CherySupplierInfoService.cs index a916be0..4e563f4 100644 --- a/API/Wood.Service/Controllers/CherySupplierInfoService.cs +++ b/API/Wood.Service/Controllers/CherySupplierInfoService.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using EFCore.BulkExtensions; +using Microsoft.EntityFrameworkCore; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using System.Security.Cryptography; using TaskManager.Contracts.Dtos; @@ -53,7 +54,7 @@ namespace TaskManager.Controllers entity.ReadState = true; entity.WriteState = true; } - _jobDbContext.BulkUpdate(entities); + await _jobDbContext.BulkUpdateAsync(entities); } diff --git a/API/Wood.Service/Controllers/CherySupplierMrpDataService.cs b/API/Wood.Service/Controllers/CherySupplierMrpDataService.cs index dc20d39..15a061c 100644 --- a/API/Wood.Service/Controllers/CherySupplierMrpDataService.cs +++ b/API/Wood.Service/Controllers/CherySupplierMrpDataService.cs @@ -1,10 +1,17 @@ -using System.Data.Common; +using EFCore.BulkExtensions; +using Microsoft.EntityFrameworkCore; +using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; +using Omu.ValueInjecter; +using Org.BouncyCastle.Crypto; +using System.Collections.Generic; +using System.Data.Common; using TaskManager.Contracts.Dtos; using TaskManager.Controllers; using TaskManager.Entity; using TaskManager.Entity.Entitys; using TaskManager.EntityFramework; using TaskManager.EntityFramework; +using TaskManager.EntityFramework.Repository; namespace TaskManager.Controllers { @@ -21,12 +28,129 @@ namespace TaskManager.Controllers { if (plist.Count > 0) { - List list = new List(); - plist.ForEach(p => + //List list = new List(); + //plist.ForEach(p => + //{ + // var con = new SUPPLIER_CON_DATE(); + + // con.Id=p.Id; + // con.SupplierCode = "8EG"; + // con.ReleaseEdition = p.ReleaseEdition; + // con.MaterialCode = p.MaterialCode; + // con.PlantId = p.PlantId; + // con.FeedbackResults = "0"; + // con.VentureType = ""; + // con.VentureSpecific = ""; + // con.Measures = ""; + // con.StartDate = p.StartDate; + // con.QuantityMeet1 =p.QuantityDemand1==null? p.QuantityDemand1.Value : 0 ; + // con.QuantityMeet2 = p.QuantityDemand2 == null ? p.QuantityDemand2.Value : 0; + // con.QuantityMeet3 =p.QuantityDemand3 ; + // con.QuantityMeet4 =p.QuantityDemand4 ; + // con.QuantityMeet5 =p.QuantityDemand5 ; + // con.QuantityMeet6 =p.QuantityDemand6 ; + // con.QuantityMeet7 =p.QuantityDemand7 ; + // con.QuantityMeet8 =p.QuantityDemand8 ; + // con.QuantityMeet9 =p.QuantityDemand9 ; + // con.QuantityMeet10 =p.QuantityDemand10 ; + // con.QuantityMeet11 =p.QuantityDemand11 ; + // con.QuantityMeet12 =p.QuantityDemand12 ; + // con.QuantityMeet13 =p.QuantityDemand13 ; + // con.QuantityMeet14 =p.QuantityDemand14 ; + // con.QuantityMeet15 =p.QuantityDemand15 ; + // con.QuantityMeet16 =p.QuantityDemand16 ; + // con.QuantityMeet17 =p.QuantityDemand17 ; + // con.QuantityMeet18 =p.QuantityDemand18 ; + // con.QuantityMeet19 =p.QuantityDemand19 ; + // con.QuantityMeet20 =p.QuantityDemand20 ; + // con.QuantityMeet21 =p.QuantityDemand21 ; + // con.QuantityMeet22 =p.QuantityDemand22 ; + // con.QuantityMeet23 =p.QuantityDemand23 ; + // con.QuantityMeet24 =p.QuantityDemand24 ; + // con.QuantityMeet25 =p.QuantityDemand25 ; + // con.QuantityMeet26 =p.QuantityDemand26 ; + // con.QuantityMeet27 =p.QuantityDemand27 ; + // con.QuantityMeet28 =p.QuantityDemand28 ; + // con.QuantityMeet29 =p.QuantityDemand29 ; + // con.QuantityMeet30 =p.QuantityDemand30 ; + // con.QuantityMeet31 = p.QuantityDemand31; + // con.CreationTime = DateTime.Now; + // list.Add(con); + //}); + + var ids=plist.Select(p => p.Id); + var existList = await _jobDbContext.Set().Where(p => ids.Contains(p.Id) && p.ReadState==false).ToListAsync(); + if (existList.Any()) + { + foreach (var itm in existList) + { + var first = plist.FirstOrDefault(p => p.Id == itm.Id); + if (first != null) + { + + itm.Id = first.Id; + itm.SupplierCode = "8EG"; + itm.ReleaseEdition = first.ReleaseEdition; + itm.MaterialCode = first.MaterialCode; + itm.PlantId = first.PlantId; + itm.FeedbackResults = "0"; + itm.VentureType = ""; + itm.VentureSpecific = ""; + itm.Measures = ""; + itm.StartDate = first.StartDate; + itm.QuantityMeet1 = first.QuantityDemand1 == null ? first.QuantityDemand1.Value : 0; + itm.QuantityMeet2 = first.QuantityDemand2 == null ? first.QuantityDemand2.Value : 0; + itm.QuantityMeet3 = first.QuantityDemand3; + itm.QuantityMeet4 = first.QuantityDemand4; + itm.QuantityMeet5 = first.QuantityDemand5; + itm.QuantityMeet6 = first.QuantityDemand6; + itm.QuantityMeet7 = first.QuantityDemand7; + itm.QuantityMeet8 = first.QuantityDemand8; + itm.QuantityMeet9 = first.QuantityDemand9; + itm.QuantityMeet10 = first.QuantityDemand10; + itm.QuantityMeet11 = first.QuantityDemand11; + itm.QuantityMeet12 = first.QuantityDemand12; + itm.QuantityMeet13 = first.QuantityDemand13; + itm.QuantityMeet14 = first.QuantityDemand14; + itm.QuantityMeet15 = first.QuantityDemand15; + itm.QuantityMeet16 = first.QuantityDemand16; + itm.QuantityMeet17 = first.QuantityDemand17; + itm.QuantityMeet18 = first.QuantityDemand18; + itm.QuantityMeet19 = first.QuantityDemand19; + itm.QuantityMeet20 = first.QuantityDemand20; + itm.QuantityMeet21 = first.QuantityDemand21; + itm.QuantityMeet22 = first.QuantityDemand22; + itm.QuantityMeet23 = first.QuantityDemand23; + itm.QuantityMeet24 = first.QuantityDemand24; + itm.QuantityMeet25 = first.QuantityDemand25; + itm.QuantityMeet26 = first.QuantityDemand26; + itm.QuantityMeet27 = first.QuantityDemand27; + itm.QuantityMeet28 = first.QuantityDemand28; + itm.QuantityMeet29 = first.QuantityDemand29; + itm.QuantityMeet30 = first.QuantityDemand30; + itm.QuantityMeet31 = first.QuantityDemand31; + itm.CreationTime = DateTime.Now; + + } + } + } + if(existList.Any()) + { + _jobDbContext.BulkUpdate(existList.ToList()); + } + + var query = from item in plist + join existItem in existList + on item.Id equals existItem.Id into gj + from subItem in gj.DefaultIfEmpty() + where subItem == null + select item; + List < SUPPLIER_CON_DATE > list = new List(); + foreach (var p in query) { var con = new SUPPLIER_CON_DATE(); - con.Id=p.Id; + con.Id = p.Id; con.SupplierCode = "8EG"; con.ReleaseEdition = p.ReleaseEdition; con.MaterialCode = p.MaterialCode; @@ -36,48 +160,78 @@ namespace TaskManager.Controllers con.VentureSpecific = ""; con.Measures = ""; con.StartDate = p.StartDate; - con.QuantityMeet1 =p.QuantityDemand1==null? p.QuantityDemand1.Value : 0 ; - con.QuantityMeet2 = p.QuantityDemand2 == null ? p.QuantityDemand2.Value : 0; - con.QuantityMeet3 =p.QuantityDemand3 ; - con.QuantityMeet4 =p.QuantityDemand4 ; - con.QuantityMeet5 =p.QuantityDemand5 ; - con.QuantityMeet6 =p.QuantityDemand6 ; - con.QuantityMeet7 =p.QuantityDemand7 ; - con.QuantityMeet8 =p.QuantityDemand8 ; - con.QuantityMeet9 =p.QuantityDemand9 ; - con.QuantityMeet10 =p.QuantityDemand10 ; - con.QuantityMeet11 =p.QuantityDemand11 ; - con.QuantityMeet12 =p.QuantityDemand12 ; - con.QuantityMeet13 =p.QuantityDemand13 ; - con.QuantityMeet14 =p.QuantityDemand14 ; - con.QuantityMeet15 =p.QuantityDemand15 ; - con.QuantityMeet16 =p.QuantityDemand16 ; - con.QuantityMeet17 =p.QuantityDemand17 ; - con.QuantityMeet18 =p.QuantityDemand18 ; - con.QuantityMeet19 =p.QuantityDemand19 ; - con.QuantityMeet20 =p.QuantityDemand20 ; - con.QuantityMeet21 =p.QuantityDemand21 ; - con.QuantityMeet22 =p.QuantityDemand22 ; - con.QuantityMeet23 =p.QuantityDemand23 ; - con.QuantityMeet24 =p.QuantityDemand24 ; - con.QuantityMeet25 =p.QuantityDemand25 ; - con.QuantityMeet26 =p.QuantityDemand26 ; - con.QuantityMeet27 =p.QuantityDemand27 ; - con.QuantityMeet28 =p.QuantityDemand28 ; - con.QuantityMeet29 =p.QuantityDemand29 ; - con.QuantityMeet30 =p.QuantityDemand30 ; + con.QuantityMeet1 = p.QuantityDemand1 == null ? p.QuantityDemand1.Value : 0; + con.QuantityMeet2 = p.QuantityDemand2 == null ? p.QuantityDemand2.Value : 0; + con.QuantityMeet3 = p.QuantityDemand3; + con.QuantityMeet4 = p.QuantityDemand4; + con.QuantityMeet5 = p.QuantityDemand5; + con.QuantityMeet6 = p.QuantityDemand6; + con.QuantityMeet7 = p.QuantityDemand7; + con.QuantityMeet8 = p.QuantityDemand8; + con.QuantityMeet9 = p.QuantityDemand9; + con.QuantityMeet10 = p.QuantityDemand10; + con.QuantityMeet11 = p.QuantityDemand11; + con.QuantityMeet12 = p.QuantityDemand12; + con.QuantityMeet13 = p.QuantityDemand13; + con.QuantityMeet14 = p.QuantityDemand14; + con.QuantityMeet15 = p.QuantityDemand15; + con.QuantityMeet16 = p.QuantityDemand16; + con.QuantityMeet17 = p.QuantityDemand17; + con.QuantityMeet18 = p.QuantityDemand18; + con.QuantityMeet19 = p.QuantityDemand19; + con.QuantityMeet20 = p.QuantityDemand20; + con.QuantityMeet21 = p.QuantityDemand21; + con.QuantityMeet22 = p.QuantityDemand22; + con.QuantityMeet23 = p.QuantityDemand23; + con.QuantityMeet24 = p.QuantityDemand24; + con.QuantityMeet25 = p.QuantityDemand25; + con.QuantityMeet26 = p.QuantityDemand26; + con.QuantityMeet27 = p.QuantityDemand27; + con.QuantityMeet28 = p.QuantityDemand28; + con.QuantityMeet29 = p.QuantityDemand29; + con.QuantityMeet30 = p.QuantityDemand30; con.QuantityMeet31 = p.QuantityDemand31; con.CreationTime = DateTime.Now; list.Add(con); - }); - - await dbContext.BulkMergeAsync(list, options => { options.Transaction = dbTransaction; options.UseTableLock = false; - - options.ColumnPrimaryKeyExpression = p =>new { p.Id, p.ReadState} ; - - - - }); + } + + + _jobDbContext.BulkInsert(list); + + //else + //{ + // existList = new List(); + //} + + //var query = from item in list + // join existItem in existList + // on item.Id equals existItem.Id into gj + // from subItem in gj.DefaultIfEmpty() + // where subItem == null + // select item; + //_jobDbContext.BulkUpdate(existList.ToList()); + //_jobDbContext.BulkInsert(query.ToList()); + + + + + + //dbContext.BulkInsert(list); + + //dbContext.BulkMerge(list, + // p=> p.Id, + // p=> p.ReadState + + + // ); + + //await dbContext.BulkMergeAsync(list, options => { options.Transaction = dbTransaction; options.UseTableLock = false; + + // options.ColumnPrimaryKeyExpression = p =>new { p.Id, p.ReadState} ; + + + + //}); } return; } diff --git a/API/Wood.Service/Controllers/CherySupplierMrpMonthService.cs b/API/Wood.Service/Controllers/CherySupplierMrpMonthService.cs index 2127693..7ae7d08 100644 --- a/API/Wood.Service/Controllers/CherySupplierMrpMonthService.cs +++ b/API/Wood.Service/Controllers/CherySupplierMrpMonthService.cs @@ -1,4 +1,5 @@ -using Magicodes.ExporterAndImporter.Core; +using EFCore.BulkExtensions; +using Magicodes.ExporterAndImporter.Core; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Data.Common; @@ -8,8 +9,8 @@ using TaskManager.Controllers; using TaskManager.Entity; using TaskManager.Entity.Entitys; using TaskManager.EntityFramework; -using Z.BulkOperations; -using Z.EntityFramework.Extensions; +using TaskManager.EntityFramework.Repository; + namespace TaskManager.Controllers @@ -45,9 +46,91 @@ namespace TaskManager.Controllers { if (plist.Count > 0) { + //List list = new List(); + //plist.ForEach(p => + //{ + // var con = new SUPPLIER_CON_MMRP(); + // con.Id = p.Id; + // con.SupplierCode = "8EG"; + // con.ReleaseEdition = p.ReleaseEdition; + // con.MaterialCode = p.MaterialCode; + // con.PlantId = p.PlantId; + // con.FeedbackResults = "0"; + // con.VentureType = ""; + // con.VentureSpecific = ""; + // con.Measures = ""; + // con.StartMonth = p.StartMonth; + // con.QuantityMeet1 = p.QuantityDemand1; + // con.QuantityMeet2 = p.QuantityDemand2; + // con.QuantityMeet3 = p.QuantityDemand3; + // con.QuantityMeet4 = p.QuantityDemand4; + // con.QuantityMeet5 = p.QuantityDemand5; + // con.QuantityMeet6 = p.QuantityDemand6; + // con.QuantityMeet7 = p.QuantityDemand7; + // con.QuantityMeet8 = p.QuantityDemand8; + // con.QuantityMeet9 = p.QuantityDemand9; + // con.QuantityMeet10 = p.QuantityDemand10; + // con.QuantityMeet11 = p.QuantityDemand11; + // con.QuantityMeet12 = p.QuantityDemand12; + // con.CreationTime = DateTime.Now; + // list.Add(con); + //}); + + //dbContext.BulkInsert(list); + + var ids = plist.Select(p => p.Id); + var existList = await _jobDbContext.Set().Where(p => ids.Contains(p.Id) && p.ReadState == false).ToListAsync(); + if (existList.Any()) + { + foreach (var itm in existList) + { + var first = plist.FirstOrDefault(p => p.Id == itm.Id); + if (first != null) + { + + itm.Id = first.Id; + + itm.SupplierCode = "8EG"; + itm.ReleaseEdition = first.ReleaseEdition; + itm.MaterialCode = first.MaterialCode; + itm.PlantId = first.PlantId; + itm.FeedbackResults = "0"; + itm.VentureType = ""; + itm.VentureSpecific = ""; + itm.Measures = ""; + itm.StartMonth = first.StartMonth; + itm.QuantityMeet1 = first.QuantityDemand1; + itm.QuantityMeet2 = first.QuantityDemand2; + itm.QuantityMeet3 = first.QuantityDemand3; + itm.QuantityMeet4 = first.QuantityDemand4; + itm.QuantityMeet5 = first.QuantityDemand5; + itm.QuantityMeet6 = first.QuantityDemand6; + itm.QuantityMeet7 = first.QuantityDemand7; + itm.QuantityMeet8 = first.QuantityDemand8; + itm.QuantityMeet9 = first.QuantityDemand9; + itm.QuantityMeet10 = first.QuantityDemand10; + itm.QuantityMeet11 = first.QuantityDemand11; + itm.QuantityMeet12 = first.QuantityDemand12; + itm.CreationTime = DateTime.Now; + + } + } + } + if (existList.Any()) + { + _jobDbContext.BulkUpdate(existList.ToList()); + } + + var query = from item in plist + join existItem in existList + on item.Id equals existItem.Id into gj + from subItem in gj.DefaultIfEmpty() + where subItem == null + select item; List list = new List(); - plist.ForEach(p => + foreach (var p in query) { + var con = new SUPPLIER_CON_MMRP(); con.Id = p.Id; con.SupplierCode = "8EG"; @@ -73,14 +156,12 @@ namespace TaskManager.Controllers con.QuantityMeet12 = p.QuantityDemand12; con.CreationTime = DateTime.Now; list.Add(con); - }); - - await dbContext.BulkMergeAsync(list, options=> { options.Transaction = dbTransaction; options.UseTableLock = false; - - options.ColumnPrimaryKeyExpression = p =>new { p.Id, p.ReadState }; - + } + _jobDbContext.BulkInsert(list); - }); + //await dbContext.BulkMergeAsync(list, options=> { options.Transaction = dbTransaction; options.UseTableLock = false; + // options.ColumnPrimaryKeyExpression = p =>new { p.Id, p.ReadState }; + //}); } return; } diff --git a/API/Wood.Service/Controllers/CherySupplierPoService.cs b/API/Wood.Service/Controllers/CherySupplierPoService.cs index a833f97..4898e51 100644 --- a/API/Wood.Service/Controllers/CherySupplierPoService.cs +++ b/API/Wood.Service/Controllers/CherySupplierPoService.cs @@ -1,9 +1,12 @@ -using System.Data.Common; +using EFCore.BulkExtensions; +using Microsoft.EntityFrameworkCore; +using System.Data.Common; using TaskManager.Contracts.Dtos; using TaskManager.Controllers; using TaskManager.Entity; using TaskManager.Entity.Entitys; using TaskManager.EntityFramework; +using TaskManager.EntityFramework.Repository; namespace TaskManager.Controllers @@ -20,33 +23,97 @@ namespace TaskManager.Controllers { if (plist.Count > 0) { + //List list = new List(); + //plist.ForEach(p => + //{ + // var con = new SUPPLIER_CON_PO(); + // con.Id = p.Id; + + // con.SupplierCode = "8EG"; + // con.PurchaseOrder =p.PurchaseOrder ; + // con.SerialNumber =p.SerialNumber ; + // con.QuantityMeet =p.QuantityDelivery==null? p.QuantityDelivery.Value : 0 ; + // con.FeedbackResults ="0" ; + // con.VentureType ="" ; + // con.VentureSpecific ="" ; + // con.Measures =""; + + // con.CreationTime = DateTime.Now; + + + // list.Add(con); + //}); + //dbContext.BulkInsert(list); + + var ids = plist.Select(p => p.Id); + var existList = await _jobDbContext.Set().Where(p => ids.Contains(p.Id) && p.ReadState == false).ToListAsync(); + if (existList.Any()) + { + foreach (var itm in existList) + { + var first = plist.FirstOrDefault(p => p.Id == itm.Id); + if (first != null) + { + + itm.Id = first.Id; + + itm.SupplierCode = "8EG"; + itm.PurchaseOrder = first.PurchaseOrder; + itm.SerialNumber =first.SerialNumber; + itm.QuantityMeet = first.QuantityDelivery == null ? first.QuantityDelivery.Value : 0; + itm.FeedbackResults = "0"; + itm.VentureType = ""; + itm.VentureSpecific = ""; + itm.Measures = ""; + + itm.CreationTime = DateTime.Now; + + } + } + } + if (existList.Any()) + { + _jobDbContext.BulkUpdate(existList.ToList()); + } + + var query = from item in plist + join existItem in existList + on item.Id equals existItem.Id into gj + from subItem in gj.DefaultIfEmpty() + where subItem == null + select item; List list = new List(); - plist.ForEach(p => + foreach (var p in query) { var con = new SUPPLIER_CON_PO(); - con.Id = p.Id; + con.Id = p.Id; - con.SupplierCode = "8EG"; - con.PurchaseOrder =p.PurchaseOrder ; - con.SerialNumber =p.SerialNumber ; - con.QuantityMeet =p.QuantityDelivery==null? p.QuantityDelivery.Value : 0 ; - con.FeedbackResults ="0" ; - con.VentureType ="" ; - con.VentureSpecific ="" ; - con.Measures =""; + con.SupplierCode = "8EG"; + con.PurchaseOrder = p.PurchaseOrder; + con.SerialNumber = p.SerialNumber; + con.QuantityMeet = p.QuantityDelivery == null ? p.QuantityDelivery.Value : 0; + con.FeedbackResults = "0"; + con.VentureType = ""; + con.VentureSpecific = ""; + con.Measures = ""; + con.CreationTime = DateTime.Now; + list.Add(con); + } + _jobDbContext.BulkInsert(list); - con.CreationTime = DateTime.Now; + //dbContext.BulkMerge(list, + // p => p.Id, + // p => p.ReadState - list.Add(con); - }); - await dbContext.BulkMergeAsync(list, options => { options.Transaction = dbTransaction; options.UseTableLock = false; + // ); + //await dbContext.BulkMergeAsync(list, options => { options.Transaction = dbTransaction; options.UseTableLock = false; - options.ColumnPrimaryKeyExpression = p => new { p.Id, p.ReadState}; + // options.ColumnPrimaryKeyExpression = p => new { p.Id, p.ReadState}; - }); + //}); } return; } diff --git a/API/Wood.Service/Controllers/CherySupplierProProcessEquipmentService.cs b/API/Wood.Service/Controllers/CherySupplierProProcessEquipmentService.cs index 0cdcbc2..88c22a0 100644 --- a/API/Wood.Service/Controllers/CherySupplierProProcessEquipmentService.cs +++ b/API/Wood.Service/Controllers/CherySupplierProProcessEquipmentService.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using EFCore.BulkExtensions; +using Microsoft.EntityFrameworkCore; using TaskManager.Contracts.Dtos; using TaskManager.Entity; using TaskManager.Entity.Entitys; @@ -51,7 +52,7 @@ namespace TaskManager.Controllers entity.ReadState = true; entity.WriteState = true; } - _jobDbContext.BulkUpdate(entities); + await _jobDbContext.BulkUpdateAsync(entities); } diff --git a/API/Wood.Service/Controllers/LogServices/CherySupplierMrpMonthService.cs b/API/Wood.Service/Controllers/LogServices/CherySupplierMrpMonthService.cs index eeac838..387a6cf 100644 --- a/API/Wood.Service/Controllers/LogServices/CherySupplierMrpMonthService.cs +++ b/API/Wood.Service/Controllers/LogServices/CherySupplierMrpMonthService.cs @@ -10,8 +10,7 @@ using TaskManager.Entity; using TaskManager.Entity.Entitys; using TaskManager.EntityFramework; using Wood.Service.Controllers; -using Z.BulkOperations; -using Z.EntityFramework.Extensions; + namespace TaskManager.Controllers diff --git a/API/Wood.Service/Controllers/NormalBaseController.cs b/API/Wood.Service/Controllers/NormalBaseController.cs index a0c4135..08127b5 100644 --- a/API/Wood.Service/Controllers/NormalBaseController.cs +++ b/API/Wood.Service/Controllers/NormalBaseController.cs @@ -1,4 +1,5 @@ -using Magicodes.ExporterAndImporter.Core; +using EFCore.BulkExtensions; +using Magicodes.ExporterAndImporter.Core; using Magicodes.ExporterAndImporter.Core.Extension; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Authorization; diff --git a/API/Wood.Service/Controllers/RecurringJobBaseController.cs b/API/Wood.Service/Controllers/RecurringJobBaseController.cs index d663477..f276a26 100644 --- a/API/Wood.Service/Controllers/RecurringJobBaseController.cs +++ b/API/Wood.Service/Controllers/RecurringJobBaseController.cs @@ -110,7 +110,7 @@ namespace TaskManager.Controllers return string.Empty; } - ////string jsonContent = System.IO.File.ReadAllText("日物料需求计划.json"); + //string jsonContent = System.IO.File.ReadAllText("日物料需求计划.json"); //string jsonContent = System.IO.File.ReadAllText("M+6月物料需求计划1.json"); ////string jsonContent = System.IO.File.ReadAllText("采购订单.json"); ////// string jsonContent = System.IO.File.ReadAllText("过焊装未过总装.json"); @@ -121,7 +121,7 @@ namespace TaskManager.Controllers //////string jsonContent = System.IO.File.ReadAllText("整车月度生产计划1.json"); ////string jsonContent = System.IO.File.ReadAllText("过涂装未过总装.json"); - // return jsonContent; + //return jsonContent; diff --git a/API/Wood.Service/Controllers/TaskSubService.cs b/API/Wood.Service/Controllers/TaskSubService.cs index e9cdb8d..643b6e9 100644 --- a/API/Wood.Service/Controllers/TaskSubService.cs +++ b/API/Wood.Service/Controllers/TaskSubService.cs @@ -1,4 +1,5 @@ using Dapper; +using EFCore.BulkExtensions; using Hangfire; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -19,7 +20,7 @@ using TaskManager.Entity.Entitys; using TaskManager.EntityFramework; using TaskManager.EntityFramework.Repository; using Wood.Util.Filters; -using Z.EntityFramework.Plus; + namespace Wood.Service.Controllers { @@ -275,7 +276,7 @@ namespace Wood.Service.Controllers item.WriteState = false; } - await _context.BulkUpdateAsync(mmrplist,options=>options.Transaction=tran); + await _context.BulkUpdateAsync(mmrplist); _context.SaveChanges(); // 提交事务 await transaction.CommitAsync(); diff --git a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs index e6310d3..ff9ed7b 100644 --- a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs +++ b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs @@ -1,7 +1,9 @@ -using Magicodes.ExporterAndImporter.Core.Models; +using EFCore.BulkExtensions; +using Magicodes.ExporterAndImporter.Core.Models; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.Configuration; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; @@ -109,19 +111,56 @@ namespace Wood.Service.Datas emps.Add(empObj); } - _context.BulkInsert(emps,options=>options.Transaction=tran); - _context.BulkMerge(entityLst, options => { - options.ColumnPrimaryKeyExpression = itm => - new + _context.BulkInsert(emps); + + + + + foreach (var empDtObj in entityLst) { - itm.SupplierCode, - itm.PlantId, - itm.WorkshopId, - itm.ProductionLineId, - itm.StationId - }; options.Transaction = tran; + empDtObj.SupplierCode = VendCode; + //以“供应商代码+工厂代码+车间代码+产线代码+工位代码+操作人员账号”为唯一标识,做新增或者更新存储 + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == empDtObj.SupplierCode + && itm.PlantId == empDtObj.PlantId + && itm.WorkshopId == empDtObj.WorkshopId + && itm.ProductionLineId == empDtObj.ProductionLineId + && itm.StationId == empDtObj.StationId + && itm.OperatorId == empDtObj.OperatorId + ); + if (firstObj == null) + { + var ret = await base.Create(empDtObj); + } + else + { + empDtObj.UId = firstObj.UId; + empDtObj.CreationTime = CommonHelper.CurrentTime; + var ret = await base.Update(empDtObj); + } } - ); + + //_context.BulkMerge(entityLst, + // itm=> itm.SupplierCode, + // itm=> itm.PlantId, + // itm=> itm.WorkshopId, + // itm=> itm.ProductionLineId, + // itm=> itm.StationId + // ); + + + //_context.BulkMerge(entityLst, options => { + // options.ColumnPrimaryKeyExpression = itm => + //new + //{ + // itm.SupplierCode, + // itm.PlantId, + // itm.WorkshopId, + // itm.ProductionLineId, + // itm.StationId + //}; options.Transaction = tran; + //} + //); //foreach (var empDtObj in entityLst) //{ @@ -201,12 +240,47 @@ namespace Wood.Service.Datas empObj.ReadState = true; await _context.AddAsync(empObj); - await _context.BulkMergeAsync(new List { entity }, options => + + empObj.TaskId = taskSubObj.TaskId; + await _supplierEmployeeRepository.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + && itm.OperatorId == entity.OperatorId + ); + if (firstObj == null) { - options.ColumnPrimaryKeyExpression = itm => - new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId, itm.OperatorId }; options.Transaction = tran; + var ret = await base.Create(entity); } - ); + else + { + throw new Exception("数据库已经存在,不能重复插入"); + } + + + + + + // _context.BulkMerge(new List { entity }, + + //itm => itm.SupplierCode, + //itm => itm.PlantId, + //itm => itm.WorkshopId, + //itm => itm.ProductionLineId, + //itm => itm.StationId + + // ); + + //await _context.BulkMergeAsync(new List { entity }, options => + //{ + // options.ColumnPrimaryKeyExpression = itm => + //new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId, itm.OperatorId }; options.Transaction = tran; + //} + //); _context.SaveChanges(); @@ -270,12 +344,45 @@ namespace Wood.Service.Datas empObj.ReadState = true; await _context.AddAsync(empObj); - await _context.BulkMergeAsync(new List { entity }, options => + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + && itm.OperatorId == entity.OperatorId + ); + if (firstObj == null) + { + throw new Exception("数据库不存在,不能更新"); + } + else { - options.ColumnPrimaryKeyExpression = itm => - new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId, itm.OperatorId }; options.Transaction = tran; + var ret = await base.Update(entity); } - ); + + + + + //_context.BulkMerge(new List { entity }, + // itm=> itm.SupplierCode, + // itm=> itm.PlantId, + // itm=> itm.WorkshopId, + // itm=> itm.ProductionLineId, + // itm=> itm.StationId, + // itm=> itm.OperatorId + + + // ); + + + //await _context.BulkMergeAsync(new List { entity }, options => + //{ + // options.ColumnPrimaryKeyExpression = itm => + //new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId, itm.OperatorId }; options.Transaction = tran; + //} + //); _context.SaveChanges(); diff --git a/API/Wood.Service/Datas/SupplierInfoDtService.cs b/API/Wood.Service/Datas/SupplierInfoDtService.cs index 8de34f9..3d377a3 100644 --- a/API/Wood.Service/Datas/SupplierInfoDtService.cs +++ b/API/Wood.Service/Datas/SupplierInfoDtService.cs @@ -1,4 +1,5 @@ -using Magicodes.ExporterAndImporter.Core.Models; +using EFCore.BulkExtensions; +using Magicodes.ExporterAndImporter.Core.Models; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -106,19 +107,56 @@ namespace Wood.Service.Datas infos.Add(empObj); //await _supplierInfoRepository.AddAsync(empObj); } - _context.BulkInsert(infos,options=>options.Transaction=tran); - _context.BulkMerge(entityLst, options => { - options.ColumnPrimaryKeyExpression = itm => - new + _context.BulkInsert(infos); + + + foreach (var empDtObj in entityLst) { - itm.SupplierCode, - itm.PlantId, - itm.WorkshopId, - itm.ProductionLineId, - itm.StationId - };options.Transaction = tran; + empDtObj.SupplierCode = VendCode; + //以“供应商代码+工厂代码+车间代码+产线代码+工位代码”为唯一标识,做新增或更新存储 + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == empDtObj.SupplierCode + && itm.PlantId == empDtObj.PlantId + && itm.WorkshopId == empDtObj.WorkshopId + && itm.ProductionLineId == empDtObj.ProductionLineId + && itm.StationId == empDtObj.StationId + ); + if (firstObj == null) + { + var ret = await base.Create(empDtObj); + } + else + { + empDtObj.UId = firstObj.UId; + empDtObj.CreationTime = CommonHelper.CurrentTime; + var ret = await base.Update(empDtObj); + } } - ); + + + + //_context.BulkMerge(entityLst, + // itm=> itm.SupplierCode, + // itm=> itm.PlantId, + // itm=> itm.WorkshopId, + // itm=> itm.ProductionLineId, + // itm=> itm.StationId + //); + + + + //_context.BulkMerge(entityLst, options => { + // options.ColumnPrimaryKeyExpression = itm => + //new + //{ + // itm.SupplierCode, + // itm.PlantId, + // itm.WorkshopId, + // itm.ProductionLineId, + // itm.StationId + //};options.Transaction = tran; + //} + //); //foreach (var empDtObj in entityLst) //{ @@ -194,8 +232,38 @@ namespace Wood.Service.Datas empObj.ReadState = true; await _context.AddAsync(empObj); + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + ); + if (firstObj == null) + { + var ret = await base.Create(entity); + } + else + { + throw new Exception("数据库已经存在,不能重复插入"); + } + + + + // _context.BulkMerge(new List { entity }, + // itm => itm.SupplierCode, + // itm => itm.PlantId, + // itm => itm.WorkshopId, + // itm => itm.ProductionLineId, + // itm => itm.StationId + //); + + + - await _context.BulkMergeAsync(new List { entity }, options => { options.ColumnPrimaryKeyExpression = itm => new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId }; options.Transaction = tran; }); + + + // await _context.BulkMergeAsync(new List { entity }, options => { options.ColumnPrimaryKeyExpression = itm => new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId }; options.Transaction = tran; }); _context.SaveChanges(); @@ -251,9 +319,30 @@ namespace Wood.Service.Datas empObj.TaskId = taskSubObj.TaskId; empObj.ReadState = true; await _context.AddAsync(empObj); + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + ); + if (firstObj == null) + { + throw new Exception("数据库不存在,不能更新"); + } + else + { + var ret = await base.Update(entity); + } - - await _context.BulkMergeAsync(new List { entity }, options => { options.ColumnPrimaryKeyExpression = itm => new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId }; options.Transaction = tran; }); + // _context.BulkMerge(new List { entity }, + // itm => itm.SupplierCode, + // itm => itm.PlantId, + // itm => itm.WorkshopId, + // itm => itm.ProductionLineId, + // itm => itm.StationId + //); + // await _context.BulkMergeAsync(new List { entity }, options => { options.ColumnPrimaryKeyExpression = itm => new { itm.SupplierCode, itm.PlantId, itm.WorkshopId, itm.ProductionLineId, itm.StationId }; options.Transaction = tran; }); _context.SaveChanges(); diff --git a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs index bbb37c1..b7853d9 100644 --- a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs +++ b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs @@ -1,4 +1,5 @@ -using Magicodes.ExporterAndImporter.Core.Models; +using EFCore.BulkExtensions; +using Magicodes.ExporterAndImporter.Core.Models; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -110,30 +111,41 @@ namespace Wood.Service.Datas // await _supplierProProcessEquipmentRepository.AddAsync(empObj); } - _context.BulkInsert(eqs,options=>options.Transaction=tran); - _context.BulkMerge(entityLst, options => { options.ColumnPrimaryKeyExpression = itm => new { itm.SupplierCode, itm.CheryProductNo, itm.DeviceCode, itm.DeviceType }; options.Transaction = tran; }); + _context.BulkInsert(eqs); - //foreach (var empDtObj in entityLst) - //{ - // empDtObj.SupplierCode = VendCode; - // //以供应商代码+奇瑞零件号+工艺编码+工艺版本为唯一数据,做新增或者更新存储 - // var firstObj = _context.Set().FirstOrDefault( - // itm => itm.SupplierCode == empDtObj.SupplierCode - // && itm.CheryProductNo == empDtObj.CheryProductNo - // && itm.DeviceCode == empDtObj.DeviceCode - // && itm.DeviceType == empDtObj.DeviceType - // ); - // if (firstObj == null) - // { - // var ret = await base.Create(empDtObj); - // } - // else - // { - // empDtObj.UId = firstObj.UId; - // empDtObj.CreationTime = CommonHelper.CurrentTime; - // var ret = await base.Update(empDtObj); - // } - //} + + //_context.BulkMerge(entityLst, + + // itm=> itm.SupplierCode, + // itm=> itm.CheryProductNo, + // itm=> itm.DeviceCode, + // itm=> itm.DeviceType + + // ); + + // _context.BulkMerge(entityLst, options => { options.ColumnPrimaryKeyExpression = itm => new { itm.SupplierCode, itm.CheryProductNo, itm.DeviceCode, itm.DeviceType }; options.Transaction = tran; }); + + foreach (var empDtObj in entityLst) + { + empDtObj.SupplierCode = VendCode; + //以供应商代码+奇瑞零件号+工艺编码+工艺版本为唯一数据,做新增或者更新存储 + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == empDtObj.SupplierCode + && itm.CheryProductNo == empDtObj.CheryProductNo + && itm.DeviceCode == empDtObj.DeviceCode + && itm.DeviceType == empDtObj.DeviceType + ); + if (firstObj == null) + { + var ret = await base.Create(empDtObj); + } + else + { + empDtObj.UId = firstObj.UId; + empDtObj.CreationTime = CommonHelper.CurrentTime; + var ret = await base.Update(empDtObj); + } + } #endregion transaction.Commit(); @@ -199,7 +211,40 @@ namespace Wood.Service.Datas empObj.ReadState = true; - await _context.BulkInsertAsync(new List() { empObj }); + + await _context.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.CheryProductNo == entity.CheryProductNo + && itm.DeviceCode == entity.DeviceCode + && itm.DeviceType == entity.DeviceType + ); + if (firstObj == null) + { + var ret = await base.Create(entity); + } + else + { + throw new Exception("数据库已经存在,不能重复插入"); + } + + + + + + // await _context.BulkInsertAsync(new List() { empObj }); + // _context.BulkMerge(new List { entity }, + + //itm => itm.SupplierCode, + //itm => itm.CheryProductNo, + //itm => itm.DeviceCode, + //itm => itm.DeviceType + + // ); + + + _context.SaveChanges(); @@ -269,20 +314,61 @@ namespace Wood.Service.Datas empObj.ReadState = true; - await _context.BulkInsertAsync(new List(){ empObj}); - _context.SaveChanges(); + await _context.AddAsync(empObj); + //await _supplierProProcessEquipmentRepository.AddAsync(empObj); - await _context.BulkMergeAsync(new List { entity }, options => + //_context.BulkMerge(new List { entity }, + + //itm => itm.SupplierCode, + //itm => itm.CheryProductNo, + //itm => itm.DeviceCode, + //itm => itm.DeviceType + + // ); + + + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.CheryProductNo == entity.CheryProductNo + && itm.DeviceCode == entity.DeviceCode + && itm.DeviceType == entity.DeviceType + ); + if (firstObj == null) { - options.ColumnPrimaryKeyExpression = itm => - new { itm.SupplierCode, itm.CheryProductNo, itm.DeviceCode, itm.DeviceType }; options.Transaction = tran; + throw new Exception("数据库不存在,不能更新"); } - ); + else + { + var ret = await base.Update(entity); + } + + + + + _context.SaveChanges(); + + + + + + + + + + + + //await _context.BulkMergeAsync(new List { entity }, options => + //{ + // options.ColumnPrimaryKeyExpression = itm => + //new { itm.SupplierCode, itm.CheryProductNo, itm.DeviceCode, itm.DeviceType }; options.Transaction = tran; + //} + //); + - diff --git a/API/Wood.Service/Wood.Service.csproj b/API/Wood.Service/Wood.Service.csproj index c192923..fff13a3 100644 --- a/API/Wood.Service/Wood.Service.csproj +++ b/API/Wood.Service/Wood.Service.csproj @@ -20,6 +20,9 @@ + + + @@ -27,7 +30,6 @@ -