From c619620da618750fe78d66bc7fc6d9d8c85952b3 Mon Sep 17 00:00:00 2001 From: zhoukeda <1315948824@qq.com> Date: Thu, 8 Jun 2023 17:36:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E8=89=BA=E6=A0=87=E5=87=86=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Tnb.EquipMgr/EquMaintainRecordService.cs | 2 +- .../Dto/ProcessChildDataInput.cs | 6 + .../Dto/ProcessParamTypeChildrenOutput.cs | 2 + .../Dto/ProcessParamTypeDaqListOutput.cs | 8 + .../Entity/PerProcessParamEditRecord.cs | 67 ++++++++ .../Entity/PerProcessStandardsD.cs | 10 ++ .../IPerProcessStandardsService.cs | 8 + .../Tnb.PerMgr/PerProcessParamTypeService.cs | 73 +++++++- .../Tnb.PerMgr/PerProcessStandardsService.cs | 159 ++++++++++++++++++ 9 files changed, 327 insertions(+), 8 deletions(-) create mode 100644 PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeDaqListOutput.cs create mode 100644 PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessParamEditRecord.cs diff --git a/EquipMgr/Tnb.EquipMgr/EquMaintainRecordService.cs b/EquipMgr/Tnb.EquipMgr/EquMaintainRecordService.cs index 62c6dc6f..4beaf951 100644 --- a/EquipMgr/Tnb.EquipMgr/EquMaintainRecordService.cs +++ b/EquipMgr/Tnb.EquipMgr/EquMaintainRecordService.cs @@ -79,7 +79,7 @@ namespace Tnb.EquipMgr { await _repository.AsSugarClient().Updateable() .SetColumns(x=>x.result==item["result"]) - .SetColumns(x=>x.maintain_descrip==item["maintain_descrip"]) + .SetColumnsIF(item.ContainsKey("maintain_descrip"),x=>x.maintain_descrip==item["maintain_descrip"]) .Where(x=>x.id==item["id"]) .ExecuteCommandAsync(); } diff --git a/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessChildDataInput.cs b/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessChildDataInput.cs index 1e8b577e..9c9b4ea0 100644 --- a/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessChildDataInput.cs +++ b/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessChildDataInput.cs @@ -3,11 +3,17 @@ namespace Tnb.PerMgr.Entities.Dto public class ProcessChildDataInput { + public string id { get; set; } public string process_standards_id { get; set; } /// /// 工艺参数id /// public string process_param_id { get; set; } + /// + /// 工艺参数类型id + /// + public string process_param_type_id { get; set; } public decimal value { get; set; } + public string daq_id { get; set; } } } \ No newline at end of file diff --git a/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeChildrenOutput.cs b/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeChildrenOutput.cs index 1725773c..c11cf8ea 100644 --- a/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeChildrenOutput.cs +++ b/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeChildrenOutput.cs @@ -14,5 +14,7 @@ namespace Tnb.PerMgr.Entities.Dto public decimal? value { get; set; } public decimal upper_value { get; set; } public decimal lower_value { get; set; } + + public string daq_id { get; set; } } } \ No newline at end of file diff --git a/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeDaqListOutput.cs b/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeDaqListOutput.cs new file mode 100644 index 00000000..1aa5c6e7 --- /dev/null +++ b/PerMgr/Tnb.PerMgr.Entities/Dto/ProcessParamTypeDaqListOutput.cs @@ -0,0 +1,8 @@ +namespace Tnb.PerMgr.Entities.Dto +{ + public class ProcessParamTypeDaqListOutput + { + public string id { get; set; } + public string label_name { get; set; } + } +} \ No newline at end of file diff --git a/PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessParamEditRecord.cs b/PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessParamEditRecord.cs new file mode 100644 index 00000000..0be7c529 --- /dev/null +++ b/PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessParamEditRecord.cs @@ -0,0 +1,67 @@ +using JNPF.Common.Contracts; +using JNPF.Common.Security; +using SqlSugar; + +namespace Tnb.PerMgr.Entities; + +/// +/// 工艺参数设定值修改记录 +/// +[SugarTable("per_process_param_edit_record")] +public partial class PerProcessParamEditRecord : BaseEntity +{ + public PerProcessParamEditRecord() + { + id = SnowflakeIdHelper.NextId(); + } + /// + /// 工艺参数id + /// + public string process_param_id { get; set; } = string.Empty; + + /// + /// 修改前值 + /// + public decimal old_value { get; set; } + + /// + /// 修改后值 + /// + public decimal new_value { get; set; } + + /// + /// 修改用户 + /// + public string modify_id { get; set; } = string.Empty; + + /// + /// 修改时间 + /// + public DateTime modify_time { get; set; } = DateTime.Now; + + /// + /// 所属组织 + /// + public string? org_id { get; set; } + + /// + /// 工艺参数名 + /// + public string? process_param_name { get; set; } + + /// + /// 设备id + /// + public string equip_id { get; set; } = string.Empty; + + /// + /// 流程任务Id + /// + public string? f_flowtaskid { get; set; } + + /// + /// 流程引擎Id + /// + public string? f_flowid { get; set; } + +} \ No newline at end of file diff --git a/PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessStandardsD.cs b/PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessStandardsD.cs index bbf44f4a..d49222ac 100644 --- a/PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessStandardsD.cs +++ b/PerMgr/Tnb.PerMgr.Entities/Entity/PerProcessStandardsD.cs @@ -18,6 +18,11 @@ public partial class PerProcessStandardsD : BaseEntity /// 工艺标准主表id /// public string process_standards_id { get; set; } = string.Empty; + + /// + /// 工艺参数类型id + /// + public string process_param_type_id { get; set; } = string.Empty; /// /// 工艺参数id @@ -38,5 +43,10 @@ public partial class PerProcessStandardsD : BaseEntity /// 下限 /// public decimal? lower_value { get; set; } + + /// + /// 数据采集id + /// + public string? daq_id { get; set; } } \ No newline at end of file diff --git a/PerMgr/Tnb.PerMgr.Interfaces/IPerProcessStandardsService.cs b/PerMgr/Tnb.PerMgr.Interfaces/IPerProcessStandardsService.cs index 6014b32c..aca8bcf1 100644 --- a/PerMgr/Tnb.PerMgr.Interfaces/IPerProcessStandardsService.cs +++ b/PerMgr/Tnb.PerMgr.Interfaces/IPerProcessStandardsService.cs @@ -1,3 +1,4 @@ +using JNPF.Common.Models; using Microsoft.AspNetCore.Mvc; namespace Tnb.PerMgr.Interfaces @@ -9,5 +10,12 @@ namespace Tnb.PerMgr.Interfaces /// /// public Task ExportTemplate(); + + + /// + /// 导入 + /// + /// + public Task Import(string id,ChunkModel input); } } \ No newline at end of file diff --git a/PerMgr/Tnb.PerMgr/PerProcessParamTypeService.cs b/PerMgr/Tnb.PerMgr/PerProcessParamTypeService.cs index efcc1955..bd9a1651 100644 --- a/PerMgr/Tnb.PerMgr/PerProcessParamTypeService.cs +++ b/PerMgr/Tnb.PerMgr/PerProcessParamTypeService.cs @@ -45,6 +45,11 @@ namespace Tnb.PerMgr { tab_id = x.id, tab_name = x.name, + daq_list = SqlFunc.Subqueryable().Where(a=>a.equip_id==equipId).ToList(a=>new ProcessParamTypeDaqListOutput + { + id = a.id, + label_name = a.label_name, + }), children = SqlFunc.Subqueryable() .LeftJoin((y,z)=>y.tolerance_category_id==z.id) .Where(y=>y.process_param_type_id==x.id) @@ -71,19 +76,67 @@ namespace Tnb.PerMgr { List list = new List(); + List insertIds = new List(); if (input != null && input.Count > 0) { foreach (var item in input) { - list.Add(new PerProcessStandardsD() + PerProcessStandardsD processStandardsD = await db.Queryable().Where(x => + x.process_param_id == item.process_param_id && x.process_standards_id == item.process_standards_id && + item.process_param_type_id == x.process_param_type_id).FirstAsync(); + + if (processStandardsD != null) { - process_standards_id = item.process_standards_id, - process_param_id = item.process_param_id, - value = item.value - }); + PerProcessStandardsH perProcessStandardsH = await db.Queryable().SingleAsync(x => x.id == processStandardsD.process_standards_id); + PerProcessParam processParam = await db.Queryable().SingleAsync(x => x.id == processStandardsD.process_param_id); + + if (processStandardsD.value != item.value) + { + PerProcessParamEditRecord record = new PerProcessParamEditRecord + { + process_param_id = item.process_param_id, + old_value = processStandardsD.value, + new_value = item.value, + modify_id = _userManager.UserId, + modify_time = DateTime.Now, + org_id = _userManager.GetUserInfo().Result.organizeId, + equip_id = perProcessStandardsH.equip_id, + process_param_name = processParam.name + }; + db.Insertable(record).ExecuteCommandAsync(); + } + + } + + if (string.IsNullOrEmpty(item.id)) + { + PerProcessStandardsD insertObj = new PerProcessStandardsD() + { + value = item.value, + process_param_id = item.process_param_id, + process_standards_id = item.process_standards_id, + process_param_type_id = item.process_param_type_id, + daq_id = item.daq_id, + }; + + insertIds.Add(insertObj.id); + await db.Insertable(insertObj).ExecuteCommandAsync(); + } + else + { + await db.Updateable() + .SetColumns(x=>x.value==item.value) + .SetColumns(x=>x.daq_id==item.daq_id) + .Where(x=>x.id==item.id).ExecuteCommandAsync(); + } + } - await db.Insertable(list).ExecuteCommandAsync(); + List notDeleteIds = input.Select(x => x.id).ToList(); + notDeleteIds.AddRange(insertIds); + await db.Deleteable().Where(x => !notDeleteIds.Contains(x.id)).ExecuteCommandAsync(); + + } }); @@ -108,6 +161,11 @@ namespace Tnb.PerMgr { tab_id = a.id, tab_name = a.name, + daq_list = SqlFunc.Subqueryable().Where(e=>e.equip_id==perProcessStandardsH.equip_id).ToList(e=>new ProcessParamTypeDaqListOutput + { + id = e.id, + label_name = e.label_name, + }), children = SqlFunc.Subqueryable() .LeftJoin((b,c)=>b.process_param_id==c.id) .LeftJoin((b,c,d)=>c.tolerance_category_id==d.id) @@ -120,7 +178,8 @@ namespace Tnb.PerMgr name = c.name, value = b.value, upper_value = d.upper_value, - lower_value = d.lower_value + lower_value = d.lower_value, + daq_id = b.daq_id }), }).ToListAsync(); diff --git a/PerMgr/Tnb.PerMgr/PerProcessStandardsService.cs b/PerMgr/Tnb.PerMgr/PerProcessStandardsService.cs index 72ea3b0d..43e5d881 100644 --- a/PerMgr/Tnb.PerMgr/PerProcessStandardsService.cs +++ b/PerMgr/Tnb.PerMgr/PerProcessStandardsService.cs @@ -1,7 +1,9 @@ +using System.Data; using JNPF; using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Helper; +using JNPF.Common.Models; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; @@ -9,6 +11,7 @@ using JNPF.FriendlyException; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; @@ -90,5 +93,161 @@ namespace Tnb.PerMgr } } + + [HttpPost] + public async Task Import([FromForm]string id, [FromForm]ChunkModel input) + { + int rowIndex = 1; + bool flag = false; + try + { + using (Stream stream = input.file.OpenReadStream()) + { + IWorkbook workbook = null; + // 2007版本 + if (input.fileName.IndexOf(".xlsx") > 0) + workbook = new XSSFWorkbook(stream); + else if (input.fileName.IndexOf(".xls") > 0) + workbook = new HSSFWorkbook(stream); + + ISheet sheet = workbook.GetSheetAt(0); + + if (sheet.LastRowNum <= 1) + throw Oops.Bah("无导入数据"); + + var db = _repository.AsSugarClient(); + List perProcessParamTypes = await db.Queryable().ToListAsync(); + List perProcessParams = await db.Queryable().ToListAsync(); + int errorColumnIndex = 5; + + ICellStyle style = workbook.CreateCellStyle(); + IFont font = workbook.CreateFont(); + font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色 + style.SetFont(font); + + List list = new List() { }; + for (rowIndex = 1; rowIndex < sheet.LastRowNum; rowIndex++) + { + IRow row = sheet.GetRow(rowIndex); + ICell cell0 = row.GetCell(0); + ICell cell1 = row.GetCell(1); + ICell cell2 = row.GetCell(2); + + PerProcessStandardsD item = new PerProcessStandardsD(); + PerProcessParamType perProcessParamType = perProcessParamTypes.FirstOrDefault(x => x.name == cell0.StringCellValue); + if (perProcessParamType != null) + { + item.process_param_type_id = perProcessParamType.id; + } + else + { + ICell errorCell = row.GetCell(errorColumnIndex) ?? row.CreateCell(errorColumnIndex); + errorCell.SetCellValue("无该工艺参数类型"); + errorCell.CellStyle = style; + flag = true; + } + + PerProcessParam perProcessParam = perProcessParams.FirstOrDefault(x => x.name == cell1.StringCellValue); + if (perProcessParam != null) + { + item.process_param_id = perProcessParam.id; + } + else + { + ICell errorCell = row.GetCell(errorColumnIndex) ?? row.CreateCell(errorColumnIndex); + errorCell.SetCellValue(errorCell.StringCellValue+",无该工艺参数"); + errorCell.CellStyle = style; + flag = true; + } + if (!TrySetNumberCellValue(cell2, ref item)) + { + ICell errorCell = row.GetCell(errorColumnIndex) ?? row.CreateCell(errorColumnIndex); + errorCell.SetCellValue(errorCell.StringCellValue+",设定值不是数字"); + errorCell.CellStyle = style; + flag = true; + } + else + { + list.Add(item); + } + } + + foreach (var item in list) + { + PerProcessStandardsD oldData = await _repository.AsSugarClient().Queryable() + .FirstAsync(x => + x.process_param_id == item.process_param_id && + x.process_param_type_id == item.process_param_type_id && x.process_standards_id == id); + if (oldData!=null) + { + if (oldData.value != item.value) + { + await _repository.AsSugarClient().Updateable() + .SetColumns(x => x.value == item.value).ExecuteCommandAsync(); + + PerProcessStandardsH perProcessStandardsH = await _repository.AsSugarClient().Queryable().SingleAsync(x => x.id == oldData.process_standards_id); + PerProcessParam processParam = await _repository.AsSugarClient().Queryable().SingleAsync(x => x.id == oldData.process_param_id); + + await _repository.AsSugarClient().Updateable() + .SetColumns(x=>x.value==item.value) + .Where(x=>x.id==oldData.id).ExecuteCommandAsync(); + + PerProcessParamEditRecord record = new PerProcessParamEditRecord + { + process_param_id = item.process_param_id, + old_value = oldData.value, + new_value = item.value, + modify_id = _userManager.UserId, + modify_time = DateTime.Now, + org_id = _userManager.GetUserInfo().Result.organizeId, + equip_id = perProcessStandardsH.equip_id, + process_param_name = processParam.name + }; + await _repository.AsSugarClient().Insertable(record).ExecuteCommandAsync(); + } + } + else + { + await _repository.AsSugarClient().Insertable(item).ExecuteCommandAsync(); + } + } + + + if (flag) + { + MemoryStream ms = new MemoryStream(); + + workbook.Write(ms); + MemoryStream ms2 = new MemoryStream(ms.ToArray()); + ms2.Position = 0; + FileStreamResult fileStreamResult = new FileStreamResult(ms2, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "导入报错说明.xlsx" }; + return fileStreamResult; + } + else + { + return new JsonResult("导入成功"); + } + } + } + catch (Exception e) + { + throw Oops.Bah("导入失败"); + } + + } + + + private bool TrySetNumberCellValue(ICell cell,ref PerProcessStandardsD item) + { + try + { + item.value = cell.CellType == CellType.Numeric ? (decimal)cell.NumericCellValue : cell.CellType == CellType.String ? Convert.ToDecimal(cell.StringCellValue) : 0; + return true; + } + catch (Exception e) + { + return false; + } + } } } \ No newline at end of file