shgx_tz_mom/ZR.Service/mes/pro/ProWorkorderServiceV2.cs

587 lines
21 KiB
C#
Raw Normal View History

2025-09-16 09:41:37 +08:00
using System;
using System.IO;
using Infrastructure;
2024-06-07 11:04:26 +08:00
using Infrastructure.Attribute;
2024-01-20 14:37:32 +08:00
using Microsoft.AspNetCore.Hosting;
using MiniExcelLibs;
2024-06-07 11:04:26 +08:00
using Model.DBModel;
2024-01-31 17:51:45 +08:00
using SqlSugar;
2024-06-07 11:04:26 +08:00
using ZR.Model.MES.pro;
using ZR.Model.MES.pro.DTO;
2024-05-31 13:22:07 +08:00
using ZR.Model.MES.wms;
2024-06-07 11:04:26 +08:00
using ZR.Service.mes.pro.IService;
2024-01-20 14:37:32 +08:00
namespace ZR.Service.mes.pro
{
[AppService(ServiceType = typeof(IProWorkorderServiceV2), ServiceLifetime = LifeTime.Transient)]
public class ProWorkorderServiceV2 : BaseService<ProWorkorder_v2>, IProWorkorderServiceV2
{
2025-09-16 09:41:37 +08:00
public (List<ProWorkOrder>, int) GetWorkorderList(
int pageNum,
int pageSize,
int year,
int week,
int date,
int isSchedule
)
2024-01-20 14:37:32 +08:00
{
2025-09-16 09:41:37 +08:00
var predicate = Expressionable
.Create<ProWorkorder_v2>()
.AndIF(year > 0, it => it.Year == year)
.AndIF(week > 0, it => it.Week == week)
.AndIF(date > 0, it => it.Date == date)
.ToExpression();
2024-01-20 14:37:32 +08:00
int totalCount = 0;
2025-09-16 09:41:37 +08:00
List<ProWorkorder_v2> proWorkorderList = Context
.Queryable<ProWorkorder_v2>()
.Where(predicate)
.OrderBy(it => it.Sort)
.ToPageList(pageNum, pageSize, ref totalCount);
2024-05-31 13:22:07 +08:00
List<ProWorkOrder> orders = new();
foreach (ProWorkorder_v2 item in proWorkorderList)
{
2025-09-16 09:41:37 +08:00
ProWorkOrder proWorkOrder =
new()
{
Id = item.Id,
Week = item.Week,
Year = item.Year,
Date = item.Date,
BlankNumber = item.BlankNumber,
ClientWorkorder = item.ClientWorkorder,
FinishedPartNumber = item.FinishedPartNumber,
ProductDescription = item.ProductDescription,
Specifications = item.Specifications,
Colour = item.Colour,
CodeNumber = item.CodeNumber,
CylinderNumber = item.CylinderNumber,
2025-12-15 15:35:42 +08:00
PQualifiedNum = item.PQqualifiedNum,
PQualifiedRate = item.PQqualifiedRate,
2025-09-16 09:41:37 +08:00
hangNumber = item.hangNumber,
PreviousNumber = item.PreviousNumber,
VehicleNumber = item.VehicleNumber,
CreatedBy = item.CreatedBy,
CreatedTime = item.CreatedTime,
UpdatedBy = item.UpdatedBy,
UpdatedTime = item.UpdatedTime,
Sort = item.Sort,
Status = item.Status,
Remark1 = item.Remark1,
Remark2 = item.Remark2,
Remark3 = item.Remark3,
Remark4 = item.Remark4,
};
2024-05-31 13:22:07 +08:00
proWorkOrder.State = CheckWorkOrder(item);
orders.Add(proWorkOrder);
}
2024-01-20 14:37:32 +08:00
2024-05-31 13:22:07 +08:00
return (orders, totalCount);
2024-01-20 14:37:32 +08:00
}
2024-01-26 16:59:00 +08:00
2025-09-16 09:41:37 +08:00
public (List<ProWorkorder_v2>, int) GetWorkorderList_Piliang(
int pageNum,
int pageSize,
int year,
int week,
int date,
int isSchedule
)
2024-01-26 16:59:00 +08:00
{
2025-09-16 09:41:37 +08:00
var predicate = Expressionable
.Create<ProWorkorder_v2>()
.AndIF(year > 0, it => it.Year == year)
.AndIF(week > 0, it => it.Week == week)
.AndIF(date > 0, it => it.Date == date)
.ToExpression();
2024-01-26 16:59:00 +08:00
int totalCount = 0;
2025-09-16 09:41:37 +08:00
List<ProWorkorder_v2> proWorkorderList = Context
.Queryable<ProWorkorder_v2>()
.Where(predicate)
.Where(it => it.Remark3 == "是")
.OrderBy(it => it.Sort)
.ToPageList(pageNum, pageSize, ref totalCount);
2024-01-26 16:59:00 +08:00
return (proWorkorderList, totalCount);
}
2025-09-16 09:41:37 +08:00
2024-01-20 14:37:32 +08:00
/// <summary>
/// 获取生产计划id
/// </summary>
/// <returns></returns>
private int Getworkplanid_max()
{
2025-09-16 09:41:37 +08:00
ProWorkorder_v2 max_workoder = Context
.Queryable<ProWorkorder_v2>()
.OrderBy(it => it.Id, OrderByType.Desc)
.First();
if (
max_workoder != null
&& !string.IsNullOrEmpty(max_workoder.Id)
&& max_workoder.Id.Substring(2, 8) == DateTime.Now.ToString("yyyyMMdd")
)
2024-01-20 14:37:32 +08:00
{
int num = Convert.ToInt32(max_workoder.Id.Substring(10)) + 1;
return num;
}
else
{
return 0;
}
}
public string ImportExceldata(List<ProWorkorder_v2> workorderList)
{
int max_id = Getworkplanid_max();
// 更新生产计划
if (workorderList != null && workorderList.Count > 0)
{
2024-01-22 20:29:25 +08:00
workorderList.ForEach(item =>
2024-01-20 14:37:32 +08:00
{
2024-01-22 20:29:25 +08:00
item.Id = "WO" + DateTime.Now.ToString("yyyyMMdd") + max_id.ToString("000");
item.Remark4 = "Excel导入";
2024-01-31 17:51:45 +08:00
// 添加属性 ------》 排序规则是 年周日+序列号000
2025-09-16 09:41:37 +08:00
item.Sort = Convert.ToInt32(
item.Year.ToString("0000").Substring(2)
+ item.Week.ToString("00")
+ item.Date.ToString("00")
+ workorderList.IndexOf(item).ToString("000")
);
2024-01-22 20:29:25 +08:00
if (item.Remark2 == "批量")
{
item.Remark3 = "是";
}
item.Status = 0;
2024-01-20 14:37:32 +08:00
max_id++;
});
2024-01-22 20:29:25 +08:00
2024-01-20 14:37:32 +08:00
foreach (ProWorkorder_v2 item in workorderList)
{
2024-01-22 20:29:25 +08:00
// 修正计划
2025-09-16 09:41:37 +08:00
ProWorklplan_v2 planItem = Context
.Queryable<ProWorklplan_v2>()
.Where(it => it.Id == item.FinishedPartNumber)
.First();
2024-01-20 14:37:32 +08:00
if (planItem != null)
{
2025-09-16 09:41:37 +08:00
Context
.Updateable<ProWorklplan_v2>()
.Where(it => it.Id == planItem.Id)
.SetColumns(it =>
it.NoSchedule == planItem.RequireNum - item.hangNumber
)
.ExecuteCommandAsync();
2024-01-20 14:37:32 +08:00
}
}
UseTran(() =>
{
// 删除之前的工单
2025-09-16 09:41:37 +08:00
Context
.Deleteable<ProWorkorder_v2>()
.Where(it => it.Year == workorderList[0].Year)
2024-01-20 14:37:32 +08:00
.Where(it => it.Week == workorderList[0].Week)
2025-09-16 09:41:37 +08:00
.Where(it => it.Date == workorderList[0].Date)
.ExecuteCommand();
2024-01-22 20:29:25 +08:00
2024-01-20 14:37:32 +08:00
//插入
Context.Insertable(workorderList).ExecuteCommand();
});
return "success";
}
//var x = Context.Storageable(workorderList)
// .SplitUpdate(it => it.Any())//存在更新
// .SplitInsert(it => true)//否则插入(更新优先级大于插入)
// .WhereColumns(it => new { it.Year, it.Week, it.Date, it.FinishedPartNumber })//如果不是主键可以这样实现多字段it=>new{it.x1,it.x2}
// .ToStorage();
//x.AsInsertable.ExecuteCommand();//插入可插入部分;
//x.AsUpdateable.IgnoreColumns(it => it.Id).ExecuteCommand();//存在更新
//string msg = string.Format(" 插入{0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4} 总共{5}",
// x.InsertList.Count,
// x.UpdateList.Count,
// x.ErrorList.Count,
// x.IgnoreList.Count,
// x.DeleteList.Count,
// x.TotalList.Count);
2025-09-16 09:41:37 +08:00
return ""; //插入可插入部分
2024-01-20 14:37:32 +08:00
}
/// <summary>
/// 下载
/// </summary>
/// <param name="year"></param>
/// <param name="week"></param>
/// <param name="date"></param>
/// <returns></returns>
public (string, string) ExportExceldata(int year, int week, int date)
{
//1.0 读取表数据
2025-09-16 09:41:37 +08:00
var list = Queryable()
.Where(it => it.Year == year && it.Week == week && it.Date == date)
.OrderBy(it => it.Sort)
.ToList();
2024-01-20 14:37:32 +08:00
//2.0 保存为excel
2025-09-16 09:41:37 +08:00
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)
App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
2024-01-20 14:37:32 +08:00
string sFileName = $"{year}年{week}周{date}日计划-{DateTime.Now:MM-dd-HHmmss}.xlsx";
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
var Sheet1 = new
{
year = year,
week = week,
2024-01-22 15:22:56 +08:00
date = date,
2024-01-20 14:37:32 +08:00
title = $"{year}年车镜实业涂装事业{week}周{date}生产滚动表",
workorder = list
};
2025-09-16 09:41:37 +08:00
string templatePath = Path.Combine(
webHostEnvironment.WebRootPath,
"ImportTemplate",
"日生产计划模板1.xlsx"
);
2024-01-20 14:37:32 +08:00
MiniExcel.SaveAsByTemplate(fullPath, templatePath, Sheet1);
2024-01-22 15:22:56 +08:00
// MiniExcel.SaveAs(fullPath, list);
2024-01-20 14:37:32 +08:00
//3.0 返回路径和文件名
return (sFileName, fullPath);
}
/// <summary>
/// 删除周计划全部
/// </summary>
/// <param name="week"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public int DeleteAllWorkorder(int year, int week, int date)
{
2025-09-16 09:41:37 +08:00
return Context
.Deleteable<ProWorkorder_v2>()
.Where(it => it.Year == year && it.Week == week && it.Date == date)
.ExecuteCommand();
2024-01-20 14:37:32 +08:00
}
2024-01-26 16:59:00 +08:00
2024-01-22 15:22:56 +08:00
/// <summary>
/// 获取生产计划id
/// </summary>
/// <returns></returns>
private int Getworkorderid_max()
{
2025-09-16 09:41:37 +08:00
ProWorkorder_v2 max_workorder = Context
.Queryable<ProWorkorder_v2>()
.OrderBy(it => it.Id, OrderByType.Desc)
.First();
if (
max_workorder != null
&& !string.IsNullOrEmpty(max_workorder.Id)
&& max_workorder.Id.Substring(2, 8) == DateTime.Now.ToString("yyyyMMdd")
)
2024-01-22 15:22:56 +08:00
{
int num = Convert.ToInt32(max_workorder.Id.Substring(10)) + 1;
return num;
}
else
{
return 0;
}
}
public int AddWorkOrder(ProWorkorder_v2 workorder)
{
2025-09-16 09:41:37 +08:00
workorder.Id =
"WO" + DateTime.Now.ToString("yyyyMMdd") + Getworkorderid_max().ToString("000");
2024-01-22 20:29:25 +08:00
workorder.Remark4 = "手动插入";
workorder.Status = 0;
if (workorder.Remark2 == "批量")
{
workorder.Remark3 = "是";
}
// 更改以下工单的顺序
if (workorder.Sort > 0)
{
int max = Convert.ToInt32(workorder.Sort.ToString().Substring(0, 6) + "999");
2025-09-16 09:41:37 +08:00
Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Sort >= workorder.Sort && it.Sort <= max)
.SetColumns(it => it.Sort == it.Sort + 1)
.ExecuteCommand();
2024-01-22 20:29:25 +08:00
}
2024-01-22 15:22:56 +08:00
return Context.Insertable(workorder).ExecuteCommand();
}
public int DeleteWorkOrder(string id)
{
return Context.Deleteable<ProWorkorder_v2>().In(id).ExecuteCommand();
}
public int UpdateWorkOrder(ProWorkorder_v2 workorder)
{
2024-01-24 16:15:23 +08:00
workorder.Status = 0;
if (workorder.Remark2 == "批量")
{
workorder.Remark3 = "是";
}
2025-09-16 09:41:37 +08:00
else
{
workorder.Remark3 = "";
}
2024-01-26 16:59:00 +08:00
2025-09-16 09:41:37 +08:00
return Context
.Updateable(workorder)
.IgnoreColumns(it => new
{
it.CreatedBy,
it.CreatedTime,
it.Remark4,
it.Sort
})
.ExecuteCommand();
2024-01-22 15:22:56 +08:00
}
2024-01-24 16:15:23 +08:00
/// <summary>
/// 更改工单顺序
/// </summary>
2024-01-31 17:51:45 +08:00
/// <param name="id">工单id</param>
/// <param name="sort">新位置占据的排序值</param>
2024-01-24 16:15:23 +08:00
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public int UpdateworkorderSort(string id, int sort)
{
2024-06-07 11:04:26 +08:00
if (sort > 0)
2024-01-24 16:15:23 +08:00
{
int finalreuslt = 0;
int max = Convert.ToInt32(sort.ToString().Substring(0, 6) + "999");
2024-01-31 17:51:45 +08:00
2025-09-16 09:41:37 +08:00
int result = Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Sort >= sort && it.Sort <= max)
.SetColumns(it => it.Sort == it.Sort + 1)
.ExecuteCommand();
2024-06-07 11:04:26 +08:00
2024-01-24 16:15:23 +08:00
if (result > 0)
{
2025-09-16 09:41:37 +08:00
finalreuslt = Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Id == id)
.SetColumns(it => it.Sort == sort)
.ExecuteCommand();
2024-01-24 16:15:23 +08:00
}
return finalreuslt;
}
return 0;
}
/// <summary>
/// 工单开始
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public int StartWorkOrder(string id)
2024-06-07 11:04:26 +08:00
{
2025-09-16 09:41:37 +08:00
return Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Id == id)
.SetColumns(it => it.Status == 1)
.ExecuteCommand();
2024-01-24 16:15:23 +08:00
}
/// <summary>
/// 工单上线取消
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public int CancelWorkOrder(string id)
{
2025-09-16 09:41:37 +08:00
return Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Id == id)
.SetColumns(it => it.Status == 0)
.ExecuteCommand();
2024-01-24 16:15:23 +08:00
}
/// <summary>
/// 生成工单号
/// </summary>
/// <param name="year"></param>
/// <param name="week"></param>
/// <param name="date"></param>
/// <returns></returns>
public int GenerateWorkorder(int year, int week, int date)
{
2024-06-07 11:04:26 +08:00
if (year > 0 && week > 0 && date > 0)
2024-01-24 16:15:23 +08:00
{
2024-01-31 17:51:45 +08:00
DateTime week_first = GetWeekStartTime(year, week);
2024-06-07 11:04:26 +08:00
string date_now = week_first.AddDays(date - 1).ToString("yyMMdd");
2024-01-31 17:51:45 +08:00
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"生成工单号前缀{date_now}");
2025-09-16 09:41:37 +08:00
List<ProWorkorder_v2> workorderList = Context
.Queryable<ProWorkorder_v2>()
.Where(it => it.Year == year && it.Week == week && it.Date == date)
.Where(it => it.Remark3 == "是")
.ToList();
2024-06-07 11:04:26 +08:00
foreach (ProWorkorder_v2 item in workorderList)
{
2024-01-24 16:15:23 +08:00
int index = workorderList.IndexOf(item) + 1;
2024-06-07 11:04:26 +08:00
item.ClientWorkorder = date_now + index.ToString("000");
2024-01-24 16:15:23 +08:00
}
2024-06-07 11:04:26 +08:00
return Context.Updateable(workorderList).ExecuteCommand();
2024-01-24 16:15:23 +08:00
}
2024-06-07 11:04:26 +08:00
return 0;
2024-01-24 16:15:23 +08:00
}
2024-01-31 17:51:45 +08:00
/// <summary>
/// 它接受年份和一年中的周数和星期几作为输入,并返回对应的日期。
/// </summary>
/// <param name="year"></param>
/// <param name="week"></param>
/// <param name="date"></param>
/// <returns></returns>
static DateTime GetDateFromWeek(int year, int weekNumber, DayOfWeek dayOfWeek)
{
DateTime jan1 = new DateTime(year, 1, 1);
2024-06-07 11:04:26 +08:00
int daysOffset = (int)dayOfWeek - (int)jan1.DayOfWeek;
2024-01-31 17:51:45 +08:00
2024-06-07 11:04:26 +08:00
DateTime firstDayOfWeek = jan1.AddDays(7 * (weekNumber - 1) - daysOffset + 2);
2024-01-31 17:51:45 +08:00
return firstDayOfWeek;
}
/// <summary>
/// 当前周的第一天(星期一)
/// </summary>
/// <param name="yearWeek">周数格式yyyywww</param>
/// <returns></returns>
2024-06-07 11:04:26 +08:00
private DateTime GetWeekStartTime(int year, int weekNum)
2024-01-31 17:51:45 +08:00
{
//本年1月1日
DateTime firstOfYear = new DateTime(year, 1, 1);
//周数
2024-06-07 11:04:26 +08:00
2024-01-31 17:51:45 +08:00
//本年1月1日与本周星期一相差的天数
2025-09-16 09:41:37 +08:00
int dayDiff =
(
firstOfYear.DayOfWeek == DayOfWeek.Sunday
? 7
: Convert.ToInt32(firstOfYear.DayOfWeek)
) - 1;
2024-01-31 17:51:45 +08:00
//第一周的星期一
DateTime firstDayOfFirstWeek = firstOfYear.AddDays(-dayDiff);
//当前周的星期一
DateTime firstDayOfThisWeek = firstDayOfFirstWeek.AddDays((weekNum - 1) * 7);
return firstDayOfThisWeek;
}
public int UpdateworkorderSort2(string oldId, int oldSort, string newId, int newSort)
{
int finalreuslt = 0;
2025-09-16 09:41:37 +08:00
int result1 = Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Id == oldId)
.SetColumns(it => it.Sort == newSort)
.ExecuteCommand();
int result2 = Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Id == newId)
.SetColumns(it => it.Sort == oldSort)
.ExecuteCommand();
2024-06-07 11:04:26 +08:00
if (result1 > 0 && result2 > 0)
{
finalreuslt = 1;
}
return finalreuslt;
}
2024-05-31 13:22:07 +08:00
2025-09-16 09:41:37 +08:00
public int UpdateWorkOrderStatus(string id, int status)
{
return Context
.Updateable<ProWorkorder_v2>()
.Where(it => it.Id == id)
.SetColumns(it => it.Status == status)
.ExecuteCommand();
}
2024-05-31 13:22:07 +08:00
/// <summary>
/// 判断
/// </summary>
/// <param name="proWorkplan"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public int CheckWorkOrder(ProWorkorder_v2 proWorkorder)
{
try
{
if (string.IsNullOrEmpty(proWorkorder.FinishedPartNumber))
{
return 0;
}
2025-09-16 09:41:37 +08:00
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.Partnumber == proWorkorder.FinishedPartNumber)
// .Where(it => it.Status == 1)
.First();
2024-05-31 13:22:07 +08:00
// 物料号不存在
if (material == null)
{
return 1;
}
// 毛坯号异常
2025-09-16 09:41:37 +08:00
if (
!string.IsNullOrEmpty(material.BlankNum)
&& material.BlankNum != proWorkorder.BlankNumber
)
2024-05-31 13:22:07 +08:00
{
return 2;
}
// 颜色异常
2025-09-16 09:41:37 +08:00
if (
!string.IsNullOrEmpty(material.Color)
&& !material.Color.Contains(proWorkorder.Colour)
)
2024-05-31 13:22:07 +08:00
{
return 3;
}
// 规格异常
2025-09-16 09:41:37 +08:00
if (
!string.IsNullOrEmpty(material.Specification)
&& !material.Specification.Contains(proWorkorder.Specifications)
)
2024-05-31 13:22:07 +08:00
{
return 4;
}
// 描述异常不包含
2025-09-16 09:41:37 +08:00
if (
!string.IsNullOrEmpty(material.Description)
&& !material.Description.Contains(proWorkorder.ProductDescription)
)
2024-05-31 13:22:07 +08:00
{
return 5;
}
// 全部正常
return 0;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
2024-01-20 14:37:32 +08:00
}
}