zhuangpei-mesbackend/DOAN.Service/MES/product/ProweekplanManageService.cs
2025-09-08 15:21:43 +08:00

424 lines
23 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DOAN.Model;
using DOAN.Model.MES.product;
using DOAN.Model.MES.product.Dto;
using DOAN.Repository;
using DOAN.Service.MES.product.IService;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace DOAN.Service.MES.product
{
[AppService(ServiceType = typeof(IProweekplanManageService), ServiceLifetime = LifeTime.Transient)]
public class ProweekplanManageService : BaseService<ProWorkorder>, IProweekplanManageService
{
/// <summary>
/// 导入Excel
/// </summary>
/// <param name="formFile"></param>
/// <param name="username"></param>
public int ImportExcel(IFormFile formFile, string username)
{
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
// 处理第2行 获取日期
IRow secondRow = sheet.GetRow(1);
NPOI.SS.UserModel.ICell cell = secondRow.GetCell(0);
string title = cell.ToString();
//提取括号内的数字
int week = extractWeek(title);
// 遍历每一行
for (int row = 6; row <= sheet.LastRowNum; row++)
{
int index = row - 5;
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
ProWeeklyPlan proWeeklyPlan = new ProWeeklyPlan();
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(() =>
{
// 先删除本周计划和本周的日期计划
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();
});
}
}
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
}
public PagedInfo<ProWeeklyPlanAndDateDto> SearchWeekplan(WeekplanQueryDto weekplanQuery)
{
return Context.Queryable<ProWeeklyPlan>()
.LeftJoin<ProWeeklyDate>((p, d) => p.PlanCode == d.PlanCode)
.WhereIF(weekplanQuery.year > 0, p => p.PlanYear == weekplanQuery.year)
.WhereIF(weekplanQuery.week > 0, p => p.PlanWeek == weekplanQuery.week)
.WhereIF(!string.IsNullOrEmpty(weekplanQuery.partnumber), p => p.Specification.Contains(weekplanQuery.partnumber))
.Select((p, d) => new ProWeeklyPlanAndDateDto
{
// 来自 ProWeeklyPlan (p)
ProWeeklyPlanId = p.Id,
PlanCode = p.PlanCode,
PlanYear = p.PlanYear,
PlanWeek = p.PlanWeek,
PlanStartDate = p.PlanStartDate,
PlanEndDate = p.PlanEndDate,
OrderCode = p.OrderCode,
ProductCode = p.ProductCode,
ProductName = p.ProductName,
Specification = p.Specification,
Color = p.Color,
PlanQty = p.PlanQty,
CompletedQty = p.CompletedQty,
RemainingQty = p.PlanQty - p.CompletedQty, // 若数据库中没有,可在此计算
ScrapQty = p.ScrapQty,
WorkshopCode = p.WorkshopCode,
WorkshopName = p.WorkshopName,
LineCode = p.LineCode,
LineName = p.LineName,
GroupCode = p.GroupCode,
GroupName = p.GroupName,
ShiftType = p.ShiftType,
PlanStatus = p.PlanStatus,
Planner = p.Planner,
Priority = p.Priority,
Sort = p.Sort,
MaterialReady = p.MaterialReady,
Remark = p.Remark,
ProWeeklyPlanCreatedBy = p.CreatedBy,
ProWeeklyPlanCreatedTime = p.CreatedTime, // 假设 p 中有此字段,否则用 DateTime.Now
ProWeeklyPlanUpdatedBy = p.UpdatedBy,
ProWeeklyPlanUpdatedTime = p.UpdatedTime,
// 来自 ProWeeklyDate (d)
ProWeeklyDateId = d.Id,
FkWeeklyId = d.FkWeeklyId,
ProWeeklyPlanPlanCode = d.PlanCode, // 假设 d 中存储的是 PlanCode若字段名不同请调整
WeekDate = d.WeekDate,
DayOfWeek = d.DayOfWeek,
ProductType = d.ProductType,
PlanNum = d.PlanNum,
IsChange = d.IsChange,
ActualQt = d.ActualQt,
CreateBy = d.CreateBy,
CreatedTime = d.CreatedTime,
UpdateBy = d.UpdateBy,
UpdatedTime = d.UpdatedTime
}).ToPage_NO_Convert(weekplanQuery);
}
public int UpdateActualAssemblyProgress(int year, int week)
{
//获取周的日期开始到结束
DateTime? WeekStartDate = Context.Queryable<ProWeeklyDate>()
.Where(it => it.FkWeeklyId == SqlFunc.Subqueryable<ProWeeklyPlan>().Where(s => s.PlanWeek == week && s.PlanYear == year).Select(s => s.Id))
.Where(it => it.DayOfWeek == "星期一")
.Select(it => it.WeekDate)
.First();
DateTime? WeekEndDate = Context.Queryable<ProWeeklyDate>()
.Where(it => it.FkWeeklyId == SqlFunc.Subqueryable<ProWeeklyPlan>().Where(s => s.PlanWeek == week && s.PlanYear == year).Select(s => s.Id))
.Where(it => it.DayOfWeek == "星期日")
.Select(it => it.WeekDate)
.First();
//
if (WeekStartDate == null && WeekEndDate == null)
{
Context
.Queryable<ProWorkorder>()
.LeftJoin<ProReportwork>((w, r) => w.Id == r.Id)
.Where((w, r) => w.WorkorderDate >= WeekStartDate && w.WorkorderDate <= WeekEndDate)
.GroupBy(it => new { it.ProductionCode, it.Specification })
.Select(it => new
{
it.ProductionCode,
it.Specification,
TotalDeliveryNum = SqlFunc.AggregateSum(it.DeliveryNum)
}).ToList();
}
return 0;
}
private int extractWeek(string title)
{
global::System.Text.RegularExpressions.Match match = Regex.Match(title, @"[(](\d+)[)]");
if (match.Success)
{
string numberStr = match.Groups[1].Value; // 获取第一个捕获组中的内容,即数字部分
if (int.TryParse(numberStr, out int weekNumber))
{
Console.WriteLine("提取到的周数是: " + weekNumber); // 输出: 35
return weekNumber;
}
else
{
Console.WriteLine("括号内不是有效的数字。");
return -1;
}
}
else
{
Console.WriteLine("未找到括号内的数字。");
return -1;
}
}
}
}