BillBomSetService.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. using Mapster;
  2. using SqlSugar;
  3. using wms.sqlsugar.model.fj;
  4. using WMS.BZModels;
  5. using WMS.BZModels.Dto.FJ.BillBom;
  6. using WMS.BZModels.Models.FJ;
  7. using WMS.BZSqlSugar;
  8. using WMS.Info;
  9. namespace WMS.BZServices.FJ
  10. {
  11. public class BillBomSetService
  12. {
  13. private readonly Repository<BillBomsetgrp> _billBomsetgrpRepository;
  14. private readonly Repository<BillBomsetinfo> _billBomsetinfoRepository;
  15. private readonly Repository<BillBominfo> _billBominfoRepository;
  16. private readonly Repository<BaseMatinfo> _matinforepository;
  17. private readonly Repository<BillPaiKu> _billPaiKurepository;
  18. private readonly Repository<BaseWarehouse> _billBaseWarehouse;
  19. public BillBomSetService(Repository<BillBomsetgrp> billBomsetgrpRepository,
  20. Repository<BillBomsetinfo> billBomsetinfoRepository,
  21. Repository<BillBominfo> billBominfoRepository,
  22. Repository<BaseMatinfo> matinforepository,
  23. Repository<BillPaiKu> billPaiKurepository,
  24. Repository<BaseWarehouse> billBaseWarehouse)
  25. {
  26. _billBomsetgrpRepository = billBomsetgrpRepository;
  27. _billBomsetinfoRepository = billBomsetinfoRepository;
  28. _billBominfoRepository = billBominfoRepository;
  29. _matinforepository = matinforepository;
  30. _billPaiKurepository = billPaiKurepository;
  31. _billBaseWarehouse = billBaseWarehouse;
  32. }
  33. public PagedInfo<BillBomsetgrpDto> GetPageList(Pagination pagination, BillBomsetgrpQueryDto billBomsetgrpQueryDto)
  34. {
  35. var list = GetQueryable(billBomsetgrpQueryDto)
  36. .ToPage<BillBomsetgrp, BillBomsetgrpDto>(pagination);
  37. return list;
  38. }
  39. public IEnumerable<dynamic> PaiKuGetList()
  40. {
  41. var list = _billBomsetgrpRepository.AsQueryable().With(SqlWith.NoLock).Where(x => x.IsStop == 0).ToList().Select(x => new { id = x.Id.ToString(), text = x.Name });
  42. return list;
  43. }
  44. public PagedInfo<BillPaiKuDto> GetPaiKuList(Pagination pagination, BillPaiKuQueryDto billPaiKuQueryDto)
  45. {
  46. var predicate = Expressionable.Create<BillPaiKu, BillBomsetgrp, BaseWarehouse>();
  47. predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto?.KeyWord), (paiku, bomsetgrp, wareHouse) =>
  48. paiku.Id.ToString() == billPaiKuQueryDto.KeyWord ||
  49. paiku.WarehousId.ToString() == billPaiKuQueryDto.KeyWord ||
  50. paiku.WarehouseCode.Contains(billPaiKuQueryDto.KeyWord) ||
  51. paiku.BomsetgrpId.ToString() == billPaiKuQueryDto.KeyWord ||
  52. paiku.BomsetgrpName.Contains(billPaiKuQueryDto.KeyWord) ||
  53. paiku.Percent.ToString() == billPaiKuQueryDto.KeyWord);
  54. predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto.WarehouseId), (paiku, bomsetgrp, wareHouse) => paiku.WarehousId.ToString() == billPaiKuQueryDto.WarehouseId);
  55. predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto.BomsetgrpId), (paiku, bomsetgrp, wareHouse) => paiku.BomsetgrpId.ToString() == billPaiKuQueryDto.BomsetgrpId);
  56. predicate = predicate.AndIF(!string.IsNullOrEmpty(billPaiKuQueryDto.Percent), (paiku, bomsetgrp, wareHouse) => paiku.Percent.ToString() == billPaiKuQueryDto.Percent);
  57. var list = _billPaiKurepository.Context.Queryable<BillPaiKu, BillBomsetgrp, BaseWarehouse>((paiku, bomsetgrp, wareHouse) => new object[]
  58. {
  59. JoinType.Left, paiku.BomsetgrpId == bomsetgrp.Id,
  60. JoinType.Left, paiku.WarehousId==wareHouse.Id,
  61. }).Where(predicate.ToExpression())
  62. .Select((paiku, bomsetgrp, wareHouse) => new BillPaiKuDto
  63. {
  64. Id = paiku.Id.ToString(),
  65. Memo = paiku.Memo,
  66. AddWho = paiku.AddWho,
  67. EditWho = paiku.EditWho,
  68. AddTime = paiku.AddTime,
  69. EditTime = paiku.EditTime,
  70. BomsetgrpId = paiku.BomsetgrpId.ToString(),
  71. BomsetgrpName = paiku.BomsetgrpName,
  72. WarehousId = paiku.WarehousId.ToString(),
  73. WarehouseCode = paiku.WarehouseCode,
  74. Percent = paiku.Percent
  75. })
  76. .MergeTable()
  77. .OrderByIF(!pagination.sidx.IsEmpty(), $"{pagination.sidx.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(pagination.sord) && pagination.sord.ToLower().Contains("desc") ? "desc" : "asc")}").ToList().Adapt<List<BillPaiKuDto>>();
  78. var page = new PagedInfo<BillPaiKuDto>
  79. {
  80. PageSize = pagination.rows,
  81. PageIndex = pagination.page,
  82. TotalNum = list.Count,
  83. Result = list
  84. };
  85. return page;
  86. }
  87. private ISugarQueryable<BillBomsetgrp> GetQueryable(BillBomsetgrpQueryDto billBomsetgrpQueryDto)
  88. {
  89. var predicate = Expressionable.Create<BillBomsetgrp>();
  90. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.KeyWord), m => m.Code.Contains(billBomsetgrpQueryDto.KeyWord)
  91. || m.ProCode.Contains(billBomsetgrpQueryDto.KeyWord) || m.ProMaterCode.Contains(billBomsetgrpQueryDto.KeyWord) || m.ShortCode.ToString().Contains(billBomsetgrpQueryDto.KeyWord));
  92. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.Code), m => m.Code.Contains(billBomsetgrpQueryDto.Code));
  93. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.Name), m => m.Name.Contains(billBomsetgrpQueryDto.Name));
  94. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.ShortCode), m => m.ShortCode.ToString().Contains(billBomsetgrpQueryDto.ShortCode));
  95. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.ProMaterCode), m => m.ProMaterCode.Contains(billBomsetgrpQueryDto.ProMaterCode));
  96. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.BomName), m => m.BomName.Contains(billBomsetgrpQueryDto.BomName));
  97. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.ProCode), m => m.ProCode.Contains(billBomsetgrpQueryDto.ProCode));
  98. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.StampType), m => m.StampType.Equals(billBomsetgrpQueryDto.StampType));
  99. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.IsStop), m => m.IsStop.Equals(billBomsetgrpQueryDto.IsStop));
  100. predicate = predicate.AndIF(!string.IsNullOrEmpty(billBomsetgrpQueryDto?.IsDelete), m => m.IsDelete.Equals(billBomsetgrpQueryDto.IsDelete));
  101. var sugarQueryable = _billBomsetgrpRepository.Queryable().Where(predicate.ToExpression());
  102. return sugarQueryable;
  103. }
  104. public IList<BillBomsetgrpDto> GetList(BillBomsetgrpQueryDto billBomsetgrpQueryDto)
  105. {
  106. var list = GetQueryable(billBomsetgrpQueryDto).ToList().Adapt<List<BillBomsetgrpDto>>();
  107. return list;
  108. }
  109. public IList<BillBomsetinfoDto> GetBillBomsetinfoItem(long bomSetHdrId)
  110. {
  111. var predicate = Expressionable.Create<BillBomsetinfo>();
  112. predicate = predicate.AndIF(bomSetHdrId > 0, m => m.BomSetHdrId.Equals(bomSetHdrId));
  113. var list = _billBomsetinfoRepository.Queryable().Where(predicate.ToExpression()).ToList().OrderBy(o => Convert.ToInt32(o.XYNo)).Adapt<List<BillBomsetinfoDto>>();
  114. return list;
  115. }
  116. public BillBomsetDto GetBillBomsetinfo(long BomsetId)
  117. {
  118. var bomset = _billBomsetgrpRepository.Queryable().Single(o => o.Id == BomsetId);
  119. if (bomset == null)
  120. {
  121. throw BZSysExCore.ThrowFailException("不存在的跺型!");
  122. }
  123. var bomsetinfos = _billBomsetinfoRepository.Queryable().Where(o => o.BomSetHdrId == bomset.Id && o.IsEmpty == 0).ToList();
  124. var dto = bomset.Adapt<BillBomsetDto>();
  125. dto.Bomsetinfos = bomsetinfos.Adapt<List<BillBomsetinfoDto>>();
  126. return dto;
  127. }
  128. public void Save09(LoginUserInfo loginUser, string keyValue, BillBomsetgrp billBomsetgrpEntity, IList<BillBomsetinfo> billBomsetinfos)
  129. {
  130. if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.Code))
  131. {
  132. throw BZSysExCore.ThrowFailException("跺型编码不能为空!");
  133. }
  134. if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.ProCode))
  135. {
  136. throw BZSysExCore.ThrowFailException("投料信息不能为空!");
  137. }
  138. if (billBomsetinfos == null || !billBomsetinfos.Any())
  139. {
  140. throw BZSysExCore.ThrowFailException("请选择跺型工字轮编号!");
  141. }
  142. if (billBomsetgrpEntity.ShortCode <= 0)
  143. {
  144. throw BZSysExCore.ThrowFailException("跺型短编码不正确!");
  145. }
  146. var infoMatCodes = billBomsetinfos.Select(o => o.MatCode).Distinct().ToList();
  147. // var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).ToDictionary(x => x.Key, x => x.Count());
  148. //var hasDuplicates = infoMatCodes.GroupBy(x => x).Any(g => g.Count() > 1);
  149. var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).Select(x => x.Key).ToList();
  150. if (duplicates != null && duplicates.Any())
  151. {
  152. throw BZSysExCore.ThrowFailException(string.Join(",", duplicates) + ",湿拉物料有重复!");
  153. }
  154. // todo 判断数量
  155. // 主数据的数量和子的数量要一致。
  156. //var groups = billBomsetinfos.GroupBy(o => o.MatCode).Select(g => (new { key = g, max = g.Max(n => n.QtyMaxCount), count = g.Count() }));
  157. //if (billBomsetinfos.Count() != billBomsetgrpEntity.HWCountQty)
  158. //{
  159. // throw BZSysExCore.ThrowFailException("填写的跺型数量不一致!");
  160. //}
  161. //if (groups.Any(o => o.max != o.count))
  162. //{
  163. // throw BZSysExCore.ThrowFailException("子跺型选择的工字轮和填写的数量不一致!");
  164. //}
  165. var first = billBomsetinfos.FirstOrDefault();
  166. for (int i = 1; i <= billBomsetgrpEntity.TotalQty; i++)
  167. {
  168. var item = billBomsetinfos.FirstOrDefault(o => o.XYNo == i.ToString());
  169. if (item == null)
  170. {
  171. billBomsetinfos.Add(new BillBomsetinfo
  172. {
  173. BomSetHdrId = first.BomSetHdrId,
  174. CategoryId = 99,
  175. XYNo = i.ToString(),
  176. IsEmpty = 1,
  177. IsStop = 1,
  178. });
  179. }
  180. }
  181. billBomsetinfos.ToList().ForEach(o =>
  182. {
  183. o.Row = Convert.ToInt32(Math.Ceiling(Convert.ToInt32(o.XYNo) / 5.0d));
  184. });
  185. 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);
  186. foreach (var row in mixRows)
  187. {
  188. billBomsetinfos.Where(o => o.Row == row.Key).ToList().ForEach(o =>
  189. {
  190. if (row.Key.HasValue)
  191. {
  192. o.IsMixRow = 1;
  193. o.MixRowCode = row.Key.Value.ToString();
  194. }
  195. });
  196. }
  197. var procodelists = _billBominfoRepository.Queryable().Where(o => billBomsetgrpEntity.ProCode == o.ProCode && o.IsDelete == 0 && o.IsStop == 0).ToList();
  198. if (!procodelists.Any())
  199. {
  200. throw BZSysExCore.ThrowFailException("投料信息不存在!");
  201. }
  202. //if (!_matinforepository.Queryable().Any(o => o.Code == billBomsetgrpEntity.ProMaterCode))
  203. //{
  204. // throw BZSysExCore.ThrowFailException("产出物料编码不存在!");
  205. //}
  206. var baseMatinfos = _billBominfoRepository.Queryable().Where(o => o.IsDelete == 0 && o.IsStop == 0 && infoMatCodes.Contains(o.MatCode)).ToList();
  207. //foreach (var item in infoMatCodes)
  208. //{
  209. // if (baseMatinfos.Where(o => o.MatCode == item).Select(o => o.ProCode).Distinct().Count() > 1)
  210. // {
  211. // throw BZSysExCore.ThrowFailException("湿拉物料存在于多个投料大类中!");
  212. // }
  213. //}
  214. //var bomProMatCodelists = _billBominfoRepository.Queryable().Where(o => o.ProCode == billBomsetgrpEntity.BomCode).Select(o => o.ProMatCode).Distinct().ToList();
  215. //billBomsetgrpEntity.ProMaterCode = string.Join(',', bomProMatCodelists);
  216. if (keyValue.IsEmpty() || keyValue == "undefined")
  217. {
  218. var mat = _billBomsetgrpRepository.GetSingle(p => p.Code == billBomsetgrpEntity.Code);
  219. if (mat != null)
  220. {
  221. throw BZSysExCore.ThrowFailException("跺型编码已存在!");
  222. }
  223. //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  224. //{
  225. // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!");
  226. //}
  227. //if (_billBomsetgrpRepository.Queryable().Any(o => o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode))
  228. //{
  229. // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!");
  230. //}
  231. //billBomsetgrpEntity.ShortCode = GetShoreCodes();
  232. //if (billBomsetgrpEntity.ShortCode <= 0)
  233. //{
  234. // throw BZSysExCore.ThrowFailException("跺型短编码不正确!");
  235. //}
  236. //if ( billBomsetgrpEntity.ShortCode > 255)
  237. //{
  238. // throw BZSysExCore.ThrowFailException("跺型已满,请删除不用的跺型!");
  239. //}
  240. //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  241. //{
  242. // //billBomsetgrpEntity.ShortCode = GetShoreCodes();
  243. // //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  244. // //{
  245. // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!");
  246. // //}
  247. //}
  248. //var list = _billBomsetinfoRepository.Queryable().Where(o => infoMatCodes.Contains(o.MatCode)).ToList();
  249. //if (list.Any())
  250. //{
  251. // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).ToArray()) + ",湿拉物料已经存在其他跺型中!");
  252. //}
  253. _billBomsetgrpRepository.UseTranAction(() =>
  254. {
  255. var id = IdFactory.NewId();
  256. billBomsetgrpEntity.Id = id;
  257. billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.IsEmpty == 0);
  258. billBomsetgrpEntity.AddWho = loginUser.UserNo;
  259. billBomsetgrpEntity.EditWho = loginUser.UserNo;
  260. billBomsetgrpEntity.AddTime = DateTime.Now;
  261. billBomsetgrpEntity.EditTime = DateTime.Now;
  262. billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray());
  263. billBomsetgrpEntity.IsStop = 1;
  264. _billBomsetgrpRepository.Insert(billBomsetgrpEntity);
  265. billBomsetinfos.ToList().ForEach(o =>
  266. {
  267. o.Id = IdFactory.NewId();
  268. var xyno = Convert.ToInt32(o.XYNo);
  269. o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d));
  270. o.BomSetHdrId = id;
  271. if (o.IsEmpty == 0 || o.CategoryId < 99)
  272. {
  273. o.SpoolType = SpoolType.WS09.ToString();
  274. var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode);
  275. if (matinfo != null)
  276. {
  277. o.MatId = matinfo.MatId;
  278. }
  279. }
  280. o.AddTime = DateTime.Now;
  281. o.EditTime = DateTime.Now;
  282. o.AddWho = loginUser.UserNo;
  283. o.EditWho = loginUser.UserNo;
  284. o.IsStop = 1;
  285. });
  286. _billBomsetinfoRepository.Insert(billBomsetinfos.ToList());
  287. });
  288. }
  289. else
  290. {
  291. var bomsetgrpId = Convert.ToInt64(keyValue);
  292. var entitys = _billBomsetgrpRepository.Queryable().Where(x => (x.Id == bomsetgrpId || x.Code == billBomsetgrpEntity.Code)).ToList();
  293. if (!entitys.Any())
  294. {
  295. throw BZSysExCore.ThrowFailException("跺型不存在!");
  296. }
  297. if (entitys.Any(o => o.Id != bomsetgrpId))
  298. {
  299. throw BZSysExCore.ThrowFailException("跺型编码已存在!");
  300. }
  301. //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.ShortCode == billBomsetgrpEntity.ShortCode))
  302. //{
  303. // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!");
  304. //}
  305. //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode))
  306. //{
  307. // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!");
  308. //}
  309. // var list = _billBomsetinfoRepository.Context.Queryable<BillBomsetinfo, BillBomsetgrp>((billBomsetinfo, billBomsetgrp) => new object[] {
  310. //JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0
  311. //&& billBomsetgrp.IsStop == 0 && billBomsetinfo.BomSetHdrId != bomsetgrpId && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList();
  312. // if (list.Any())
  313. // {
  314. // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).ToArray()) + ",湿拉物料已经存在其他跺型中!");
  315. // }
  316. _billBomsetgrpRepository.UseTranAction(() =>
  317. {
  318. billBomsetgrpEntity.Id = bomsetgrpId;
  319. //if (_billBomsetgrpRepository.Queryable().Any(x => x.Id != bomsetgrpId && (x.Code == billBomsetgrpEntity.Code || x.Name == billBomsetgrpEntity.Name) && x.IsStop == 0))
  320. //{
  321. // throw BZSysExCore.ThrowFailException("跺型已存在!");
  322. //}
  323. //billBomsetgrpEntity.Id = bomsetgrpId;
  324. billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.IsEmpty == 0);
  325. billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray());
  326. billBomsetgrpEntity.EditWho = loginUser.UserNo;
  327. billBomsetgrpEntity.EditTime = DateTime.Now;
  328. billBomsetgrpEntity.IsStop = 1;
  329. _billBomsetgrpRepository.UpdateEntity(billBomsetgrpEntity);
  330. _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand();
  331. billBomsetinfos.ToList().ForEach(o =>
  332. {
  333. o.Id = IdFactory.NewId();
  334. var xyno = Convert.ToInt32(o.XYNo);
  335. o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d));
  336. o.BomSetHdrId = billBomsetgrpEntity.Id;
  337. if (o.IsEmpty == 0 || o.CategoryId < 99)
  338. {
  339. o.SpoolType = SpoolType.WS09.ToString();
  340. var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode);
  341. if (matinfo != null)
  342. {
  343. o.MatId = matinfo.MatId;
  344. }
  345. }
  346. o.AddTime = DateTime.Now;
  347. o.EditTime = DateTime.Now;
  348. o.AddWho = loginUser.UserNo;
  349. o.EditWho = loginUser.UserNo;
  350. o.IsStop = 1;
  351. });
  352. _billBomsetinfoRepository.Insert(billBomsetinfos.ToList());
  353. });
  354. }
  355. }
  356. /// <summary>
  357. /// 保存排库信息
  358. /// </summary>
  359. /// <param name="loginUser"></param>
  360. /// <param name="keyValue"></param>
  361. /// <param name="billPaiKuEntity"></param>
  362. public void SavePK(LoginUserInfo loginUser, string keyValue, BillPaiKu billPaiKuEntity)
  363. {
  364. try
  365. {
  366. if (billPaiKuEntity.Percent != Math.Truncate(billPaiKuEntity.Percent) || billPaiKuEntity.Percent > 100)
  367. {
  368. throw BZSysExCore.ThrowFailException("占比只能传入整数,且必须小于100!");
  369. }
  370. if (billPaiKuEntity.BomsetgrpId <= 0)
  371. {
  372. throw BZSysExCore.ThrowFailException("垛型编码有误!");
  373. }
  374. if (billPaiKuEntity.WarehousId <= 0)
  375. {
  376. throw BZSysExCore.ThrowFailException("仓库id有误!");
  377. }
  378. var bomsetgrp = _billBomsetgrpRepository.AsQueryable().With(SqlWith.NoLock).First(x => x.Id == billPaiKuEntity.BomsetgrpId);
  379. if (bomsetgrp == null)
  380. {
  381. throw BZSysExCore.ThrowFailException($"未找到对应垛型{billPaiKuEntity.BomsetgrpId}!");
  382. }
  383. var warehouse = _billBaseWarehouse.AsQueryable().With(SqlWith.NoLock).First(x => x.Id == billPaiKuEntity.WarehousId);
  384. if (warehouse == null)
  385. {
  386. throw BZSysExCore.ThrowFailException($"未找到对应仓库{billPaiKuEntity.WarehousId}!");
  387. }
  388. if (keyValue == "undefined") keyValue = "0";
  389. var paiKuId = Convert.ToInt64(keyValue);
  390. var paiKu = _billPaiKurepository.GetFirst(x => x.Id == paiKuId);
  391. if (paiKu != null) //更新
  392. {
  393. //判断此垛型排库占比总数是否超过100
  394. var percent = _billPaiKurepository.GetList(x => x.BomsetgrpId == bomsetgrp.Id && x.Id != paiKu.Id).Sum(x => x.Percent);
  395. if ((percent + billPaiKuEntity.Percent) > 100)
  396. {
  397. throw BZSysExCore.ThrowFailException($"{bomsetgrp.Name}超过最大占比,除当前编辑信息外总比重为{percent},请重新调整!");
  398. }
  399. paiKu.BomsetgrpId = bomsetgrp.Id;
  400. paiKu.BomsetgrpName = bomsetgrp.Name;
  401. paiKu.WarehousId = warehouse.Id;
  402. paiKu.WarehouseCode = warehouse.Code;
  403. paiKu.Percent = billPaiKuEntity.Percent;
  404. _billPaiKurepository.Update(paiKu);
  405. }
  406. else //新增
  407. {
  408. //判断此垛型排库占比总数是否超过100
  409. var percent = _billPaiKurepository.GetList(x => x.BomsetgrpId == bomsetgrp.Id).Sum(x => x.Percent);
  410. if ((percent + billPaiKuEntity.Percent) > 100)
  411. {
  412. throw BZSysExCore.ThrowFailException($"{bomsetgrp.Name}超过最大占比,请重新调整!");
  413. }
  414. BillPaiKu item = new BillPaiKu()
  415. {
  416. BomsetgrpId = bomsetgrp.Id,
  417. BomsetgrpName = bomsetgrp.Name,
  418. WarehousId = warehouse.Id,
  419. WarehouseCode = warehouse.Code,
  420. Percent = billPaiKuEntity.Percent
  421. };
  422. _billPaiKurepository.Insert(item);
  423. }
  424. }
  425. catch (Exception ex)
  426. {
  427. throw BZSysExCore.ThrowFailException(ex.Message);
  428. }
  429. }
  430. public void Save(LoginUserInfo loginUser, string keyValue, BillBomsetgrp billBomsetgrpEntity, IList<BillBomsetinfo> billBomsetinfos)
  431. {
  432. if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.Code))
  433. {
  434. throw BZSysExCore.ThrowFailException("跺型编码不能为空!");
  435. }
  436. if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.ProCode))
  437. {
  438. throw BZSysExCore.ThrowFailException("投料信息不能为空!");
  439. }
  440. if (billBomsetinfos == null || !billBomsetinfos.Any())
  441. {
  442. throw BZSysExCore.ThrowFailException("请选择跺型工字轮编号!");
  443. }
  444. if (billBomsetgrpEntity.ShortCode <= 0)
  445. {
  446. throw BZSysExCore.ThrowFailException("跺型短编码不正确!");
  447. }
  448. var infoMatCodes = billBomsetinfos.Select(o => o.MatCode).Distinct().ToList();
  449. // var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).ToDictionary(x => x.Key, x => x.Count());
  450. //var hasDuplicates = infoMatCodes.GroupBy(x => x).Any(g => g.Count() > 1);
  451. var duplicates = infoMatCodes.GroupBy(x => x).Where(g => g.Count() > 1).Select(x => x.Key).ToList();
  452. if (duplicates != null && duplicates.Any())
  453. {
  454. throw BZSysExCore.ThrowFailException(string.Join(",", duplicates) + ",湿拉物料有重复!");
  455. }
  456. var procodelists = _billBominfoRepository.Queryable().Where(o => o.IsDelete == 0 && o.IsStop == 0 && billBomsetgrpEntity.ProCode == o.ProCode).ToList();
  457. if (!procodelists.Any())
  458. {
  459. throw BZSysExCore.ThrowFailException("投料信息不存在!");
  460. }
  461. //在Bill_BomInfo 中MatCode不存在2个ProCode中
  462. var baseMatinfos = _billBominfoRepository.Queryable().Where(o => o.IsDelete == 0 && o.IsStop == 0 && infoMatCodes.Contains(o.MatCode)).ToList();
  463. //foreach (var item in infoMatCodes)
  464. //{
  465. // if (baseMatinfos.Where(o => o.MatCode == item).Select(o => o.ProCode).Distinct().Count() > 1)
  466. // {
  467. // throw BZSysExCore.ThrowFailException("湿拉物料存在于多个投料大类中!");
  468. // }
  469. //}
  470. //if (!_matinforepository.Queryable().Any(o => o.Code == billBomsetgrpEntity.ProMaterCode))
  471. //{
  472. // throw BZSysExCore.ThrowFailException("产出物料编码不存在!");
  473. //}
  474. //billBomsetgrpEntity.BomCode = baseMatinfos.First().ProCode;
  475. //var bomProMatCodelists = _billBominfoRepository.Queryable().Where(o => o.ProCode == billBomsetgrpEntity.BomCode).Select(o => o.ProMatCode).Distinct().ToList();
  476. //billBomsetgrpEntity.ProMaterCode = string.Join(',', bomProMatCodelists);
  477. var first = billBomsetinfos.FirstOrDefault();
  478. for (int i = 1; i <= billBomsetgrpEntity.TotalQty; i++)
  479. {
  480. var item = billBomsetinfos.FirstOrDefault(o => o.XYNo == i.ToString());
  481. if (item == null)
  482. {
  483. billBomsetinfos.Add(new BillBomsetinfo
  484. {
  485. BomSetHdrId = first.BomSetHdrId,
  486. CategoryId = 99,
  487. XYNo = i.ToString(),
  488. IsEmpty = 1,
  489. IsStop = 1,
  490. });
  491. }
  492. }
  493. if (keyValue.IsEmpty() || keyValue == "undefined")
  494. {
  495. var mat = _billBomsetgrpRepository.GetSingle(p => p.Code == billBomsetgrpEntity.Code);
  496. if (mat != null)
  497. {
  498. throw BZSysExCore.ThrowFailException("跺型编码已存在!");
  499. }
  500. //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  501. //{
  502. // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!");
  503. //}
  504. //if (_billBomsetgrpRepository.Queryable().Any(o => o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode))
  505. //{
  506. // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!");
  507. //}
  508. //billBomsetgrpEntity.ShortCode = GetShoreCodes();
  509. //if (billBomsetgrpEntity.ShortCode > 255)
  510. //{
  511. // throw BZSysExCore.ThrowFailException("跺型已满,请删除不用的跺型!");
  512. //}
  513. //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  514. //{
  515. // billBomsetgrpEntity.ShortCode = GetShoreCodes();
  516. // if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  517. // {
  518. // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新提交跺型!");
  519. // }
  520. //}
  521. //var list = _billBomsetinfoRepository.Context.Queryable<BillBomsetinfo, BillBomsetgrp>((billBomsetinfo, billBomsetgrp) => new object[] {
  522. //JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0 && billBomsetgrp.IsStop == 0 && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList();
  523. //if (list.Any())
  524. //{
  525. // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).Distinct().ToArray()) + ",湿拉物料已经存在其他跺型中!");
  526. //}
  527. _billBomsetgrpRepository.UseTranAction(() =>
  528. {
  529. var id = IdFactory.NewId();
  530. billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99);
  531. billBomsetgrpEntity.Id = id;
  532. billBomsetgrpEntity.AddWho = loginUser.UserNo;
  533. billBomsetgrpEntity.EditWho = loginUser.UserNo;
  534. billBomsetgrpEntity.AddTime = DateTime.Now;
  535. billBomsetgrpEntity.EditTime = DateTime.Now;
  536. billBomsetgrpEntity.IsStop = 1;
  537. billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray());
  538. _billBomsetgrpRepository.Insert(billBomsetgrpEntity);
  539. //_billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand();
  540. billBomsetinfos.ToList().ForEach(o =>
  541. {
  542. o.Id = IdFactory.NewId();
  543. var xyno = Convert.ToInt32(o.XYNo);
  544. o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d));
  545. o.BomSetHdrId = id;
  546. if (o.CategoryId < 99)
  547. {
  548. var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode);
  549. if (matinfo != null)
  550. {
  551. o.MatId = matinfo.MatId;
  552. }
  553. }
  554. o.AddTime = DateTime.Now;
  555. o.EditTime = DateTime.Now;
  556. o.AddWho = loginUser.UserNo;
  557. o.EditWho = loginUser.UserNo;
  558. //o.Row = 0;
  559. o.IsStop = 1;
  560. });
  561. _billBomsetinfoRepository.Insert(billBomsetinfos.ToList());
  562. });
  563. }
  564. else
  565. {
  566. var bomsetgrpId = Convert.ToInt64(keyValue);
  567. var entitys = _billBomsetgrpRepository.Queryable().Where(x => x.Id == bomsetgrpId || x.Code == billBomsetgrpEntity.Code).ToList();
  568. if (!entitys.Any())
  569. {
  570. throw BZSysExCore.ThrowFailException("跺型不存在!");
  571. }
  572. if (entitys.Any(o => o.Id != bomsetgrpId))
  573. {
  574. throw BZSysExCore.ThrowFailException("跺型编码已存在!");
  575. }
  576. //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.ShortCode == billBomsetgrpEntity.ShortCode))
  577. //{
  578. // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新修改短编号!");
  579. //}
  580. //if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != bomsetgrpId && o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == billBomsetgrpEntity.BomCode))
  581. //{
  582. // throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!");
  583. //}
  584. //var list = _billBomsetinfoRepository.Context.Queryable<BillBomsetinfo, BillBomsetgrp>((billBomsetinfo, billBomsetgrp) => new object[] {
  585. //JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0
  586. //&& billBomsetgrp.IsStop == 0 && billBomsetinfo.BomSetHdrId != bomsetgrpId && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList();
  587. //if (list.Any())
  588. //{
  589. // throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).Distinct().ToArray()) + ",湿拉物料已经存在其他跺型中!");
  590. //}
  591. _billBomsetgrpRepository.UseTranAction(() =>
  592. {
  593. billBomsetgrpEntity.Id = bomsetgrpId;
  594. //billBomsetgrpEntity.ShortCode = entitys.First().ShortCode;
  595. billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99);
  596. billBomsetgrpEntity.EditWho = loginUser.UserNo;
  597. billBomsetgrpEntity.EditTime = DateTime.Now;
  598. billBomsetgrpEntity.IsStop = 1;
  599. billBomsetgrpEntity.BomCode = String.Join("|", billBomsetinfos.Where(o => !string.IsNullOrEmpty(o.MatCode)).Select(o => o.MatCode).Distinct().ToArray());
  600. _billBomsetgrpRepository.UpdateEntity(billBomsetgrpEntity);
  601. _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand();
  602. billBomsetinfos.ToList().ForEach(o =>
  603. {
  604. o.Id = IdFactory.NewId();
  605. var xyno = Convert.ToInt32(o.XYNo);
  606. o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d));
  607. o.BomSetHdrId = billBomsetgrpEntity.Id;
  608. if (o.CategoryId < 99)
  609. {
  610. var matinfo = baseMatinfos.FirstOrDefault(c => c.MatCode == o.MatCode);
  611. if (matinfo != null)
  612. {
  613. o.MatId = matinfo.MatId;
  614. }
  615. }
  616. o.AddTime = DateTime.Now;
  617. o.EditTime = DateTime.Now;
  618. o.AddWho = loginUser.UserNo;
  619. o.EditWho = loginUser.UserNo;
  620. //o.Row = 0;
  621. o.IsStop = 1;
  622. });
  623. _billBomsetinfoRepository.Insert(billBomsetinfos.ToList());
  624. });
  625. }
  626. }
  627. public void SaveException(LoginUserInfo loginUser, string keyValue, BillBomsetgrp billBomsetgrpEntity, IList<BillBomsetinfo> billBomsetinfos)
  628. {
  629. if (string.IsNullOrWhiteSpace(billBomsetgrpEntity.Code))
  630. {
  631. throw BZSysExCore.ThrowFailException("跺型编码不能为空!");
  632. }
  633. if (!billBomsetinfos.Any())
  634. {
  635. throw BZSysExCore.ThrowFailException("请选择工字轮!");
  636. }
  637. if (billBomsetinfos == null || !billBomsetinfos.Any())
  638. {
  639. throw BZSysExCore.ThrowFailException("请选择跺型工字轮编号!");
  640. }
  641. var first = billBomsetinfos.FirstOrDefault();
  642. for (int i = 1; i <= billBomsetgrpEntity.TotalQty; i++)
  643. {
  644. var item = billBomsetinfos.FirstOrDefault(o => o.XYNo == i.ToString());
  645. if (item == null)
  646. {
  647. billBomsetinfos.Add(new BillBomsetinfo
  648. {
  649. BomSetHdrId = first.BomSetHdrId,
  650. CategoryId = 99,
  651. XYNo = i.ToString(),
  652. IsEmpty = 1,
  653. IsStop = 1,
  654. });
  655. }
  656. }
  657. if (keyValue.IsEmpty() || keyValue == "undefined")
  658. {
  659. var mat = _billBomsetgrpRepository.GetSingle(p => p.Code == billBomsetgrpEntity.Code);
  660. if (mat != null)
  661. {
  662. throw BZSysExCore.ThrowFailException("跺型编码已存在!");
  663. }
  664. //billBomsetgrpEntity.ShortCode = GetShoreCodes();
  665. //if (billBomsetgrpEntity.ShortCode > 255)
  666. //{
  667. // throw BZSysExCore.ThrowFailException("跺型已满,请删除不用的跺型!");
  668. //}
  669. //if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  670. //{
  671. // billBomsetgrpEntity.ShortCode = GetShoreCodes();
  672. // if (_billBomsetgrpRepository.Queryable().Any(o => o.ShortCode == billBomsetgrpEntity.ShortCode))
  673. // {
  674. // throw BZSysExCore.ThrowFailException("跺型内部编码已存在,请重新提交跺型!");
  675. // }
  676. //}
  677. _billBomsetgrpRepository.UseTranAction(() =>
  678. {
  679. var id = IdFactory.NewId();
  680. billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99);
  681. billBomsetgrpEntity.Id = id;
  682. billBomsetgrpEntity.AddWho = loginUser.UserNo;
  683. billBomsetgrpEntity.EditWho = loginUser.UserNo;
  684. billBomsetgrpEntity.AddTime = DateTime.Now;
  685. billBomsetgrpEntity.EditTime = DateTime.Now;
  686. billBomsetgrpEntity.IsStop = 1;
  687. _billBomsetgrpRepository.Insert(billBomsetgrpEntity);
  688. //_billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand();
  689. billBomsetinfos.ToList().ForEach(o =>
  690. {
  691. o.Id = IdFactory.NewId();
  692. var xyno = Convert.ToInt32(o.XYNo);
  693. //if (billBomsetgrpEntity.StampChildType == 4)
  694. //{
  695. o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d));
  696. //}
  697. o.BomSetHdrId = id;
  698. o.AddTime = DateTime.Now;
  699. o.EditTime = DateTime.Now;
  700. o.AddWho = loginUser.UserNo;
  701. o.EditWho = loginUser.UserNo;
  702. o.IsStop = 1;
  703. });
  704. _billBomsetinfoRepository.Insert(billBomsetinfos.ToList());
  705. });
  706. }
  707. else
  708. {
  709. var bomsetgrpId = Convert.ToInt64(keyValue);
  710. var entitys = _billBomsetgrpRepository.Queryable().Where(x => x.Id == bomsetgrpId || x.Code == billBomsetgrpEntity.Code).ToList();
  711. if (!entitys.Any())
  712. {
  713. throw BZSysExCore.ThrowFailException("跺型编码不存在!");
  714. }
  715. if (entitys.Any(o => o.Id != bomsetgrpId))
  716. {
  717. throw BZSysExCore.ThrowFailException("跺型编码已存在!");
  718. }
  719. _billBomsetgrpRepository.UseTranAction(() =>
  720. {
  721. billBomsetgrpEntity.Id = bomsetgrpId;
  722. billBomsetgrpEntity.ShortCode = entitys.First().ShortCode;
  723. billBomsetgrpEntity.HWCountQty = billBomsetinfos.Count(o => o.CategoryId < 99);
  724. billBomsetgrpEntity.EditWho = loginUser.UserNo;
  725. billBomsetgrpEntity.EditTime = DateTime.Now;
  726. billBomsetgrpEntity.IsStop = 1;
  727. _billBomsetgrpRepository.UpdateEntity(billBomsetgrpEntity);
  728. _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == billBomsetgrpEntity.Id).ExecuteCommand();
  729. billBomsetinfos.ToList().ForEach(o =>
  730. {
  731. o.Id = IdFactory.NewId();
  732. var xyno = Convert.ToInt32(o.XYNo);
  733. //if (billBomsetgrpEntity.StampChildType == 4)
  734. //{
  735. o.Row = Convert.ToInt32(Math.Ceiling(xyno / 5.0d));
  736. //}
  737. o.BomSetHdrId = billBomsetgrpEntity.Id;
  738. o.AddTime = DateTime.Now;
  739. o.EditTime = DateTime.Now;
  740. o.AddWho = loginUser.UserNo;
  741. o.EditWho = loginUser.UserNo;
  742. o.IsStop = 1;
  743. });
  744. _billBomsetinfoRepository.Insert(billBomsetinfos.ToList());
  745. });
  746. }
  747. }
  748. public int GetShoreCodes()
  749. {
  750. var lists = _billBomsetgrpRepository.Queryable().OrderBy(o => o.ShortCode).Select(o => o.ShortCode).ToArray();
  751. if (!lists.Any())
  752. {
  753. //没有数据的时候返回1.
  754. return 1;
  755. }
  756. else
  757. {
  758. var numberArrs = lists.Adapt<List<int>>();
  759. var numbers = FindDisappearedNumbers(numberArrs);
  760. if (numbers.Any())
  761. {
  762. //找到缺失的数字
  763. return numbers[0];
  764. }
  765. else
  766. {
  767. //没有缺失的数字,则返回最大值加1.
  768. var maxShortCode = _billBomsetgrpRepository.Queryable().Select(o => SqlFunc.AggregateMax(o.ShortCode)).Single();
  769. return ++maxShortCode;
  770. }
  771. }
  772. }
  773. public IList<int> FindDisappearedNumbers(List<int> nums)
  774. {
  775. var res = new List<int>();
  776. if (nums[nums.Count - 1] != nums.Count)
  777. {
  778. var len = nums[nums.Count - 1] - nums.Count;
  779. for (int i = 0; i < len; i++)
  780. {
  781. nums.Insert(i, 1);
  782. }
  783. }
  784. var numsarr = nums.ToArray();
  785. for (int i = 0; i < numsarr.Length; i++)
  786. {
  787. //记录索引,位置为i处的值的绝对值减1
  788. //(因为数值范围从1..n所以要减1)作为临时索引位
  789. int index = Math.Abs(numsarr[i]) - 1;
  790. if (index < 0)
  791. {
  792. index = 0;
  793. }
  794. //如果此处的值大于0,那么将其变为负数,
  795. //没有变为负数的值即为不存在于数组中的数字,
  796. //直接变为负数不至于使其值丢失,只需要取绝对值即可恢复原值
  797. if (index < numsarr.Length && numsarr[index] > 0)
  798. {
  799. numsarr[index] = -numsarr[index];
  800. }
  801. }
  802. //所有值大于0的即为“迷失的数字”
  803. for (int i = 0; i < numsarr.Length; i++)
  804. {
  805. if (numsarr[i] > 0)
  806. {
  807. res.Add(i + 1);
  808. }
  809. }
  810. return res;
  811. }
  812. // Function to return the missing element
  813. public static int findMissing(int[] arr, int n)
  814. {
  815. int l = 0, h = n - 1;
  816. int mid;
  817. while (h > l)
  818. {
  819. mid = l + (h - l) / 2;
  820. // Check if middle element is consistent
  821. if (arr[mid] - mid == arr[0])
  822. {
  823. // No inconsistency till middle elements
  824. // When missing element is just after
  825. // the middle element
  826. if (arr[mid + 1] - arr[mid] > 1)
  827. return arr[mid] + 1;
  828. else
  829. {
  830. // Move right
  831. l = mid + 1;
  832. }
  833. }
  834. else
  835. {
  836. // Inconsistency found
  837. // When missing element is just before
  838. // the middle element
  839. if (arr[mid] - arr[mid - 1] > 1)
  840. return arr[mid] - 1;
  841. else
  842. {
  843. // Move left
  844. h = mid - 1;
  845. }
  846. }
  847. }
  848. // No missing element found
  849. return -1;
  850. }
  851. public void DeletePK(long id)
  852. {
  853. try
  854. {
  855. _billPaiKurepository.Deleteable().Where(o => o.Id == id).ExecuteCommand();
  856. }
  857. catch (Exception ex)
  858. {
  859. throw ex;
  860. }
  861. }
  862. public void Delete(long id)
  863. {
  864. var entity = _billBomsetgrpRepository.GetSingle(o => o.Id == id);
  865. if (entity == null)
  866. {
  867. throw BZSysExCore.ThrowFailException("跺型不存在!");
  868. }
  869. _billBomsetgrpRepository.Deleteable().Where(it => it.Id == entity.Id).ExecuteCommand();
  870. _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == entity.Id).ExecuteCommand();
  871. }
  872. public void Deletes(string[] ids, string userId)
  873. {
  874. if (ids == null || ids.Length == 0)
  875. {
  876. throw BZSysExCore.ThrowFailException("跺型Id不能为空!");
  877. }
  878. var list = _billBomsetgrpRepository.Queryable().Where(o => ids.Contains(o.Id.ToString())).ToList();
  879. if (!list.Any())
  880. {
  881. throw new ArgumentException("没有选择跺型数据!");
  882. }
  883. _billBomsetgrpRepository.UseTranAction(() =>
  884. {
  885. foreach (var item in list)
  886. {
  887. _billBomsetgrpRepository.Deleteable().Where(it => it.Id == item.Id).ExecuteCommand();
  888. _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == item.Id).ExecuteCommand();
  889. }
  890. });
  891. //list.ForEach(o =>
  892. //{
  893. // o.IsDelete = 1;
  894. // o.EditWho = userId;
  895. // o.EditTime = DateTime.Now;
  896. //});
  897. //var result = _billBomsetgrpRepository.UpdateRange(list.ToArray());
  898. }
  899. public void DeleteByCode(string code)
  900. {
  901. var entity = _billBomsetgrpRepository.GetSingle(o => o.Code == code);
  902. if (entity == null)
  903. {
  904. throw BZSysExCore.ThrowFailException("跺型不存在!");
  905. }
  906. _billBomsetgrpRepository.Deleteable().Where(it => it.Id == entity.Id).ExecuteCommand();
  907. _billBomsetinfoRepository.Deleteable().Where(it => it.BomSetHdrId == entity.Id).ExecuteCommand();
  908. }
  909. /// <summary>
  910. ///
  911. /// </summary>
  912. /// <param name="Ids">跺型编号</param>
  913. /// <param name="locationStop">是否</param>
  914. /// <param name="userId">操作人</param>
  915. /// <returns></returns>
  916. /// <exception cref="ArgumentException"></exception>
  917. public void ChangeEnableds(List<string> Ids, LocationStop locationStop, string userId)
  918. {
  919. if (Ids == null || !Ids.Any())
  920. {
  921. throw new ArgumentException("没有选择跺型数据!");
  922. }
  923. var billBomsetgrplist = _billBomsetgrpRepository.Queryable().Where(o => Ids.Contains(o.Id.ToString())).ToList();
  924. if (!billBomsetgrplist.Any())
  925. {
  926. throw new ArgumentException("没有选择跺型数据!");
  927. }
  928. var now = DateTime.Now;
  929. billBomsetgrplist.ForEach(o =>
  930. {
  931. o.IsStop = (int)locationStop;
  932. o.EditWho = userId;
  933. o.EditTime = now;
  934. });
  935. var infos = _billBomsetinfoRepository.Queryable().Where(it => Ids.Contains(it.BomSetHdrId.ToString())).ToList();
  936. if (!infos.Any())
  937. {
  938. throw new ArgumentException("没有选择跺型数据!");
  939. }
  940. //启用
  941. if (locationStop == LocationStop.LocationInvoke)
  942. {
  943. var infoMatCodes = infos.Select(o => o.MatCode).Distinct().ToList();
  944. foreach (var item in billBomsetgrplist)
  945. {
  946. if (_billBomsetgrpRepository.Queryable().Any(o => o.Id != item.Id && o.IsDelete == 0 && o.IsStop == 0 && o.BomCode == item.BomCode))
  947. {
  948. throw BZSysExCore.ThrowFailException("投料信息已存在于其他跺型!");
  949. }
  950. var list = _billBomsetinfoRepository.Context.Queryable<BillBomsetinfo, BillBomsetgrp>((billBomsetinfo, billBomsetgrp) => new object[] {
  951. JoinType.Left,billBomsetinfo.BomSetHdrId == billBomsetgrp.Id }).Where((billBomsetinfo, billBomsetgrp) => billBomsetgrp.IsDelete == 0
  952. && billBomsetgrp.IsStop == 0 && billBomsetinfo.BomSetHdrId != item.Id && infoMatCodes.Contains(billBomsetinfo.MatCode)).ToList();
  953. if (list.Any())
  954. {
  955. throw BZSysExCore.ThrowFailException(string.Join(",", list.Select(o => o.MatCode).ToArray()) + ",湿拉物料已经存在其他跺型中!");
  956. }
  957. }
  958. }
  959. infos.ForEach(o =>
  960. {
  961. o.IsStop = (int)locationStop;
  962. o.EditWho = userId;
  963. o.EditTime = now;
  964. });
  965. // var result = _billBomsetgrpRepository.UpdateRange(list.ToArray());
  966. _billBomsetgrpRepository.UseTranAction(() =>
  967. {
  968. _billBomsetgrpRepository.UpdateRange(billBomsetgrplist.ToArray());
  969. _billBomsetinfoRepository.UpdateRange(infos.ToArray());
  970. });
  971. }
  972. /// <summary>
  973. ///
  974. /// </summary>
  975. /// <param name="typenum">1为09的跺型。</param>
  976. /// <returns></returns>
  977. public IEnumerable<dynamic> GetSelectCodeListByType(string typenum)
  978. {
  979. return typenum == "1"
  980. ? _billBomsetgrpRepository.Queryable().Where(o => o.IsStop == 0 && o.StampType == 1).ToList().Select(o => new { id = o.Code.ToString(), text = o.Name })
  981. : _billBomsetgrpRepository.Queryable().Where(o => o.IsStop == 0 && o.StampType != 1).ToList().Select(o => new { id = o.Code.ToString(), text = o.Name });
  982. }
  983. }
  984. }