using Admin.NET.Core.Service; using Admin.NET.Application.Entity; using Microsoft.AspNetCore.Http; using Admin.NET.Core; using Furion.DatabaseAccessor; using DocumentFormat.OpenXml.Vml.Spreadsheet; using AngleSharp.Dom; namespace Admin.NET.Application; /// /// 库区信息服务 /// [ApiDescriptionSettings(ApplicationConst.WmsBaseGroupName, Order = 100)] public class WmsAreaService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _repWmsWarehouse; private readonly SqlSugarRepository _repWmsPlace; private readonly WmsPlaceService _wmsPlaceService; private readonly WmsContainerService _wmsContainerService; private readonly SqlSugarRepository _wmsContainerTypeRep; private readonly SqlSugarRepository _wmsContainerPlaceRep; private readonly SqlSugarRepository _wmsContainerRep; public WmsAreaService( SqlSugarRepository wmsContainerRep, SqlSugarRepository rep , SqlSugarRepository wmsWarehouseRep, SqlSugarRepository repWmsPlace, WmsPlaceService wmsPlaceService, WmsContainerService wmsContainerService, SqlSugarRepository wmsContainerTypeRep, SqlSugarRepository wmsContainerPlaceRep) { _wmsContainerRep = wmsContainerRep; _rep = rep; _repWmsWarehouse = wmsWarehouseRep; _repWmsPlace = repWmsPlace; _wmsPlaceService = wmsPlaceService; _wmsContainerService = wmsContainerService; _wmsContainerTypeRep = wmsContainerTypeRep; _wmsContainerPlaceRep = wmsContainerPlaceRep; } /// /// 分页查询库区信息 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Page")] public async Task> Page(WmsAreaInput input) { var query = _rep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u => u.AreaCode.Contains(input.SearchKey.Trim()) || u.AreaName.Contains(input.SearchKey.Trim()) || u.ErpCode.Contains(input.SearchKey.Trim()) ) .WhereIF(input.AreaType != null, u => u.AreaType == input.AreaType) .WhereIF(input.IsVirtually != null, u => u.IsVirtually == input.IsVirtually) .WhereIF(input.IsDisabled != null, u => u.IsDisabled == input.IsDisabled) //ly-0612下拉数据用 .WhereIF(!string.IsNullOrWhiteSpace(input.AreaCode), u => u.AreaCode.Contains(input.AreaCode.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.AreaName), u => u.AreaName.Contains(input.AreaName.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.ErpCode), u => u.ErpCode.Contains(input.ErpCode.Trim())) .WhereIF(input.WarehouseId > 0, u => u.WarehouseId == input.WarehouseId) .Select(); return await query.OrderBuilder(input, "", "AreaCode").ToPagedListAsync(input.Page, input.PageSize); } /// /// 增加库区信息 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Add")] [UnitOfWork] public async Task Add(AddWmsAreaInput input) { var entity = input.Adapt(); if (entity.IsVirtually == null) { entity.IsVirtually = false; } //验重 await CheckExisit(entity); //ly-所属仓库 var Info = await _repWmsWarehouse.GetFirstAsync(u => u.Id == input.WarehouseId); entity.WarehouseName = Info.Name; entity.WarehouseCode = Info.Code; if (Info == null) { throw Oops.Oh(errorMessage: @$"所属仓库不存在!"); } if (Info.IsDisabled == true) { throw Oops.Oh($"不能使用已禁用的仓库"); } await _rep.InsertAsync(entity); //await CommonAddZT(entity); WmsContainerPlace willAddWmsContainerPlace = new WmsContainerPlace(); long placeId = 0; var exist_PlaceObj = await _repWmsPlace.GetFirstAsync(u => u.PlaceCode == ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode); if (exist_PlaceObj == null) { // 添加在途库区库位 AddWmsPlaceInput addWmsPlaceInput = new AddWmsPlaceInput { PlaceCode = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode, PlaceName = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaName, AreaId = entity.Id, AreaCode = entity.AreaCode, AreaName = entity.AreaName, PlaceType = PlaceTypeEnum.在途库位.ToInt(), PlaceStatus = PlaceStatusEnum.正常.ToInt(), IsDisabled = false, IsDelete = false, }; placeId = await _wmsPlaceService.Add(addWmsPlaceInput); willAddWmsContainerPlace.PlaceId = placeId; willAddWmsContainerPlace.PlaceCode = addWmsPlaceInput.PlaceCode; willAddWmsContainerPlace.PlaceName = addWmsPlaceInput.PlaceName; } else { placeId = exist_PlaceObj.Id; willAddWmsContainerPlace.PlaceId = placeId; willAddWmsContainerPlace.PlaceCode = exist_PlaceObj.PlaceCode; willAddWmsContainerPlace.PlaceName = exist_PlaceObj.PlaceName; } // 查找虚拟容器容器类型 var containerType = await _wmsContainerTypeRep .GetFirstAsync(n => n.TypeCode == OperationsContainerTypeEnum.VIR.ToString() && n.IsDelete == false); if (containerType == null) throw Oops.Oh($"{OperationsContainerTypeEnum.VIR.ToString()}虚拟容器类型不存在!"); long containerId = 0; var exist_ContainerObj = await _wmsContainerRep.GetFirstAsync(u => u.ContainerCode == ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode); if (exist_ContainerObj == null) { // 添加在途库区容器 var addWmsContainerInput = new AddWmsContainerInput { IsVirtually = true, ContainerCode = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode, ContainerName = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaName, ContainerTypeId = containerType.Id, ContainerTypeName = containerType.TypeName, IsDisabled = false, }; //虚拟容器不需要容器类型 containerId = await _wmsContainerService.Add(addWmsContainerInput); willAddWmsContainerPlace.ContainerId = containerId; willAddWmsContainerPlace.ContainerCode = addWmsContainerInput.ContainerCode; willAddWmsContainerPlace.ContainerName = addWmsContainerInput.ContainerName; } else { containerId = exist_ContainerObj.Id; willAddWmsContainerPlace.ContainerId = containerId; willAddWmsContainerPlace.ContainerCode = exist_ContainerObj.ContainerCode; willAddWmsContainerPlace.ContainerName = exist_ContainerObj.ContainerName; } var exist_ContainerPlaceObj = await _wmsContainerPlaceRep.GetFirstAsync(u => u.PlaceCode == willAddWmsContainerPlace.PlaceCode && u.ContainerCode == willAddWmsContainerPlace.ContainerCode); if (exist_ContainerPlaceObj == null) { // 创建容器库位关系 await _wmsContainerPlaceRep.InsertAsync(willAddWmsContainerPlace); } return entity.Id; } /// /// 公共处理 在途库位,在途容器 /// /// /// private async Task CommonAddZT(WmsBaseArea entity) { WmsContainerPlace willAddWmsContainerPlace = new WmsContainerPlace(); long placeId = 0; var exist_PlaceObj = await _repWmsPlace.GetFirstAsync(u => u.PlaceCode == ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode); if (exist_PlaceObj == null) { // 添加在途库区库位 AddWmsPlaceInput addWmsPlaceInput = new AddWmsPlaceInput { PlaceCode = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode, PlaceName = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaName, AreaId = entity.Id, AreaCode = entity.AreaCode, AreaName = entity.AreaName, PlaceType = PlaceTypeEnum.在途库位.ToInt(), PlaceStatus = PlaceStatusEnum.正常.ToInt(), IsDisabled = false, IsDelete = false, }; placeId = await _wmsPlaceService.Add(addWmsPlaceInput); willAddWmsContainerPlace.PlaceId = placeId; willAddWmsContainerPlace.PlaceCode = addWmsPlaceInput.PlaceCode; willAddWmsContainerPlace.PlaceName = addWmsPlaceInput.PlaceName; } else { placeId = exist_PlaceObj.Id; willAddWmsContainerPlace.PlaceId = placeId; willAddWmsContainerPlace.PlaceCode = exist_PlaceObj.PlaceCode; willAddWmsContainerPlace.PlaceName = exist_PlaceObj.PlaceName; } // 查找虚拟容器容器类型 var containerType = await _wmsContainerTypeRep .GetFirstAsync(n => n.TypeCode == OperationsContainerTypeEnum.VIR.ToString() && n.IsDelete == false); if (containerType == null) throw Oops.Oh($"{OperationsContainerTypeEnum.VIR.ToString()}虚拟容器类型不存在!"); long containerId = 0; var exist_ContainerObj = await _wmsContainerRep.GetFirstAsync(u => u.ContainerCode == ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode); if (exist_ContainerObj == null) { // 添加在途库区容器 var addWmsContainerInput = new AddWmsContainerInput { IsVirtually = true, ContainerCode = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode, ContainerName = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaName, ContainerTypeId = containerType.Id, ContainerTypeName = containerType.TypeName, IsDisabled = false, }; containerId = await _wmsContainerService.Add(addWmsContainerInput); willAddWmsContainerPlace.ContainerId = containerId; willAddWmsContainerPlace.ContainerCode = addWmsContainerInput.ContainerCode; willAddWmsContainerPlace.ContainerName = addWmsContainerInput.ContainerName; } else { containerId = exist_ContainerObj.Id; willAddWmsContainerPlace.ContainerId = containerId; willAddWmsContainerPlace.ContainerCode = exist_ContainerObj.ContainerCode; willAddWmsContainerPlace.ContainerName = exist_ContainerObj.ContainerName; } var exist_ContainerPlaceObj = await _wmsContainerPlaceRep.GetFirstAsync(u => u.PlaceCode == willAddWmsContainerPlace.PlaceCode && u.ContainerCode == willAddWmsContainerPlace.ContainerCode); if (exist_ContainerPlaceObj == null) { // 创建容器库位关系 await _wmsContainerPlaceRep.InsertAsync(willAddWmsContainerPlace); } } /// /// 删除库区信息 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Delete")] public async Task Delete(DeleteWmsAreaInput input) { var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002); ////ly-存在绑定关系,不可删除 //var entityWmsWarehouse = await _repWmsWarehouse.GetFirstAsync(u => u.Code == entity.WarehouseCode); //if (entityWmsWarehouse != null) //{ // throw Oops.Oh("存在绑定关系,不可删除"); //} var entityWmsPlace = await _repWmsPlace.GetFirstAsync(u => u.AreaId == entity.Id); if (entityWmsPlace != null) { throw Oops.Oh("存在绑定关系,不可删除"); } //await _rep.FakeDeleteAsync(entity); //假删除 await _rep.DeleteAsync(entity); //真删除 } /// /// 更新库区信息 /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Update")] [UnitOfWork] public async Task Update(UpdateWmsAreaInput input) { var entity = input.Adapt(); //验重 await CheckExisit(entity, true); //ly-所属仓库 var Info = await _repWmsWarehouse.GetFirstAsync(u => u.Id == input.WarehouseId); entity.WarehouseName = Info.Name; entity.WarehouseCode = Info.Code; if (Info == null) { throw Oops.Oh(errorMessage: @$"所属仓库不存在!"); } if (Info.IsDisabled == true) { throw Oops.Oh($"不能使用已禁用的仓库"); } await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); await CommonAddZT(entity); } /// /// 获取库区信息 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "Detail")] public async Task Detail([FromQuery] QueryByIdWmsAreaInput input) { return await _rep.GetFirstAsync(u => u.Id == input.Id); } /// /// 获取库区信息列表 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "List")] public async Task> List([FromQuery] WmsAreaInput input) { return await _rep.AsQueryable().Select().ToListAsync(); } #region 私有方法 /// /// 根据联合主键验证数据是否已存在-数据库 /// /// /// /// private async Task CheckExisit(WmsBaseArea input, bool isEdit = false) { //输出数据已存在错误 ErrorCodeItemMetadataAttribute metadata = ErrorCodeEnum.D1006.GetErrorCodeItemMetadata(); WmsBaseArea _existItem = null; if (!isEdit)//新增 { _existItem = await _rep.GetFirstAsync(u => u.AreaCode.Equals(input.AreaCode)); if (_existItem != null) throw Oops.Oh($"库区编号[{input.AreaCode}]{metadata.ErrorMessage}"); _existItem = await _rep.GetFirstAsync(u => u.AreaName.Equals(input.AreaName)); if (_existItem != null) throw Oops.Oh($"库区名称[{input.AreaName}]{metadata.ErrorMessage}"); } else//编辑 { //当前编辑数据以外是否存在重复 _existItem = await _rep.GetFirstAsync(u => u.Id != input.Id && u.AreaCode.Equals(input.AreaCode)); if (_existItem != null) throw Oops.Oh($"库区编号[{input.AreaCode}]{metadata.ErrorMessage}"); _existItem = await _rep.GetFirstAsync(u => u.Id != input.Id && u.AreaName.Equals(input.AreaName)); if (_existItem != null) throw Oops.Oh($"库区名称[{input.AreaName}]{metadata.ErrorMessage}"); } } #endregion }