using Mapster; using SqlSugar; using wms.sqlsugar.model.fj; using WMS.BZModels; using WMS.BZModels.Dto.FJ.BillBom; using WMS.BZModels.Models.FJ; using WMS.BZSqlSugar; using WMS.Info; namespace WMS.BZServices.FJ { public class BillBomSetService { private readonly Repository _billBomsetgrpRepository; private readonly Repository _billBomsetinfoRepository; private readonly Repository _billBominfoRepository; private readonly Repository _matinforepository; private readonly Repository _billPaiKurepository; private readonly Repository _billBaseWarehouse; public BillBomSetService(Repository billBomsetgrpRepository, Repository billBomsetinfoRepository, Repository billBominfoRepository, Repository matinforepository, Repository billPaiKurepository, Repository billBaseWarehouse) { _billBomsetgrpRepository = billBomsetgrpRepository; _billBomsetinfoRepository = billBomsetinfoRepository; _billBominfoRepository = billBominfoRepository; _matinforepository = matinforepository; _billPaiKurepository = billPaiKurepository; _billBaseWarehouse = billBaseWarehouse; } public PagedInfo GetPageList(Pagination pagination, BillBomsetgrpQueryDto billBomsetgrpQueryDto) { var list = GetQueryable(billBomsetgrpQueryDto) .ToPage(pagination); return list; } public IEnumerable PaiKuGetList() { var list = _billBomsetgrpRepository.AsQueryable().With(SqlWith.NoLock).Where(x => x.IsStop == 0).ToList().Select(x => new { id = x.Id.ToString(), text = x.Name }); return list; } public PagedInfo GetPaiKuList(Pagination pagination, BillPaiKuQueryDto billPaiKuQueryDto) { var predicate = Expressionable.Create(); predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto?.KeyWord), (paiku, bomsetgrp, wareHouse) => paiku.Id.ToString() == billPaiKuQueryDto.KeyWord || paiku.WarehousId.ToString() == billPaiKuQueryDto.KeyWord || paiku.WarehouseCode.Contains(billPaiKuQueryDto.KeyWord) || paiku.BomsetgrpId.ToString() == billPaiKuQueryDto.KeyWord || paiku.BomsetgrpName.Contains(billPaiKuQueryDto.KeyWord) || paiku.Percent.ToString() == billPaiKuQueryDto.KeyWord); predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto.WarehouseId), (paiku, bomsetgrp, wareHouse) => paiku.WarehousId.ToString() == billPaiKuQueryDto.WarehouseId); predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto.BomsetgrpId), (paiku, bomsetgrp, wareHouse) => paiku.BomsetgrpId.ToString() == billPaiKuQueryDto.BomsetgrpId); predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto.Percent), (paiku, bomsetgrp, wareHouse) => paiku.Percent.ToString() == billPaiKuQueryDto.Percent); var list = _billPaiKurepository.Context.Queryable((paiku, bomsetgrp, wareHouse) => new object[] { JoinType.Left, paiku.BomsetgrpId == bomsetgrp.Id, JoinType.Left, paiku.WarehousId==wareHouse.Id, }).Where(predicate.ToExpression()) .Select((paiku, bomsetgrp, wareHouse) => new BillPaiKuDto { Id = paiku.Id.ToString(), Memo = paiku.Memo, AddWho = paiku.AddWho, EditWho = paiku.EditWho, AddTime = paiku.AddTime, EditTime = paiku.EditTime, BomsetgrpId = paiku.BomsetgrpId.ToString(), BomsetgrpName = paiku.BomsetgrpName, WarehousId = paiku.WarehousId.ToString(), WarehouseCode = paiku.WarehouseCode, Percent = paiku.Percent }) .MergeTable() .OrderByIF(!pagination.sidx.IsEmpty(), $"{pagination.sidx.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(pagination.sord) && pagination.sord.ToLower().Contains("desc") ? "desc" : "asc")}").ToList().Adapt>(); var page = new PagedInfo { PageSize = pagination.rows, PageIndex = pagination.page, TotalNum = list.Count, Result = list }; return page; } private ISugarQueryable GetQueryable(BillBomsetgrpQueryDto billBomsetgrpQueryDto) { var predicate = Expressionable.Create(); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.KeyWord), m => m.Code.Contains(billBomsetgrpQueryDto.KeyWord) || m.ProCode.Contains(billBomsetgrpQueryDto.KeyWord) || m.ProMaterCode.Contains(billBomsetgrpQueryDto.KeyWord) || m.ShortCode.ToString().Contains(billBomsetgrpQueryDto.KeyWord)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.Code), m => m.Code.Contains(billBomsetgrpQueryDto.Code)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.Name), m => m.Name.Contains(billBomsetgrpQueryDto.Name)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.ShortCode), m => m.ShortCode.ToString().Contains(billBomsetgrpQueryDto.ShortCode)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.ProMaterCode), m => m.ProMaterCode.Contains(billBomsetgrpQueryDto.ProMaterCode)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.BomName), m => m.BomName.Contains(billBomsetgrpQueryDto.BomName)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.ProCode), m => m.ProCode.Contains(billBomsetgrpQueryDto.ProCode)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.StampType), m => m.StampType.Equals(billBomsetgrpQueryDto.StampType)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.IsStop), m => m.IsStop.Equals(billBomsetgrpQueryDto.IsStop)); predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.IsDelete), m => m.IsDelete.Equals(billBomsetgrpQueryDto.IsDelete)); var sugarQueryable = _billBomsetgrpRepository.Queryable().Where(predicate.ToExpression()); return sugarQueryable; } public IList GetList(BillBomsetgrpQueryDto billBomsetgrpQueryDto) { var list = GetQueryable(billBomsetgrpQueryDto).ToList().Adapt>(); return list; } public IList GetBillBomsetinfoItem(long bomSetHdrId) { var predicate = Expressionable.Create(); predicate = predicate.AndIF(bomSetHdrId > 0, m => m.BomSetHdrId.Equals(bomSetHdrId)); var list = _billBomsetinfoRepository.Queryable().Where(predicate.ToExpression()).ToList().OrderBy(o => Convert.ToInt32(o.XYNo)).Adapt>(); return list; } public BillBomsetDto GetBillBomsetinfo(long BomsetId) { var bomset = _billBomsetgrpRepository.Queryable().Single(o => o.Id == BomsetId); if (bomset == null) { throw BZSysExCore.ThrowFailException("不存在的跺型!"); } var bomsetinfos = _billBomsetinfoRepository.Queryable().Where(o => o.BomSetHdrId == bomset.Id && o.IsEmpty == 0).ToList(); var dto = bomset.Adapt(); dto.Bomsetinfos = bomsetinfos.Adapt>(); return dto; } public void Save09(LoginUserInfo loginUser, string keyValue, BillBomsetgrp billBomsetgrpEntity, IList billBomsetinfos) { if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.Code)) { throw BZSysExCore.ThrowFailException("跺型编码不能为空!"); } if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.ProCode)) { throw BZSysExCore.ThrowFailException("投料信息不能为空!"); } if (billBomsetinfos == null || !billBomsetinfos.Any()) { throw BZSysExCore.ThrowFailException("请选择跺型工字轮编号!"); } if (billBomsetgrpEntity.ShortCode <= 0) { throw BZSysExCore.ThrowFailException("跺型短编码不正确!"); } var infoMatCodes = billBomsetinfos.Select(o => o.MatCode).Distinct().ToList(); // var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).ToDictionary(x => x.Key, x => x.Count()); //var hasDuplicates = infoMatCodes.GroupBy(x => x).Any(g => g.Count() > 1); var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).Select(x => x.Key).ToList(); if (duplicates != null && duplicates.Any()) { throw BZSysExCore.ThrowFailException(string.Join(",", duplicates) + ",湿拉物料有重复!"); } // todo 判断数量 // 主数据的数量和子的数量要一致。 //var groups = billBomsetinfos.GroupBy(o => o.MatCode).Select(g => (new { key = g, max = g.Max(n => n.QtyMaxCount), count = g.Count() })); //if (billBomsetinfos.Count() != billBomsetgrpEntity.HWCountQty) //{ // throw BZSysExCore.ThrowFailException("填写的跺型数量不一致!"); //} //if (groups.Any(o => o.max != o.count)) //{ // throw BZSysExCore.ThrowFailException("子跺型选择的工字轮和填写的数量不一致!"); //} var first = billBomsetinfos.FirstOrDefault(); for (int i = 1; i <= billBomsetgrpEntity.TotalQty; i++) { var item = billBomsetinfos.FirstOrDefault(o => o.XYNo == i.ToString()); if (item == null) { billBomsetinfos.Add(new BillBomsetinfo { BomSetHdrId = first.BomSetHdrId, CategoryId = 99, XYNo = i.ToString(), IsEmpty = 1, IsStop = 1, }); } } billBomsetinfos.ToList().ForEach(o => { o.Row = Convert.ToInt32(Math.Ceiling(Convert.ToInt32(o.XYNo) / 5.0d)); }); var mixRows = billBomsetinfos.GroupBy(o => new { o.Row, o.MatCode }).Where(o => o.Count() != 5).ToList().GroupBy(o => o.Key.Row).Where(o => o.Count() > 1); foreach (var row in mixRows) { billBomsetinfos.Where(o => o.Row == row.Key).ToList().ForEach(o => { if (row.Key.HasValue) { o.IsMixRow = 1; o.MixRowCode = row.Key.Value.ToString(); } }); } var procodelists = _billBominfoRepository.Queryable().Where(o => billBomsetgrpEntity.ProCode == o.ProCode && o.IsDelete == 0 && o.IsStop == 0).ToList(); if (!procodelists.Any()) { throw BZSysExCore.ThrowFailException("投料信息不存在!"); } //if (!_matinforepository.Queryable().Any(o => o.Code == billBomsetgrpEntity.ProMaterCode)) //{ // throw BZSysExCore.ThrowFailException("产出物料编码不存在!"); //} var baseMatinfos = _billBominfoRepository.Queryable().Where(o => o.IsDelete == 0 && o.IsStop == 0 && infoMatCodes.Contains(o.MatCode)).ToList(); //foreach (var item in infoMatCodes) //{ // if (baseMatinfos.Where(o => o.MatCode == item).Select(o => o.ProCode).Distinct().Count() > 1) // { // throw BZSysExCore.ThrowFailException("湿拉物料存在于多个投料大类中!"); // } //} //var bomProMatCodelists = _billBominfoRepository.Queryable().Where(o => o.ProCode == billBomsetgrpEntity.BomCode).Select(o => o.ProMatCode).Distinct().ToList(); //billBomsetgrpEntity.ProMaterCode = string.Join(',', bomProMatCodelists); if (keyValue.IsEmpty() || keyValue == "undefined") { var mat = _billBomsetgrpRepository.GetSingle(p => p.Code == billBomsetgrpEntity.Code); if (mat != null) { throw BZSysExCore.ThrowFailException("跺型编码已存在!"); } //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) //{ // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!"); //} //if (_billBomsetgrpRepository.Queryable().Any(o => o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode)) //{ // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!"); //} //billBomsetgrpEntity.ShortCode = GetShoreCodes(); //if (billBomsetgrpEntity.ShortCode <= 0) //{ // throw BZSysExCore.ThrowFailException("跺型短编码不正确!"); //} //if ( billBomsetgrpEntity.ShortCode > 255) //{ // throw BZSysExCore.ThrowFailException("跺型已满,请删除不用的跺型!"); //} //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) //{ // //billBomsetgrpEntity.ShortCode = GetShoreCodes(); // //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) // //{ // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!"); // //} //} //var list = _billBomsetinfoRepository.Queryable().Where(o => infoMatCodes.Contains(o.MatCode)).ToList(); //if (list.Any()) //{ // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).ToArray()) + ",湿拉物料已经存在其他跺型中!"); //} _billBomsetgrpRepository.UseTranAction(() => { var id = IdFactory.NewId(); billBomsetgrpEntity.Id = id; billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.IsEmpty == 0); billBomsetgrpEntity.AddWho = loginUser.UserNo; billBomsetgrpEntity.EditWho = loginUser.UserNo; billBomsetgrpEntity.AddTime = DateTime.Now; billBomsetgrpEntity.EditTime = DateTime.Now; billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray()); billBomsetgrpEntity.IsStop = 1; _billBomsetgrpRepository.Insert(billBomsetgrpEntity); billBomsetinfos.ToList().ForEach(o => { o.Id = IdFactory.NewId(); var xyno = Convert.ToInt32(o.XYNo); o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d)); o.BomSetHdrId = id; if (o.IsEmpty == 0 || o.CategoryId < 99) { o.SpoolType = SpoolType.WS09.ToString(); var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode); if (matinfo != null) { o.MatId = matinfo.MatId; } } o.AddTime = DateTime.Now; o.EditTime = DateTime.Now; o.AddWho = loginUser.UserNo; o.EditWho = loginUser.UserNo; o.IsStop = 1; }); _billBomsetinfoRepository.Insert(billBomsetinfos.ToList()); }); } else { var bomsetgrpId = Convert.ToInt64(keyValue); var entitys = _billBomsetgrpRepository.Queryable().Where(x => (x.Id == bomsetgrpId || x.Code == billBomsetgrpEntity.Code)).ToList(); if (!entitys.Any()) { throw BZSysExCore.ThrowFailException("跺型不存在!"); } if (entitys.Any(o => o.Id != bomsetgrpId)) { throw BZSysExCore.ThrowFailException("跺型编码已存在!"); } //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.ShortCode == billBomsetgrpEntity.ShortCode)) //{ // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!"); //} //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode)) //{ // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!"); //} // var list = _billBomsetinfoRepository.Context.Queryable((billBomsetinfo, billBomsetgrp) => new object[] { //JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0 //&& billBomsetgrp.IsStop == 0 && billBomsetinfo.BomSetHdrId != bomsetgrpId && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList(); // if (list.Any()) // { // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).ToArray()) + ",湿拉物料已经存在其他跺型中!"); // } _billBomsetgrpRepository.UseTranAction(() => { billBomsetgrpEntity.Id = bomsetgrpId; //if (_billBomsetgrpRepository.Queryable().Any(x => x.Id != bomsetgrpId && (x.Code == billBomsetgrpEntity.Code || x.Name == billBomsetgrpEntity.Name) && x.IsStop == 0)) //{ // throw BZSysExCore.ThrowFailException("跺型已存在!"); //} //billBomsetgrpEntity.Id = bomsetgrpId; billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.IsEmpty == 0); billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray()); billBomsetgrpEntity.EditWho = loginUser.UserNo; billBomsetgrpEntity.EditTime = DateTime.Now; billBomsetgrpEntity.IsStop = 1; _billBomsetgrpRepository.UpdateEntity(billBomsetgrpEntity); _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand(); billBomsetinfos.ToList().ForEach(o => { o.Id = IdFactory.NewId(); var xyno = Convert.ToInt32(o.XYNo); o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d)); o.BomSetHdrId = billBomsetgrpEntity.Id; if (o.IsEmpty == 0 || o.CategoryId < 99) { o.SpoolType = SpoolType.WS09.ToString(); var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode); if (matinfo != null) { o.MatId = matinfo.MatId; } } o.AddTime = DateTime.Now; o.EditTime = DateTime.Now; o.AddWho = loginUser.UserNo; o.EditWho = loginUser.UserNo; o.IsStop = 1; }); _billBomsetinfoRepository.Insert(billBomsetinfos.ToList()); }); } } /// /// 保存排库信息 /// /// /// /// public void SavePK(LoginUserInfo loginUser, string keyValue, BillPaiKu billPaiKuEntity) { try { if (billPaiKuEntity.Percent != Math.Truncate(billPaiKuEntity.Percent) || billPaiKuEntity.Percent > 100) { throw BZSysExCore.ThrowFailException("占比只能传入整数,且必须小于100!"); } if (billPaiKuEntity.BomsetgrpId <= 0) { throw BZSysExCore.ThrowFailException("垛型编码有误!"); } if (billPaiKuEntity.WarehousId <= 0) { throw BZSysExCore.ThrowFailException("仓库id有误!"); } var bomsetgrp = _billBomsetgrpRepository.AsQueryable().With(SqlWith.NoLock).First(x => x.Id == billPaiKuEntity.BomsetgrpId); if (bomsetgrp == null) { throw BZSysExCore.ThrowFailException($"未找到对应垛型{billPaiKuEntity.BomsetgrpId}!"); } var warehouse = _billBaseWarehouse.AsQueryable().With(SqlWith.NoLock).First(x => x.Id == billPaiKuEntity.WarehousId); if (warehouse == null) { throw BZSysExCore.ThrowFailException($"未找到对应仓库{billPaiKuEntity.WarehousId}!"); } if (keyValue == "undefined") keyValue = "0"; var paiKuId = Convert.ToInt64(keyValue); var paiKu = _billPaiKurepository.GetFirst(x => x.Id == paiKuId); if (paiKu != null) //更新 { //判断此垛型排库占比总数是否超过100 var percent = _billPaiKurepository.GetList(x => x.BomsetgrpId == bomsetgrp.Id && x.Id != paiKu.Id).Sum(x => x.Percent); if ((percent + billPaiKuEntity.Percent) > 100) { throw BZSysExCore.ThrowFailException($"{bomsetgrp.Name}超过最大占比,除当前编辑信息外总比重为{percent},请重新调整!"); } paiKu.BomsetgrpId = bomsetgrp.Id; paiKu.BomsetgrpName = bomsetgrp.Name; paiKu.WarehousId = warehouse.Id; paiKu.WarehouseCode = warehouse.Code; paiKu.Percent = billPaiKuEntity.Percent; _billPaiKurepository.Update(paiKu); } else //新增 { //判断此垛型排库占比总数是否超过100 var percent = _billPaiKurepository.GetList(x => x.BomsetgrpId == bomsetgrp.Id).Sum(x => x.Percent); if ((percent + billPaiKuEntity.Percent) > 100) { throw BZSysExCore.ThrowFailException($"{bomsetgrp.Name}超过最大占比,请重新调整!"); } BillPaiKu item = new BillPaiKu() { BomsetgrpId = bomsetgrp.Id, BomsetgrpName = bomsetgrp.Name, WarehousId = warehouse.Id, WarehouseCode = warehouse.Code, Percent = billPaiKuEntity.Percent }; _billPaiKurepository.Insert(item); } } catch (Exception ex) { throw BZSysExCore.ThrowFailException(ex.Message); } } public void Save(LoginUserInfo loginUser, string keyValue, BillBomsetgrp billBomsetgrpEntity, IList billBomsetinfos) { if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.Code)) { throw BZSysExCore.ThrowFailException("跺型编码不能为空!"); } if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.ProCode)) { throw BZSysExCore.ThrowFailException("投料信息不能为空!"); } if (billBomsetinfos == null || !billBomsetinfos.Any()) { throw BZSysExCore.ThrowFailException("请选择跺型工字轮编号!"); } if (billBomsetgrpEntity.ShortCode <= 0) { throw BZSysExCore.ThrowFailException("跺型短编码不正确!"); } var infoMatCodes = billBomsetinfos.Select(o => o.MatCode).Distinct().ToList(); // var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).ToDictionary(x => x.Key, x => x.Count()); //var hasDuplicates = infoMatCodes.GroupBy(x => x).Any(g => g.Count() > 1); var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).Select(x => x.Key).ToList(); if (duplicates != null && duplicates.Any()) { throw BZSysExCore.ThrowFailException(string.Join(",", duplicates) + ",湿拉物料有重复!"); } var procodelists = _billBominfoRepository.Queryable().Where(o => o.IsDelete == 0 && o.IsStop == 0 && billBomsetgrpEntity.ProCode == o.ProCode).ToList(); if (!procodelists.Any()) { throw BZSysExCore.ThrowFailException("投料信息不存在!"); } //在Bill_BomInfo 中MatCode不存在2个ProCode中 var baseMatinfos = _billBominfoRepository.Queryable().Where(o => o.IsDelete == 0 && o.IsStop == 0 && infoMatCodes.Contains(o.MatCode)).ToList(); //foreach (var item in infoMatCodes) //{ // if (baseMatinfos.Where(o => o.MatCode == item).Select(o => o.ProCode).Distinct().Count() > 1) // { // throw BZSysExCore.ThrowFailException("湿拉物料存在于多个投料大类中!"); // } //} //if (!_matinforepository.Queryable().Any(o => o.Code == billBomsetgrpEntity.ProMaterCode)) //{ // throw BZSysExCore.ThrowFailException("产出物料编码不存在!"); //} //billBomsetgrpEntity.BomCode = baseMatinfos.First().ProCode; //var bomProMatCodelists = _billBominfoRepository.Queryable().Where(o => o.ProCode == billBomsetgrpEntity.BomCode).Select(o => o.ProMatCode).Distinct().ToList(); //billBomsetgrpEntity.ProMaterCode = string.Join(',', bomProMatCodelists); var first = billBomsetinfos.FirstOrDefault(); for (int i = 1; i <= billBomsetgrpEntity.TotalQty; i++) { var item = billBomsetinfos.FirstOrDefault(o => o.XYNo == i.ToString()); if (item == null) { billBomsetinfos.Add(new BillBomsetinfo { BomSetHdrId = first.BomSetHdrId, CategoryId = 99, XYNo = i.ToString(), IsEmpty = 1, IsStop = 1, }); } } if (keyValue.IsEmpty() || keyValue == "undefined") { var mat = _billBomsetgrpRepository.GetSingle(p => p.Code == billBomsetgrpEntity.Code); if (mat != null) { throw BZSysExCore.ThrowFailException("跺型编码已存在!"); } //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) //{ // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!"); //} //if (_billBomsetgrpRepository.Queryable().Any(o => o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode)) //{ // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!"); //} //billBomsetgrpEntity.ShortCode = GetShoreCodes(); //if (billBomsetgrpEntity.ShortCode > 255) //{ // throw BZSysExCore.ThrowFailException("跺型已满,请删除不用的跺型!"); //} //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) //{ // billBomsetgrpEntity.ShortCode = GetShoreCodes(); // if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) // { // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新提交跺型!"); // } //} //var list = _billBomsetinfoRepository.Context.Queryable((billBomsetinfo, billBomsetgrp) => new object[] { //JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0 && billBomsetgrp.IsStop == 0 && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList(); //if (list.Any()) //{ // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).Distinct().ToArray()) + ",湿拉物料已经存在其他跺型中!"); //} _billBomsetgrpRepository.UseTranAction(() => { var id = IdFactory.NewId(); billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99); billBomsetgrpEntity.Id = id; billBomsetgrpEntity.AddWho = loginUser.UserNo; billBomsetgrpEntity.EditWho = loginUser.UserNo; billBomsetgrpEntity.AddTime = DateTime.Now; billBomsetgrpEntity.EditTime = DateTime.Now; billBomsetgrpEntity.IsStop = 1; billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray()); _billBomsetgrpRepository.Insert(billBomsetgrpEntity); //_billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand(); billBomsetinfos.ToList().ForEach(o => { o.Id = IdFactory.NewId(); var xyno = Convert.ToInt32(o.XYNo); o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d)); o.BomSetHdrId = id; if (o.CategoryId < 99) { var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode); if (matinfo != null) { o.MatId = matinfo.MatId; } } o.AddTime = DateTime.Now; o.EditTime = DateTime.Now; o.AddWho = loginUser.UserNo; o.EditWho = loginUser.UserNo; //o.Row = 0; o.IsStop = 1; }); _billBomsetinfoRepository.Insert(billBomsetinfos.ToList()); }); } else { var bomsetgrpId = Convert.ToInt64(keyValue); var entitys = _billBomsetgrpRepository.Queryable().Where(x => x.Id == bomsetgrpId || x.Code == billBomsetgrpEntity.Code).ToList(); if (!entitys.Any()) { throw BZSysExCore.ThrowFailException("跺型不存在!"); } if (entitys.Any(o => o.Id != bomsetgrpId)) { throw BZSysExCore.ThrowFailException("跺型编码已存在!"); } //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.ShortCode == billBomsetgrpEntity.ShortCode)) //{ // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!"); //} //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode)) //{ // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!"); //} //var list = _billBomsetinfoRepository.Context.Queryable((billBomsetinfo, billBomsetgrp) => new object[] { //JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0 //&& billBomsetgrp.IsStop == 0 && billBomsetinfo.BomSetHdrId != bomsetgrpId && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList(); //if (list.Any()) //{ // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).Distinct().ToArray()) + ",湿拉物料已经存在其他跺型中!"); //} _billBomsetgrpRepository.UseTranAction(() => { billBomsetgrpEntity.Id = bomsetgrpId; //billBomsetgrpEntity.ShortCode = entitys.First().ShortCode; billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99); billBomsetgrpEntity.EditWho = loginUser.UserNo; billBomsetgrpEntity.EditTime = DateTime.Now; billBomsetgrpEntity.IsStop = 1; billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray()); _billBomsetgrpRepository.UpdateEntity(billBomsetgrpEntity); _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand(); billBomsetinfos.ToList().ForEach(o => { o.Id = IdFactory.NewId(); var xyno = Convert.ToInt32(o.XYNo); o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d)); o.BomSetHdrId = billBomsetgrpEntity.Id; if (o.CategoryId < 99) { var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode); if (matinfo != null) { o.MatId = matinfo.MatId; } } o.AddTime = DateTime.Now; o.EditTime = DateTime.Now; o.AddWho = loginUser.UserNo; o.EditWho = loginUser.UserNo; //o.Row = 0; o.IsStop = 1; }); _billBomsetinfoRepository.Insert(billBomsetinfos.ToList()); }); } } public void SaveException(LoginUserInfo loginUser, string keyValue, BillBomsetgrp billBomsetgrpEntity, IList billBomsetinfos) { if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.Code)) { throw BZSysExCore.ThrowFailException("跺型编码不能为空!"); } if (!billBomsetinfos.Any()) { throw BZSysExCore.ThrowFailException("请选择工字轮!"); } if (billBomsetinfos == null || !billBomsetinfos.Any()) { throw BZSysExCore.ThrowFailException("请选择跺型工字轮编号!"); } var first = billBomsetinfos.FirstOrDefault(); for (int i = 1; i <= billBomsetgrpEntity.TotalQty; i++) { var item = billBomsetinfos.FirstOrDefault(o => o.XYNo == i.ToString()); if (item == null) { billBomsetinfos.Add(new BillBomsetinfo { BomSetHdrId = first.BomSetHdrId, CategoryId = 99, XYNo = i.ToString(), IsEmpty = 1, IsStop = 1, }); } } if (keyValue.IsEmpty() || keyValue == "undefined") { var mat = _billBomsetgrpRepository.GetSingle(p => p.Code == billBomsetgrpEntity.Code); if (mat != null) { throw BZSysExCore.ThrowFailException("跺型编码已存在!"); } //billBomsetgrpEntity.ShortCode = GetShoreCodes(); //if (billBomsetgrpEntity.ShortCode > 255) //{ // throw BZSysExCore.ThrowFailException("跺型已满,请删除不用的跺型!"); //} //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) //{ // billBomsetgrpEntity.ShortCode = GetShoreCodes(); // if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode)) // { // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新提交跺型!"); // } //} _billBomsetgrpRepository.UseTranAction(() => { var id = IdFactory.NewId(); billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99); billBomsetgrpEntity.Id = id; billBomsetgrpEntity.AddWho = loginUser.UserNo; billBomsetgrpEntity.EditWho = loginUser.UserNo; billBomsetgrpEntity.AddTime = DateTime.Now; billBomsetgrpEntity.EditTime = DateTime.Now; billBomsetgrpEntity.IsStop = 1; _billBomsetgrpRepository.Insert(billBomsetgrpEntity); //_billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand(); billBomsetinfos.ToList().ForEach(o => { o.Id = IdFactory.NewId(); var xyno = Convert.ToInt32(o.XYNo); //if (billBomsetgrpEntity.StampChildType == 4) //{ o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d)); //} o.BomSetHdrId = id; o.AddTime = DateTime.Now; o.EditTime = DateTime.Now; o.AddWho = loginUser.UserNo; o.EditWho = loginUser.UserNo; o.IsStop = 1; }); _billBomsetinfoRepository.Insert(billBomsetinfos.ToList()); }); } else { var bomsetgrpId = Convert.ToInt64(keyValue); var entitys = _billBomsetgrpRepository.Queryable().Where(x => x.Id == bomsetgrpId || x.Code == billBomsetgrpEntity.Code).ToList(); if (!entitys.Any()) { throw BZSysExCore.ThrowFailException("跺型编码不存在!"); } if (entitys.Any(o => o.Id != bomsetgrpId)) { throw BZSysExCore.ThrowFailException("跺型编码已存在!"); } _billBomsetgrpRepository.UseTranAction(() => { billBomsetgrpEntity.Id = bomsetgrpId; billBomsetgrpEntity.ShortCode = entitys.First().ShortCode; billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99); billBomsetgrpEntity.EditWho = loginUser.UserNo; billBomsetgrpEntity.EditTime = DateTime.Now; billBomsetgrpEntity.IsStop = 1; _billBomsetgrpRepository.UpdateEntity(billBomsetgrpEntity); _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand(); billBomsetinfos.ToList().ForEach(o => { o.Id = IdFactory.NewId(); var xyno = Convert.ToInt32(o.XYNo); //if (billBomsetgrpEntity.StampChildType == 4) //{ o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d)); //} o.BomSetHdrId = billBomsetgrpEntity.Id; o.AddTime = DateTime.Now; o.EditTime = DateTime.Now; o.AddWho = loginUser.UserNo; o.EditWho = loginUser.UserNo; o.IsStop = 1; }); _billBomsetinfoRepository.Insert(billBomsetinfos.ToList()); }); } } public int GetShoreCodes() { var lists = _billBomsetgrpRepository.Queryable().OrderBy(o => o.ShortCode).Select(o => o.ShortCode).ToArray(); if (!lists.Any()) { //没有数据的时候返回1. return 1; } else { var numberArrs = lists.Adapt>(); var numbers = FindDisappearedNumbers(numberArrs); if (numbers.Any()) { //找到缺失的数字 return numbers[0]; } else { //没有缺失的数字,则返回最大值加1. var maxShortCode = _billBomsetgrpRepository.Queryable().Select(o => SqlFunc.AggregateMax(o.ShortCode)).Single(); return ++maxShortCode; } } } public IList FindDisappearedNumbers(List nums) { var res = new List(); if (nums[nums.Count - 1] != nums.Count) { var len = nums[nums.Count - 1] - nums.Count; for (int i = 0; i < len; i++) { nums.Insert(i, 1); } } var numsarr = nums.ToArray(); for (int i = 0; i < numsarr.Length; i++) { //记录索引,位置为i处的值的绝对值减1 //(因为数值范围从1..n所以要减1)作为临时索引位 int index = Math.Abs(numsarr[i]) - 1; if (index < 0) { index = 0; } //如果此处的值大于0,那么将其变为负数, //没有变为负数的值即为不存在于数组中的数字, //直接变为负数不至于使其值丢失,只需要取绝对值即可恢复原值 if (index < numsarr.Length && numsarr[index] > 0) { numsarr[index] = -numsarr[index]; } } //所有值大于0的即为“迷失的数字” for (int i = 0; i < numsarr.Length; i++) { if (numsarr[i] > 0) { res.Add(i + 1); } } return res; } // Function to return the missing element public static int findMissing(int[] arr, int n) { int l = 0, h = n - 1; int mid; while (h > l) { mid = l + (h - l) / 2; // Check if middle element is consistent if (arr[mid] - mid == arr[0]) { // No inconsistency till middle elements // When missing element is just after // the middle element if (arr[mid + 1] - arr[mid] > 1) return arr[mid] + 1; else { // Move right l = mid + 1; } } else { // Inconsistency found // When missing element is just before // the middle element if (arr[mid] - arr[mid - 1] > 1) return arr[mid] - 1; else { // Move left h = mid - 1; } } } // No missing element found return -1; } public void DeletePK(long id) { try { _billPaiKurepository.Deleteable().Where(o => o.Id == id).ExecuteCommand(); } catch (Exception ex) { throw ex; } } public void Delete(long id) { var entity = _billBomsetgrpRepository.GetSingle(o => o.Id == id); if (entity == null) { throw BZSysExCore.ThrowFailException("跺型不存在!"); } _billBomsetgrpRepository.Deleteable().Where(it => it.Id == entity.Id).ExecuteCommand(); _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == entity.Id).ExecuteCommand(); } public void Deletes(string[] ids, string userId) { if (ids == null || ids.Length == 0) { throw BZSysExCore.ThrowFailException("跺型Id不能为空!"); } var list = _billBomsetgrpRepository.Queryable().Where(o => ids.Contains(o.Id.ToString())).ToList(); if (!list.Any()) { throw new ArgumentException("没有选择跺型数据!"); } _billBomsetgrpRepository.UseTranAction(() => { foreach (var item in list) { _billBomsetgrpRepository.Deleteable().Where(it => it.Id == item.Id).ExecuteCommand(); _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == item.Id).ExecuteCommand(); } }); //list.ForEach(o => //{ // o.IsDelete = 1; // o.EditWho = userId; // o.EditTime = DateTime.Now; //}); //var result = _billBomsetgrpRepository.UpdateRange(list.ToArray()); } public void DeleteByCode(string code) { var entity = _billBomsetgrpRepository.GetSingle(o => o.Code == code); if (entity == null) { throw BZSysExCore.ThrowFailException("跺型不存在!"); } _billBomsetgrpRepository.Deleteable().Where(it => it.Id == entity.Id).ExecuteCommand(); _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == entity.Id).ExecuteCommand(); } /// /// /// /// 跺型编号 /// 是否 /// 操作人 /// /// public void ChangeEnableds(List Ids, LocationStop locationStop, string userId) { if (Ids == null || !Ids.Any()) { throw new ArgumentException("没有选择跺型数据!"); } var billBomsetgrplist = _billBomsetgrpRepository.Queryable().Where(o => Ids.Contains(o.Id.ToString())).ToList(); if (!billBomsetgrplist.Any()) { throw new ArgumentException("没有选择跺型数据!"); } var now = DateTime.Now; billBomsetgrplist.ForEach(o => { o.IsStop = (int)locationStop; o.EditWho = userId; o.EditTime = now; }); var infos = _billBomsetinfoRepository.Queryable().Where(it => Ids.Contains(it.BomSetHdrId.ToString())).ToList(); if (!infos.Any()) { throw new ArgumentException("没有选择跺型数据!"); } //启用 if (locationStop == LocationStop.LocationInvoke) { var infoMatCodes = infos.Select(o => o.MatCode).Distinct().ToList(); foreach (var item in billBomsetgrplist) { if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != item.Id && o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == item.BomCode)) { throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!"); } var list = _billBomsetinfoRepository.Context.Queryable((billBomsetinfo, billBomsetgrp) => new object[] { JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0 && billBomsetgrp.IsStop == 0 && billBomsetinfo.BomSetHdrId != item.Id && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList(); if (list.Any()) { throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).ToArray()) + ",湿拉物料已经存在其他跺型中!"); } } } infos.ForEach(o => { o.IsStop = (int)locationStop; o.EditWho = userId; o.EditTime = now; }); // var result = _billBomsetgrpRepository.UpdateRange(list.ToArray()); _billBomsetgrpRepository.UseTranAction(() => { _billBomsetgrpRepository.UpdateRange(billBomsetgrplist.ToArray()); _billBomsetinfoRepository.UpdateRange(infos.ToArray()); }); } /// /// /// /// 1为09的跺型。 /// public IEnumerable GetSelectCodeListByType(string typenum) { return typenum == "1" ? _billBomsetgrpRepository.Queryable().Where(o => o.IsStop == 0 && o.StampType == 1).ToList().Select(o => new { id = o.Code.ToString(), text = o.Name }) : _billBomsetgrpRepository.Queryable().Where(o => o.IsStop == 0 && o.StampType != 1).ToList().Select(o => new { id = o.Code.ToString(), text = o.Name }); } } }