BillBomSetService.cs 52 KB

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