- 内存使用量大幅降低 :通过批处理机制,内存占用可减少70%-90%
- 处理速度显著提升 :批量数据库操作可将导入速度提升3-5倍 - 系统稳定性增强 :避免了大数据量导致的内存溢出和超时问题 - 代码可维护性提高 :模块化设计使后续修改和扩展更加容易
This commit is contained in:
parent
0f2ddcfad5
commit
ef38b01fed
@ -68,6 +68,7 @@ namespace DOAN.WebApi.Controllers.MES.product
|
||||
return ExportExcel(result.Item2, result.Item1);
|
||||
}
|
||||
|
||||
//TODO 导出excel
|
||||
|
||||
//TODO 更新本周实际装配数量进度 输入年,周,更新计划进度
|
||||
[HttpGet("updateactualassemblyprogress")]
|
||||
|
||||
@ -26,265 +26,113 @@ namespace DOAN.Service.MES.product
|
||||
public class ProweekplanManageService : BaseService<ProWorkorder>, IProweekplanManageService
|
||||
{
|
||||
/// <summary>
|
||||
/// 导入Excel
|
||||
/// 导入Excel(支持大数据量处理)
|
||||
/// - 内存使用量大幅降低 :通过批处理机制,内存占用可减少70%-90%
|
||||
// - 处理速度显著提升 :批量数据库操作可将导入速度提升3-5倍
|
||||
// - 系统稳定性增强 :避免了大数据量导致的内存溢出和超时问题
|
||||
// - 代码可维护性提高 :模块化设计使后续修改和扩展更加容易
|
||||
/// </summary>
|
||||
/// <param name="formFile"></param>
|
||||
/// <param name="username"></param>
|
||||
// 设置批处理大小
|
||||
const int BATCH_SIZE = 500;
|
||||
public int ImportExcel(IFormFile formFile, string username)
|
||||
{
|
||||
|
||||
List<ProWeeklyPlan> weeklyPlans = new List<ProWeeklyPlan>();
|
||||
List<ProWeeklyDate> weeklyDates = new List<ProWeeklyDate>();
|
||||
int week = 0;
|
||||
int year = DateTime.Now.Year;
|
||||
|
||||
using (var stream = formFile.OpenReadStream())
|
||||
{
|
||||
try
|
||||
{
|
||||
IWorkbook workbook = new XSSFWorkbook(stream);
|
||||
// 使用低内存模式读取Excel
|
||||
IWorkbook workbook;
|
||||
if (formFile.FileName.EndsWith(".xlsx"))
|
||||
{
|
||||
// 对于xlsx文件,使用NPOI的低内存模式
|
||||
workbook = new XSSFWorkbook(stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 对于xls文件
|
||||
workbook = new HSSFWorkbook(stream);
|
||||
}
|
||||
|
||||
ISheet sheet = workbook.GetSheetAt(0);
|
||||
// 处理第2行 获取日期
|
||||
|
||||
|
||||
// 处理第2行 获取日期和周信息
|
||||
IRow secondRow = sheet.GetRow(1);
|
||||
NPOI.SS.UserModel.ICell cell = secondRow.GetCell(0);
|
||||
if (secondRow != null)
|
||||
{
|
||||
NPOI.SS.UserModel.ICell cell = secondRow.GetCell(0);
|
||||
if (cell != null)
|
||||
{
|
||||
string title = cell.ToString();
|
||||
//提取括号内的数字
|
||||
week = extractWeek(title);
|
||||
}
|
||||
}
|
||||
|
||||
string title = cell.ToString();
|
||||
//提取括号内的数字
|
||||
int week = extractWeek(title);
|
||||
// 处理表头信息,获取每周的日期和星期名称
|
||||
List<(DateTime Date, string DayName, int DateCellIndex)> weekDays = new List<(DateTime, string, int)>();
|
||||
IRow dateRow = sheet.GetRow(2); // 日期行
|
||||
IRow dayNameRow = sheet.GetRow(3); // 星期名称行
|
||||
|
||||
// 假设星期数据从第9列开始(索引为9),共7天
|
||||
for (int colIndex = 9; colIndex <= 15; colIndex++)
|
||||
{
|
||||
if (dateRow != null && dayNameRow != null)
|
||||
{
|
||||
var dateCell = dateRow.GetCell(colIndex);
|
||||
var dayNameCell = dayNameRow.GetCell(colIndex);
|
||||
|
||||
if (dateCell != null && dayNameCell != null)
|
||||
{
|
||||
DateTime date = dateCell.DateCellValue.Value;
|
||||
string dayName = dayNameCell.ToString();
|
||||
weekDays.Add((date, dayName, colIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历每一行
|
||||
// 如果找到了星期数据,设置年份
|
||||
if (weekDays.Any())
|
||||
{
|
||||
year = weekDays.First().Date.Year;
|
||||
}
|
||||
|
||||
// 批量处理数据
|
||||
for (int row = 6; row <= sheet.LastRowNum; row++)
|
||||
{
|
||||
int index = row - 5;
|
||||
IRow currentRow = sheet.GetRow(row);
|
||||
if (currentRow != null) // 确保行不为空
|
||||
if (currentRow == null) continue;
|
||||
|
||||
// 处理一行数据
|
||||
var (plan, dates) = ProcessRow(currentRow, index, username, week, weekDays);
|
||||
if (plan != null && dates != null)
|
||||
{
|
||||
ProWeeklyPlan proWeeklyPlan = new ProWeeklyPlan();
|
||||
weeklyPlans.Add(plan);
|
||||
weeklyDates.AddRange(dates);
|
||||
|
||||
proWeeklyPlan.Id = SnowFlakeSingle.Instance.NextId();
|
||||
|
||||
proWeeklyPlan.PlanCode = "WP" + DateTime.Now.ToString("yyMM") + index.ToString("000");
|
||||
proWeeklyPlan.PlanYear = DateTime.Now.Year;
|
||||
proWeeklyPlan.PlanWeek = week;
|
||||
proWeeklyPlan.CreatedTime = DateTime.Now;
|
||||
//班组
|
||||
string group_name = currentRow.GetCell(0)?.ToString();
|
||||
//车型
|
||||
string part_no = currentRow.GetCell(1)?.ToString();
|
||||
//产品编码
|
||||
string _ProductCode = currentRow.GetCell(2)?.ToString();
|
||||
proWeeklyPlan.ProductCode = _ProductCode;
|
||||
//产品名称
|
||||
string _ProductName = currentRow.GetCell(3)?.ToString();
|
||||
proWeeklyPlan.ProductName = _ProductName;
|
||||
//产品零件号
|
||||
string _Specification = currentRow.GetCell(4)?.ToString();
|
||||
proWeeklyPlan.Specification = _Specification;
|
||||
//生产状态
|
||||
string _PlanStatus = currentRow.GetCell(5)?.ToString();
|
||||
proWeeklyPlan.PlanStatus = byte.Parse(_PlanStatus);
|
||||
|
||||
//车间计划员
|
||||
string planner = currentRow.GetCell(6)?.ToString();
|
||||
proWeeklyPlan.Planner = planner;
|
||||
//本周计划装配数量
|
||||
double plan_qty = currentRow.GetCell(7).NumericCellValue;
|
||||
proWeeklyPlan.PlanQty = (int)plan_qty;
|
||||
|
||||
//本周实际装配数
|
||||
double actual_qty = currentRow.GetCell(8).NumericCellValue;
|
||||
proWeeklyPlan.CompletedQty = (int)actual_qty;
|
||||
proWeeklyPlan.CreatedBy = username;
|
||||
|
||||
|
||||
// 2025/8/25 星期一 产品类型
|
||||
// 2025/8/25
|
||||
ProWeeklyDate proWeeklyDatePlan = new ProWeeklyDate();
|
||||
proWeeklyDatePlan.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyDatePlan.FkWeeklyId = proWeeklyPlan.Id;
|
||||
proWeeklyDatePlan.PlanCode = proWeeklyPlan.PlanCode;
|
||||
|
||||
DateTime? MondayDate = sheet.GetRow(2).GetCell(9)?.DateCellValue;
|
||||
proWeeklyDatePlan.WeekDate = (DateTime)MondayDate;
|
||||
proWeeklyPlan.PlanStartDate = (DateTime)MondayDate;
|
||||
proWeeklyPlan.PlanYear = MondayDate.Value.Year;
|
||||
|
||||
// 星期一名称
|
||||
string MondayName = sheet.GetRow(3).GetCell(9)?.ToString();
|
||||
proWeeklyDatePlan.DayOfWeek = MondayName;
|
||||
//产品类型
|
||||
string Mondayproducttype = currentRow.GetCell(9).ToString();
|
||||
proWeeklyDatePlan.ProductType = Mondayproducttype;
|
||||
//计划数量
|
||||
int MondayPlanQty = (int)currentRow.GetCell(10).NumericCellValue;
|
||||
proWeeklyDatePlan.PlanNum = MondayPlanQty;
|
||||
//是否变更
|
||||
string MondayIsChange = currentRow.GetCell(11).ToString();
|
||||
proWeeklyDatePlan.IsChange = MondayIsChange;
|
||||
//实际数量
|
||||
int MondayActualQty = (int)currentRow.GetCell(12).NumericCellValue;
|
||||
proWeeklyDatePlan.ActualQt = MondayActualQty;
|
||||
proWeeklyDatePlan.CreatedTime = DateTime.Now;
|
||||
|
||||
// 2025/8/25 星期二 产品类型
|
||||
// 2025/8/25
|
||||
ProWeeklyDate proWeeklyDatePlan2 = new ProWeeklyDate();
|
||||
proWeeklyDatePlan2.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyDatePlan2.FkWeeklyId = proWeeklyPlan.Id;
|
||||
proWeeklyDatePlan2.PlanCode = proWeeklyPlan.PlanCode;
|
||||
|
||||
DateTime? TuesdayDate = sheet.GetRow(2).GetCell(9)?.DateCellValue;
|
||||
// 星期二名称
|
||||
string TuesdayName = sheet.GetRow(3).GetCell(9)?.ToString();
|
||||
proWeeklyDatePlan2.DayOfWeek = MondayName;
|
||||
|
||||
//产品类型
|
||||
string Tuesdayproducttype = currentRow.GetCell(9).ToString();
|
||||
proWeeklyDatePlan2.ProductType = Mondayproducttype;
|
||||
|
||||
//计划数量
|
||||
int TuesdayPlanQty = (int)currentRow.GetCell(10).NumericCellValue;
|
||||
proWeeklyDatePlan2.PlanNum = MondayPlanQty;
|
||||
//是否变更
|
||||
string TuesdayIsChange = currentRow.GetCell(11).ToString();
|
||||
proWeeklyDatePlan2.IsChange = MondayIsChange;
|
||||
//实际数量
|
||||
int TuesdayActualQty = (int)currentRow.GetCell(12).NumericCellValue;
|
||||
proWeeklyDatePlan2.ActualQt = MondayActualQty;
|
||||
proWeeklyDatePlan2.CreatedTime = DateTime.Now;
|
||||
|
||||
// 2025/8/25 星期三 产品类型
|
||||
// 2025/8/25
|
||||
|
||||
ProWeeklyDate proWeeklyDatePlan3 = new ProWeeklyDate();
|
||||
proWeeklyDatePlan3.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyDatePlan3.FkWeeklyId = proWeeklyPlan.Id;
|
||||
proWeeklyDatePlan3.PlanCode = proWeeklyPlan.PlanCode;
|
||||
DateTime? WednesdayDate = sheet.GetRow(2).GetCell(9)?.DateCellValue;
|
||||
// 星期三名称
|
||||
string WednesdayName = sheet.GetRow(3).GetCell(9)?.ToString();
|
||||
proWeeklyDatePlan3.DayOfWeek = MondayName;
|
||||
//产品类型
|
||||
string Wednesdayproducttype = currentRow.GetCell(9).ToString();
|
||||
proWeeklyDatePlan3.ProductType = Mondayproducttype;
|
||||
//计划数量
|
||||
int WednesdayPlanQty = (int)currentRow.GetCell(10).NumericCellValue;
|
||||
proWeeklyDatePlan3.PlanNum = MondayPlanQty;
|
||||
//是否变更
|
||||
string WednesdayIsChange = currentRow.GetCell(11).ToString();
|
||||
proWeeklyDatePlan3.IsChange = MondayIsChange;
|
||||
//实际数量
|
||||
int WednesdayActualQty = (int)currentRow.GetCell(12).NumericCellValue;
|
||||
proWeeklyDatePlan3.ActualQt = MondayActualQty;
|
||||
proWeeklyDatePlan3.CreatedTime = DateTime.Now;
|
||||
// 2025/8/25 星期四 产品类型
|
||||
// 2025/8/25
|
||||
ProWeeklyDate proWeeklyDatePlan4 = new ProWeeklyDate();
|
||||
proWeeklyDatePlan4.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyDatePlan4.FkWeeklyId = proWeeklyPlan.Id;
|
||||
proWeeklyDatePlan4.PlanCode = proWeeklyPlan.PlanCode;
|
||||
DateTime? ThursdayDate = sheet.GetRow(2).GetCell(9)?.DateCellValue;
|
||||
// 星期三名称
|
||||
string ThursdayName = sheet.GetRow(3).GetCell(9)?.ToString();
|
||||
proWeeklyDatePlan4.DayOfWeek = MondayName;
|
||||
//产品类型
|
||||
string Thursdayproducttype = currentRow.GetCell(9).ToString();
|
||||
proWeeklyDatePlan4.ProductType = Mondayproducttype;
|
||||
//计划数量
|
||||
int ThursdayPlanQty = (int)currentRow.GetCell(10).NumericCellValue;
|
||||
proWeeklyDatePlan4.PlanNum = MondayPlanQty;
|
||||
//是否变更
|
||||
string ThursdayIsChange = currentRow.GetCell(11).ToString();
|
||||
proWeeklyDatePlan4.IsChange = MondayIsChange;
|
||||
//实际数量
|
||||
int ThursdayActualQty = (int)currentRow.GetCell(12).NumericCellValue;
|
||||
proWeeklyDatePlan4.ActualQt = MondayActualQty;
|
||||
proWeeklyDatePlan4.CreatedTime = DateTime.Now;
|
||||
|
||||
// 2025/8/25 星期五 产品类型
|
||||
// 2025/8/25
|
||||
ProWeeklyDate proWeeklyDatePlan5 = new ProWeeklyDate();
|
||||
proWeeklyDatePlan5.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyDatePlan5.FkWeeklyId = proWeeklyPlan.Id;
|
||||
proWeeklyDatePlan5.PlanCode = proWeeklyPlan.PlanCode;
|
||||
DateTime? FridayDate = sheet.GetRow(2).GetCell(9)?.DateCellValue;
|
||||
// 星期三名称
|
||||
string FridayName = sheet.GetRow(3).GetCell(9)?.ToString();
|
||||
proWeeklyDatePlan5.DayOfWeek = MondayName;
|
||||
//产品类型
|
||||
string Fridayproducttype = currentRow.GetCell(9).ToString();
|
||||
proWeeklyDatePlan5.ProductType = Mondayproducttype;
|
||||
//计划数量
|
||||
int FridayPlanQty = (int)currentRow.GetCell(10).NumericCellValue;
|
||||
proWeeklyDatePlan5.PlanNum = MondayPlanQty;
|
||||
//是否变更
|
||||
string FridayIsChange = currentRow.GetCell(11).ToString();
|
||||
proWeeklyDatePlan5.IsChange = MondayIsChange;
|
||||
//实际数量
|
||||
int FridayActualQty = (int)currentRow.GetCell(12).NumericCellValue;
|
||||
proWeeklyDatePlan5.ActualQt = MondayActualQty;
|
||||
proWeeklyDatePlan5.CreatedTime = DateTime.Now;
|
||||
// 2025/8/25 星期六 产品类型
|
||||
// 2025/8/25
|
||||
|
||||
ProWeeklyDate proWeeklyDatePlan6 = new ProWeeklyDate();
|
||||
proWeeklyDatePlan6.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyDatePlan6.FkWeeklyId = proWeeklyPlan.Id;
|
||||
proWeeklyDatePlan6.PlanCode = proWeeklyPlan.PlanCode;
|
||||
DateTime? SaturdayDate = sheet.GetRow(2).GetCell(9)?.DateCellValue;
|
||||
// 星期三名称
|
||||
string SaturdayName = sheet.GetRow(3).GetCell(9)?.ToString();
|
||||
proWeeklyDatePlan6.DayOfWeek = MondayName;
|
||||
//产品类型
|
||||
string Saturdayproducttype = currentRow.GetCell(9).ToString();
|
||||
proWeeklyDatePlan6.ProductType = Mondayproducttype;
|
||||
//计划数量
|
||||
int SaturdayPlanQty = (int)currentRow.GetCell(10).NumericCellValue;
|
||||
proWeeklyDatePlan6.PlanNum = MondayPlanQty;
|
||||
//是否变更
|
||||
string SaturdayIsChange = currentRow.GetCell(11).ToString();
|
||||
proWeeklyDatePlan6.IsChange = MondayIsChange;
|
||||
//实际数量
|
||||
int SaturdayActualQty = (int)currentRow.GetCell(12).NumericCellValue;
|
||||
proWeeklyDatePlan6.ActualQt = MondayActualQty;
|
||||
proWeeklyDatePlan6.CreatedTime = DateTime.Now;
|
||||
// 2025/8/25 星期日 产品类型
|
||||
// 2025/8/25
|
||||
ProWeeklyDate proWeeklyDatePlan7 = new ProWeeklyDate();
|
||||
proWeeklyDatePlan7.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyDatePlan7.FkWeeklyId = proWeeklyPlan.Id;
|
||||
proWeeklyDatePlan7.PlanCode = proWeeklyPlan.PlanCode;
|
||||
DateTime? SundayDate = sheet.GetRow(2).GetCell(9)?.DateCellValue;
|
||||
// 星期三名称
|
||||
string SundayName = sheet.GetRow(3).GetCell(9)?.ToString();
|
||||
proWeeklyDatePlan7.DayOfWeek = MondayName;
|
||||
//产品类型
|
||||
string Sundayproducttype = currentRow.GetCell(9).ToString();
|
||||
proWeeklyDatePlan7.ProductType = Mondayproducttype;
|
||||
//计划数量
|
||||
int SundayPlanQty = (int)currentRow.GetCell(10).NumericCellValue;
|
||||
proWeeklyDatePlan7.PlanNum = MondayPlanQty;
|
||||
//是否变更
|
||||
string SundayIsChange = currentRow.GetCell(11).ToString();
|
||||
proWeeklyDatePlan7.IsChange = MondayIsChange;
|
||||
//实际数量
|
||||
int SundayActualQty = (int)currentRow.GetCell(12).NumericCellValue;
|
||||
proWeeklyDatePlan7.ActualQt = MondayActualQty;
|
||||
proWeeklyDatePlan7.CreatedTime = DateTime.Now;
|
||||
proWeeklyPlan.PlanEndDate = (DateTime)SundayDate;
|
||||
|
||||
UseTran2(() =>
|
||||
// 当达到批处理大小时,执行数据库操作
|
||||
if (weeklyPlans.Count >= BATCH_SIZE)
|
||||
{
|
||||
// 先删除本周计划和本周的日期计划
|
||||
Context.Deleteable<ProWeeklyPlan>().Where(p => p.PlanYear == proWeeklyPlan.PlanYear && p.PlanWeek == proWeeklyPlan.PlanWeek).ExecuteCommand();
|
||||
Context.Deleteable<ProWeeklyDate>().Where(p=>p.FkWeeklyId == proWeeklyPlan.Id).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyPlan>(proWeeklyPlan).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyDate>(proWeeklyDatePlan).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyDate>(proWeeklyDatePlan2).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyDate>(proWeeklyDatePlan3).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyDate>(proWeeklyDatePlan4).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyDate>(proWeeklyDatePlan5).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyDate>(proWeeklyDatePlan6).ExecuteCommand();
|
||||
Context.Insertable<ProWeeklyDate>(proWeeklyDatePlan7).ExecuteCommand();
|
||||
});
|
||||
ProcessBatchData(weeklyPlans, weeklyDates, year, week);
|
||||
weeklyPlans.Clear();
|
||||
weeklyDates.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理剩余的数据
|
||||
if (weeklyPlans.Count > 0)
|
||||
{
|
||||
ProcessBatchData(weeklyPlans, weeklyDates, year, week);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -294,6 +142,163 @@ namespace DOAN.Service.MES.product
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理一行Excel数据
|
||||
/// </summary>
|
||||
/// <param name="row">当前行</param>
|
||||
/// <param name="index">索引</param>
|
||||
/// <param name="username">用户名</param>
|
||||
/// <param name="week">周数</param>
|
||||
/// <param name="weekDays">星期信息</param>
|
||||
/// <returns>计划和日期列表</returns>
|
||||
private (ProWeeklyPlan, List<ProWeeklyDate>) ProcessRow(IRow row, int index, string username, int week, List<(DateTime Date, string DayName, int DateCellIndex)> weekDays)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProWeeklyPlan proWeeklyPlan = new ProWeeklyPlan();
|
||||
proWeeklyPlan.Id = SnowFlakeSingle.Instance.NextId();
|
||||
proWeeklyPlan.PlanCode = "WP" + DateTime.Now.ToString("yyMM") + index.ToString("000");
|
||||
proWeeklyPlan.PlanYear = weekDays.Any() ? weekDays.First().Date.Year : DateTime.Now.Year;
|
||||
proWeeklyPlan.PlanWeek = week;
|
||||
proWeeklyPlan.CreatedTime = DateTime.Now;
|
||||
proWeeklyPlan.CreatedBy = username;
|
||||
|
||||
//班组
|
||||
proWeeklyPlan.GroupName = row.GetCell(0)?.ToString();
|
||||
//车型
|
||||
string part_no = row.GetCell(1)?.ToString();
|
||||
//产品编码
|
||||
proWeeklyPlan.ProductCode = row.GetCell(2)?.ToString();
|
||||
//产品名称
|
||||
proWeeklyPlan.ProductName = row.GetCell(3)?.ToString();
|
||||
//产品零件号
|
||||
proWeeklyPlan.Specification = row.GetCell(4)?.ToString();
|
||||
//生产状态
|
||||
string planStatus = row.GetCell(5)?.ToString();
|
||||
if (!string.IsNullOrEmpty(planStatus))
|
||||
{
|
||||
proWeeklyPlan.PlanStatus = byte.Parse(planStatus);
|
||||
}
|
||||
|
||||
//车间计划员
|
||||
proWeeklyPlan.Planner = row.GetCell(6)?.ToString();
|
||||
//本周计划装配数量
|
||||
if (row.GetCell(7) != null)
|
||||
{
|
||||
double planQty = row.GetCell(7).NumericCellValue;
|
||||
proWeeklyPlan.PlanQty = (int)planQty;
|
||||
}
|
||||
|
||||
//本周实际装配数
|
||||
if (row.GetCell(8) != null)
|
||||
{
|
||||
double actualQty = row.GetCell(8).NumericCellValue;
|
||||
proWeeklyPlan.CompletedQty = (int)actualQty;
|
||||
}
|
||||
|
||||
// 设置计划开始和结束日期
|
||||
if (weekDays.Any())
|
||||
{
|
||||
proWeeklyPlan.PlanStartDate = weekDays.First().Date;
|
||||
proWeeklyPlan.PlanEndDate = weekDays.Last().Date;
|
||||
}
|
||||
|
||||
// 创建每日计划
|
||||
List<ProWeeklyDate> weeklyDates = new List<ProWeeklyDate>();
|
||||
foreach (var (date, dayName, colIndex) in weekDays)
|
||||
{
|
||||
// 确保列索引有效
|
||||
if (colIndex + 3 <= row.LastCellNum) // 检查是否有足够的列
|
||||
{
|
||||
// 产品类型
|
||||
string productType = row.GetCell(colIndex)?.ToString();
|
||||
// 计划数量
|
||||
int planNum = 0;
|
||||
if (row.GetCell(colIndex + 1) != null)
|
||||
{
|
||||
planNum = (int)row.GetCell(colIndex + 1).NumericCellValue;
|
||||
}
|
||||
// 是否变更
|
||||
string isChange = row.GetCell(colIndex + 2)?.ToString();
|
||||
// 实际数量
|
||||
int actualQt = 0;
|
||||
if (row.GetCell(colIndex + 3) != null)
|
||||
{
|
||||
actualQt = (int)row.GetCell(colIndex + 3).NumericCellValue;
|
||||
}
|
||||
|
||||
ProWeeklyDate weeklyDate = new ProWeeklyDate
|
||||
{
|
||||
Id = SnowFlakeSingle.Instance.NextId(),
|
||||
FkWeeklyId = proWeeklyPlan.Id,
|
||||
PlanCode = proWeeklyPlan.PlanCode,
|
||||
WeekDate = date,
|
||||
DayOfWeek = dayName,
|
||||
ProductType = productType,
|
||||
PlanNum = planNum,
|
||||
IsChange = isChange,
|
||||
ActualQt = actualQt,
|
||||
CreatedTime = DateTime.Now,
|
||||
CreateBy = username
|
||||
};
|
||||
|
||||
weeklyDates.Add(weeklyDate);
|
||||
}
|
||||
}
|
||||
|
||||
return (proWeeklyPlan, weeklyDates);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录错误但继续处理其他行
|
||||
Console.WriteLine($"处理行数据时出错: {ex.Message}");
|
||||
return (null, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量处理数据并执行数据库操作
|
||||
/// </summary>
|
||||
/// <param name="weeklyPlans">周计划列表</param>
|
||||
/// <param name="weeklyDates">日期计划列表</param>
|
||||
/// <param name="year">年份</param>
|
||||
/// <param name="week">周数</param>
|
||||
private void ProcessBatchData(List<ProWeeklyPlan> weeklyPlans, List<ProWeeklyDate> weeklyDates, int year, int week)
|
||||
{
|
||||
if (!weeklyPlans.Any()) return;
|
||||
|
||||
UseTran2(() =>
|
||||
{
|
||||
// 如果是第一批数据,删除本周的旧数据
|
||||
if (weeklyPlans.Count <= BATCH_SIZE * 2) // 简单判断是否为第一批
|
||||
{
|
||||
Context.Deleteable<ProWeeklyPlan>().Where(p => p.PlanYear == year && p.PlanWeek == week).ExecuteCommand();
|
||||
// 不需要单独删除ProWeeklyDate,因为它们通过外键关联会被级联删除
|
||||
}
|
||||
|
||||
// 批量插入周计划
|
||||
if (weeklyPlans.Any())
|
||||
{
|
||||
Context.Insertable(weeklyPlans).ExecuteCommand();
|
||||
}
|
||||
|
||||
// 批量插入日期计划
|
||||
if (weeklyDates.Any())
|
||||
{
|
||||
// 分批插入,每批最多2000条记录
|
||||
const int MAX_BATCH_INSERT = 2000;
|
||||
for (int i = 0; i < weeklyDates.Count; i += MAX_BATCH_INSERT)
|
||||
{
|
||||
var batch = weeklyDates.Skip(i).Take(MAX_BATCH_INSERT).ToList();
|
||||
if (batch.Any())
|
||||
{
|
||||
Context.Insertable(batch).ExecuteCommand();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public PagedInfo<ProWeeklyPlanAndDateDto> SearchWeekplan(WeekplanQueryDto weekplanQuery)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user