using Aucma.Scada.Model.domain; using Aucma.Scada.Model.dto; using HighWayIot.Common; using HighWayIot.Log4net; using SqlSugar; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace HighWayIot.Repository.service.Impl { public class BaseSpaceInfoServiceImpl : IBaseSpaceInfoService { private Repository _mesRepository = new Repository("mes"); private LogHelper logHelper = LogHelper.Instance; private JsonChange jsonChange = JsonChange.Instance; /// /// 入库通过物料类型获取指定货道,如果没有对应类型的货道返回空白类型的货道 /// /// /// /// public BaseSpaceInfo InStoreGetSpaceInfoByMaterialType(string store, string materialType) { BaseSpaceInfo spaceInfo = null; List spaceInfos; try { Expression> exp = s1 => true; exp = exp.And(x => x.storeCode == store && x.materialType == materialType && x.spaceStatus == 1 && x.spaceCapacity > (x.spaceStock + x.onRouteAmount)); //相同型号、启用状态、库存未满的货道信息 spaceInfos = _mesRepository.GetList(exp); if (spaceInfos.Count == 0) //没有指定该类型物料的货道信息,需获取空白货道信息进行分配 { spaceInfos = GetEmptySpaceInfo(store); } logHelper.Info($"根据仓库{store};物料:{materialType};获取到的货道信息:{jsonChange.ModeToJson(spaceInfos)}"); spaceInfo = inStoreFilter(spaceInfos); if(spaceInfo != null) { logHelper.Info($"仓库{store};物料:{materialType};匹配的入库货道信息:{jsonChange.ModeToJson(spaceInfo)}"); spaceInfo.materialType = materialType; } else { logHelper.Info($"仓库{store};物料:{materialType};未匹配到可用货道"); } } catch (Exception ex) { logHelper.Error("入库通过物料类型获取货道信息异常", ex); } return spaceInfo; } /// /// 入库过滤逻辑 /// /// private BaseSpaceInfo inStoreFilter(List spaceInfos) { BaseSpaceInfo spaceInfo = null; if (spaceInfos.Count > 0) { //获取库存未满库存最多的货道 spaceInfo = spaceInfos.Where(x => x.spaceCapacity != x.spaceStock).OrderByDescending(x => x.spaceStock).First(); } else { logHelper.Info("入库过滤未获取到匹配的货道信息"); } return spaceInfo; } /// /// 获取空货道:未分配物料型号的空白货道 /// /// private List GetEmptySpaceInfo(string store) { List spaceInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.materialType == null && x.storeCode == store && x.spaceCapacity != x.spaceStock); spaceInfos = _mesRepository.GetList(exp); } catch (Exception ex) { logHelper.Error("获取空货道异常", ex); } return spaceInfos; } /// /// 出库通过物料类型获取指定货道 /// /// /// /// public BaseSpaceInfo OutStoreGetSpaceInfoByMaterialCode(string store, string materialType) { BaseSpaceInfo spaceInfo = null; List spaceInfos; try { Expression> exp = s1 => true; exp = exp.And(x => x.storeCode == store && x.materialType == materialType && x.spaceStatus == 1 && x.spaceStock - x.outRouteAmount > 0); //相同型号、启用状态、库存不为空的货道信息 spaceInfos = _mesRepository.GetList(exp); logHelper.Info($"根据仓库{store};物料:{materialType};获取到的货道信息:{jsonChange.ModeToJson(spaceInfos)}"); spaceInfo = outStoreFilter(spaceInfos); logHelper.Info($"仓库{store};物料:{materialType};匹配的入库货道信息:{jsonChange.ModeToJson(spaceInfo)}"); } catch (Exception ex) { logHelper.Error("出库通过物料类型获取货道信息异常", ex); } return spaceInfo; } /// /// 出库过滤逻辑 /// /// private BaseSpaceInfo outStoreFilter(List spaceInfos) { BaseSpaceInfo spaceInfo = null; if (spaceInfos.Count > 0) { //获取库存最少的货道(优先清空货道) spaceInfo = spaceInfos.Where(x => x.spaceStock > 0).OrderBy(x => x.spaceStock).First(); } else { logHelper.Info("出库过滤未获取到匹配的货道信息"); } return spaceInfo; } /// /// 通过货道编号获取货道信息 /// /// /// /// public BaseSpaceInfo GetSpaceInfoBySpaceCode(string store, string spaceCode) { BaseSpaceInfo spaceInfo = null; try { spaceInfo = _mesRepository.GetFirst(x => x.storeCode == store && x.spaceCode == spaceCode); } catch (Exception ex) { logHelper.Error("通过货道编号获取货道信息异常", ex); } return spaceInfo; } /// /// 通过仓库编号获取货道信息 /// /// /// public List GetSpaceInfosByStoreCode(string storeCode) { List spaceInfos = null; try { spaceInfos = _mesRepository.GetList(x => x.storeCode == storeCode).ToList(); } catch (Exception ex) { logHelper.Error("获取货道信息异常", ex); } return spaceInfos; } /// /// 更新货道信息 /// /// /// public bool UpdateSpaceInfo(BaseSpaceInfo spaceInfo) { bool result = false; try { result = _mesRepository.Update(spaceInfo); } catch (Exception ex) { logHelper.Error("更新货道信息异常", ex); } return result; } /// /// /// /// /// /// public List GetMaterialStock(string shellStoreCode, string linerStoreCode) { List result = null; try { var _db = _mesRepository.Context; List query = _db.Queryable().AS("GET_STORE_MATERIALSTOCK").Where("STORE_CODE in (@shellStore,@linerStore)", new { shellStore = shellStoreCode, linerStore = linerStoreCode }).ToList(); if(query.Count > 0) { result = query.GroupBy(s => s.PARENT_NAME) .Select(g => (dynamic)new MaterialStockDto { parentMaterialName = g.Key, shellStock = g.Where(x => x.STORE_CODE == shellStoreCode).Select(x => x.SPACE_STOCK).FirstOrDefault(), linerStock = g.Where(x => x.STORE_CODE == linerStoreCode).Select(x => x.SPACE_STOCK).FirstOrDefault() }).ToList(); } } catch (Exception ex) { logHelper.Error("获取物料库存异常", ex); } return result; } public List GetBaseSpaceInfosByMaterialType(string storeCode, string materialType) { List spaceInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.storeCode == storeCode && x.materialType == materialType && x.spaceStatus == 1); //相同型号、启用状态的货道信息 spaceInfos = _mesRepository.GetList(exp); if (spaceInfos.Count == 0) //没有指定该类型物料的货道信息,需获取空白货道信息进行分配 { spaceInfos = GetEmptySpaceInfo(storeCode); } logHelper.Info($"根据仓库{storeCode};物料:{materialType};获取到的货道信息:{jsonChange.ModeToJson(spaceInfos)}"); } catch (Exception ex) { logHelper.Error("获取货道信息异常", ex); } return spaceInfos; } } }