BillBomSetService.cs 45 KB

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