using Mapster; using Microsoft.AspNetCore.DataProtection; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using wms.sqlsugar.model.sx; using WMS.BZModels; using WMS.BZModels.Dto.SX.BillInvDtos; using WMS.BZModels.Dto.SX.WareCellDtos; using WMS.BZModels.Dto.SX.TaskDtos; using WMS.Info; using WMS.BZSqlSugar; using Dm; using NPOI.SS.Formula.Functions; namespace WMS.BZServices.SX { public class WareCellService { private readonly Repository _warecellRepository; private readonly Repository _warehouseRepository; public WareCellService(Repository warecellRepository, Repository warehouseRepository) { _warecellRepository = warecellRepository; _warehouseRepository = warehouseRepository; } public IEnumerable GetSelectSRMNameList() { return _warecellRepository.Queryable().Select(o => o.SCRel).Distinct().ToList().OrderBy(o => o).Select(o => new { id = o, text = o }); } /// /// 获取仓库有多少排 /// /// 仓库编号 /// public int GetMaxLine(string houseNo) { try { return _warecellRepository.Queryable().Where(it => it.WarehouseCode == houseNo).Max(it => it.Row); } catch (Exception ex) { throw ex; } } public StockLocationViewDto GetLocList(WarecellQuery query) { var result = new StockLocationViewDto(); var predicate = Expressionable.Create(); predicate = predicate.AndIF(!string.IsNullOrEmpty(query.SCRel), m => m.SCRel == query.SCRel); var list = _warecellRepository.Queryable().Where(predicate.ToExpression()).ToList(); var grouplist = from p in list group p by p.Shelf into g let q = list.Where(x => x.Shelf == g.Key) orderby g.Key ascending select new { Shelf = q.Select(y => y.Shelf).FirstOrDefault(), }; string LeftShelf = string.Empty; string RightShelf = string.Empty; if (grouplist.Count() > 0) { LeftShelf = grouplist.ElementAt(0).Shelf; var leftlist = list.Where(p => p.Shelf == LeftShelf).ToList(); result.LeftJson = new List(); result.LeftJson = TransToLocationView(leftlist.Adapt>()); if (leftlist.Any()) { result.X = leftlist.Select(p => p.Col).OrderByDescending(p => p).First(); result.Y = leftlist.Select(p => p.Layer).OrderByDescending(p => p).First(); } } if (grouplist.Count() > 1) { RightShelf = grouplist.ElementAt(1).Shelf; var rightlist = list.Where(p => p.Shelf == RightShelf).ToList(); result.RightJson = new List(); result.RightJson = TransToLocationView(rightlist.Adapt>()); if (rightlist.Any()) { int tempX = rightlist.Select(p => p.Col).OrderByDescending(p => p).First(); int tempY = rightlist.Select(p => p.Layer).OrderByDescending(p => p).First(); if (tempX > result.X) { result.X = tempX; } if (tempY > result.Y) { result.Y = tempY; } } } return result; } private List TransToLocationView(List locationEntities) { var stockColor = "#52c41a"; var emptyColor = "#e0e0e0"; var lockColor = "#ffff00"; var faultColor = "#ff0000";//故障 var dicColor = new Dictionary() { }; dicColor.Add((int)LocationState.LocationState_Empty, emptyColor); dicColor.Add((int)LocationState.LocationState_Full, stockColor); dicColor.Add((int)LocationState.LocationState_StockIn, lockColor); dicColor.Add((int)LocationState.LocationState_StockOut, lockColor); dicColor.Add((int)LocationState.LocationState_StockMove, lockColor); dicColor.Add(6, faultColor); double viewHeight = 0.3; double viewWidth = 0.8; double viewHeightPadding = 0.5; var list = new List(); if (locationEntities != null && locationEntities.Any()) { foreach (var item in locationEntities) { var itemView = item.Adapt(); //itemView.LocationState = item.StateNum.HasValue? item.StateNum.Value.ToString():"1"; itemView.LocationStateName = item.StateNum == 2 || item.StateNum == 4 ? "有货" : "无货"; double y1 = item.Depth == 1 ? item.Layer + 0.1 : viewHeightPadding + item.Layer + 0.1; double y2 = Math.Round(y1 + viewHeight, 1, MidpointRounding.AwayFromZero); double x1 = item.Col + 0.1; double x2 = Math.Round(x1 + viewWidth, 1, MidpointRounding.AwayFromZero); itemView.value = new List() { y1,y2,x1,x2 }; string color = ""; dicColor.TryGetValue(item.StateNum.Value, out color); //if ((!string.IsNullOrEmpty(item.LocationLock) && item.LocationLock != DictionaryConst.LocationLockNoneCode)) //{ // color = lockColor; //} if (item.IsStop == 1 || item.StateNum == 0) { color = faultColor; } itemView.itemStyle = new ItemStyle() { normal = new Normal() { color = color } }; list.Add(itemView); } } return list; } /// /// 获取储位信息 /// /// public IEnumerable GetLocList(string houserNo, int line) { var predicate = Expressionable.Create().And(o => o.IsStop == 0); predicate = predicate.AndIF(!string.IsNullOrEmpty(houserNo), m => m.WarehouseCode == houserNo); predicate = predicate.AndIF(line > 0, m => m.Row == line); var resultList = _warecellRepository.Queryable().Where(predicate.ToExpression()).ToList().Adapt>(); return resultList; } public PagedInfo GetPageList(Pagination pagination, WarecellListQueryDto warecellListQueryDto) { var predicate = Expressionable.Create(); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.KeyWord), (warecell, warehouse) => warecell.Code.Contains(warecellListQueryDto.KeyWord) || warecell.Name.Contains(warecellListQueryDto.KeyWord)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.WarehouseId), (warecell, warehouse) => warecell.WarehouseId.ToString().Equals(warecellListQueryDto.WarehouseId)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.WareAreaId), (warecell, warehouse) => warecell.WareAreaId.ToString().Contains(warecellListQueryDto.WareAreaId)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.IsStop), (warecell, warehouse) => warecell.IsStop.Equals(warecellListQueryDto.IsStop)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.Code), (warecell, warehouse) => warecell.Code.Contains(warecellListQueryDto.Code)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.Name), (warecell, warehouse) => warecell.Name.Contains(warecellListQueryDto.Name)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.Tunnel), (warecell, warehouse) => warecell.Tunnel.ToString().Contains(warecellListQueryDto.Tunnel)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.Layer), (warecell, warehouse) => warecell.Layer.Equals(warecellListQueryDto.Layer)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.SCRel), (warecell, warehouse) => warecell.SCRel.Contains(warecellListQueryDto.SCRel)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.Row), (warecell, warehouse) => warecell.Row == int.Parse(warecellListQueryDto.Row)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.Col), (warecell, warehouse) => warecell.Col == int.Parse(warecellListQueryDto.Col)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.ContGrpBarCode), (warecell, warehouse) => warecell.ContGrpBarCode.Contains(warecellListQueryDto.ContGrpBarCode)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.TypeNum), (warecell, warehouse) => warecell.TypeNum.Equals(warecellListQueryDto.TypeNum)); predicate = predicate.AndIF(!string.IsNullOrEmpty(warecellListQueryDto?.StateNum), (warecell, warehouse) => warecell.StateNum.Equals(warecellListQueryDto.StateNum)); predicate = predicate.AndIF(warecellListQueryDto != null && warecellListQueryDto.AddTimeFrom.HasValue, (warecell, warehouse) => warecell.AddTime >= warecellListQueryDto.AddTimeFrom); predicate = predicate.AndIF(warecellListQueryDto != null && warecellListQueryDto.AddTimeTo.HasValue, (warecell, warehouse) => warecell.AddTime <= warecellListQueryDto.AddTimeTo); var list = _warecellRepository.Context.Queryable((warecell, warehouse) => new object[] { JoinType.Left, warecell.WarehouseId == warehouse.Id }).Where(predicate.ToExpression()) .Select((warecell, warehouse) => new WarecellDto { Id = warecell.Id.ToString(), WarehouseName = warehouse.Name, WarehouseId = warecell.WarehouseId.ToString(), WarehouseCode = warecell.WarehouseCode, WareAreaId = warecell.WareAreaId.ToString(), IsStop = warecell.IsStop, Code = warecell.Code, Name = warecell.Name, StateNum = warecell.StateNum, TypeNum = warecell.TypeNum, Size = warecell.Size, Row = warecell.Row, Col = warecell.Col, Layer = warecell.Layer, Depth = warecell.Depth, Tunnel = warecell.Tunnel, SCRel = warecell.SCRel, ContGrpId = warecell.ContGrpId == null ? "" : warecell.ContGrpId.ToString(), ContGrpBarCode = warecell.ContGrpBarCode, Shelf = warecell.Shelf, Memo = warecell.Memo, AddWho = warecell.AddWho, AddTime = warecell.AddTime, EditWho = warecell.EditWho, EditTime = warecell.EditTime, }).MergeTable() .ToPage(pagination); //.ToPage(pagination); return list; } public int GetEmptyWareCell() { var predicate = Expressionable.Create(); predicate = predicate.And(m => m.StateNum.Equals((int)LocationState.LocationState_Empty) && m.IsStop == 0); var count = _warecellRepository.Queryable().Where(predicate.ToExpression()).Count(); return count; } /// /// /// /// 货位编号 /// 是否 /// 操作人 /// /// public bool ChangeWareCells(List wareCellIds, LocationStop locationStop, string userId) { if (wareCellIds == null || !wareCellIds.Any()) { throw new ArgumentException("没有选择货位!"); } var list = _warecellRepository.Queryable().Where(o => wareCellIds.Contains(o.Id.ToString())).ToList(); if (!list.Any()) { throw new ArgumentException("没有选择货位!"); } list.ForEach(o => { o.IsStop = (int)locationStop; o.EditWho = userId; o.EditTime = DateTime.Now; }); var result = _warecellRepository.UpdateRange(list.ToArray()); return result; } public void Save(LoginUserInfo loginUser, string keyValue, BaseWarecell entity) { try { if (string.IsNullOrWhiteSpace(entity.Code)) { throw new ArgumentException("货位编码不能为空"); } var warehouse = _warehouseRepository.Queryable().First(o => o.Id == entity.WarehouseId); if (warehouse == null) { throw new ArgumentException("不存在的仓库!"); } if (keyValue.IsEmpty() || keyValue == "undefined") { var mat = _warecellRepository.GetSingle(p => p.Code == entity.Code); if (mat != null) { throw new ArgumentException("货位编码已存在"); } BaseWarecell item = new BaseWarecell() { WarehouseId = warehouse.Id, AddWho = loginUser.UserNo, Code = entity.Code, EditWho = loginUser.UserNo, Name = entity.Name, WarehouseCode = warehouse.Code, WareAreaId = entity.WareAreaId, IsStop = 1, StateNum = 1, TypeNum = 1, Size = 1, Row = entity.Row, Col = entity.Col, Layer = entity.Layer, Depth = entity.Depth, Tunnel = entity.Tunnel, SCRel = entity.SCRel, ContGrpId = 0, ContGrpBarCode = "", Memo= string.IsNullOrWhiteSpace(entity.Memo)?" ": entity.Memo, Shelf = entity.Shelf }; _warecellRepository.Insert(item); } else { entity.EditTime = DateTime.Now; entity.EditWho = loginUser.UserNo; _warecellRepository.AsUpdateable(entity).IgnoreColumns(it => it.AddTime).IgnoreColumns(it => it.AddWho).IgnoreColumns(it => it.Size). IgnoreColumns(it => it.TypeNum).IgnoreColumns(it => it.StateNum).IgnoreColumns(it => it.IsStop).IgnoreColumns(it => it.ContGrpId).IgnoreColumns(it => it.ContGrpBarCode) .IgnoreColumns(it => it.WareAreaId).IgnoreColumns(it => it.WarehouseCode).IgnoreColumns(it => it.WarehouseId).Where(it => it.Code == keyValue).ExecuteCommand(); } } catch (Exception ex) { throw ex; } } public void Delete(string ModuleNo) { try { _warecellRepository.Deleteable().Where(it => it.Code == ModuleNo).ExecuteCommand(); } catch (Exception ex) { throw ex; } } } }