zhuangpei-mesbackend/DOAN.Service/MES/product/ProWorkorderService.cs
2026-02-05 08:50:08 +08:00

1445 lines
58 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 Aliyun.OSS;
using DOAN.Common;
using DOAN.Model;
using DOAN.Model.MES.base_;
using DOAN.Model.MES.base_.Dto;
using DOAN.Model.MES.BI.Dto;
using DOAN.Model.MES.exception;
using DOAN.Model.MES.group;
using DOAN.Model.MES.product;
using DOAN.Model.MES.product.Dto;
using DOAN.Model.System;
using DOAN.Repository;
using DOAN.Service.MES.product.IService;
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Converter;
using Mapster;
using MathNet.Numerics.Distributions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using MimeKit.Tnef;
using MiniExcelLibs;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using NPOI.XWPF.UserModel;
using Org.BouncyCastle.Asn1.Pkcs;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace DOAN.Service.MES.product
{
/// <summary>
/// 生产工单Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IProWorkorderService), ServiceLifetime = LifeTime.Transient)]
public class ProWorkorderService : BaseService<ProWorkorder>, IProWorkorderService
{
/// <summary>
/// 查询生产工单列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<ProWorkorderDto3> GetList(ProWorkorderQueryDto parm)
{
if (parm.WorkorderDate != null && parm.WorkorderDate.Length > 0)
{
parm.WorkorderDate[0] = parm.WorkorderDate[0].Date;
parm.WorkorderDate[1] = parm.WorkorderDate[1].Date;
}
var predicate = Expressionable.Create<ProWorkorder>()
.AndIF(!string.IsNullOrEmpty(parm.ProductionName), it => it.ProductionName.Contains(parm.ProductionName))
.AndIF(!string.IsNullOrEmpty(parm.ProductionCode), it => it.ProductionCode.Contains(parm.ProductionCode))
.AndIF(!string.IsNullOrEmpty(parm.LineCode), it => it.LineCode == parm.LineCode)
.AndIF(!string.IsNullOrEmpty(parm.GroupCode), it => it.GroupCode == parm.GroupCode)
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[0] > DateTime.MinValue, it => it.WorkorderDate >= parm.WorkorderDate[0])
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[1] > DateTime.MinValue, it => it.WorkorderDate <= parm.WorkorderDate[1])
;
var query = Queryable()
.Where(predicate.ToExpression());
var finalQuery = Context.Queryable(query)
.LeftJoin<BaseMaterialList>((q, m) => q.ProductionCode == m.Code)
.LeftJoin<BaseMaterialList>((q, m, m2) => q.ProductionName == m2.Name)
.LeftJoin<BaseWorkRoute>((q, m, m2, r) => q.LineCode == r.Code)
.LeftJoin<BaseGroup>((q, m, m2, r, g) => q.GroupCode == g.GroupCode)
.LeftJoin<BaseUnit>((q, m, m2, r, g, u) => q.Unit == u.UnitCode)
.Select((q, m, m2, r, g, u) => new ProWorkorderDto3()
{
IsMatch_material_code = m.Code,
IsMatch_material_name = m2.Name,
IsMatch_line = r.Code,
IsMatch_group = g.GroupCode,
IsMatch_Unit = u.UnitCode,
}, true);
var response = finalQuery.MergeTable().Distinct().OrderBy(it => it.WorkorderDate).ToPage<ProWorkorderDto3, ProWorkorderDto3>(parm);
//var query = Queryable()
// .Where(predicate.ToExpression());
////TODO 添加校验
//var query1 = Context.Queryable(query).LeftJoin<BaseMaterialList>((q, m) => q.ProductionCode == m.Code)
// .Select((q, m) => new ProWorkorderDto3()
// {
// IsMatch_material_code = m.Code
// }, true);
//var query2 = Context.Queryable(query1).LeftJoin<BaseMaterialList>((q1, m) => q1.ProductionName == m.Name)
// .Select((q1, m) => new ProWorkorderDto3()
// {
// IsMatch_material_name = m.Name
// }, true);
//var query3 = Context.Queryable(query2).LeftJoin<BaseWorkRoute>((q2, r) => q2.LineCode == r.Code)
// .Select((q2, r) => new ProWorkorderDto3()
// {
// IsMatch_line = r.Code??""
// }, true);
//var query4 = Context.Queryable(query3).LeftJoin<BaseGroup>((q3, g) => q3.GroupCode == g.GroupCode)
// .Select((q3, g) => new ProWorkorderDto3()
// {
// IsMatch_group = g.GroupCode
// }, true);
//var response = query4.MergeTable().OrderBy(it => it.WorkorderDate).ToPage<ProWorkorderDto3, ProWorkorderDto3>(parm);
return response;
}
/// <summary>
/// 获取工单无校验
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<ProWorkorderDto> GetList_NOCheck(ProWorkorderQueryDto parm)
{
var predicate = Expressionable.Create<ProWorkorder>()
.AndIF(!string.IsNullOrEmpty(parm.ProductionName), it => it.ProductionName.Contains(parm.ProductionName))
.AndIF(!string.IsNullOrEmpty(parm.Workorder), it => it.Workorder.Contains(parm.Workorder))
.AndIF(!string.IsNullOrEmpty(parm.ProductionCode), it => it.ProductionCode.Contains(parm.ProductionCode))
.AndIF(!string.IsNullOrEmpty(parm.LineCode), it => it.LineCode == parm.LineCode)
.AndIF(!string.IsNullOrEmpty(parm.GroupCode), it => it.GroupCode == parm.GroupCode)
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[0] > DateTime.MinValue, it => it.WorkorderDate >= parm.WorkorderDate[0])
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[1] > DateTime.MinValue, it => it.WorkorderDate <= parm.WorkorderDate[1])
;
var query = Queryable()
.Where(predicate.ToExpression()).ToPage<ProWorkorder,ProWorkorderDto>(parm);
return query;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public ProWorkorder GetInfo(string Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
/// <summary>
/// 添加生产工单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ProWorkorder AddProWorkorder(ProWorkorder model)
{
model.Id = SnowFlakeSingle.Instance.NextId().ToString();
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改生产工单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateProWorkorder(ProWorkorder model)
{
//增加日志
/*ProWorkorderUpdateLog logObj = new ProWorkorderUpdateLog();
logObj.Id = XueHua;
logObj.Workorder = model.Workorder;
logObj.Log = "修改生产工单";
logObj.ChangeTime = DateTime.Now;
logObj.Operator = model.CreatedBy;
Context.Insertable(logObj).ExecuteCommand();*/
var response = Update(w => w.Id == model.Id, it => new ProWorkorder()
{
ProductionName = model.ProductionName,
ProductionCode = model.ProductionCode,
CustomCode = model.CustomCode,
DeliveryNum = model.DeliveryNum,
Unit = model.Unit,
Specification = model.Specification,
IsCarton = model.IsCarton,
PackageCapacity = model.PackageCapacity,
LineCode = model.LineCode,
GroupCode = model.GroupCode,
WorkorderDate = model.WorkorderDate,
Priority = model.Priority,
Status = model.Status,
Remark = model.Remark,
Beat=model.Beat,
UpdatedBy = model.UpdatedBy,
UpdatedTime = model.UpdatedTime,
});
return response;
}
/// <summary>
/// 生成工单号
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public int Generate_workorder(ProWorkorderQueryDto2 parm)
{
DateTime update_time = parm.WorkorderDate.ToLocalTime().Date;
List<ProWorkorder> proWorkorderList = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == update_time)
.Where(it => it.Status == 1)
.OrderBy(it => it.Sort).ToList();
string maxs = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == update_time)
.Where(it => it.Status == 3).Max(it => it.Workorder);
if (proWorkorderList != null && proWorkorderList.Count() > 0)
{
string baseSort = update_time.ToString("yyyyMMdd");
int index = 1;
if (!string.IsNullOrEmpty(maxs))
{
index = Convert.ToInt32(maxs.Substring(maxs.Length - 3)) + 1;
}
foreach (ProWorkorder item in proWorkorderList)
{
item.Workorder = baseSort + index.ToString("000");
item.Sort = index * 10;
index++;
}
}
return Context.Updateable(proWorkorderList).ExecuteCommand();
}
/// <summary>
/// 插入工单、新增工单
/// </summary>
/// <param name="proWorkorder"></param>
/// <param name="next_id"></param>
/// <returns></returns>
///
public int Insert_workOrder(ProWorkorder proWorkorder, string next_id)
{
int result = 0;
proWorkorder.Id = XueHua;
proWorkorder.WorkorderDate= DOANConvertDateTime.ConvertLocalDate(proWorkorder.WorkorderDate??DateTime.MinValue);
DateTime handleDate = proWorkorder.WorkorderDate.Value;
//插入工单
if (!string.IsNullOrEmpty(next_id) && next_id != "-1")
{
// 向前插工单
UseTran2(() =>
{
ProWorkorder maxs = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == handleDate)
.OrderByDescending(it =>SqlFunc.Right(it.Workorder,3)).First();
int index = 1;
if (!string.IsNullOrEmpty(maxs.Workorder))
{
index = Convert.ToInt32(maxs.Workorder.Substring(maxs.Workorder.Length - 3)) + 1;
}
proWorkorder.Workorder = "K" + handleDate.ToString("yyyyMMdd") + "_" + proWorkorder.GroupCode + "_" + proWorkorder.LineCode + "_" + index.ToString("000");
int sortNum = Context.Queryable<ProWorkorder>().Where(it => it.Id == next_id).Select(it => it.Sort.Value).First();
// 调整序号
Context.Updateable<ProWorkorder>()
.Where(it => it.WorkorderDate == handleDate)
.Where(it => it.Sort >= sortNum)
.SetColumns(it => new ProWorkorder() { Sort = it.Sort + 10 })
.ExecuteCommand();
proWorkorder.Sort = sortNum;
proWorkorder.Status = 1;
//增加日志
ProWorkorderUpdateLog logObj = new ProWorkorderUpdateLog();
logObj.Id = XueHua;
logObj.Workorder = proWorkorder.Workorder;
logObj.Log = "手动新增";
logObj.ChangeTime = DateTime.Now;
logObj.Operator = proWorkorder.CreatedBy;
logObj.ChangeTime= DateTime.Now;
logObj.CreatedBy = proWorkorder.CreatedBy;
UseTran2(() =>
{
result = Context.Insertable(proWorkorder).ExecuteCommand();
Context.Insertable(logObj).ExecuteCommand();
});
});
}
else
{
// 新增工单
int sortNum = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == handleDate).Max(it => it.Sort.Value);
ProWorkorder maxWorkorder = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == handleDate)
.OrderByDescending(it =>SqlFunc.Right(it.Workorder,3)).First();
int index = 0;
if (maxWorkorder == null)
{
index = 1;
proWorkorder.Sort = 10;
}
else
{
string maxs = maxWorkorder.Workorder;
index = Convert.ToInt32(maxs.Substring(maxs.Length - 3)) + 1;
proWorkorder.Sort = maxWorkorder.Sort + 10;
}
proWorkorder.Workorder = "H" + handleDate.ToString("yyyyMMdd") + "_" + proWorkorder.GroupCode + "_" + proWorkorder.LineCode + "_" + index.ToString("000");
proWorkorder.Status = 1;
//增加日志
ProWorkorderUpdateLog logObj = new ProWorkorderUpdateLog();
logObj.Id = XueHua;
logObj.Workorder = proWorkorder.Workorder;
logObj.Log = "手动新增";
logObj.ChangeTime = DateTime.Now;
logObj.Operator = proWorkorder.CreatedBy;
logObj.ChangeTime= DateTime.Now;
logObj.CreatedBy = proWorkorder.CreatedBy;
UseTran2(() =>
{
result = Context.Insertable(proWorkorder).ExecuteCommand();
Context.Insertable(logObj).ExecuteCommand();
});
}
return result;
}
/// <summary>
/// 移动工单移动工单
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
/// <returns></returns>
public int MoveWorkorder(string id, int type)
{
int result = 0;
ProWorkorder toMove = Context.Queryable<ProWorkorder>().Where(it => it.Id == id).First();
var pervious = Context.Queryable<ProWorkorder>()
.Where(it => it.WorkorderDate == toMove.WorkorderDate);
//上移动
if (type == 1)
{
pervious = pervious.Where(it => it.Sort <= toMove.Sort).OrderByDescending(it => it.Sort);
}
//下移
else if (type == 2)
{
pervious = pervious.Where(it => it.Sort >= toMove.Sort).OrderBy(it => it.Sort);
}
ProWorkorder exchange = pervious.Skip(1).Take(1).First();
if (exchange != null)
{
int temp = toMove.Sort.Value;
toMove.Sort = exchange.Sort;
exchange.Sort = temp;
result += Context.Updateable(toMove).ExecuteCommand();
result += Context.Updateable(exchange).ExecuteCommand();
}
return result;
}
/// <summary>
/// 设置工单状态1 init,2 start 3 end 4pause
/// </summary>
/// <param name="id"></param>
/// <param name="status"></param>
/// <returns></returns>
public int SetWorkorderStatus(ProWorkorderStatusDto param)
{
return Context.Updateable<ProWorkorder>()
.SetColumns(it => it.Status == param.Status)
.SetColumns(it => it.UpdatedTime == DateTime.Now)
.Where(it => it.Id == param.Id).ExecuteCommand();
}
/// <summary>
/// 导入工单 必须整删除 整改
/// </summary>
/// <param name="formFile"></param>
/// <returns></returns>
public TData ImportData(IFormFile formFile, string username)
{
TData result = new TData();
string msg = "";
List<ProWorkorder> workorderList = new();
DateTime dateValue = DateTime.MinValue;
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);
// 将单元格的数字值转换为DateTime
dateValue = cell.DateCellValue.Value;
// 遍历每一行
for (int row = 3; row <= sheet.LastRowNum; row++)
{
string tempMsg = "";
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
ProWorkorder workorder = new ProWorkorder();
//存货编码
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(0);
workorder.ProductionCode = currentCell_01?.ToString();
if (currentCell_01 == null || string.IsNullOrEmpty(workorder.ProductionCode))
{
//continue;
tempMsg += "存货编码,";
}
//产品名称
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(1);
workorder.ProductionName = currentCell_02?.ToString();
if (currentCell_02 == null || string.IsNullOrEmpty(workorder.ProductionName))
{
//continue;
tempMsg += "产品名称,";
}
//规格型号
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(2);
workorder.Specification = currentCell_04?.ToString();
if (currentCell_04 == null || string.IsNullOrEmpty(workorder.Specification))
{
//continue;
tempMsg += "规格型号,";
}
//项目号
NPOI.SS.UserModel.ICell currentCell_041 = currentRow.GetCell(3);
workorder.Project = currentCell_041?.ToString();
if (currentCell_041 == null || string.IsNullOrEmpty(workorder.Project))
{
tempMsg += "项目号,";
//continue;
}
//单位
NPOI.SS.UserModel.ICell currentCell_06 = currentRow.GetCell(4);
workorder.Unit = currentCell_06?.ToString();
//交货数量
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(5);
workorder.DeliveryNum = (int)currentCell_07?.NumericCellValue;
if (currentCell_07 == null || currentCell_07.NumericCellValue == 0)
{
tempMsg += "计划数量,";
}
//是否是纸箱
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(6);
if (currentCell_08 == null)
{
workorder.IsCarton = 0;
}
else
{
if (currentCell_08.ToString() == "是")
{
workorder.IsCarton = 1;
}
else if (currentCell_08.ToString() == "否" || string.IsNullOrEmpty(currentCell_08.ToString()))
{
workorder.IsCarton = 0;
}
}
//装箱容积
/*NPOI.SS.UserModel.ICell currentCell_0811 = currentRow.GetCell(6);
workorder.PackageCapacity = (int)currentCell_0811?.NumericCellValue;*/
// 组别code
NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(7);
if (currentCell_09 == null)
{
workorder.GroupCode = string.Empty;
}
else
{
if (currentCell_09.CellType == CellType.Numeric)
{
workorder.GroupCode = currentCell_09.NumericCellValue.ToString();
}
else
{
workorder.GroupCode = currentCell_09.StringCellValue;
}
}
if (string.IsNullOrEmpty(workorder.GroupCode))
{
tempMsg += "组别,";
}
//AB组
NPOI.SS.UserModel.ICell currentCell_091 = currentRow.GetCell(8);
workorder.AorBgroup = currentCell_091?.ToString();
//if (currentCell_091 == null || string.IsNullOrEmpty(workorder.AorBgroup))
//{
// continue;
//}
//组长
NPOI.SS.UserModel.ICell currentCell_092 = currentRow.GetCell(9);
workorder.Groupleader = currentCell_092?.ToString();
//if (currentCell_092 == null || string.IsNullOrEmpty(workorder.Groupleader))
//{
// continue;
//}
// 线别code
NPOI.SS.UserModel.ICell currentCell_10 = currentRow.GetCell(10);
if (currentCell_10 == null)
{
workorder.LineCode = string.Empty;
}
else
{
if (currentCell_10.CellType == CellType.Numeric)
{
workorder.LineCode = currentCell_10.NumericCellValue.ToString();
}
else
{
workorder.LineCode = currentCell_10.StringCellValue;
}
}
if (string.IsNullOrEmpty(workorder.LineCode))
{
tempMsg += "线别,";
}
NPOI.SS.UserModel.ICell currentCell_12 = currentRow.GetCell(11);
workorder.Beat = (int)currentCell_12?.NumericCellValue;
if (currentCell_12 == null || currentCell_12.NumericCellValue == 0)
{
tempMsg += "节拍,";
}
NPOI.SS.UserModel.ICell currentCell_010 = currentRow.GetCell(12);
workorder.Remark = currentCell_010.StringCellValue;
// 优先级
NPOI.SS.UserModel.ICell currentCell_11 = currentRow.GetCell(13);
if (currentCell_11.StringCellValue == "紧急")
{
workorder.Priority = 3;
}
else
{
if (currentCell_11.StringCellValue == "插单")
{
workorder.Priority = 2;
}
else if (currentCell_11.StringCellValue == "正常" || string.IsNullOrEmpty(currentCell_11.StringCellValue))
{
workorder.Priority = 1;
}
}
if (!string.IsNullOrEmpty(tempMsg))
{
msg += $"第{row + 1}行{tempMsg.TrimEnd(',')}不能为空{Environment.NewLine}";
}
else
{
workorder.Id = XueHua;
workorder.CreatedBy = username;
workorder.CreatedTime = DateTime.Now;
workorder.WorkorderDate = dateValue;
workorder.Status = 1;
//工单 2024-9-13-组-线-序号
int index = (row - 2);
workorder.Workorder = dateValue.ToString("yyyyMMdd") + "_" + workorder.GroupCode + "_" + workorder.LineCode + "_" + index.ToString("000");
workorder.Sort = index * 10;
CultureInfo culture = CultureInfo.CurrentCulture;
workorderList.Add(workorder);
}
}
}
}
catch (Exception ex)
{
result.Tag = 0;
result.Message = "读取EXCEL文件失败";
return result;
}
}
if (string.IsNullOrEmpty(msg))
{
UseTran2(() =>
{
Context.Deleteable<ProWorkorder>().Where(it => it.WorkorderDate == dateValue).ExecuteCommand();
Context.Insertable(workorderList).ExecuteCommand();
});
}
else
{
result.Tag = 0;
result.Message = msg;
}
return result;
}
/// <summary>
/// 分批导入,追加导入
/// </summary>
/// <param name="formFile"></param>
/// <param name="username"></param>
/// <returns></returns>
public TData ImportDataAppend(IFormFile formFile, string username)
{
TData result = new TData();
List<ProWorkorder> workorderList = new();
DateTime dateValue = DateTime.MinValue;
string msg = "";
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);
// 将单元格的数字值转换为DateTime
dateValue = cell.DateCellValue.Value;
// 遍历每一行
for (int row = 3; row <= sheet.LastRowNum; row++)
{
string tempMsg = "";
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
ProWorkorder workorder = new ProWorkorder();
//存货编码
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(0);
workorder.ProductionCode = currentCell_01?.ToString();
if (currentCell_01 == null || string.IsNullOrEmpty(workorder.ProductionCode))
{
tempMsg += "存货编码,";
//continue;
}
//产品名称
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(1);
workorder.ProductionName = currentCell_02?.ToString();
if (currentCell_02 == null || string.IsNullOrEmpty(workorder.ProductionName))
{
tempMsg += "产品名称,";
//continue;
}
//规格型号
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(2);
workorder.Specification = currentCell_04?.ToString();
if (currentCell_04 == null || string.IsNullOrEmpty(workorder.Specification))
{
tempMsg += "规格型号,";
//continue;
}
//项目号
NPOI.SS.UserModel.ICell currentCell_041 = currentRow.GetCell(3);
workorder.Project = currentCell_041?.ToString();
if (currentCell_041 == null || string.IsNullOrEmpty(workorder.Project))
{
tempMsg += "项目号,";
//continue;
}
//单位
NPOI.SS.UserModel.ICell currentCell_06 = currentRow.GetCell(4);
workorder.Unit = currentCell_06?.ToString();
//交货数量
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(5);
workorder.DeliveryNum = (int)currentCell_07?.NumericCellValue;
if (currentCell_07 == null || currentCell_07.NumericCellValue == 0)
{
tempMsg += "计划数量,";
}
//是否是纸箱
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(6);
if (currentCell_08 == null)
{
workorder.IsCarton = 0;
}
else
{
if (currentCell_08.ToString() == "是")
{
workorder.IsCarton = 1;
}
else if (currentCell_08.ToString() == "否" || string.IsNullOrEmpty(currentCell_08.ToString()))
{
workorder.IsCarton = 0;
}
}
//装箱容积
/*NPOI.SS.UserModel.ICell currentCell_0811 = currentRow.GetCell(6);
workorder.PackageCapacity = (int)currentCell_0811?.NumericCellValue;*/
// 组别code
NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(7);
if (currentCell_09 == null)
{
workorder.GroupCode = string.Empty;
}
else
{
if (currentCell_09.CellType == CellType.Numeric)
{
workorder.GroupCode = currentCell_09.NumericCellValue.ToString();
}
else
{
workorder.GroupCode = currentCell_09.StringCellValue;
}
}
if (string.IsNullOrEmpty(workorder.GroupCode))
{
tempMsg += "组别,";
}
//AB组
NPOI.SS.UserModel.ICell currentCell_091 = currentRow.GetCell(8);
workorder.AorBgroup = currentCell_091?.ToString();
//if (currentCell_091 == null || string.IsNullOrEmpty(workorder.AorBgroup))
//{
// continue;
//}
//组长
NPOI.SS.UserModel.ICell currentCell_092 = currentRow.GetCell(9);
workorder.Groupleader = currentCell_092?.ToString();
//if (currentCell_092 == null || string.IsNullOrEmpty(workorder.Groupleader))
//{
// continue;
//}
// 线别code
NPOI.SS.UserModel.ICell currentCell_10 = currentRow.GetCell(10);
if (currentCell_10 == null)
{
workorder.LineCode = string.Empty;
}
else
{
if (currentCell_10.CellType == CellType.Numeric)
{
workorder.LineCode = currentCell_10.NumericCellValue.ToString();
}
else
{
workorder.LineCode = currentCell_10.StringCellValue;
}
}
if(string.IsNullOrEmpty(workorder.LineCode))
{
tempMsg += "线别,";
}
NPOI.SS.UserModel.ICell currentCell_12 = currentRow.GetCell(11);
if (currentCell_12 == null || currentCell_12.NumericCellValue == 0)
{
tempMsg += "节拍,";
}
workorder.Beat = (int)currentCell_12?.NumericCellValue;
NPOI.SS.UserModel.ICell currentCell_010 = currentRow.GetCell(12);
workorder.Remark = currentCell_010.StringCellValue;
// 优先级
NPOI.SS.UserModel.ICell currentCell_11 = currentRow.GetCell(13);
if (currentCell_11.StringCellValue == "紧急")
{
workorder.Priority = 3;
}
else
{
if (currentCell_11.StringCellValue == "插单")
{
workorder.Priority = 2;
}
else if (currentCell_11.StringCellValue == "正常" || string.IsNullOrEmpty(currentCell_11.StringCellValue))
{
workorder.Priority = 1;
}
}
if (!string.IsNullOrEmpty(tempMsg))
{
msg += $"第{row + 1}行{tempMsg.TrimEnd(',')}不能为空{Environment.NewLine}";
}
else
{
workorder.Id = XueHua;
workorder.CreatedBy = username;
workorder.CreatedTime = DateTime.Now;
workorder.WorkorderDate = dateValue;
workorder.Status = 1;
//获取当前日期工单序列号 和序号
DateTime currentDate = dateValue.Date;
var MaxWorkorder = Context.Queryable<ProWorkorder>()
.Where(it => it.WorkorderDate == currentDate)
.OrderByDescending(it => it.Sort)
.Select(it => new { it.Workorder, it.Sort })
.First();
//工单 2024-9-13-组-线-序号
int index = (row - 2);
//int flowNum = index +
// Convert.ToInt16(MaxWorkorder.Workorder.Substring(MaxWorkorder.Workorder.Length - 3, 3));
int flowNum = index;
if(MaxWorkorder!= null)
{
flowNum = index + Convert.ToInt16(MaxWorkorder.Workorder.Substring(MaxWorkorder.Workorder.Length - 3, 3));
}
workorder.Workorder = dateValue.ToString("yyyyMMdd") + "_" + workorder.GroupCode + "_" + workorder.LineCode + "_" + flowNum.ToString("000");
workorder.Sort = index * 10 + Convert.ToInt16(MaxWorkorder?.Sort ?? 0);
CultureInfo culture = CultureInfo.CurrentCulture;
workorderList.Add(workorder);
}
}
}
}
catch (Exception ex)
{
result.Tag = 0;
result.Message = ex.Message;
return result;
}
}
if (string.IsNullOrEmpty(msg))
{
UseTran2(() =>
{
// Context.Deleteable<ProWorkorder>().Where(it => it.WorkorderDate == dateValue).ExecuteCommand();
Context.Insertable(workorderList).ExecuteCommand();
});
}
else
{
result.Tag = 0;
result.Message = msg;
}
return result;
}
/// <summary>
/// 工单导出
/// </summary>
/// <param name="exportTime"></param>
/// <param name="pager"></param>
/// <returns></returns>
public PagedInfo<ProWorkorder> WorkOrderExport(DateTime exportTime, PagerInfo pager)
{
exportTime = exportTime.Date;
return Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == exportTime).ToPage(pager);
}
/// <summary>
/// 获取物料信息
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<BaseMaterialList> GetMaterialInfo(BaseMaterialListQueryDto5 parm)
{
var predicate = Expressionable.Create<BaseMaterialList>()
.OrIF(!string.IsNullOrEmpty(parm.Name_or_Code), it => it.Name.Contains(parm.Name_or_Code))
.OrIF(!string.IsNullOrEmpty(parm.Name_or_Code), it => it.Code.Contains(parm.Name_or_Code))
;
return Context.Queryable<BaseMaterialList>()
.Where(predicate.ToExpression()).Take(20).ToList();
}
/// <summary>
/// 获取客户信息
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public List<BaseCustom> GetCustomInfo(BaseCustomQueryDto2 parm)
{
var predicate = Expressionable
.Create<BaseCustom>()
.AndIF(
!string.IsNullOrEmpty(parm.CustomNo),
it => it.CustomNo.Contains(parm.CustomNo)
)
//.AndIF(
// !string.IsNullOrEmpty(parm.CustomNo),
// it => it.CustomName.Contains(parm.CustomNo)
//)
.And(it => it.Status == 1);
var response = Context.Queryable<BaseCustom>()
.Where(predicate.ToExpression())
.OrderBy(it => it.CustomNo)
.Take(20)
.ToList();
return response;
}
public List<BaseWorkRoute> GetProcessRoute(DateTime dateTime)
{
throw new NotImplementedException();
}
/*/// <summary>
/// 获取工艺路线
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public List<BaseWorkRoute> GetProcessRoute(DateTime dateTime)
{
DateTime dataTO = dateTime.ToLocalTime().Date;
var query = Context.Queryable<GroupSchedule>().Where(it => it.ScheduleDate == dataTO);
return Context
.Queryable(query)
.LeftJoin<BaseWorkRoute>((q, p) => q.FkBelongRouteCode == p.Code)
.Select((q, p) => p)
.Distinct()
.ToList();
}*/
public List<BaseWorkRoute> GetAllRoute()
{
return Context.Queryable<BaseWorkRoute>().ToList();
}
/// <summary>
/// 获取班组
/// </summary>
/// <param name="route_code"></param>
/// <returns></returns>
/*public List<GroupSchedule> GetGroupList(string route_code, DateTime dateTime)
{
dateTime = dateTime.ToLocalTime().Date;
return Context.Queryable<GroupSchedule>().Where(it => it.FkBelongRouteCode == route_code)
.Where(it => it.ScheduleDate == dateTime)
.ToList();
}*/
public List<GroupSchedule> GetGroupList(string route_code, DateTime dateTime)
{
throw new NotImplementedException();
}
public List<BaseGroup> GetGroupList()
{
return Context.Queryable<BaseGroup>().Where(it => it.Status == 1).ToList();
}
/// <summary>
/// 查询BOM 及其所需数量
/// </summary>
/// <param name="workorder_num"></param>
/// <returns></returns>
public List<WorkOrderBom> SearchBOMNum(string workorder_num)
{
List<WorkOrderBom> workOrderBoms = new List<WorkOrderBom>();
List<BaseMaterialBom> baseMaterialBoms = null;
ProWorkorder proworkorder = Context.Queryable<ProWorkorder>().Where(it => it.Workorder == workorder_num).First();
if (proworkorder != null)
{
baseMaterialBoms = Context.Queryable<BaseMaterialBom>().Where(it => it.InvCode == proworkorder.ProductionCode).ToList();
if (baseMaterialBoms != null && baseMaterialBoms
.Count() > 0)
{
foreach (var item in baseMaterialBoms)
{
WorkOrderBom objectMaterial = new WorkOrderBom();
objectMaterial.SubInvCode=item.SubInvCode;
objectMaterial.SubInvName = item.SubInvName;
objectMaterial.Iusequantity_Single = item.Iusequantity;
float num = float.Parse(item.Iusequantity) * proworkorder.DeliveryNum.Value;
objectMaterial.Iusequantity_All = num.ToString();
objectMaterial.BOMVersion = item.BOMVersion;
workOrderBoms.Add(objectMaterial);
}
}
}
return workOrderBoms;
}
/// <summary>
/// 工单日志
/// </summary>
/// <param name="workorder"></param>
/// <param name="log"></param>
/// <returns></returns>
public int WorkOrderLog(string workorder, string log, string Operator)
{
ProWorkorderUpdateLog logObj = new ProWorkorderUpdateLog();
logObj.Id = XueHua;
logObj.Workorder = workorder;
logObj.Log = log;
logObj.ChangeTime = DateTime.Now;
logObj.Operator = Operator;
logObj.ChangeTime= DateTime.Now;
logObj.CreatedBy = Operator;
return Context.Insertable(logObj).ExecuteCommand();
}
/// <summary>
/// 工单进度跟踪
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public PagedInfo<ProWorkorderTranceProgressDto> GetWorkorderTraceProgressList(ProWorkorderQueryDto query)
{
var predicate = Expressionable.Create<ProWorkorder>()
.AndIF(!string.IsNullOrEmpty(query.ProductionName), it => it.ProductionName.Contains(query.ProductionName))
.AndIF(!string.IsNullOrEmpty(query.ProductionCode), it => it.ProductionCode.Contains(query.ProductionCode))
.AndIF(!string.IsNullOrEmpty(query.LineCode), it => it.LineCode == query.LineCode)
.AndIF(!string.IsNullOrEmpty(query.GroupCode), it => it.GroupCode == query.GroupCode)
.AndIF(query.WorkorderDate != null && query.WorkorderDate[0] > DateTime.MinValue, it => it.WorkorderDate >= query.WorkorderDate[0])
.AndIF(query.WorkorderDate != null && query.WorkorderDate[1] > DateTime.MinValue, it => it.WorkorderDate <= query.WorkorderDate[1])
.AndIF(query.Status>-1,it=>it.Status==query.Status)
;
var query2 = Queryable()
.Where(predicate.ToExpression());
return Context.Queryable(query2).LeftJoin<ProReportwork>((q, r) => q.Workorder == r.FkWorkorder)
.Select((q, r) => new ProWorkorderTranceProgressDto()
{
PlanNum = q.DeliveryNum,
ActualNum = r.FinishedNum,
}, true).ToPage_NO_Convert<ProWorkorderTranceProgressDto>(query);
}
public int Insert_workOrder2(ProWorkorder proWorkorder, string next_id)
{
int result = 0;
proWorkorder.Id = XueHua;
proWorkorder.WorkorderDate = proWorkorder.WorkorderDate.Value.Date;
if (!string.IsNullOrEmpty(next_id) && next_id != "-1")
{
UseTran2(() =>
{
int sortNum = Context.Queryable<ProWorkorder>().Where(it => it.Id == next_id).Select(it => it.Sort.Value).First();
Context.Updateable<ProWorkorder>()
.Where(it => it.WorkorderDate == proWorkorder.WorkorderDate)
.Where(it => it.Sort >= sortNum)
.SetColumns(it => new ProWorkorder() { Sort = it.Sort + 10 })
.ExecuteCommand();
proWorkorder.Sort = sortNum;
proWorkorder.Status = 1;
Context.Insertable(proWorkorder).ExecuteCommand();
});
}
else
{
DateTime dateOnly = proWorkorder.WorkorderDate.Value.Date;
int sortNum = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == dateOnly).Max(it => it.Sort.Value);
proWorkorder.Sort = sortNum + 10;
proWorkorder.Status = 1;
Context.Insertable(proWorkorder).ExecuteCommand();
}
Generate_workorder(new ProWorkorderQueryDto2()
{
WorkorderDate = proWorkorder.WorkorderDate.Value
});
return result;
}
/// <summary>
/// 导入工单
/// </summary>
/// <param name="formFile"></param>
/// <returns></returns>
public int ImportData2(IFormFile formFile, string username)
{
int result = 0;
List<ProWorkorder> workorderList = new();
DateTime dateValue = DateTime.MinValue;
using (var stream = formFile.OpenReadStream())
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
// 处理第2行 获取日期
IRow secondRow = sheet.GetRow(1);
NPOI.SS.UserModel.ICell cell = secondRow.GetCell(0);
// 将单元格的数字值转换为DateTime
dateValue = cell.DateCellValue.Value;
// 遍历每一行
for (int row = 3; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
ProWorkorder workorder = new ProWorkorder();
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(0);
workorder.ProductionCode = currentCell_01.ToString();
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(1);
workorder.ProductionName = currentCell_02.ToString();
NPOI.SS.UserModel.ICell currentCell_03 = currentRow.GetCell(2);
workorder.CustomCode = currentCell_03.ToString();
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(3);
workorder.Unit = currentCell_04.ToString();
NPOI.SS.UserModel.ICell currentCell_05 = currentRow.GetCell(4);
workorder.DeliveryNum = (int)currentCell_05.NumericCellValue;
NPOI.SS.UserModel.ICell currentCell_06 = currentRow.GetCell(5);
workorder.PackageCapacity = (int)currentCell_06.NumericCellValue;
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(6);
workorder.GroupCode = currentCell_07.ToString();
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(7);
workorder.LineCode = currentCell_08.ToString();
NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(8);
workorder.Remark = currentCell_09.StringCellValue;
workorderList.Add(workorder);
}
}
}
List<ProWorkorder> updates = new();
List<ProWorkorder> inserts = new();
try
{
if (workorderList.Count > 0 && dateValue > DateTime.MinValue)
{
List<ProWorkorder> Exists = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == dateValue).Where(it => it.Status == 0).ToList();
foreach (ProWorkorder item2 in workorderList)
{
int index = 0;
foreach (ProWorkorder item in Exists)
{
if (item.ProductionCode == item2.ProductionCode)
{
item2.Id = item.Id;
item2.UpdatedBy = username;
item2.UpdatedTime = DateTime.Now;
item2.WorkorderDate = dateValue;
CultureInfo culture = CultureInfo.CurrentCulture;
updates.Add(item2);
index++;
}
continue;
}
if (index == 0)
{
item2.Id = XueHua;
item2.CreatedBy = username;
item2.CreatedTime = DateTime.Now;
item2.WorkorderDate = dateValue;
item2.Status = 1;
CultureInfo culture = CultureInfo.CurrentCulture;
inserts.Add(item2);
}
}
}
if (inserts.Count > 0 || updates.Count > 0)
{
UseTran2(() =>
{
//string[] codes = inserts.Select(it => it.ProductionCode).ToArray();
//Context.Deleteable<ProWorkorder>().Where(it => it.WorkorderDate == dateValue)
//.Where(it => it.Status == 0).Where(it => codes.Contains(it.ProductionCode))
//.ExecuteCommand();
result += Context.Insertable(inserts).ExecuteCommand();
result += Context.Updateable(updates).IgnoreColumns(true).ExecuteCommand();
});
}
}
catch (Exception ex)
{
result = -1;
}
return result;
}
/// <summary>
/// 甘特图
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<GanttChartDto> GetGanttChart(GanttChartQueryDto query)
{
List<string> groupCodes = new List<string>();
// 确定要查询的组别(全部或指定)
if (query == null || query.GroupCode == null || !query.GroupCode.Any())
{
groupCodes = Context.Queryable<ProWorkorder>()
.Where(p => !string.IsNullOrEmpty(p.GroupCode))
.Select(p => p.GroupCode)
.Distinct()
.ToList();
}
else
{
groupCodes = query.GroupCode
.Where(item => !string.IsNullOrEmpty(item))
.ToList();
}
var sortedResult = groupCodes
.OrderByDescending(GroupCode =>
!string.IsNullOrEmpty(GroupCode) && char.IsDigit(GroupCode[0]) // 数字开头的排前面
)
.ThenBy(GroupCode => GroupCode.Length) // 按长度升序
.ThenBy(GroupCode => GroupCode) // 按字符串本身升序
.ToList();
var result = new List<GanttChartDto>();
// 遍历每个组别,获取该组的所有工单
foreach (var groupCode in sortedResult)
{
var workorders = Context.Queryable<ProWorkorder>()
.Where(p => p.GroupCode == groupCode)
.WhereIF(query != null && query.QueryDate > DateTime.MinValue,
p => p.WorkorderDate.Value == query.QueryDate)
.Select(p => new GanttChartChildrenDto
{
Workorder = p.Workorder,
ProductionName = p.ProductionName,
DeliveryNum = (double)p.DeliveryNum,
Beat = (double)p.Beat,
StartTime = p.StartTime,
EndTime = p.EndTime,
CalculatedValue = (double)(p.DeliveryNum * p.Beat /3600)
})
.ToList();
// 重新计算每组内的工单时间从0点开始顺序排列
var lastEndTime = new DateTime(query.QueryDate.Year, query.QueryDate.Month, query.QueryDate.Day, 8, 0, 0); // 当天0点
foreach (var workorder in workorders)
{
workorder.StartTime = lastEndTime;
workorder.EndTime = lastEndTime.AddHours(workorder.CalculatedValue);
lastEndTime = (DateTime)workorder.EndTime;
}
// 将当前组的数据添加到结果中
result.Add(new GanttChartDto
{
GroupCode = groupCode,
Workorders = workorders,
AllWorkTime = Math.Round(workorders.Sum(w => w.CalculatedValue), 1)
});
}
return result;
}
public List<BaseGroup> GetOnlyGroupList()
{
var result = Context.Queryable<BaseGroup>()
.Select(b => new BaseGroup()
{
GroupCode = b.GroupCode,
GroupName = b.GroupName
})
.ToList()
.OrderBy(b => b.Id)
.ToList();
return result;
}
}
}