zhuangpei-mesbackend/DOAN.Service/MES/product/ProWorkorderService.cs
2024-08-02 13:39:01 +08:00

528 lines
19 KiB
C#

using System;
using SqlSugar;
using Infrastructure.Attribute;
using DOAN.Model;
using DOAN.Model.MES.product;
using DOAN.Model.MES.product.Dto;
using DOAN.Repository;
using DOAN.Service.MES.product.IService;
using System.Linq;
using SqlSugar.Extensions;
using MimeKit.Tnef;
using DOAN.Model.MES.base_;
using Microsoft.AspNetCore.Http;
using DOAN.Model.System;
using MiniExcelLibs;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Data.Common;
using NPOI.XWPF.UserModel;
using NPOI.HSSF.UserModel;
using Microsoft.AspNetCore.Components.Forms;
using System.Globalization;
using System.ComponentModel;
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.CustomCode), it => it.CustomCode.Contains(parm.CustomCode))
.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])
.AndIF(parm.Year > 0, it => it.Year == parm.Year)
.AndIF(parm.Week > 0, it => it.Week == parm.Week)
.AndIF(parm.Date > 0, it => it.Date == parm.Date)
;
var query = Queryable()
.Where(predicate.ToExpression());
var query1 = Context.Queryable(query).LeftJoin<BaseMaterialList>((q, m) => q.ProductionCode == m.Code)
.Select((q, m) => new ProWorkorderDto3()
{
LackMaterial = m.Code,
}, true);
var query2= Context.Queryable(query1).LeftJoin<BaseWorkRoute>((q,r)=>q.RouteId== r.Code)
.Select((q, r) => new ProWorkorderDto3()
{
LackId = r.Id,
}, true);
var response = query2.MergeTable().OrderBy(it => it.WorkorderDate).ToPage<ProWorkorderDto3, ProWorkorderDto3>(parm);
return response;
}
/// <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)
{
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,
PackageNum = model.PackageNum,
Sort = model.Sort,
WorkorderDate = model.WorkorderDate,
Year = model.Year,
Week = model.Week,
Date = model.Date,
RouteId= model.RouteId,
GroupId= model.GroupId,
Priority = model.Priority,
Status = model.Status,
Remark = model.Remark,
CreatedBy = model.CreatedBy,
CreatedTime = model.CreatedTime,
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.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 = proWorkorder.WorkorderDate.Value.Date;
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();
});
Generate_workorder(new ProWorkorderQueryDto2()
{
WorkorderDate = proWorkorder.WorkorderDate.Value
});
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>
/// 导入工单
/// </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.PackageNum = (int)currentCell_06.NumericCellValue;
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(6);
workorder.GroupId = (int)currentCell_07.NumericCellValue;
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(7);
workorder.RouteId = 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;
item2.Year = dateValue.Year;
CultureInfo culture = CultureInfo.CurrentCulture;
item2.Week = culture.Calendar.GetWeekOfYear(dateValue, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek);
item2.Date = (int)dateValue.DayOfWeek;
updates.Add(item2);
index++;
}
continue;
}
if (index == 0)
{
item2.Id = XueHua;
item2.CreatedBy = username;
item2.CreatedTime = DateTime.Now;
item2.WorkorderDate = dateValue;
item2.Status = 1;
item2.Year = dateValue.Year;
CultureInfo culture = CultureInfo.CurrentCulture;
item2.Week = culture.Calendar.GetWeekOfYear(dateValue, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek);
item2.Date = (int)dateValue.DayOfWeek;
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="formFile"></param>
/// <returns></returns>
public int ImportData(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.PackageNum = (int)currentCell_06.NumericCellValue;
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(6);
workorder.GroupId = (int)currentCell_07.NumericCellValue;
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(7);
workorder.RouteId = currentCell_08.ToString();
NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(8);
workorder.Remark = currentCell_09.StringCellValue;
workorder.Id = XueHua;
workorder.CreatedBy = username;
workorder.CreatedTime = DateTime.Now;
workorder.WorkorderDate = dateValue;
workorder.Status = 1;
workorder.Year = dateValue.Year;
CultureInfo culture = CultureInfo.CurrentCulture;
workorder.Week = culture.Calendar.GetWeekOfYear(dateValue, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek);
workorder.Date = (int)dateValue.DayOfWeek;
workorderList.Add(workorder);
}
}
}
UseTran2(() =>
{
Context.Deleteable<ProWorkorder>().Where(it => it.WorkorderDate == dateValue).ExecuteCommand();
result = Context.Insertable(workorderList).ExecuteCommand();
});
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);
}
}
}