学 赵 1 year ago
parent
commit
400e732a6b
  1. 2
      code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/models/vmi/vmi.js
  2. 114
      code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs
  3. 4
      code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Vmi/VmiBalanceBase.cs
  4. 19
      code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Vmi/VmiLog.cs
  5. 8
      code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Vmi/VmiType.cs
  6. 9
      code/src/Shared/Win.Sfs.Shared/SharedExtensions.cs

2
code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/wwwroot/models/vmi/vmi.js

@ -7,7 +7,7 @@ function useSchema() {
type: "object",
properties: {
realPartCode: {
title: "LU零件号",
title: "厂内零件号",
type: "string",
rules: [
{

114
code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs

@ -21,7 +21,6 @@ using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Extensions;
using RestSharp.Extensions;
using SettleAccount.Job.SignalR;
using SqlSugar;
@ -37,6 +36,7 @@ using Win.Sfs.SettleAccount.Entities.BQ.Dtos;
using Win.Sfs.SettleAccount.Entities.BQ.Vmi;
using Win.Sfs.Shared.Filter;
using Win.Sfs.Shared.RepositoryBase;
using Win.Sfs.Shared;
namespace Win.Sfs.SettleAccount.Entities.BQ;
@ -82,7 +82,7 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran
{
using var ms = new MemoryStream();
await files.FirstOrDefault().OpenReadStream().CopyToAsync(ms).ConfigureAwait(false);
return this.ImportInternal<PUB_ADJ_DETAIL_IMP_DTO>(ms.ToArray());
return this.ImportInternal<PUB_ADJ_DETAIL_IMP_DTO>(ms.ToArray(), out var validationResults);
}
/// <summary>
@ -279,7 +279,7 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran
{
query = query.Where(input.Filters.ToLambda<VmiLog>());
}
if(input.LogTypes.Count > 0)
if (input.LogTypes.Count > 0)
{
query = query.Where(o => input.LogTypes.Contains(o.LogType));
}
@ -295,7 +295,7 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran
[HttpPost]
public async Task<string> LogExport(RequestDto input)
{
var entities = await _logRepository.GetListByFilterAsync(input.Filters, input.Sorting, input.MaxResultCount, input.SkipCount).ConfigureAwait(false);
var entities = await _logRepository.GetListByFilterAsync(input.Filters, input.Sorting).ConfigureAwait(false);
var fileName = $"库存事务_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}.xlsx";
var content = this.GetContent(entities, "库存事务_");
await _fileContainer.SaveAsync(fileName, content, true).ConfigureAwait(false);
@ -367,45 +367,88 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran
/// </summary>
/// <param name="files"></param>
[HttpPost]
public async Task Import(List<IFormFile> files)
public async Task<IActionResult> Import(List<IFormFile> files)
{
using var ms = new MemoryStream();
await files.FirstOrDefault().OpenReadStream().CopyToAsync(ms).ConfigureAwait(false);
var list = this.ImportInternal<VmiLog>(ms.ToArray());
foreach (var file in list)
try
{
using var ms = new MemoryStream();
var file = files.FirstOrDefault();
await file.OpenReadStream().CopyToAsync(ms).ConfigureAwait(false);
var data = ms.ToArray();
var list = this.ImportInternal<VmiLog>(data, out var validationResults);
if (validationResults.Any(o => o.Count > 0))
{
using var workbook = new XLWorkbook(new MemoryStream(data));
var ws = workbook.Worksheets.FirstOrDefault();
var header = ws.Row(1);
var errorIndex = ws.ColumnsUsed().Count();
header.Cell(errorIndex).Value = "提示信息";
for (int i = 2; i < ws.RowsUsed().Count(); i++)
{
ws.Row(2).Cell(errorIndex).Value = string.Join(',', validationResults[i - 2].Select(o => o.ErrorMessage));
}
SetStyle(ws);
using var stream = new MemoryStream();
workbook.SaveAs(stream);
stream.Seek(0, SeekOrigin.Begin);
var fileName = $"{file.Name}_错误信息.xlsx";
await this._fileContainer.SaveAsync(fileName, stream, true).ConfigureAwait(false);
return new JsonResult(new { code = 400, message = "输入异常", fileName });
}
foreach (var item in list)
{
Adjust(item);
}
return new JsonResult(new { code = 200, message = "ok" });
}
catch (Exception ex)
{
Adjust(file);
Console.WriteLine(ex.ToString());
throw;
}
}
private List<T> ImportInternal<T>(byte[] data)
private List<T> ImportInternal<T>(byte[] data, out List<List<ValidationResult>> validationResults)
{
var list = new List<T>();
using var workbook = new XLWorkbook(new MemoryStream(data));
var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty)
.Where(o => o.GetAttributes<ImporterHeaderAttribute>().Any())
.ToDictionary(o => o.GetAttribute<ImporterHeaderAttribute>().Name, o => o);
var ws = workbook.Worksheets.FirstOrDefault();
for (int rowIndex = 2; rowIndex <= ws.RowsUsed().Count(); rowIndex++)
validationResults = new List<List<ValidationResult>>();
try
{
var row = ws.Row(rowIndex);
var model = Activator.CreateInstance<T>();
list.Add(model);
for (var columnIndex = 1; columnIndex < ws.ColumnsUsed().Count(); columnIndex++)
using var workbook = new XLWorkbook(new MemoryStream(data));
var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty)
.Where(o => o.GetAttributes<ImporterHeaderAttribute>().Any())
.ToDictionary(o => o.GetAttribute<ImporterHeaderAttribute>().Name, o => o);
var ws = workbook.Worksheets.FirstOrDefault();
for (int rowIndex = 2; rowIndex <= ws.RowsUsed().Count(); rowIndex++)
{
var cell = row.Cell(columnIndex);
var headerName = ws.Cell(1, columnIndex).Value.ToString().Trim();
if (properties.TryGetValue(headerName, out var property))
var row = ws.Row(rowIndex);
var model = Activator.CreateInstance<T>();
for (var columnIndex = 1; columnIndex < ws.ColumnsUsed().Count(); columnIndex++)
{
var value = cell.Value.ToString();
if (!string.IsNullOrEmpty(value))
var cell = row.Cell(columnIndex);
var headerName = ws.Cell(1, columnIndex).Value.ToString().Trim();
if (properties.TryGetValue(headerName, out var property))
{
property.SetValue(model, Convert.ChangeType(value, property.PropertyType));
var value = cell.Value.ToString();
if (!string.IsNullOrEmpty(value))
{
property.SetValue(model, Convert.ChangeType(value, property.PropertyType));
}
}
}
list.Add(model);
var results = new List<ValidationResult>();
Validator.TryValidateObject(model, new ValidationContext(model, null, null), results);
validationResults.Add(results);
}
return list;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
return list;
}
private byte[] GetContent<TExport>(List<TExport> entities, string name = "sheet1")
@ -435,6 +478,15 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran
SetCell(item, cell, property);
}
}
SetStyle(ws);
using var stream = new MemoryStream();
workbook.SaveAs(stream);
stream.Seek(0, SeekOrigin.Begin);
return stream.ToArray();
}
private static void SetStyle(IXLWorksheet ws)
{
ws.RangeUsed().Style.Border.TopBorder =
ws.RangeUsed().Style.Border.RightBorder =
ws.RangeUsed().Style.Border.BottomBorder =
@ -446,10 +498,6 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran
ws.RangeUsed().SetAutoFilter();
ws.ColumnsUsed().AdjustToContents();
ws.RowsUsed().AdjustToContents();
using var stream = new MemoryStream();
workbook.SaveAs(stream);
stream.Seek(0, SeekOrigin.Begin);
return stream.ToArray();
}
private static void SetCell<TExportModel>(TExportModel? model, IXLCell cell, PropertyInfo property)
@ -466,7 +514,7 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran
}
else if (propertyType.IsEnum)
{
cell.Value = (Enum.Parse(propertyType, value) as Enum)?.GetDisplayName();
cell.Value = (Enum.Parse(propertyType, value) as Enum)?.GetDisplayName();
}
else if (propertyType == typeof(DateTime))
{

4
code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Vmi/VmiBalanceBase.cs

@ -13,7 +13,7 @@ public abstract class VmiBalanceBase : Entity<Guid>
public VmiBalanceBase(Guid id) : base(id)
{
}
[Display(Name = "LU零件号", Order = 6)]
[Display(Name = "厂内零件号", Order = 6)]
public string RealPartCode { get; set; }//原始
[Display(Name = "客户零件号", Order = 7)]
@ -70,7 +70,7 @@ public abstract class VmiBalanceBase : Entity<Guid>
[Display(Name = "结算生产码", Order = 24)]
public string SettlementVinCode { get; set; }
[Display(Name = "结算生产码", Order = 25)]
[Display(Name = "结算厂内生产码", Order = 25)]
public string SettlementPartCode { get; set; }
[Display(Name = "是否补货", Order = 26)]

19
code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Vmi/VmiLog.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Win.Sfs.SettleAccount.Entities.BQ.Vmi;
@ -6,7 +7,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Vmi;
/// <summary>
/// 库存事务
/// </summary>
public class VmiLog : VmiBalanceBase
public class VmiLog : VmiBalanceBase,IValidatableObject
{
public VmiLog()
{
@ -23,6 +24,22 @@ public class VmiLog : VmiBalanceBase
this.Id = id;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(string.IsNullOrEmpty(this.RealPartCode))
{
yield return new ValidationResult("LU零件号不能为空",new string[] { nameof(this.RealPartCode)});
}
if (string.IsNullOrEmpty(this.ErpToLoc))
{
yield return new ValidationResult("ERP库位不能为空", new string[] { nameof(this.ErpToLoc) });
}
if (string.IsNullOrEmpty(this.OrderNum))
{
yield return new ValidationResult("客户客户订单号不能为空", new string[] { nameof(this.OrderNum) });
}
}
[Display(Name = "库存事务分类", Order = 0)]
public VmiLogType LogType { get; set; }

8
code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/Vmi/VmiType.cs

@ -7,14 +7,10 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Vmi;
/// </summary>
public enum VmiType
{
/// <summary>
/// 入库
/// </summary>
[Display(Name = "入库")]
In,
/// <summary>
/// 出库
/// </summary>
[Display(Name = "出库")]
Out
}

9
code/src/Shared/Win.Sfs.Shared/SharedExtensions.cs

@ -1,4 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
@ -11,5 +13,12 @@ namespace Win.Sfs.Shared
var hash = MD5.HashData(Encoding.UTF8.GetBytes(input));
return new Guid(hash);
}
public static string GetDisplayName(this Enum enumValue)
{
var type = enumValue.GetType();
var field = type.GetField(enumValue.ToString());
return field.GetCustomAttribute<DisplayAttribute>()?.Name ?? field.Name;
}
}
}

Loading…
Cancel
Save