using Furion.DatabaseAccessor; using Furion.DatabaseAccessor.Extensions; using Furion.DependencyInjection; using Furion.DynamicApiController; using Furion.FriendlyException; using Admin.NET.Core; using Mapster; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Linq.Dynamic.Core; using Microsoft.AspNetCore.Http; using System.Text; using System.Web; using System.ComponentModel; using System.Data; namespace Admin.NET.Application { /// /// 容器信息服务 /// [ApiDescriptionSettings("WmsBase", Name = "WmsContainer", Order = 100)] [Route("api")] public class WmsContainerService : IWmsContainerService, IDynamicApiController, ITransient { private readonly IRepository _wmsContainerRep; private readonly IRepository _sysDictTypeRep; private readonly IRepository _sysDictDataRep; private readonly ISysExcelTemplateService _sysExcelTemplateService; private readonly static object _lock = new(); public WmsContainerService( IRepository wmsContainerRep ,IRepository sysDictTypeRep ,IRepository sysDictDataRep ,ISysExcelTemplateService sysExcelTemplateService ) { _wmsContainerRep = wmsContainerRep; _sysDictTypeRep = sysDictTypeRep; _sysDictDataRep = sysDictDataRep; _sysExcelTemplateService = sysExcelTemplateService; } /// /// 分页查询容器信息 /// /// /// [HttpGet("WmsContainer/page")] public async Task> Page([FromQuery] WmsContainerSearch input) { var wmsContainers = await _wmsContainerRep.DetachedEntities .Where(!string.IsNullOrEmpty(input.ContainerCode), u => u.ContainerCode == input.ContainerCode) .Where(!string.IsNullOrEmpty(input.ContainerName), u => u.ContainerName == input.ContainerName) .Where(input.ContainerTypeId != null, u => u.ContainerTypeId == input.ContainerTypeId) .Where(!string.IsNullOrEmpty(input.ContainerTypeName), u => u.ContainerTypeName == input.ContainerTypeName) .Where(input.PackagingId != null, u => u.PackagingId == input.PackagingId) .Where(input.SpecLength != null, u => u.SpecLength == input.SpecLength) .Where(input.SpecWidth != null, u => u.SpecWidth == input.SpecWidth) .Where(input.SpecHeight != null, u => u.SpecHeight == input.SpecHeight) .Where(input.LimitLength != null, u => u.LimitLength == input.LimitLength) .Where(input.LimitWidth != null, u => u.LimitWidth == input.LimitWidth) .Where(input.LimitHeight != null, u => u.LimitHeight == input.LimitHeight) .Where(input.MaxWeight != null, u => u.MaxWeight == input.MaxWeight) .Where(!string.IsNullOrEmpty(input.ParentContainerName), u => u.ParentContainerName == input.ParentContainerName) .Where(input.ParentContainerId != null, u => u.ParentContainerId == input.ParentContainerId) .Where(input.IsVirtually != null, u => u.IsVirtually == input.IsVirtually) .Where(input.IsDisabled != null, u => u.IsDisabled == input.IsDisabled) .Where(input.CreatedTime!=null, u => u.CreatedTime>= Convert.ToDateTime(input.CreatedTime[0]) && u.CreatedTime<= Convert.ToDateTime(input.CreatedTime[1])) .Where(input.UpdatedTime!=null, u => u.UpdatedTime>= Convert.ToDateTime(input.UpdatedTime[0]) && u.UpdatedTime<= Convert.ToDateTime(input.UpdatedTime[1])) .Where(!string.IsNullOrEmpty(input.CreatedUserName), u => u.CreatedUserName == input.CreatedUserName) .Where(!string.IsNullOrEmpty(input.UpdatedUserName), u => u.UpdatedUserName == input.UpdatedUserName) .OrderBy(PageInputOrder.OrderBuilder(input)) .ProjectToType() .ToADPagedListAsync(input.PageNo, input.PageSize); return wmsContainers; } /// /// 不分页查询容器信息列表 /// /// 容器信息查询参数 /// (容器信息)实例列表 [HttpGet("WmsContainer/listNonPage")] public async Task> ListNonPageAsync([FromQuery] WmsContainerSearchNonPage input) { var pContainerCode = input.ContainerCode?.Trim() ?? ""; var pContainerName = input.ContainerName?.Trim() ?? ""; var pContainerTypeId = input.ContainerTypeId; var pContainerTypeName = input.ContainerTypeName?.Trim() ?? ""; var pPackagingId = input.PackagingId; var pSpecLength = input.SpecLength; var pSpecWidth = input.SpecWidth; var pSpecHeight = input.SpecHeight; var pLimitLength = input.LimitLength; var pLimitWidth = input.LimitWidth; var pLimitHeight = input.LimitHeight; var pMaxWeight = input.MaxWeight; var pParentContainerName = input.ParentContainerName?.Trim() ?? ""; var pParentContainerId = input.ParentContainerId; var pIsVirtually = input.IsVirtually; var pIsDisabled = input.IsDisabled; var pCreatedTime = input.CreatedTime; var pUpdatedTime = input.UpdatedTime; var pCreatedUserName = input.CreatedUserName?.Trim() ?? ""; var pUpdatedUserName = input.UpdatedUserName?.Trim() ?? ""; var wmsContainers = await _wmsContainerRep.DetachedEntities .Where(!string.IsNullOrEmpty(pContainerCode), u => u.ContainerCode == pContainerCode) .Where(!string.IsNullOrEmpty(pContainerName), u => u.ContainerName == pContainerName) .Where(pContainerTypeId != null, u => u.ContainerTypeId == pContainerTypeId) .Where(!string.IsNullOrEmpty(pContainerTypeName), u => u.ContainerTypeName == pContainerTypeName) .Where(pPackagingId != null, u => u.PackagingId == pPackagingId) .Where(pSpecLength != null, u => u.SpecLength == pSpecLength) .Where(pSpecWidth != null, u => u.SpecWidth == pSpecWidth) .Where(pSpecHeight != null, u => u.SpecHeight == pSpecHeight) .Where(pLimitLength != null, u => u.LimitLength == pLimitLength) .Where(pLimitWidth != null, u => u.LimitWidth == pLimitWidth) .Where(pLimitHeight != null, u => u.LimitHeight == pLimitHeight) .Where(pMaxWeight != null, u => u.MaxWeight == pMaxWeight) .Where(!string.IsNullOrEmpty(pParentContainerName), u => u.ParentContainerName == pParentContainerName) .Where(pParentContainerId != null, u => u.ParentContainerId == pParentContainerId) .Where(pIsVirtually != null, u => u.IsVirtually == pIsVirtually) .Where(pIsDisabled != null, u => u.IsDisabled == pIsDisabled) .Where(input.CreatedTime!=null, u => u.CreatedTime>= Convert.ToDateTime(input.CreatedTime[0]) && u.CreatedTime<= Convert.ToDateTime(input.CreatedTime[1])) .Where(input.UpdatedTime!=null, u => u.UpdatedTime>= Convert.ToDateTime(input.UpdatedTime[0]) && u.UpdatedTime<= Convert.ToDateTime(input.UpdatedTime[1])) .Where(!string.IsNullOrEmpty(pCreatedUserName), u => u.CreatedUserName == pCreatedUserName) .Where(!string.IsNullOrEmpty(pUpdatedUserName), u => u.UpdatedUserName == pUpdatedUserName) .OrderBy(PageInputOrder.OrderNonPageBuilder(input)) .ProjectToType() .ToListAsync(); return wmsContainers; } /// /// 获取容器信息 /// /// /// [HttpGet("WmsContainer/detail")] public async Task Get([FromQuery] QueryeWmsContainerInput input) { return (await _wmsContainerRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt(); } /// /// 获取容器信息列表 /// /// /// [HttpGet("WmsContainer/list")] public async Task> List([FromQuery] WmsContainerInput input) { return await _wmsContainerRep.DetachedEntities.ProjectToType().ToListAsync(); } #region 增、删、改 /// /// 增加容器信息 /// /// /// [HttpPost("WmsContainer/add")] public async Task Add(AddWmsContainerInput input) { var wmsContainer = input.Adapt(); //验证 await CheckExisit(wmsContainer); wmsContainer.CreatedUserId = wmsContainer.UpdatedUserId = SysHelper.GetUserId(); wmsContainer.CreatedUserName = wmsContainer.UpdatedUserName = SysHelper.GetUserName(); wmsContainer.CreatedTime = wmsContainer.UpdatedTime = SysHelper.GetNowTime(); await _wmsContainerRep.InsertAsync(wmsContainer); } /// /// 删除容器信息 /// /// /// [HttpPost("WmsContainer/delete")] public async Task Delete(DeleteWmsContainerInput input) { var wmsContainer = await _wmsContainerRep.FirstOrDefaultAsync(u => u.Id == input.Id); await _wmsContainerRep.DeleteAsync(wmsContainer); } /// /// 更新容器信息 /// /// /// [HttpPost("WmsContainer/edit")] public async Task Update(UpdateWmsContainerInput input) { var isExist = await _wmsContainerRep.AnyAsync(u => u.Id == input.Id, false); if (!isExist) throw Oops.Oh(ErrorCode.D1002); var wmsContainer = input.Adapt(); //验证 await CheckExisit(wmsContainer,true); wmsContainer.UpdatedUserId = SysHelper.GetUserId(); wmsContainer.UpdatedUserName = SysHelper.GetUserName(); wmsContainer.UpdatedTime = SysHelper.GetNowTime(); await _wmsContainerRep.UpdateAsync(wmsContainer,ignoreNullValues:true); } #endregion #region 导入 /// /// Excel模板导入容器信息功能 /// /// Excel模板文件 /// 导入的记录数 [HttpPost("WmsContainer/importExcel")] public async Task ImportExcelAsync(IFormFile file) { int _HeadStartLine = 2;//第1行是说明,第2行是列名 int _DataStartLine = 3;//第3行开始是数据 DataTable importDataTable = ExcelUtil.ImportExcelToDataTable(file, _HeadStartLine, _DataStartLine); var addList =await CommonImport(importDataTable, _DataStartLine); lock (_lock) { _wmsContainerRep.InsertAsync(addList); } await Task.CompletedTask; return addList.Count; } /// /// DataTable转换实体对象列表 /// /// /// 模版列名开始行 /// private async Task> CommonImport(DataTable dataTable, int dataStartLine) { var details = new List(); int index = dataStartLine;//模版列名开始行 foreach (System.Data.DataRow row in dataTable.Rows) { index++; //导入模版定制化代码(替换模版使用) var addItem = new WmsContainer() { CreatedTime = SysHelper.GetNowTime(), CreatedUserId = SysHelper.GetUserId(), CreatedUserName = SysHelper.GetUserName(), UpdatedTime = SysHelper.GetNowTime(), UpdatedUserId = SysHelper.GetUserId(), UpdatedUserName = SysHelper.GetUserName() }; #region 定义变量 var _ContainerCode = "";//编号 var _ContainerName = "";//名称 var _ContainerTypeId = "";//类型ID var _ContainerTypeName = "";//类型名称 var _PackagingId = "";//容器关系ID var _SpecLength = "";//长度 var _SpecWidth = "";//宽度 var _SpecHeight = "";//高度 var _LimitLength = "";//限长 var _LimitWidth = "";//限宽 var _LimitHeight = "";//限高 var _MaxWeight = "";//载重上限 var _ParentContainerName = "";//父容器名称 var _ParentContainerId = "";//父容器Id var _IsVirtually = "";//是否虚拟 var _IsDisabled = "";//是否禁用 var _Id = "";//Id主键 #endregion #region 取值 _ContainerCode = row["编号"]?.ToString() ; _ContainerName = row["名称"]?.ToString() ; _ContainerTypeId = row["类型ID"]?.ToString() ; _ContainerTypeName = row["类型名称"]?.ToString() ; _PackagingId = row["容器关系ID"]?.ToString() ; _SpecLength = row["长度"]?.ToString() ; _SpecWidth = row["宽度"]?.ToString() ; _SpecHeight = row["高度"]?.ToString() ; _LimitLength = row["限长"]?.ToString() ; _LimitWidth = row["限宽"]?.ToString() ; _LimitHeight = row["限高"]?.ToString() ; _MaxWeight = row["载重上限"]?.ToString() ; _ParentContainerName = row["父容器名称"]?.ToString() ; _ParentContainerId = row["父容器Id"]?.ToString() ; _IsVirtually = row["是否虚拟"]?.ToString() ; _IsDisabled = row["是否禁用"]?.ToString() ; _Id = row["Id主键"]?.ToString() ; #endregion #region 验证 if (string.IsNullOrEmpty(_ContainerCode)) { throw Oops.Oh($"第{index}行[编号]{_ContainerCode}不能为空!"); } if(!string.IsNullOrEmpty(_ContainerCode)) { addItem.ContainerCode = (string)_ContainerCode; } if (string.IsNullOrEmpty(_ContainerName)) { throw Oops.Oh($"第{index}行[名称]{_ContainerName}不能为空!"); } if(!string.IsNullOrEmpty(_ContainerName)) { addItem.ContainerName = (string)_ContainerName; } if (string.IsNullOrEmpty(_ContainerTypeId)) { throw Oops.Oh($"第{index}行[类型ID]{_ContainerTypeId}不能为空!"); } if(!string.IsNullOrEmpty(_ContainerTypeId)) { if (!long.TryParse(_ContainerTypeId, out long outContainerTypeId)&&!string.IsNullOrEmpty(_ContainerTypeId)) { throw Oops.Oh($"第{index}行[类型ID]{_ContainerTypeId}值不正确!"); } if (outContainerTypeId <= 0&&!string.IsNullOrEmpty(_ContainerTypeId)) { throw Oops.Oh($"第{index}行[类型ID]{_ContainerTypeId}值不能小于等于0!"); } else { addItem.ContainerTypeId = outContainerTypeId; } } if (string.IsNullOrEmpty(_ContainerTypeName)) { throw Oops.Oh($"第{index}行[类型名称]{_ContainerTypeName}不能为空!"); } if(!string.IsNullOrEmpty(_ContainerTypeName)) { addItem.ContainerTypeName = (string)_ContainerTypeName; } if (string.IsNullOrEmpty(_PackagingId)) { throw Oops.Oh($"第{index}行[容器关系ID]{_PackagingId}不能为空!"); } if(!string.IsNullOrEmpty(_PackagingId)) { if (!long.TryParse(_PackagingId, out long outPackagingId)&&!string.IsNullOrEmpty(_PackagingId)) { throw Oops.Oh($"第{index}行[容器关系ID]{_PackagingId}值不正确!"); } if (outPackagingId <= 0&&!string.IsNullOrEmpty(_PackagingId)) { throw Oops.Oh($"第{index}行[容器关系ID]{_PackagingId}值不能小于等于0!"); } else { addItem.PackagingId = outPackagingId; } } if(!string.IsNullOrEmpty(_SpecLength)) { if (!decimal.TryParse(_SpecLength, out decimal outSpecLength)&&!string.IsNullOrEmpty(_SpecLength)) { throw Oops.Oh($"第{index}行[长度]{_SpecLength}值不正确!"); } if (outSpecLength <= 0&&!string.IsNullOrEmpty(_SpecLength)) { throw Oops.Oh($"第{index}行[长度]{_SpecLength}值不能小于等于0!"); } else { addItem.SpecLength = outSpecLength; } } if(!string.IsNullOrEmpty(_SpecWidth)) { if (!decimal.TryParse(_SpecWidth, out decimal outSpecWidth)&&!string.IsNullOrEmpty(_SpecWidth)) { throw Oops.Oh($"第{index}行[宽度]{_SpecWidth}值不正确!"); } if (outSpecWidth <= 0&&!string.IsNullOrEmpty(_SpecWidth)) { throw Oops.Oh($"第{index}行[宽度]{_SpecWidth}值不能小于等于0!"); } else { addItem.SpecWidth = outSpecWidth; } } if(!string.IsNullOrEmpty(_SpecHeight)) { if (!decimal.TryParse(_SpecHeight, out decimal outSpecHeight)&&!string.IsNullOrEmpty(_SpecHeight)) { throw Oops.Oh($"第{index}行[高度]{_SpecHeight}值不正确!"); } if (outSpecHeight <= 0&&!string.IsNullOrEmpty(_SpecHeight)) { throw Oops.Oh($"第{index}行[高度]{_SpecHeight}值不能小于等于0!"); } else { addItem.SpecHeight = outSpecHeight; } } if(!string.IsNullOrEmpty(_LimitLength)) { if (!decimal.TryParse(_LimitLength, out decimal outLimitLength)&&!string.IsNullOrEmpty(_LimitLength)) { throw Oops.Oh($"第{index}行[限长]{_LimitLength}值不正确!"); } if (outLimitLength <= 0&&!string.IsNullOrEmpty(_LimitLength)) { throw Oops.Oh($"第{index}行[限长]{_LimitLength}值不能小于等于0!"); } else { addItem.LimitLength = outLimitLength; } } if(!string.IsNullOrEmpty(_LimitWidth)) { if (!decimal.TryParse(_LimitWidth, out decimal outLimitWidth)&&!string.IsNullOrEmpty(_LimitWidth)) { throw Oops.Oh($"第{index}行[限宽]{_LimitWidth}值不正确!"); } if (outLimitWidth <= 0&&!string.IsNullOrEmpty(_LimitWidth)) { throw Oops.Oh($"第{index}行[限宽]{_LimitWidth}值不能小于等于0!"); } else { addItem.LimitWidth = outLimitWidth; } } if(!string.IsNullOrEmpty(_LimitHeight)) { if (!decimal.TryParse(_LimitHeight, out decimal outLimitHeight)&&!string.IsNullOrEmpty(_LimitHeight)) { throw Oops.Oh($"第{index}行[限高]{_LimitHeight}值不正确!"); } if (outLimitHeight <= 0&&!string.IsNullOrEmpty(_LimitHeight)) { throw Oops.Oh($"第{index}行[限高]{_LimitHeight}值不能小于等于0!"); } else { addItem.LimitHeight = outLimitHeight; } } if(!string.IsNullOrEmpty(_MaxWeight)) { if (!decimal.TryParse(_MaxWeight, out decimal outMaxWeight)&&!string.IsNullOrEmpty(_MaxWeight)) { throw Oops.Oh($"第{index}行[载重上限]{_MaxWeight}值不正确!"); } if (outMaxWeight <= 0&&!string.IsNullOrEmpty(_MaxWeight)) { throw Oops.Oh($"第{index}行[载重上限]{_MaxWeight}值不能小于等于0!"); } else { addItem.MaxWeight = outMaxWeight; } } if(!string.IsNullOrEmpty(_ParentContainerName)) { addItem.ParentContainerName = (string)_ParentContainerName; } if(!string.IsNullOrEmpty(_ParentContainerId)) { if (!long.TryParse(_ParentContainerId, out long outParentContainerId)&&!string.IsNullOrEmpty(_ParentContainerId)) { throw Oops.Oh($"第{index}行[父容器Id]{_ParentContainerId}值不正确!"); } if (outParentContainerId <= 0&&!string.IsNullOrEmpty(_ParentContainerId)) { throw Oops.Oh($"第{index}行[父容器Id]{_ParentContainerId}值不能小于等于0!"); } else { addItem.ParentContainerId = outParentContainerId; } } if(!string.IsNullOrEmpty(_IsVirtually)) { if (!int.TryParse(_IsVirtually, out int outIsVirtually)&&!string.IsNullOrEmpty(_IsVirtually)) { throw Oops.Oh($"第{index}行[是否虚拟]{_IsVirtually}值不正确!"); } if (outIsVirtually <= 0&&!string.IsNullOrEmpty(_IsVirtually)) { throw Oops.Oh($"第{index}行[是否虚拟]{_IsVirtually}值不能小于等于0!"); } else { addItem.IsVirtually = outIsVirtually; } } if(!string.IsNullOrEmpty(_IsDisabled)) { if(!_IsDisabled.Equals("是") && !_IsDisabled.Equals("否")) { throw Oops.Oh($"第{index}行[是否禁用]{_IsDisabled}值不正确!"); } else { bool outIsDisabled = _IsDisabled.Equals("是") ? true : false; addItem.IsDisabled = outIsDisabled; } } if(!string.IsNullOrEmpty(_Id)) { if (!long.TryParse(_Id, out long outId)&&!string.IsNullOrEmpty(_Id)) { throw Oops.Oh($"第{index}行[Id主键]{_Id}值不正确!"); } if (outId <= 0&&!string.IsNullOrEmpty(_Id)) { throw Oops.Oh($"第{index}行[Id主键]{_Id}值不能小于等于0!"); } else { addItem.Id = outId; } } #endregion details.Add(addItem); } //验重 await CheckExisitForImport(details); return details; } /// /// 根据版本下载容器信息的Excel导入模板 /// /// 模板版本 /// 下载的模板文件 [HttpGet("WmsContainer/downloadExcelTemplate")] public IActionResult DownloadExcelTemplate([FromQuery] string version) { string _path = TemplateConst.EXCEL_TEMPLATEFILE_导入模版路径 + $"\\WmsContainer{TemplateConst.EXCEL_TEMPLATEFILE_导入模版名称后缀}.xlsx"; var fileName = HttpUtility.UrlEncode($"导入模板(容器信息).xlsx", Encoding.GetEncoding("UTF-8")); return new FileStreamResult(new FileStream(_path, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName }; } #endregion #region 私有方法 /// /// 根据联合主键验证数据是否已存在-数据库 /// /// /// /// private async Task CheckExisit( WmsContainer input,bool isEdit=false) { bool isExist = false; if (!isEdit)//新增 { //数据是否存在重复 isExist = await _wmsContainerRep.AnyAsync(u => u.ContainerCode.Equals(input.ContainerCode) ,false); } else//编辑 { //当前编辑数据以外是否存在重复 isExist = await _wmsContainerRep.AnyAsync(u => u.Id != input.Id &&u.ContainerCode.Equals(input.ContainerCode) ,false); } if (isExist) throw Oops.Oh(ErrorCode.E0001); } /// /// 根据联合主键验证数据是否已存在-导入时验证 /// /// /// private async Task CheckExisitForImport(List inputs) { //根据联合主键验证表格中中是否已存在相同数据 if (inputs?.Count <= 0) { throw Oops.Oh($"导入数据不能为空"); } //数据是否重复 var existExcelItem = inputs.GroupBy(g => new { g.ContainerCode }) .Where(g => g.Count() > 1) .Select(s => new { s.Key.ContainerCode }).FirstOrDefault(); if (existExcelItem != null) { var wmsContainer = existExcelItem.Adapt(); var item= existExcelItem.Adapt(); throw Oops.Oh($"导入的表格中,编号[{item.ContainerCode}]已存在"); } //根据联合主键验证数据库中是否已存在相同数据 var existDBItem = await _wmsContainerRep.DetachedEntities.FirstOrDefaultAsync(w=> inputs.Select(s=>"" +s.ContainerCode ) .Contains("" +w.ContainerCode )); if (existDBItem != null) { var wmsContainer = existExcelItem.Adapt(); var item= existExcelItem.Adapt(); throw Oops.Oh($"系统中,编号[{item.ContainerCode}]已存在"); } } #endregion } }