|  |  |  | 
|---|
|  |  |  | using WIDESEA_DTO; | 
|---|
|  |  |  | using AngleSharp.Dom; | 
|---|
|  |  |  | using Mapster; | 
|---|
|  |  |  | using System.Threading.Tasks; | 
|---|
|  |  |  | using WIDESEA_Common; | 
|---|
|  |  |  | using WIDESEA_Core; | 
|---|
|  |  |  | using WIDESEA_DTO; | 
|---|
|  |  |  | using WIDESEA_Model.Models; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | namespace WIDESEA_StorageBasicService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | private readonly LogFactory LogFactory = new LogFactory(); | 
|---|
|  |  |  | private readonly IUnitOfWorkManage _unitOfWorkManage; | 
|---|
|  |  |  | private readonly IDt_TaskRepository _taskRepository; | 
|---|
|  |  |  | private readonly IDt_TaskService _taskService; | 
|---|
|  |  |  | private readonly IStockInfoRepository _stockInfoRepository; | 
|---|
|  |  |  | private readonly IStockInfoDetailRepository _stockInfoDetailRepository; | 
|---|
|  |  |  | private readonly IDt_WareAreaInfoRepository _wareAreaInfoRepository; | 
|---|
|  |  |  | private readonly IPointStackerRelationRepository _pointStackerRelationRepository; | 
|---|
|  |  |  | private readonly ITaskExecuteDetailRepository _taskExecuteDetailRepository; | 
|---|
|  |  |  | private readonly ILocationStatusChangeRecordRepository _locationStatusChangeRecordRepository; | 
|---|
|  |  |  | private readonly IMapper _mapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public LocationInfoService(ILocationInfoRepository BaseDal, | 
|---|
|  |  |  | IUnitOfWorkManage unitOfWorkManage, | 
|---|
|  |  |  | 
|---|
|  |  |  | IDt_WareAreaInfoRepository wareAreaInfoRepository, | 
|---|
|  |  |  | IPointStackerRelationRepository pointStackerRelationRepository, | 
|---|
|  |  |  | ITaskExecuteDetailRepository taskExecuteDetailRepository, | 
|---|
|  |  |  | ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository) : base(BaseDal) | 
|---|
|  |  |  | ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository, | 
|---|
|  |  |  | IStockInfoDetailRepository stockInfoDetailRepository, | 
|---|
|  |  |  | IMapper mapper, | 
|---|
|  |  |  | IDt_TaskService taskService) : base(BaseDal) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _unitOfWorkManage = unitOfWorkManage; | 
|---|
|  |  |  | _taskRepository = taskRepository; | 
|---|
|  |  |  | 
|---|
|  |  |  | _pointStackerRelationRepository = pointStackerRelationRepository; | 
|---|
|  |  |  | _taskExecuteDetailRepository = taskExecuteDetailRepository; | 
|---|
|  |  |  | _locationStatusChangeRecordRepository = locationStatusChangeRecordRepository; | 
|---|
|  |  |  | _stockInfoDetailRepository = stockInfoDetailRepository; | 
|---|
|  |  |  | _mapper = mapper; | 
|---|
|  |  |  | _taskService = taskService; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public override WebResponseContent UpdateData(SaveModel saveModel) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | int id = saveModel.MainData["id"].ObjToInt(); | 
|---|
|  |  |  | int status = saveModel.MainData["locationStatus"].ObjToInt(); | 
|---|
|  |  |  | var location = BaseDal.QueryFirst(x => x.Id == id); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LocationChangeRecordDto changeRecordDto = new LocationChangeRecordDto() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | AfterStatus = status, | 
|---|
|  |  |  | BeforeStatus = location.LocationStatus, | 
|---|
|  |  |  | TaskNum = 0, | 
|---|
|  |  |  | LocationId = id, | 
|---|
|  |  |  | LocationCode = location.LocationCode, | 
|---|
|  |  |  | ChangeType = (int)StatusChangeTypeEnum.ManualOperation | 
|---|
|  |  |  | }; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _locationStatusChangeRecordRepository.AddStatusChangeRecord(changeRecordDto); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return base.UpdateData(saveModel); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | #region 初始化库位 | 
|---|
|  |  |  | public async Task<WebResponseContent> initializeLocation(int locationID) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | WebResponseContent content = new WebResponseContent(); | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | DtLocationInfo? location = BaseDal.QueryData(x => x.Id == locationID).FirstOrDefault(); | 
|---|
|  |  |  | int LastStatus = location.LocationStatus; | 
|---|
|  |  |  | if (location == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return content.Error("未找到货位信息!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | DtStockInfo stock = _stockInfoRepository.QueryFirst(x => x.LocationId == location.Id); | 
|---|
|  |  |  | if(stock == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | location.LocationStatus= (int)LocationEnum.Lock; | 
|---|
|  |  |  | BaseDal.UpdateData(location); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _unitOfWorkManage.BeginTran(); | 
|---|
|  |  |  | DtStockInfo_Hty stockInfo_Hty = stock.Adapt<DtStockInfo_Hty>(); | 
|---|
|  |  |  | stockInfo_Hty.ModifyDate = DateTime.Now; | 
|---|
|  |  |  | await DeleteStockInfoAsync(stock.Id); | 
|---|
|  |  |  | List<DtStockInfoDetail> detail = _stockInfoDetailRepository.QueryData(x => x.StockId == stock.Id).ToList(); | 
|---|
|  |  |  | if (detail != null && detail.Count() > 0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<DtStockInfoDetail_Hty> details = detail.Adapt<List<DtStockInfoDetail_Hty>>(); | 
|---|
|  |  |  | await DeleteStockInfoDetailsAsync(detail); | 
|---|
|  |  |  | AddStockInfoDetailHty(details); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | await AddStockInfoHtyAsync(stockInfo_Hty); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | location.LocationStatus = (int)LocationEnum.Lock; | 
|---|
|  |  |  | BaseDal.UpdateData(location); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(location, LastStatus, (int)StatusChangeTypeEnum.ManualOperation, 0); | 
|---|
|  |  |  | _unitOfWorkManage.CommitTran(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return content.OK(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | catch (Exception ex) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _unitOfWorkManage.RollbackTran(); | 
|---|
|  |  |  | return content.Error(ex.Message); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #region 创建原始货位数据 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | { | 
|---|
|  |  |  | string[] chineseNumbers = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; | 
|---|
|  |  |  | var locationList = new List<DtLocationInfo>(); | 
|---|
|  |  |  | for (int line = 1; line <= x; line++) | 
|---|
|  |  |  | for (int line = 3; line <= x; line++) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | for (int column = 1; column <= y; column++) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | LocationType = locType, | 
|---|
|  |  |  | Remark = "", | 
|---|
|  |  |  | Depth = locType > 1 ? (((line - 1) % 4) + 1) == 2 || (((line - 1) % 4) + 1) == 3 ? 1 : 2 : 1, | 
|---|
|  |  |  | RoadwayNo = locType > 1 ? $"SC{((line - 1) / 4) + 1}" : $"SC{((line - 1) / 2) + 1}", | 
|---|
|  |  |  | RoadwayNo = locType > 1 ? $"GWSC{((line - 1) / 4) + 1}" : $"GWSC{((line - 1) / 2) + 1}", | 
|---|
|  |  |  | LocationStatus = LocationEnum.Free.ObjToInt(), | 
|---|
|  |  |  | AreaId = areaId, | 
|---|
|  |  |  | Creater = "systeam", | 
|---|
|  |  |  | 
|---|
|  |  |  | SourceAddress = originalLocationID, | 
|---|
|  |  |  | TaskNum = taskNo, | 
|---|
|  |  |  | TargetAddress = emptyLocation.LocationCode, | 
|---|
|  |  |  | TaskState = InTaskStatusEnum.RelocationNew.ObjToInt(), | 
|---|
|  |  |  | //TaskState = TaskInStatusEnum.RelocationNew.ObjToInt(), | 
|---|
|  |  |  | TaskType = TaskTypeEnum.RelocationIn.ObjToInt(), | 
|---|
|  |  |  | }; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion 创建初始货位方法 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #region 库存移入历史 | 
|---|
|  |  |  | private async Task DeleteStockInfoAsync(int stockId) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var isStockUpdated = await _stockInfoRepository.DeleteDataByIdAsync(stockId); | 
|---|
|  |  |  | if (!isStockUpdated) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("库存信息更新失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private async Task AddStockInfoHtyAsync(DtStockInfo_Hty dtStock) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var isStockAdd = await SqlSugarHelper.DbWMS.InsertNav(dtStock).IncludesAllFirstLayer().ExecuteCommandAsync(); | 
|---|
|  |  |  | if (!isStockAdd) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("库存历史信息添加失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private async Task DeleteStockInfoDetailsAsync(IEnumerable<DtStockInfoDetail> details) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var ids = details.Select(x => (object)x.Id).ToArray(); | 
|---|
|  |  |  | var isStockDetailUpdated = await _stockInfoDetailRepository.DeleteDataByIdsAsync(ids); | 
|---|
|  |  |  | if (!isStockDetailUpdated) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("库存详情信息更新失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private void AddStockInfoDetailHty(List<DtStockInfoDetail_Hty> details) | 
|---|
|  |  |  | { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | var isStockAdd = SqlSugarHelper.DbWMS.Insertable(details).ExecuteCommand(); | 
|---|
|  |  |  | if (isStockAdd==0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("库存明细历史信息添加失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion 内部方法 | 
|---|
|  |  |  | } | 
|---|