工艺标准接口修改

This commit is contained in:
2023-06-08 17:36:22 +08:00
parent 4b818980de
commit c619620da6
9 changed files with 327 additions and 8 deletions

View File

@@ -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<IActionResult> 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<PerProcessParamType> perProcessParamTypes = await db.Queryable<PerProcessParamType>().ToListAsync();
List<PerProcessParam> perProcessParams = await db.Queryable<PerProcessParam>().ToListAsync();
int errorColumnIndex = 5;
ICellStyle style = workbook.CreateCellStyle();
IFont font = workbook.CreateFont();
font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色
style.SetFont(font);
List<PerProcessStandardsD> list = new List<PerProcessStandardsD>() { };
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<PerProcessStandardsD>()
.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<PerProcessStandardsD>()
.SetColumns(x => x.value == item.value).ExecuteCommandAsync();
PerProcessStandardsH perProcessStandardsH = await _repository.AsSugarClient().Queryable<PerProcessStandardsH>().SingleAsync(x => x.id == oldData.process_standards_id);
PerProcessParam processParam = await _repository.AsSugarClient().Queryable<PerProcessParam>().SingleAsync(x => x.id == oldData.process_param_id);
await _repository.AsSugarClient().Updateable<PerProcessStandardsD>()
.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<PerProcessParamEditRecord>(record).ExecuteCommandAsync();
}
}
else
{
await _repository.AsSugarClient().Insertable<PerProcessStandardsD>(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;
}
}
}
}