Commit 9a506098 by yiyu.li
parents 4a64b74f 8f49119f
......@@ -693,18 +693,24 @@ namespace Siger.ApiWMS.Controllers
public IActionResult ExportLocation()
{
var data = SearchLocation(0, 0, out int count, false);
var excelData = new List<List<string>>();
//title
excelData.Add(GetExcelTitle());
var titleData = new List<string>()
{
"仓库名称"
};
var range = locationtype.GetList(f => f.status == (int)RowState.Valid && f.projectid == ProjectId).OrderBy(f => f.id).Select(f => f.name).ToList();
titleData.AddRange(range);
titleData.Add("是否停用");
titleData.Add("ID");
var excelData = new List<List<string>> { titleData };
//data
foreach (var item in data)
{
var tmp = new List<string>();
tmp.Add(item.storeID.ToString());
var tmp = new List<string>();
tmp.Add(item.storageName);
tmp.AddRange(item.field.Select(q => q.val));
tmp.Add(item.state == (int)RowState.Valid ? "否" : "是");
tmp.Add(item.serialNumber);
//tmp.Add(item.serialNumber);
tmp.Add(item.storeID.ToString());
excelData.Add(tmp);
}
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Http;
......@@ -13,6 +14,7 @@ using Siger.Middlelayer.Log;
using Siger.Middlelayer.Repository.Repositories.Interface;
using Siger.Middlelayer.Utility.Helpers;
using Siger.Middlelayer.Utility.ImportEntities;
using Siger.Middlelayer.WmsRepository.Entities;
using Siger.Middlelayer.WmsRepository.Repositories.Interface;
namespace Siger.ApiWMS.Controllers
......@@ -107,17 +109,18 @@ namespace Siger.ApiWMS.Controllers
private CommonImportResult ImportStorageLocation(string temporaryFilePath)
{
EpPlusExcelHelper<ImportStorageLocation> excelHelper = null;
EPPlusWmsExcelHelper<ImportStorageLocation> excelHelper = null;
try
{
excelHelper = new EpPlusExcelHelper<ImportStorageLocation>(temporaryFilePath);
var messages = excelHelper.CheckExcel();
if (messages.Any())
excelHelper = new EPPlusWmsExcelHelper<ImportStorageLocation>(temporaryFilePath);
var locationTypes = _location_TypeRepository.GetList(q => q.projectid == ProjectId && q.status == (int)RowState.Valid).ToList();
if (!locationTypes.Any())
{
return new CommonImportResult(0, string.Join(';', messages));
throw new BadRequestException(RequestEnum.LocationTypeNotFound);
}
var data = excelHelper.ConvertSheetToList();
var result = _locationRepository.ImportStorageLocation(data, ProjectId, UserId);
var sonLocationTypes = GetSonTypes(0, locationTypes).Select(q => new Tuple<int, string>(q.id, q.name)).ToList();
var data = excelHelper.ConvertSheetToList(sonLocationTypes);
var result = _locationRepository.ImportStorageLocations(data, ProjectId, UserId);
return result;
}
catch (Exception e)
......@@ -131,5 +134,12 @@ namespace Siger.ApiWMS.Controllers
}
}
private IEnumerable<siger_wms_storage_location_type> GetSonTypes(int id, List<siger_wms_storage_location_type> types)
{
var query = from c in types where c.parentid == id select c;
return query.ToList().Concat(query.ToList().SelectMany(t => GetSonTypes(t.id, types)));
}
}
}
\ No newline at end of file
......@@ -1522,5 +1522,15 @@ namespace Siger.Middlelayer.Common
[Description("状态填写可用或停用")]
StatusInputUsefulOrDisabled,
[Description("请填写大于0的整数ID")]
PleaseInputNotZeroIntID,
[Description("请填写仓库名称")]
PleaseInputStorageName,
[Description("请填写仓库名称后面的储位层级")]
PleaseInputLocation,
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using Siger.Middlelayer.Utility.ExcelImport;
using Siger.Middlelayer.Utility.ImportEntities;
using ExcelColumn = Siger.Middlelayer.Utility.ExcelImport.ExcelColumn;
namespace Siger.Middlelayer.Utility.Helpers
{
public class EPPlusWmsExcelHelper<T> : IDisposable where T : ImportBase, new()
{
private ExcelWorksheet Sheet { get; }
private readonly int _language = 0;
public EPPlusWmsExcelHelper(int language = 0)
{
_language = language;
}
public EPPlusWmsExcelHelper(string filePath, int language = 0)
{
_language = language;
if (File.Exists(filePath))
{
var file = new FileInfo(filePath);
var excelPackage = new ExcelPackage(file);
if (excelPackage.Workbook != null && excelPackage.Workbook.Worksheets.Count > 0)
{
Sheet = excelPackage.Workbook.Worksheets.First();
}
else
{
throw new Exception("read excel failed.");
}
}
}
/// <summary>
/// convert excel sheet1 to list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IEnumerable<ImportStorageLocations> ConvertSheetToList(List<Tuple<int, string>> locationTypes)
{
if (Sheet == null)
{
throw new ArgumentNullException(nameof(Sheet));
}
var columns = new List<string> { "ID", "仓库名称" };
foreach(var item in locationTypes)
{
columns.Add(item.Item2);
}
var endRow = 2;
for (var row = 2; row <= Sheet.Dimension.Rows; row++)
{
var columnValues = new List<string>();
for (var i = 1; i <= columns.Count; i++)
{
var val = Sheet.Cells[row, i];
if (val != null)
{
columnValues.Add(val.Text);
}
}
if (columnValues.All(string.IsNullOrWhiteSpace))
{
endRow = row - 1;
break;
}
endRow = row;
}
IList<int> rows = new List<int>();
for (var i = 2; i <= endRow; i++)
{
rows.Add(i);
}
var collection = new List<ImportStorageLocations>();
foreach(var row in rows)
{
var tnew = new ImportStorageLocations();
int i = 1;
foreach (var col in columns)
{
var val = Sheet.Cells[row, i].Value.ToString();
if (i == 1)
{
tnew.ID = val;
}
if (i == 2)
{
tnew.StorageName = val;
}
if (i > 2)
{
var typeId = locationTypes.FirstOrDefault(q => q.Item2 == col);
tnew.Locations.Add(new LocationModels
{
LocationType = typeId == null ? 0 : typeId.Item1,
Location = val
});
}
i++;
}
collection.Add(tnew);
}
return collection;
}
public void Dispose()
{
Sheet?.Dispose();
}
}
}
......@@ -16,4 +16,24 @@ namespace Siger.Middlelayer.Utility.ImportEntities
[ExcelColumn("上级库位")]
public string ParentLocation { get; set; }
}
public class ImportStorageLocations
{
[ExcelColumn("ID")]
public string ID { get; set; }
[ExcelColumn("仓库名称")]
public string StorageName { get; set; }
public int StorageId { get; set; }
public List<LocationModels> Locations { get; set; } = new List<LocationModels>();
}
public class LocationModels
{
public int LocationType { get; set; }
public string Location { get; set; }
}
}
......@@ -11,5 +11,8 @@ namespace Siger.Middlelayer.WmsRepository.Repositories.Interface
string GenNo(int locationid);
string GenInventoryLevel(int material_id, int ProjectId);
CommonImportResult ImportStorageLocation(IEnumerable<ImportStorageLocation> types, int projectId, int mid);
CommonImportResult ImportStorageLocations(IEnumerable<ImportStorageLocations> locations, int projectid, int userid);
}
}
......@@ -4,12 +4,14 @@ using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Siger.Middlelayer.Common;
using Siger.Middlelayer.Common.Extensions;
using Siger.Middlelayer.Common.Helpers;
using Siger.Middlelayer.Common.ModuleEnum;
using Siger.Middlelayer.Utility.ExcelImport;
using Siger.Middlelayer.Utility.ImportEntities;
using Siger.Middlelayer.WmsRepository.Entities;
using Siger.Middlelayer.WmsRepository.Repositories.Interface;
using Siger.Middlelayer.WmsRepository.Request;
namespace Siger.Middlelayer.WmsRepository.Repositories
{
......@@ -75,6 +77,201 @@ namespace Siger.Middlelayer.WmsRepository.Repositories
return str;
}
private bool InsertData(ImportStorageLocations locationEntity, int projectid, int userid, List<siger_wms_storage_location_type> sonLocationTypes)
{
var locationTypeId = sonLocationTypes.LastOrDefault()?.id ?? 0;
var ids = new List<int>();
var parentid = 0;
foreach (var locationType in sonLocationTypes)
{
var Location = locationEntity.Locations.FirstOrDefault(q => q.LocationType == locationType.id);
if (Location == null)
{
return false;
}
var tmp = new siger_wms_storage_location
{
storageid = locationEntity.StorageId,
parentid = parentid,
name = "",
realname = Location.Location,
typeid = Location.LocationType,
serial_number = GenSerialNumber(),
creator = userid,
create_time = DateTime.Now,
updator = userid,
update_time = DateTime.Now,
projectid = projectid,
status = (int)RowState.Valid,
locationid = Location.LocationType == locationTypeId ? locationEntity.ID.ToInt() : 0
};
parentid = InsertLocation(tmp, locationEntity.StorageName, projectid);
if (parentid > 0)
{
ids.Add(parentid);
continue;
}
else
{
foreach (var id in ids)
{
var entity = dbContext.siger_wms_storage_location.FirstOrDefault(q => q.id == id);
dbContext.siger_wms_storage_location.Remove(entity);
}
return false;
}
}
return true;
}
private int InsertLocation(siger_wms_storage_location tmp, string waveHouseName, int projectid)
{
int id = 0;
dbContext.siger_wms_storage_location.Add(tmp);
if (dbContext.SaveChanges() <= 0)
{
return id;
}
//更新编号、层级名称
var sn = new StringBuilder();
sn.Append(tmp.id);
var parent = dbContext.siger_wms_storage_location.FirstOrDefault(f => f.id == tmp.parentid && tmp.parentid != 0 && f.projectid == projectid && f.status == (int)RowState.Valid);
var parentname = "";
if (parent != null)
{
parentname = parent.name;
}
while (parent != null)
{
sn.Insert(0, parent.id + "_");
parent = dbContext.siger_wms_storage_location.FirstOrDefault(f => f.id == parent.parentid && parent.parentid != 0 && f.projectid == projectid && f.status == (int)RowState.Valid);
}
//仓库
sn.Insert(0, tmp.storageid + "_");
tmp.serial_number = sn.ToString();
//上级为仓库
if (tmp.parentid == 0)
{
tmp.name = $"{waveHouseName} -> {tmp.realname}";
}
else
{
tmp.name = $"{parentname} -> {tmp.realname}";
}
dbContext.siger_wms_storage_location.Update(tmp);
if (dbContext.SaveChanges() <= 0)
{
return id;
}
return tmp.id;
}
private IEnumerable<siger_wms_storage_location_type> GetSonTypes(int id, List<siger_wms_storage_location_type> types)
{
var query = from c in types where c.parentid == id select c;
return query.ToList().Concat(query.ToList().SelectMany(t => GetSonTypes(t.id, types)));
}
public CommonImportResult ImportStorageLocations(IEnumerable<ImportStorageLocations> locations, int projectid, int userid)
{
var locationTypes = dbContext.siger_wms_storage_location_type.Where(q => q.projectid == projectid && q.status == (int)RowState.Valid).ToList();
if (!locationTypes.Any())
{
throw new BadRequestException(RequestEnum.LocationTypeNotFound);
}
var sonLocationTypes = GetSonTypes(0, locationTypes).ToList();
var lastLocationTypeId = sonLocationTypes.LastOrDefault()?.id ?? 0;
int rowIndex = 2;
var errors = new List<string>();
var list = new List<ImportStorageLocations>();
foreach (var loca in locations)
{
if(!int.TryParse(loca.ID, out int id))
{
errors.Add($"{rowIndex},{(int)RequestEnum.PleaseInputNotZeroIntID}");
}
if (string.IsNullOrEmpty(loca.StorageName))
{
errors.Add($"{rowIndex},{(int)RequestEnum.PleaseInputStorageName}");
}
if (loca.Locations.Count != sonLocationTypes.Count)
{
errors.Add($"{rowIndex},{(int)RequestEnum.LocationLevelError}");
return new CommonImportResult(0, string.Join(";", errors));
}
var storage = dbContext.siger_wms_storage.FirstOrDefault(q => q.name == loca.StorageName && q.projectid == projectid && q.status == (int)RowState.Valid);
if (storage == null)
{
errors.Add($"{rowIndex},{(int)RequestEnum.StorageError}");
}
else
{
loca.StorageId = storage.id;
}
foreach (var typeId in loca.Locations)
{
if(typeId.LocationType == 0)
{
errors.Add($"{rowIndex},{(int)RequestEnum.LocationLevelError}");
}
if (string.IsNullOrEmpty(typeId.Location))
{
errors.Add($"{rowIndex},{(int)RequestEnum.PleaseInputLocation}");
}
if(storage != null && typeId.LocationType > 0 && loca.ID.ToInt() > 0)
{
var locationIdExist = dbContext.siger_wms_storage_location.FirstOrDefault(q => q.projectid == projectid &&
q.status == (int)RowState.Valid && q.typeid == lastLocationTypeId && q.locationid == loca.ID.ToInt() &&
q.storageid == storage.id);
if (locationIdExist != null || locations.Count(q => q.StorageName == loca.StorageName && q.ID == loca.ID) > 1)
{
errors.Add($"{rowIndex},{(int)RequestEnum.IDExist}");
break;
}
}
}
if (errors.Any())
{
return new CommonImportResult(0, string.Join(";", errors));
}
list.Add(loca);
rowIndex++;
}
rowIndex = 2;
foreach (var loca in list)
{
try
{
if(!InsertData(loca, projectid, userid, locationTypes))
{
errors.Add($"{rowIndex},{(int)RequestEnum.ImportFailed}");
return new CommonImportResult(0, string.Join(";", errors));
}
}
catch(Exception)
{
errors.Add($"{rowIndex},{(int)RequestEnum.ImportFailed}");
return new CommonImportResult(0, string.Join(";", errors));
}
rowIndex++;
}
return new CommonImportResult(1, "1");
}
public CommonImportResult ImportStorageLocation(IEnumerable<ImportStorageLocation> locations, int projectid,int userid)
{
var errors = new List<string>();
......
......@@ -228,6 +228,7 @@ CREATE TABLE IF NOT EXISTS `siger_automation_fixture_tools_assembly` (
`son` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '工装GUID',
`attachment` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '附件',
`filename` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '附件名称',
`remark` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
`projectid` int(11) NOT NULL DEFAULT 0,
`status` int(11) NOT NULL DEFAULT 1,
`creator` int(11) NOT NULL DEFAULT 0,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment