using System;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using BarTender;
using DOAN.Infrastructure.Helper;
using DOAN.Model;
using DOAN.Model.MES.base_;
using DOAN.Model.MES.base_.Dto;
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 MathNet.Numerics.RootFinding;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using MimeKit.Tnef;
using MiniExcelLibs;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.XWPF.UserModel;
using QuestPDF;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using SqlSugar;
using SqlSugar.Extensions;
namespace DOAN.Service.MES.product
{
///
/// 生产工单Service业务层处理
///
[AppService(ServiceType = typeof(IProWorkorderService), ServiceLifetime = LifeTime.Transient)]
public class ProWorkorderService : BaseService, IProWorkorderService
{
///
/// 查询生产工单列表
///
///
///
public PagedInfo GetList(ProWorkorderQueryDto parm)
{
if (parm.WorkorderDate != null && parm.WorkorderDate.Length > 0)
{
parm.WorkorderDate[0] = DOANConvertDate.ConvertLocalDate(parm.WorkorderDate[0]);
parm.WorkorderDate[1] = DOANConvertDate.ConvertLocalDate(parm.WorkorderDate[1]);
parm.WorkorderDate[0] = parm.WorkorderDate[0].Date;
parm.WorkorderDate[1] = parm.WorkorderDate[1].Date;
}
var predicate = Expressionable
.Create()
.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.FeedOrder),
it => it.FeedOrder.Contains(parm.FeedOrder)
)
.AndIF(
!string.IsNullOrEmpty(parm.CustomerOrder),
it => it.CustomerOrder.Contains(parm.CustomerOrder)
)
.AndIF(!string.IsNullOrEmpty(parm.RouteCode), it => it.RouteCode == parm.RouteCode)
.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((q, m) => q.productionCode == m.Code)
.LeftJoin((q, m, m2) => q.productionName == m2.Name)
.LeftJoin((q, m, m2, r) => q.RouteCode == r.Code)
.LeftJoin((q, m, m2, r, g) => q.GroupCode == g.GroupCode)
.LeftJoin((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(parm);
//var query = Queryable()
// .Where(predicate.ToExpression());
////TODO 添加校验
//var query1 = Context.Queryable(query).LeftJoin((q, m) => q.productionCode == m.Code)
// .Select((q, m) => new ProWorkorderDto3()
// {
// IsMatch_material_code = m.Code
// }, true);
//var query2 = Context.Queryable(query1).LeftJoin((q1, m) => q1.productionName == m.Name)
// .Select((q1, m) => new ProWorkorderDto3()
// {
// IsMatch_material_name = m.Name
// }, true);
//var query3 = Context.Queryable(query2).LeftJoin((q2, r) => q2.RouteCode == r.Code)
// .Select((q2, r) => new ProWorkorderDto3()
// {
// IsMatch_line = r.Code??""
// }, true);
//var query4 = Context.Queryable(query3).LeftJoin((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(parm);
return response;
}
///
/// 获取工单无校验
///
///
///
public PagedInfo GetList_NOCheck(ProWorkorderQueryDto parm)
{
if (parm.WorkorderDate != null && parm.WorkorderDate.Length > 0)
{
parm.WorkorderDate[0] = DOANConvertDate.ConvertLocalDate(parm.WorkorderDate[0]);
parm.WorkorderDate[1] = DOANConvertDate.ConvertLocalDate(parm.WorkorderDate[1]);
parm.WorkorderDate[0] = parm.WorkorderDate[0].Date;
parm.WorkorderDate[1] = parm.WorkorderDate[1].Date;
}
var predicate = Expressionable
.Create()
.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.FeedOrder),
it => it.FeedOrder.Contains(parm.FeedOrder)
)
.AndIF(
!string.IsNullOrEmpty(parm.CustomerOrder),
it => it.CustomerOrder.Contains(parm.CustomerOrder)
)
.AndIF(!string.IsNullOrEmpty(parm.RouteCode), it => it.RouteCode == parm.RouteCode)
.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(parm);
return query;
}
///
/// 获取详情
///
///
///
public ProWorkorder GetInfo(string Id)
{
var response = Queryable().Where(x => x.Id == Id).First();
return response;
}
///
/// 添加生产工单
///
///
///
public ProWorkorder AddProWorkorder(ProWorkorder model)
{
model.Id = SnowFlakeSingle.Instance.NextId().ToString();
return Context.Insertable(model).ExecuteReturnEntity();
}
///
/// 修改生产工单
///
///
///
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(model, true);
return response;
}
///
/// 生成工单号
///
///
///
public int Generate_workorder(ProWorkorderQueryDto2 parm)
{
DateTime update_time = parm.WorkorderDate.ToLocalTime().Date;
List proWorkorderList = Context
.Queryable()
.Where(it => it.WorkorderDate == update_time)
.Where(it => it.Status == 1)
.OrderBy(it => it.Sort)
.ToList();
string maxs = Context
.Queryable()
.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();
}
///
/// 插入工单、新增工单
///
///
///
///
///
public int Insert_workOrder(ProWorkorder proWorkorder, string next_id)
{
int result = 0;
proWorkorder.Id = XueHua;
proWorkorder.WorkorderDate = DOANConvertDate.ConvertLocalDate(
proWorkorder.WorkorderDate ?? DateTime.MinValue
);
// 获取 产品代号
List ProductCodeList = Context
.Queryable()
.Where(it => it.DictType == "product_code")
.ToList();
DateTime handleDate = proWorkorder.WorkorderDate.Value;
//插入工单
if (!string.IsNullOrEmpty(next_id) && next_id != "-1")
{
// 向前插工单
UseTran2(() =>
{
ProWorkorder maxs = Context
.Queryable()
.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.RouteCode + "_" + index.ToString("000");
string nickCode = ProductCodeList
.Where(it => it.DictLabel == proWorkorder.productionCode)
.Select(it => it.DictValue)
.FirstOrDefault();
proWorkorder.Workorder =
"K"
+ handleDate.ToString("yyyyMMdd")
+ "_"
+ proWorkorder.RouteCode
+ proWorkorder.GroupCode
+ "_"
+ nickCode
+ "_"
+ index.ToString("000");
int sortNum = Context
.Queryable()
.Where(it => it.Id == next_id)
.Select(it => it.Sort.Value)
.First();
// 调整序号
Context
.Updateable()
.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()
.Where(it => it.WorkorderDate == handleDate)
.Max(it => it.Sort.Value);
ProWorkorder maxWorkorder = Context
.Queryable()
.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.RouteCode + "_" + index.ToString("000");
string nickCode = ProductCodeList
.Where(it => it.DictLabel == proWorkorder.productionCode)
.Select(it => it.DictValue)
.FirstOrDefault();
proWorkorder.Workorder =
"H"
+ handleDate.ToString("yyyyMMdd")
+ "_"
+ proWorkorder.RouteCode
+ proWorkorder.GroupCode
+ "_"
+ nickCode
+ "_"
+ 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;
}
///
/// 移动工单移动工单
///
///
///
///
public int MoveWorkorder(string id, int type)
{
int result = 0;
ProWorkorder toMove = Context
.Queryable()
.Where(it => it.Id == id)
.First();
var pervious = Context
.Queryable()
.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;
}
///
/// 导入工单 必须整删除 整改
///
///
///
public int ImportData(IFormFile formFile, string username)
{
int result = 0;
List workorderList = new();
DateTime dateValue = DateTime.MinValue;
// 获取 产品代号
List ProductCodeList = Context
.Queryable()
.Where(it => it.DictType == "product_code")
.ToList();
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;
#region 读取excel
// 遍历每一行
for (int row = 4; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
ProWorkorder workorder = new ProWorkorder();
//00主体品名
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(0);
workorder.productionName = currentCell_01?.ToString();
if (
currentCell_01 == null
|| string.IsNullOrEmpty(workorder.productionName)
)
{
continue;
}
//01主体型号
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(1);
workorder.productionCode = currentCell_02?.ToString();
if (
currentCell_02 == null
|| string.IsNullOrEmpty(workorder.productionCode)
)
{
continue;
}
//02单位
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(2);
workorder.Unit = currentCell_04?.ToString();
if (currentCell_04 == null || string.IsNullOrEmpty(workorder.Unit))
{
continue;
}
//3 plan_num
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(3);
workorder.PlanNum = (int)currentCell_07?.NumericCellValue;
//4 材料型号
NPOI.SS.UserModel.ICell currentCell_11 = currentRow.GetCell(4);
workorder.MaterialName = currentCell_11?.ToString();
if (
currentCell_11 == null
|| string.IsNullOrEmpty(workorder.MaterialName)
)
{
continue;
}
//5 材料编号
NPOI.SS.UserModel.ICell currentCell_12 = currentRow.GetCell(5);
workorder.MaterialCode = currentCell_12?.ToString();
if (
currentCell_12 == null
|| string.IsNullOrEmpty(workorder.MaterialCode)
)
{
continue;
}
//6 材质
NPOI.SS.UserModel.ICell currentCell_13 = currentRow.GetCell(6);
workorder.MaterialtextureCode = currentCell_13?.ToString();
if (
currentCell_13 == null
|| string.IsNullOrEmpty(workorder.MaterialtextureCode)
)
{
continue;
}
//7 炉号
NPOI.SS.UserModel.ICell currentCell_14 = currentRow.GetCell(7);
workorder.StoveCode = currentCell_14?.ToString();
if (currentCell_14 == null || string.IsNullOrEmpty(workorder.StoveCode))
{
continue;
}
//8 图号
NPOI.SS.UserModel.ICell currentCell_15 = currentRow.GetCell(8);
workorder.DrawingCode = currentCell_15?.ToString();
if (
currentCell_15 == null
|| string.IsNullOrEmpty(workorder.DrawingCode)
)
{
continue;
}
//
//9 版本
NPOI.SS.UserModel.ICell currentCell_16 = currentRow.GetCell(9);
workorder.Version = currentCell_16?.ToString();
if (currentCell_16 == null || string.IsNullOrEmpty(workorder.Version))
{
continue;
}
//10指示日期
NPOI.SS.UserModel.ICell cell17 = currentRow.GetCell(10);
// 将单元格的数字值转换为DateTime
workorder.InstructionDate = cell17.DateCellValue.Value;
//装箱容积
/*NPOI.SS.UserModel.ICell currentCell_0811 = currentRow.GetCell(6);
workorder.PackageCapacity = (int)currentCell_0811?.NumericCellValue;*/
// 11车间code
NPOI.SS.UserModel.ICell currentCell_18 = currentRow.GetCell(11);
if (currentCell_18 == null)
{
workorder.WorkshopCode = string.Empty;
}
else
{
if (currentCell_18.CellType == CellType.Numeric)
{
workorder.WorkshopCode =
currentCell_18.NumericCellValue.ToString();
}
else
{
workorder.WorkshopCode = currentCell_18.StringCellValue;
}
}
//12 组别code
NPOI.SS.UserModel.ICell currentCell_19 = currentRow.GetCell(12);
if (currentCell_19 == null)
{
workorder.GroupCode = string.Empty;
}
else
{
if (currentCell_19.CellType == CellType.Numeric)
{
workorder.GroupCode =
currentCell_19.NumericCellValue.ToString();
}
else
{
workorder.GroupCode = currentCell_19.StringCellValue;
}
}
//13 线别code
NPOI.SS.UserModel.ICell currentCell_20 = currentRow.GetCell(13);
if (currentCell_20 == null)
{
workorder.RouteCode = string.Empty;
}
else
{
if (currentCell_20.CellType == CellType.Numeric)
{
workorder.RouteCode =
currentCell_20.NumericCellValue.ToString();
}
else
{
workorder.RouteCode = currentCell_20.StringCellValue;
}
}
//14 优先级
NPOI.SS.UserModel.ICell currentCell_21 = currentRow.GetCell(14);
if (currentCell_21.StringCellValue == "紧急")
{
workorder.Priority = 3;
}
else
{
if (currentCell_21.StringCellValue == "插单")
{
workorder.Priority = 2;
}
else if (
currentCell_21.StringCellValue == "正常"
|| string.IsNullOrEmpty(currentCell_11.StringCellValue)
)
{
workorder.Priority = 1;
}
}
//15节拍
NPOI.SS.UserModel.ICell currentCell_22 = currentRow.GetCell(15);
workorder.Beat = (int)currentCell_22?.NumericCellValue;
//16进料单号(领料)
NPOI.SS.UserModel.ICell currentCell_010 = currentRow.GetCell(16);
workorder.FeedOrder = currentCell_010?.StringCellValue;
//17 客户单号(出货)
NPOI.SS.UserModel.ICell currentCell_011 = currentRow.GetCell(17);
workorder.CustomerOrder = currentCell_011?.StringCellValue;
//18备注
NPOI.SS.UserModel.ICell currentCell_012 = currentRow.GetCell(18);
workorder.Remark01 = currentCell_012?.StringCellValue;
workorder.Id = XueHua;
workorder.CreatedBy = username;
workorder.CreatedTime = DateTime.Now;
workorder.WorkorderDate = dateValue;
workorder.Status = 1;
//工单 2024-9-13-组-线-序号
int index = (row - 3);
string nickCode = ProductCodeList
.Where(it => it.DictLabel == workorder.productionCode)
.Select(it => it.DictValue)
.FirstOrDefault();
workorder.Workorder =
dateValue.ToString("yyyyMMdd")
+ "_"
+ workorder.GroupCode
+ workorder.RouteCode
+ "_"
+ nickCode
+ "_"
+ index.ToString("000");
workorder.Sort = index * 10;
CultureInfo culture = CultureInfo.CurrentCulture;
workorderList.Add(workorder);
}
}
#endregion
}
catch (Exception ex)
{
return -1;
}
}
UseTran2(() =>
{
Context
.Deleteable()
.Where(it => it.WorkorderDate == dateValue)
.ExecuteCommand();
result = Context.Insertable(workorderList).ExecuteCommand();
});
return result;
}
///
/// 分批导入,追加导入
///
///
///
///
public int ImportDataAppend(IFormFile formFile, string username)
{
int result = 0;
List workorderList = new();
DateTime dateValue = DateTime.MinValue;
// 获取 产品代号
List ProductCodeList = Context
.Queryable()
.Where(it => it.DictType == "product_code")
.ToList();
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 = 4; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
ProWorkorder workorder = new ProWorkorder();
#region 读取excel
//00主体品名
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(0);
workorder.productionName = currentCell_01?.ToString();
if (
currentCell_01 == null
|| string.IsNullOrEmpty(workorder.productionName)
)
{
continue;
}
//01主体型号
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(1);
workorder.productionCode = currentCell_02?.ToString();
if (
currentCell_02 == null
|| string.IsNullOrEmpty(workorder.productionCode)
)
{
continue;
}
//02单位
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(2);
workorder.Unit = currentCell_04?.ToString();
if (currentCell_04 == null || string.IsNullOrEmpty(workorder.Unit))
{
continue;
}
//3 plan_num
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(3);
workorder.PlanNum = (int)currentCell_07?.NumericCellValue;
//4 材料型号
NPOI.SS.UserModel.ICell currentCell_11 = currentRow.GetCell(4);
workorder.MaterialName = currentCell_11?.ToString();
if (
currentCell_11 == null
|| string.IsNullOrEmpty(workorder.MaterialName)
)
{
continue;
}
//5 材料编号
NPOI.SS.UserModel.ICell currentCell_12 = currentRow.GetCell(5);
workorder.MaterialCode = currentCell_12?.ToString();
if (
currentCell_12 == null
|| string.IsNullOrEmpty(workorder.MaterialCode)
)
{
continue;
}
//6 材质
NPOI.SS.UserModel.ICell currentCell_13 = currentRow.GetCell(6);
workorder.MaterialtextureCode = currentCell_13?.ToString();
if (
currentCell_13 == null
|| string.IsNullOrEmpty(workorder.MaterialtextureCode)
)
{
continue;
}
//7 炉号
NPOI.SS.UserModel.ICell currentCell_14 = currentRow.GetCell(7);
workorder.StoveCode = currentCell_14?.ToString();
if (currentCell_14 == null || string.IsNullOrEmpty(workorder.StoveCode))
{
continue;
}
//8 图号
NPOI.SS.UserModel.ICell currentCell_15 = currentRow.GetCell(8);
workorder.DrawingCode = currentCell_15?.ToString();
if (
currentCell_15 == null
|| string.IsNullOrEmpty(workorder.DrawingCode)
)
{
continue;
}
//
//9 版本
NPOI.SS.UserModel.ICell currentCell_16 = currentRow.GetCell(9);
workorder.Version = currentCell_16?.ToString();
if (currentCell_16 == null || string.IsNullOrEmpty(workorder.Version))
{
continue;
}
//10指示日期
NPOI.SS.UserModel.ICell cell17 = currentRow.GetCell(10);
// 将单元格的数字值转换为DateTime
workorder.InstructionDate = cell17.DateCellValue.Value;
//装箱容积
/*NPOI.SS.UserModel.ICell currentCell_0811 = currentRow.GetCell(6);
workorder.PackageCapacity = (int)currentCell_0811?.NumericCellValue;*/
// 11车间code
NPOI.SS.UserModel.ICell currentCell_18 = currentRow.GetCell(11);
if (currentCell_18 == null)
{
workorder.WorkshopCode = string.Empty;
}
else
{
if (currentCell_18.CellType == CellType.Numeric)
{
workorder.WorkshopCode =
currentCell_18.NumericCellValue.ToString();
}
else
{
workorder.WorkshopCode = currentCell_18.StringCellValue;
}
}
//12 组别code
NPOI.SS.UserModel.ICell currentCell_19 = currentRow.GetCell(12);
if (currentCell_19 == null)
{
workorder.GroupCode = string.Empty;
}
else
{
if (currentCell_19.CellType == CellType.Numeric)
{
workorder.GroupCode =
currentCell_19.NumericCellValue.ToString();
}
else
{
workorder.GroupCode = currentCell_19.StringCellValue;
}
}
//13 线别code
NPOI.SS.UserModel.ICell currentCell_20 = currentRow.GetCell(13);
if (currentCell_20 == null)
{
workorder.RouteCode = string.Empty;
}
else
{
if (currentCell_20.CellType == CellType.Numeric)
{
workorder.RouteCode =
currentCell_20.NumericCellValue.ToString();
}
else
{
workorder.RouteCode = currentCell_20.StringCellValue;
}
}
//14 优先级
NPOI.SS.UserModel.ICell currentCell_21 = currentRow.GetCell(14);
if (currentCell_21.StringCellValue == "紧急")
{
workorder.Priority = 3;
}
else
{
if (currentCell_21.StringCellValue == "插单")
{
workorder.Priority = 2;
}
else if (
currentCell_21.StringCellValue == "正常"
|| string.IsNullOrEmpty(currentCell_11.StringCellValue)
)
{
workorder.Priority = 1;
}
}
//15节拍
NPOI.SS.UserModel.ICell currentCell_22 = currentRow.GetCell(15);
workorder.Beat = (int)currentCell_22?.NumericCellValue;
//16进料单号(领料)
NPOI.SS.UserModel.ICell currentCell_010 = currentRow.GetCell(16);
workorder.FeedOrder = currentCell_010?.StringCellValue;
//17 客户单号(出货)
NPOI.SS.UserModel.ICell currentCell_011 = currentRow.GetCell(17);
workorder.CustomerOrder = currentCell_011?.StringCellValue;
//18备注
NPOI.SS.UserModel.ICell currentCell_012 = currentRow.GetCell(18);
workorder.Remark01 = currentCell_012?.StringCellValue;
#endregion
workorder.Id = XueHua;
workorder.CreatedBy = username;
workorder.CreatedTime = DateTime.Now;
workorder.WorkorderDate = dateValue;
workorder.Status = 1;
//获取当前日期工单序列号 和序号
DateTime currentDate = dateValue.Date;
var MaxWorkorder = Context
.Queryable()
.Where(it => it.WorkorderDate == currentDate)
.OrderByDescending(it => it.Sort)
.Select(it => new { it.Workorder, it.Sort })
.First();
//工单 2024-9-13-组-线-序号
int index = (row - 3);
int flowNum =
index
+ Convert.ToInt16(
MaxWorkorder.Workorder.Substring(
MaxWorkorder.Workorder.Length - 3,
3
)
);
//workorder.Workorder = dateValue.ToString("yyyyMMdd") + "_" + workorder.GroupCode + "_" + workorder.RouteCode + "_" + flowNum.ToString("000");
string nickCode = ProductCodeList
.Where(it => it.DictLabel == workorder.productionCode)
.Select(it => it.DictValue)
.FirstOrDefault();
workorder.Workorder =
dateValue.ToString("yyyyMMdd")
+ "_"
+ workorder.GroupCode
+ workorder.RouteCode
+ "_"
+ nickCode
+ "_"
+ index.ToString("000");
workorder.Sort = index * 10 + Convert.ToInt16(MaxWorkorder.Sort);
CultureInfo culture = CultureInfo.CurrentCulture;
workorderList.Add(workorder);
}
}
}
catch (Exception ex)
{
return -1;
}
}
UseTran2(() =>
{
// Context.Deleteable().Where(it => it.WorkorderDate == dateValue).ExecuteCommand();
result = Context.Insertable(workorderList).ExecuteCommand();
});
return result;
}
///
/// 工单导出
///
///
///
///
public PagedInfo WorkOrderExport(DateTime exportTime, PagerInfo pager)
{
exportTime = exportTime.Date;
return Context
.Queryable()
.Where(it => it.WorkorderDate == exportTime)
.ToPage(pager);
}
///
/// 获取物料信息
///
///
///
public List GetMaterialInfo(BaseMaterialListQueryDto5 parm)
{
var predicate = Expressionable
.Create()
.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()
.Where(predicate.ToExpression())
.Take(20)
.ToList();
}
///
/// 获取客户信息
///
///
///
public List GetCustomInfo(BaseCustomQueryDto2 parm)
{
var predicate = Expressionable
.Create()
.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()
.Where(predicate.ToExpression())
.OrderBy(it => it.CustomNo)
.Take(20)
.ToList();
return response;
}
public List GetProcessRoute(DateTime dateTime)
{
throw new NotImplementedException();
}
/*///
/// 获取工艺路线
///
///
///
public List GetProcessRoute(DateTime dateTime)
{
DateTime dataTO = dateTime.ToLocalTime().Date;
var query = Context.Queryable().Where(it => it.ScheduleDate == dataTO);
return Context
.Queryable(query)
.LeftJoin((q, p) => q.FkBelongRouteCode == p.Code)
.Select((q, p) => p)
.Distinct()
.ToList();
}*/
public List GetAllRoute()
{
return Context.Queryable().ToList();
}
///
/// 获取班组
///
///
///
/*public List GetGroupList(string route_code, DateTime dateTime)
{
dateTime = dateTime.ToLocalTime().Date;
return Context.Queryable().Where(it => it.FkBelongRouteCode == route_code)
.Where(it => it.ScheduleDate == dateTime)
.ToList();
}*/
//public List GetGroupList(string route_code, DateTime dateTime)
//{
// throw new NotImplementedException();
//}
public List GetGroupList()
{
return Context.Queryable().Where(it => it.Status == 1).ToList();
}
///
/// 查询BOM 及其所需数量
///
///
///
public List SearchBOMNum(string workorder_num)
{
List workOrderBoms = new List();
List baseMaterialBoms = null;
ProWorkorder proworkorder = Context
.Queryable()
.Where(it => it.Workorder == workorder_num)
.First();
if (proworkorder != null)
{
baseMaterialBoms = Context
.Queryable()
.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.PlanNum.Value;
objectMaterial.Iusequantity_All = num.ToString();
objectMaterial.BOMVersion = item.BOMVersion;
workOrderBoms.Add(objectMaterial);
}
}
}
return workOrderBoms;
}
///
/// 工单日志
///
///
///
///
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();
}
///
/// 工单进度跟踪
///
///
///
public PagedInfo GetWorkorderTraceProgressList(
ProWorkorderQueryDto query
)
{
var predicate = Expressionable
.Create()
.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.RouteCode),
it => it.RouteCode == query.RouteCode
)
.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((q, r) => q.Workorder == r.FkWorkorder)
.Select(
(q, r) =>
new ProWorkorderTranceProgressDto()
{
PlanNum = q.PlanNum,
ActualNum = r.FinishedNum,
},
true
)
.ToPage_NO_Convert(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()
.Where(it => it.Id == next_id)
.Select(it => it.Sort.Value)
.First();
Context
.Updateable()
.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()
.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;
}
///
/// https://www.questpdf.com/
/// 要打印的工单号
///
///
public async Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray)
{
var dataList = Context
.Queryable()
.Where(it => workorderArray.Contains(it.Workorder))
.ToList();
if (dataList.Count() == 0)
{
return (null, null);
}
var ms = new MemoryStream();
Settings.License = LicenseType.Community;
Settings.CheckIfAllTextGlyphsAreAvailable = false;
await Task.Run(() =>
{
var document = QuestPDF.Fluent.Document.Create(container =>
{
int PageWidth = 40 * 10;
int PageHeight = 30 * 10;
for (int i = 0; i < dataList.Count(); i++)
{
byte[] imageBytes = PrintHelper.CreateQcCode(
dataList[i].Workorder,
PageHeight * 40 + 100,
PageHeight * 20
);
container.Page(page =>
{
// 设置页面大小为A4,默认有页边距
//page.Size(new PageSize(40*10,30*10)); // 移除默认页边距或设置自定义边距
page.Size(PageWidth, PageHeight);
page.DefaultTextStyle(TextStyle.Default.FontSize(10 * 2 + 2));
//page.DefaultTextStyle(TextStyle.Default.FontSize(1));
page.Content()
.Column(column =>
{
column
.Item()
.Table(table =>
{
// 动态计算列宽,减去必要的边距
float columnWidth = (PageWidth) / 8;
table.ColumnsDefinition(columns =>
{
for (int j = 0; j < 8; j++)
columns.ConstantColumn(columnWidth);
});
// 创建一个单元格跨越所有列,用于放置图片
table
.Cell()
.ColumnSpan(8)
.Height(PageHeight / 14 * 8)
.Image(imageBytes);
// 剩余内容...
// 注意:确保剩余内容的高度不超过剩余的页面空间
// 这里只是一个简单的例子,实际应用中你可能需要更复杂的逻辑来计算剩余可用空间
table
.Cell()
.ColumnSpan(2)
.Border(1)
.MinHeight(PageHeight / 7)
.AlignLeft()
.Padding(1)
.Text("编号");
table
.Cell()
.ColumnSpan(6)
.Border(1)
.MinHeight(PageHeight / 14)
.AlignLeft()
.Padding(1)
.Text(dataList[i].Workorder);
table
.Cell()
.ColumnSpan(2)
.Border(1)
.MinHeight(PageHeight / 14)
.AlignLeft()
.Padding(1)
.Text("炉号");
table
.Cell()
.ColumnSpan(6)
.Border(1)
.MinHeight(PageHeight / 14)
.AlignLeft()
.Padding(1)
.Text(dataList[i].StoveCode);
table
.Cell()
.ColumnSpan(2)
.Border(1)
.MinHeight(PageHeight / 14)
.AlignLeft()
.Padding(1)
.Text("数量");
table
.Cell()
.ColumnSpan(6)
.Border(1)
.MinHeight(PageHeight / 14)
.AlignLeft()
.Padding(1)
.Text(dataList[i].PlanNum);
for (int j = 0; j < 8; j++)
{
table
.Cell()
.Border(1)
.MinHeight(PageHeight / 14)
.AlignCenter()
.Text($"[{j + 1}]");
}
});
});
});
}
});
document.GeneratePdf(ms);
ms.Seek(0, SeekOrigin.Begin);
});
var fileName = $"工单({DateTime.Now.ToString("yyyyMMdd")}).pdf";
return new(fileName, ms);
}
///
/// 根据模板打印工单信息
///
/// 工单号列表与打印路径
///
public async Task PrintTicketsByTemplate(
ProWorkorderExportDto param
)
{
var dataList = await Context
.Queryable()
.Where(p => param.WorkorderArray.Contains(p.Workorder))
.ToListAsync();
if (dataList.Count == 0)
{
return new CustomException(500, "未找到匹配的工单数据");
}
Application bartenderApp = null;
Format bartenderFormat = null;
try
{
bartenderApp = new Application { Visible = false };
bartenderFormat = bartenderApp.Formats.Open(param.Path);
// 4. 遍历数据并打印
foreach (var data in dataList)
{
bartenderFormat.SetNamedSubStringValue("workorder", data.Workorder);
bartenderFormat.SetNamedSubStringValue("stoveCode", data.StoveCode);
bartenderFormat.SetNamedSubStringValue("qty", data.PlanNum.ToString());
await Task.Delay(500);
bartenderFormat.PrintOut(false, false); // 静默打印
}
return new CustomException(200, "标签打印成功");
}
catch (Exception ex)
{
// 5. 错误处理(记录日志)
Console.WriteLine($"打印标签时出错: {ex.Message}");
return new CustomException(500, $"打印标签失败: {ex.Message}");
}
finally
{
// 6. 确保资源释放(逆序关闭)
if (bartenderFormat != null)
{
bartenderFormat.Close(BtSaveOptions.btDoNotSaveChanges);
}
if (bartenderApp != null)
{
bartenderApp.Quit(BtSaveOptions.btDoNotSaveChanges);
}
}
}
}
}