Files
tnb.server/PerMgr/Tnb.PerMgr/PerProcessStandardsService.cs
2024-09-29 09:28:28 +08:00

342 lines
16 KiB
C#

using JNPF.Common.Configuration;
using JNPF.Common.Core.Manager;
using JNPF.Common.Core.Manager.Files;
using JNPF.Common.Filter;
using JNPF.Common.Models;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Systems.Common;
using JNPF.Systems.Entitys.Permission;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
using JNPF.VisualDev.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using SqlSugar;
using Tnb.PerMgr.Entities;
using Tnb.PerMgr.Interfaces;
using Tnb.PerMgr.Entities.Dto;
using Tnb.BasicData.Entities;
using Tnb.EquipMgr.Entities;
using JNPF.Systems.Entitys.System;
using Tnb.BasicData;
namespace Tnb.PerMgr
{
/// <summary>
/// 工艺标准
/// </summary>
[ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
[OverideVisualDev(ModuleId)]
public class PerProcessStandardsService : IPerProcessStandardsService,IOverideVisualDevService, IDynamicApiController, ITransient
{
private const string ModuleId = "26498481456149";
private readonly ISqlSugarRepository<PerProcessStandardsH> _repository;
private readonly IUserManager _userManager;
private readonly FileManager _fileManager;
private readonly FileService _fileService;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
public PerProcessStandardsService(ISqlSugarRepository<PerProcessStandardsH> repository,
FileService fileService,
FileManager fileManager,
IRunService runService,
IVisualDevService visualDevService,
IUserManager userManager)
{
_userManager = userManager;
_repository = repository;
_fileManager = fileManager;
_fileService = fileService;
_runService = runService;
_visualDevService = visualDevService;
OverideFuncs.GetListAsync = GetList;
}
private async Task<dynamic> GetList(VisualDevModelListQueryInput input)
{
ISqlSugarClient db = _repository.AsSugarClient();
Dictionary<string, object>? queryJson = !string.IsNullOrEmpty(input.queryJson) ? Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(input.queryJson) : new Dictionary<string, object>();
string code = queryJson.ContainsKey("code") ? queryJson["code"].ToString() : "";
string processType = queryJson.ContainsKey("process_type") ? queryJson["process_type"].ToString() : "";
string equipId = queryJson.ContainsKey("equip_id") ? queryJson["equip_id"].ToString() : "";
SqlSugarPagedList<PerProcessStandardListOutput> result = await db.Queryable<PerProcessStandardsH>()
.LeftJoin<BasMaterial>((a, b) => a.output_material_id == b.id)
.LeftJoin<ToolMolds>((a,b,c)=>a.molds_id==c.id)
.LeftJoin<EqpEquipment>((a,b,c,d)=>a.equip_id==d.id)
.LeftJoin<DictionaryTypeEntity>((a,b,c,d,e) =>e.EnCode == "ProcrssType")
.LeftJoin<DictionaryDataEntity>((a,b,c,d,e,f) => f.DictionaryTypeId==e.Id && f.EnCode==a.process_type)
.LeftJoin<UserEntity>((a,b,c,d,e,f,g)=>a.create_id==g.Id)
.WhereIF(!string.IsNullOrEmpty(code),a=>a.code.Contains(code))
.WhereIF(!string.IsNullOrEmpty(processType),a=>a.process_type==processType)
.WhereIF(!string.IsNullOrEmpty(equipId),a=>a.equip_id==equipId)
.Select((a, b, c, d,e,f,g) => new PerProcessStandardListOutput
{
id = a.id,
code = a.code,
process_type = f.FullName,
molds_id = c.mold_name,
molds_id_id = a.molds_id,
equip_id = d.name,
equip_id_id = a.equip_id,
output_material_id = b.code+"/" +b.name,
output_material_id_id = a.output_material_id,
moulding_cycle = a.moulding_cycle,
version = a.version,
enabled = a.enabled==1 ? "是" : "否",
remark = a.remark,
create_time = a.create_time.Value.ToString(DbTimeFormat.SS),
create_id = g.RealName,
create_id_id = a.create_id
}).OrderByDescending(a => a.code).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<PerProcessStandardListOutput>.SqlSugarPageResult(result);
}
[AllowAnonymous]
[HttpGet]
public async Task<IActionResult> ExportTemplate()
{
try
{
var db = _repository.AsSugarClient();
string[] perProcessParamTypes = await db.Queryable<PerProcessParamType>().OrderBy(x => x.ordinal).Select(x => x.name).ToArrayAsync();
string[] perProcessParams = await db.Queryable<PerProcessParam>().OrderBy(x => x.ordinal).Select(x => x.name).ToArrayAsync();
XSSFWorkbook workbook = new XSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("BOM详情");
IRow row1 = sheet.CreateRow(0);
string[] titles = new[] { "工艺参数类型", "工艺参数", "设定值" };
for (int i = 0; i < titles.Length; i++)
{
ICell cell1 = row1.CreateCell(i);
cell1.SetCellValue(titles[i]);
sheet.SetColumnWidth(i, 15 * 256);
}
// int rowIndex = 1;
// var column = sheet.GetColumn(0);
// 设置下拉项
var validationHelper = sheet.GetDataValidationHelper();
var constraint = validationHelper.CreateExplicitListConstraint(perProcessParamTypes);
var region = new CellRangeAddressList(1, 1000, 0, 0);
var validation = validationHelper.CreateValidation(constraint, region);
sheet.AddValidationData(validation);
var constraint2 = validationHelper.CreateExplicitListConstraint(perProcessParams);
var region2 = new CellRangeAddressList(1, 1000, 1, 1);
var validation2 = validationHelper.CreateValidation(constraint2, region2);
sheet.AddValidationData(validation2);
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 = "template.xlsx" };
return fileStreamResult;
}
catch (Exception e)
{
Console.WriteLine(e);
throw Oops.Bah("导出失败");
}
}
[HttpPost]
public async Task<Dictionary<string, string>> Import([FromForm] string id, [FromForm] ChunkModel input)
{
int rowIndex = 1;
bool flag = false;
IWorkbook? workbook = null;
try
{
using (Stream stream = input.file.OpenReadStream())
{
// 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 (workbook == null || sheet == null)
throw Oops.Bah("无导入数据");
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();
item.process_standards_id = id;
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);
}
if (row.GetCell(errorColumnIndex) != null)
{
row.GetCell(errorColumnIndex).SetCellValue(row.GetCell(errorColumnIndex).StringCellValue.Substring(1));
}
}
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)
{
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);
string fileName = $"工艺标准导入报错{DateTime.Now.Ticks}.xlsx";
using (MemoryStream ms2 = new MemoryStream(ms.ToArray()))
{
ms2.Position = 0;
await _fileManager.UploadFileByType(ms2, FileVariable.TemporaryFilePath, fileName);
}
return new Dictionary<string, string>()
{
["isSuccess"] = "false",
["msg"] = "部分数据导入失败",
["fileName"] = fileName,
};
}
else
{
return new Dictionary<string, string>()
{
["isSuccess"] = "true",
["msg"] = "导入成功",
};
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
JNPF.Logging.Log.Error("工艺标准导入失败", 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)
{
Console.WriteLine(e);
return false;
}
}
}
}