| using HslCommunication.WebSocket; | 
| using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; | 
| using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; | 
| using SqlSugar; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEA_Common.CommonEnum; | 
| using WIDESEA_Common.LocationEnum; | 
| using WIDESEA_Common.StockEnum; | 
| using WIDESEA_Core; | 
| using WIDESEA_Core.BaseRepository; | 
| using WIDESEA_Core.BaseServices; | 
| using WIDESEA_Core.Const; | 
| using WIDESEA_Core.DB; | 
| using WIDESEA_Core.Enums; | 
| using WIDESEA_Core.Helper; | 
| using WIDESEA_Core.Seed; | 
| using WIDESEA_Core.Utilities; | 
| using WIDESEA_DTO.Basic; | 
| using WIDESEA_IBasicRepository; | 
| using WIDESEA_IBasicService; | 
| using WIDESEA_IStockRepository; | 
| using WIDESEA_Model.Models; | 
|   | 
| namespace WIDESEA_BasicService | 
| { | 
|     public partial class LocationInfoService : ServiceBase<Dt_LocationInfo, ILocationInfoRepository>, ILocationInfoService | 
|     { | 
|         private readonly IUnitOfWorkManage _unitOfWorkManage; | 
|         private readonly IBasicRepository _basicRepository; | 
|         private readonly IStockInfoRepository _stockInfoRepository; | 
|         public ILocationInfoRepository Repository => BaseDal; | 
|   | 
|         public LocationInfoService(ILocationInfoRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IBasicRepository basicRepository, IStockInfoRepository stockInfoRepository) : base(BaseDal) | 
|         { | 
|             _unitOfWorkManage = unitOfWorkManage; | 
|             _basicRepository = basicRepository; | 
|             _stockInfoRepository = stockInfoRepository; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 批量启用货位 | 
|         /// </summary> | 
|         /// <param name="keys">货位主键数组</param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LocationEnableStatus(int[] keys) | 
|         { | 
|             List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); | 
|             locationInfos.ForEach(x => | 
|             { | 
|                 x.EnableStatus = EnableStatusEnum.Normal.ObjToInt(); | 
|             }); | 
|             Repository.UpdateData(locationInfos); | 
|   | 
|             return WebResponseContent.Instance.OK(); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 批量禁用货位 | 
|         /// </summary> | 
|         /// <param name="keys">货位主键数组</param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LocationDisableStatus(int[] keys) | 
|         { | 
|             List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); | 
|             locationInfos.ForEach(x => | 
|             { | 
|                 x.EnableStatus = EnableStatusEnum.Disable.ObjToInt(); | 
|             }); | 
|             Repository.UpdateData(locationInfos); | 
|   | 
|             return WebResponseContent.Instance.OK(); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 单个启用货位 | 
|         /// </summary> | 
|         /// <param name="key">货位主键</param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LocationEnableStatus(int key) | 
|         { | 
|             return LocationEnableStatus(new int[] { key }); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 单个禁用货位 | 
|         /// </summary> | 
|         /// <param name="key">货位主键</param> | 
|         /// <returns></returns> | 
|         public WebResponseContent LocationDisableStatus(int key) | 
|         { | 
|             return LocationDisableStatus(new int[] { key }); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 获取辅料仓库存 | 
|         /// </summary> | 
|         /// <returns></returns> | 
|         public WebResponseContent GetContainerInfo() | 
|         { | 
|             try | 
|             { | 
|                 SqlSugarClient TesugarClient = new SqlSugarClient(new ConnectionConfig | 
|                 { | 
|                     ConfigId = MainDb.CurrentDbConnId, | 
|                     ConnectionString = AppSettings.Get("TeConnectionString").DecryptDES(AppSecret.DB), | 
|                     IsAutoCloseConnection = true, | 
|                     DbType = MainDb.DbType, | 
|                 }); | 
|   | 
|                 var ContainerInfos = TesugarClient.Queryable("ContainerInfo", "x").Where("WaId", "=", 8).Where("UpState", "=", 2).ToList(); | 
|                 if (ContainerInfos != null) | 
|                 { | 
|                     List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>(); | 
|                     foreach (var item in ContainerInfos) | 
|                     { | 
|                         dynamic ruleConfig = item; | 
|                         int id = ruleConfig.CId;//主键 | 
|                         string BoxNo = ruleConfig.BoxNo;//箱号 | 
|                         string LNo = ruleConfig.LNo;//货位编号 | 
|                         Dt_StockInfo stockInfo = new Dt_StockInfo() | 
|                         { | 
|                             LocationCode = LNo == "平库位" ? LNo : GetLocationCode(LNo), | 
|                             PalletCode = BoxNo, | 
|                             StockStatus = StockStatusEmun.入库完成.ObjToInt(), | 
|                             WarehouseId = 5, | 
|                             PalletType = PalletTypeEnum.LargestPallet.ObjToInt(), | 
|                             Details = new List<Dt_StockInfoDetail>() | 
|                         }; | 
|                         var ContainerDetailsInfos = TesugarClient.Queryable("ContainerDetailsInfo", "x").Where("CId", "=", id).ToList(); | 
|                         if (ContainerDetailsInfos.Count < 1) stockInfo.PalletType = PalletTypeEnum.Empty.ObjToInt(); | 
|                         foreach (var ContainerDetailsInfo in ContainerDetailsInfos) | 
|                         { | 
|                             dynamic ruleConfig1 = ContainerDetailsInfo; | 
|                             string MaterialCode = ruleConfig1.MaterialCode;//物料编号 | 
|                             string MaterialName = ruleConfig1.MaterialName;//物料名称 | 
|                             string MaterialLot = ruleConfig1.MaterialLot;//物料批号 | 
|                             var PackQty = ruleConfig1.PackQty;//数量 | 
|                             string ProduceData = ruleConfig1.ProduceData;//生成日期 | 
|                             string ValidData = ruleConfig1.ValidData;//有效期 | 
|                             string Unit = ruleConfig1.Unit;//单位 | 
|                             string Standard = ruleConfig1.Standard;//备注 | 
|                             int MId = ruleConfig1.MId;//入库单明细行号 | 
|                             string Model = ruleConfig1.Model;//单据编号 | 
|                             Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail() | 
|                             { | 
|                                 MaterielCode = MaterialCode, | 
|                                 MaterielName = MaterialName, | 
|                                 BatchNo = MaterialLot, | 
|                                 OrderNo = Model, | 
|                                 SerialNumber = "", | 
|                                 StockQuantity = (float)PackQty, | 
|                                 OutboundQuantity = 0, | 
|                                 Unit = Unit, | 
|                                 Status = StockStatusEmun.入库完成.ObjToInt(), | 
|                                 ProductionDate = ProduceData, | 
|                                 EffectiveDate = ValidData ?? DateTime.Parse(ProduceData).AddYears(2).ToString("yyyy-MM-dd"), | 
|                                 Remark = Standard, | 
|                                 InboundOrderRowNo = MId, | 
|                             }; | 
|                             stockInfo.Details.Add(stockInfoDetail); | 
|                         } | 
|                         stockInfos.Add(stockInfo); | 
|                     } | 
|                     var LocationCodes = stockInfos.Where(x => x.LocationCode != "平库位").Select(stockInfo => stockInfo.LocationCode).ToList(); | 
|                     List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => LocationCodes.Contains(x.LocationCode)); | 
|                     locationInfos.ForEach(x => | 
|                     { | 
|                         x.LocationStatus = LocationStatusEnum.InStock.ObjToInt(); | 
|                         x.EnableStatus = EnableStatusEnum.Normal.ObjToInt(); | 
|                     }); | 
|                     _unitOfWorkManage.BeginTran(); | 
|                     _stockInfoRepository.Db.InsertNav(stockInfos).Include(x => x.Details).ExecuteCommand(); | 
|                     Repository.UpdateData(locationInfos); | 
|                     _unitOfWorkManage.CommitTran(); | 
|                 } | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 _unitOfWorkManage.RollbackTran(); | 
|                 return WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|             return WebResponseContent.Instance.OK(); | 
|         } | 
|         /// <summary> | 
|         /// 获取货位编号 | 
|         /// </summary> | 
|         /// <param name="locationCode"></param> | 
|         /// <returns></returns> | 
|         string GetLocationCode(string locationCode) | 
|         { | 
|             var a = locationCode.Substring(0, 1); | 
|             var Row = a switch | 
|             { | 
|                 "A" => "001", | 
|                 "B" => "002", | 
|                 "C" => "003", | 
|                 "D" => "004", | 
|                 "E" => "005", | 
|                 _ => throw new Exception($"未定义的排,地址:【{locationCode}】"), | 
|             }; | 
|             var Column = locationCode.Substring(4, 2); | 
|             if (locationCode.Length == 7) | 
|                 Column = locationCode.Substring(5, 2); | 
|             var Layer = locationCode.Substring(2, 2); | 
|             locationCode = "AGV_FL-" + Row + "-0" + Column + "-0" + Layer + "-01"; | 
|             return locationCode; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 初始化货位 | 
|         /// </summary> | 
|         /// <param name="initializationLocationDTO"></param> | 
|         /// <returns></returns> | 
|         public WebResponseContent InitializationLocation(InitializationLocationDTO initializationLocationDTO) | 
|         { | 
|             try | 
|             { | 
|                 (bool, string, object?) result = ModelValidate.ValidateModelData(initializationLocationDTO); | 
|                 if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2); | 
|   | 
|                 int depth = initializationLocationDTO.Depth; | 
|                 List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>(); | 
|                 for (int i = 0; i < initializationLocationDTO.MaxRow; i++) | 
|                 { | 
|                     if ((i + 1) % initializationLocationDTO.MaxRow == 1) | 
|                     { | 
|                         depth = initializationLocationDTO.Depth; | 
|                     } | 
|                     else if ((i + 1) % initializationLocationDTO.MaxRow == initializationLocationDTO.Depth + 1) | 
|                     { | 
|                         depth = 1; | 
|                     } | 
|                     else if ((i + 1) % initializationLocationDTO.MaxRow > 1 && (i + 1) % initializationLocationDTO.MaxRow <= initializationLocationDTO.Depth) | 
|                     { | 
|                         depth -= 1; | 
|                     } | 
|                     else | 
|                     { | 
|                         depth += 1; | 
|                     } | 
|                     for (int j = 0; j < initializationLocationDTO.MaxColumn; j++) | 
|                     { | 
|                         for (int k = 0; k < initializationLocationDTO.MaxLayer; k++) | 
|                         { | 
|                             Dt_LocationInfo locationInfo = new Dt_LocationInfo() | 
|                             { | 
|                                 WarehouseId = 0, | 
|                                 Column = j + 1, | 
|                                 EnableStatus = EnableStatusEnum.Normal.ObjToInt(), | 
|                                 Layer = k + 1, | 
|                                 LocationStatus = LocationStatusEnum.Free.ObjToInt(), | 
|                                 LocationType = LocationTypeEnum.Undefined.ObjToInt(), | 
|                                 RoadwayNo = $"{initializationLocationDTO.Roadway.ToString()}", | 
|                                 Row = i + 1, | 
|                                 Depth = depth, | 
|                             }; | 
|                             locationInfo.LocationCode = $"{locationInfo.RoadwayNo}-{locationInfo.Row.ToString().PadLeft(3, '0')}-{locationInfo.Column.ToString().PadLeft(3, '0')}-{locationInfo.Layer.ToString().PadLeft(3, '0')}-{locationInfo.Depth.ToString().PadLeft(2, '0')}"; | 
|                             locationInfo.LocationName = $"{locationInfo.RoadwayNo}巷道{locationInfo.Row.ToString().PadLeft(3, '0')}行{locationInfo.Column.ToString().PadLeft(3, '0')}列{locationInfo.Layer.ToString().PadLeft(3, '0')}层{locationInfo.Depth.ToString().PadLeft(2, '0')}深"; | 
|                             locationInfos.Add(locationInfo); | 
|                         } | 
|                     } | 
|                 } | 
|                 BaseDal.AddData(locationInfos); | 
|                 return WebResponseContent.Instance.OK(); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 return WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|         } | 
|     } | 
| } |