周计划新增与导入的修改

This commit is contained in:
2025-10-11 16:10:26 +08:00
parent a669664fcc
commit e6a9ca32c4

View File

@@ -6,6 +6,7 @@ using Model.DBModel;
using SqlSugar;
using System;
using System.IO;
using System.Linq;
using ZR.Model.mes.pro;
using ZR.Model.MES.pro.DTO;
using ZR.Model.MES.wms;
@@ -20,6 +21,12 @@ namespace ZR.Service.mes.pro
public (List<ProWorklplanDto>, int) GetAllData(int pageNum, int pageSize, int year, int week, string partNumber, string color)
{
// 如果年份为空小于等于0使用当天的年份
if (year <= 0)
{
year = DateTime.Now.Year;
}
var predicate = Expressionable.Create<ProWorklplan_v2>()
.AndIF(year > 0, it => it.Year == year)
.AndIF(week > 0, it => it.Week == week)
@@ -31,7 +38,7 @@ namespace ZR.Service.mes.pro
.Where(predicate)
.OrderBy(it => it.Id)
.ToPageList(pageNum, pageSize, ref totalCount);
// 计划正确检查
// 计划是否正确检查
List<ProWorklplanDto> proWorkplanDtoList = new List<ProWorklplanDto>();
foreach (ProWorklplan_v2 item in proWorkplanList)
{
@@ -77,69 +84,74 @@ namespace ZR.Service.mes.pro
public int AddWorkPlan(ProWorklplan_v2 proWorkplan)
{
proWorkplan.Id = "MP" + DateTime.Now.ToString("yyyyMMdd") + Getworkplanid_max().ToString("000");
proWorkplan.Remark = "手动插入";
return Context.Insertable(proWorkplan).ExecuteCommand();
}
/// <summary>
/// 获取生产计划id
/// </summary>
/// <returns></returns>
private int Getworkplanid_max()
{
ProWorklplan_v2 max_workplan = Context.Queryable<ProWorklplan_v2>().OrderBy(it => it.Id, OrderByType.Desc).First();
if (max_workplan != null && !string.IsNullOrEmpty(max_workplan.Id) && max_workplan.Id.Substring(2, 8) == DateTime.Now.ToString("yyyyMMdd"))
// 获取当前日期和周信息
DateTime now = DateTime.Now;
int currentYear = now.Year;
int currentWeek = GetWeekNumber(now);
if (proWorkplan.Year <= 0)
{
int num = Convert.ToInt32(max_workplan.Id.Substring(10)) + 1;
return num;
proWorkplan.Year = currentYear;
}
if (proWorkplan.Week <= 0)
{
proWorkplan.Week = currentWeek;
}
string timeStr;
// 判断是否为本周
if (proWorkplan.Year == currentYear && proWorkplan.Week == currentWeek)
{
// 本周使用当前日期
timeStr = now.ToString("yyyyMMdd");
}
else
{
return 0;
// 非本周使用该周的周一日期yyyyMMdd格式
DateTime weekMonday = GetMondayOfWeek(proWorkplan.Year, proWorkplan.Week);
timeStr = weekMonday.ToString("yyyyMMdd");
}
}
// 修改
public int UpdateWorkPlan(ProWorklplan_v2 proWorkplan)
{
return Context.Updateable<ProWorklplan_v2>()
.SetColumns(it => new ProWorklplan_v2()
{
BlankNum = proWorkplan.BlankNum,
Partnumber = proWorkplan.Partnumber,
ProductName = proWorkplan.ProductName,
Specification = proWorkplan.Specification,
ColorCode = proWorkplan.ColorCode,
RequireNum = proWorkplan.RequireNum,
EveryHangerNum = proWorkplan.EveryHangerNum,
ProductionBeat = proWorkplan.ProductionBeat,
AllHangerNum = proWorkplan.AllHangerNum,
TurnNumber = proWorkplan.TurnNumber,
ProductTime = proWorkplan.ProductTime,
RequireHanger = proWorkplan.RequireHanger,
})
.Where(it => it.Id == proWorkplan.Id)
.ExecuteCommand();
}
public int DeleteWorkPlan(string id)
{
return Context.Deleteable<ProWorklplan_v2>().In(id).ExecuteCommand();
proWorkplan.Id = "MP" + timeStr + Getworkplanid_max(timeStr).ToString("000");
proWorkplan.Remark = "手动插入";
return Context.Insertable(proWorkplan).ExecuteCommand();
}
public string ImportExceldata(List<ProWorklplan_v2> worklplanList)
{
int max_id = Getworkplanid_max();
if (worklplanList == null || worklplanList.Count == 0)
{
return "导入数据为空";
}
// 获取第一条数据的年份和周信息
ProWorklplan_v2 firstPlan = worklplanList.First();
int year = firstPlan.Year;
int week = firstPlan.Week;
// 获取当前日期和周信息
DateTime now = DateTime.Now;
int currentYear = now.Year;
int currentWeek = GetWeekNumber(now);
string timeStr;
// 判断是否为本周
if (year == currentYear && week == currentWeek)
{
// 本周使用当前日期
timeStr = now.ToString("yyyyMMdd");
}
else
{
// 非本周使用该周的周一日期yyyyMMdd格式
DateTime weekMonday = GetMondayOfWeek(year, week);
timeStr = weekMonday.ToString("yyyyMMdd");
}
int max_id = Getworkplanid_max(timeStr);
worklplanList.ForEach(it =>
{
it.Id = "MP" + DateTime.Now.ToString("yyyyMMdd") + max_id.ToString("000");
it.Id = "MP" + timeStr + max_id.ToString("000");
it.Remark = "Excel导入";
max_id++;
});
@@ -147,7 +159,7 @@ namespace ZR.Service.mes.pro
var x = Context.Storageable(worklplanList)
.SplitUpdate(it => it.Any())//存在更新
.SplitInsert(it => true)//否则插入(更新优先级大于插入)
.WhereColumns(it => new { it.Year, it.Week, it.Partnumber })//如果不是主键可以这样实现多字段it=>new{it.x1,it.x2}
.WhereColumns(it => new { it.Id, it.Year, it.Week, it.Partnumber })//如果不是主键可以这样实现多字段it=>new{it.x1,it.x2}
.ToStorage();
x.AsInsertable.ExecuteCommand();//插入可插入部分;
@@ -175,6 +187,12 @@ namespace ZR.Service.mes.pro
/// <returns></returns>
public (string, string) ExportExceldata(int year, int week)
{
// 如果年份为空小于等于0使用当天的年份
if (year <= 0)
{
year = DateTime.Now.Year;
}
//1.0 读取表数据
var list = Queryable().Where(it => it.Year == year && it.Week == week).ToList();
@@ -205,10 +223,6 @@ namespace ZR.Service.mes.pro
//3.0 返回路径和文件名
return (sFileName, fullPath);
}
public List<ProWorkorder> GetWorkorderListByPlanId(string id)
@@ -223,7 +237,6 @@ namespace ZR.Service.mes.pro
public int AddWorkorder(ProWorkorder proWorkorder)
{
return Context.Insertable(proWorkorder).ExecuteCommand();
}
@@ -237,15 +250,14 @@ namespace ZR.Service.mes.pro
return Context.Deleteable<ProWorkorder>().In(id).ExecuteCommand();
}
/// <summary>
/// 周计划汇总
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public WorkplanSummaryDto GetWeekSummary(int year, int week)
{
// 如果年份为空小于等于0使用当天的年份
if (year <= 0)
{
year = DateTime.Now.Year;
}
return Context.Queryable<ProWorklplan_v2>().Where(it => it.Year == year && it.Week == week)
.GroupBy(it => new { it.Week })
.Select(it => new WorkplanSummaryDto
@@ -257,18 +269,16 @@ namespace ZR.Service.mes.pro
})
.First();
}
/// <summary>
/// 删除周计划全部
/// </summary>
/// <param name="week"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public int DeleteAllWorkPlan(int year, int week)
{
// 如果年份为空小于等于0使用当天的年份
if (year <= 0)
{
year = DateTime.Now.Year;
}
return Context.Deleteable<ProWorklplan_v2>().Where(it => it.Year == year && it.Week == week).ExecuteCommand();
}
@@ -303,5 +313,116 @@ namespace ZR.Service.mes.pro
}
}
// 修改
public int UpdateWorkPlan(ProWorklplan_v2 proWorkplan)
{
return Context.Updateable<ProWorklplan_v2>()
.SetColumns(it => new ProWorklplan_v2()
{
BlankNum = proWorkplan.BlankNum,
Partnumber = proWorkplan.Partnumber,
ProductName = proWorkplan.ProductName,
Specification = proWorkplan.Specification,
ColorCode = proWorkplan.ColorCode,
RequireNum = proWorkplan.RequireNum,
EveryHangerNum = proWorkplan.EveryHangerNum,
ProductionBeat = proWorkplan.ProductionBeat,
AllHangerNum = proWorkplan.AllHangerNum,
TurnNumber = proWorkplan.TurnNumber,
ProductTime = proWorkplan.ProductTime,
RequireHanger = proWorkplan.RequireHanger,
})
.Where(it => it.Id == proWorkplan.Id)
.ExecuteCommand();
}
public int DeleteWorkPlan(string id)
{
return Context.Deleteable<ProWorklplan_v2>().In(id).ExecuteCommand();
}
/// <summary>
/// 获取指定日期所在的周数不依赖Globalization
/// </summary>
private int GetWeekNumber(DateTime date)
{
// 每年1月1日所在的周可能属于上一年的最后一周
// 这里简化处理假设每年1月1日所在的周为第1周
DateTime startOfYear = new DateTime(date.Year, 1, 1);
// 计算与1月1日的天数差
int daysDiff = (date - startOfYear).Days;
// 计算1月1日是星期几
int firstDayOfWeek = (int)startOfYear.DayOfWeek;
if (firstDayOfWeek == 0) firstDayOfWeek = 7; // 将Sunday(0)转为7
// 调整到周一为一周的开始
int adjustedDays = daysDiff + (8 - firstDayOfWeek);
// 计算周数
int weekNumber = adjustedDays / 7;
// 处理特殊情况1月1日可能属于上一年的最后一周
if (weekNumber == 0)
{
// 获取上一年的最后一天
DateTime lastDayOfPrevYear = startOfYear.AddDays(-1);
// 计算上一年最后一天所在的周数
return GetWeekNumber(lastDayOfPrevYear);
}
return weekNumber;
}
/// <summary>
/// 获取指定年份和周数的周一日期不依赖Globalization
/// </summary>
private DateTime GetMondayOfWeek(int year, int week)
{
// 获取该年的第一天
DateTime firstDayOfYear = new DateTime(year, 1, 1);
// 计算1月1日是星期几
int firstDayOfWeek = (int)firstDayOfYear.DayOfWeek;
if (firstDayOfWeek == 0) firstDayOfWeek = 7; // 将Sunday(0)转为7
// 计算第一周的周一
DateTime firstMonday = firstDayOfYear.AddDays(8 - firstDayOfWeek - 7);
// 如果第一周的周一在上一年则第一周的周一为本年的1月1日之后的第一个周一
if (firstMonday.Year < year)
{
firstMonday = firstMonday.AddDays(7);
}
// 计算目标周的周一
DateTime targetMonday = firstMonday.AddDays((week - 1) * 7);
return targetMonday;
}
/// <summary>
/// 获取生产计划id
/// </summary>
/// <returns></returns>
private int Getworkplanid_max(string timeStr)
{
ProWorklplan_v2 max_workplan = Context.Queryable<ProWorklplan_v2>()
.Where(it => it.Id.StartsWith("MP" + timeStr))
.OrderBy(it => it.Id, OrderByType.Desc)
.First();
if (max_workplan != null && !string.IsNullOrEmpty(max_workplan.Id))
{
int num = Convert.ToInt32(max_workplan.Id.Substring(2 + timeStr.Length)) + 1;
return num;
}
else
{
return 1;
}
}
}
}