PretreatmentAppService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using Business.Core.MongoDBHelper;
  2. using Business.Core.Utilities;
  3. using Business.ResourceExamineManagement.Dto;
  4. using Bussiness.Model.MES.IC;
  5. using Bussiness.Model.Production;
  6. using SixLabors.ImageSharp;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Volo.Abp.Application.Services;
  13. namespace Business.ResourceExamineManagement
  14. {
  15. public class PretreatmentAppService : ApplicationService
  16. {
  17. SnowFlake help = new SnowFlake();
  18. //构造函数
  19. public PretreatmentAppService() {
  20. }
  21. /// <summary>
  22. /// BOM预处理
  23. /// </summary>
  24. /// <param name="orderid"></param>
  25. /// <param name="BomId"></param>
  26. /// <param name="Quantity"></param>
  27. public List<BomChildExamineDto> BomPretreatment(long? BomId, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist)
  28. {
  29. if (BomId == null)
  30. {
  31. //throw new bu
  32. }
  33. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  34. var bom = bomlist.Find(s => s.Id == BomId);
  35. if (bom == null)
  36. {
  37. return returnlist;
  38. }
  39. var dto = new BomChildExamineDto();
  40. dto.item_id = bom.icitem_id;
  41. dto.bom_id = BomId.Value;
  42. dto.bom_number = bom.bom_number;
  43. dto.level = 1;
  44. dto.id = help.NextId();
  45. dto.parent_id = help.NextId();
  46. dto.qty = 1;
  47. dto.num = "1";
  48. dto.num_order = 1;
  49. dto.isbom = 1;
  50. dto.is_replace = 0;
  51. dto.haveicsubs = 0;
  52. dto.substitute_code = "";
  53. dto.icitem_ids = "";
  54. int type = 0;
  55. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  56. return returnlist;
  57. }
  58. /// <summary>
  59. /// BOM预处理层级组装
  60. /// </summary>
  61. /// <param name="bomlist"></param>
  62. /// <param name="bomchildlist"></param>
  63. /// <param name="icitemlist"></param>
  64. /// <param name="dto"></param>
  65. /// <param name="returnlist"></param>
  66. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist, int type)
  67. {
  68. int level = dto.level + 1;//初始化定义level层级
  69. var bom = bomlist.Where(s => s.Id == dto.bom_id).FirstOrDefault();
  70. ic_item item = new ic_item();
  71. if (bom != null)
  72. {
  73. item = icitemlist.Where(a => a.Id == bom.icitem_id).FirstOrDefault();
  74. }
  75. else
  76. {
  77. item = icitemlist.Where(a => a.Id == dto.item_id).FirstOrDefault();
  78. }
  79. if (item == null)
  80. {
  81. return;
  82. }
  83. dto.item_id = item.Id;
  84. dto.item_name = bom.item_name;
  85. dto.item_code = bom.item_number;
  86. dto.model = item.model;
  87. dto.unit = bom.unit;
  88. dto.erp_cls = item.erp_cls;
  89. dto.erp_cls_name = item.erp_cls_name;
  90. dto.type = type;
  91. dto.item_number = item.number;
  92. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  93. returnlist.Add(dto);
  94. var childlist = bomchildlist.Where(a => a.bom_id == bom.Id).ToList();
  95. int idx = 1;
  96. foreach (var c in childlist)
  97. {
  98. string childNum = dto.num + "." + idx.ToString();
  99. long order_num = long.Parse(dto.num_order.ToString() + (idx.ToString().PadLeft(3, '0')));
  100. var icitem = icitemlist.Where(a => a.Id == c.icitem_id).FirstOrDefault();
  101. var childBom = bomlist.Where(a => a.icitem_id == c.icitem_id).FirstOrDefault();
  102. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  103. if (childBom != null)
  104. {
  105. var cdto = new BomChildExamineDto();
  106. cdto.id = help.NextId();
  107. cdto.level = level;
  108. cdto.parent_id = dto.id;
  109. cdto.bom_child_id = c.Id;
  110. cdto.qty = c.qty.Value;
  111. cdto.backflush = c.backflush;
  112. cdto.num = childNum;
  113. cdto.num_order = order_num;
  114. cdto.isbom = 1;
  115. cdto.is_replace = c.is_replace;
  116. cdto.haveicsubs = c.haveicsubs;
  117. cdto.substitute_code = c.substitute_code;
  118. cdto.icitem_ids = c.icitem_ids;
  119. cdto.type = type;
  120. cdto.item_id = childBom.icitem_id;
  121. cdto.bom_id = childBom.Id;
  122. cdto.bom_number = childBom.bom_number;
  123. //递归寻找子级
  124. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist, type);
  125. }
  126. else
  127. {
  128. if (icitem != null)
  129. {
  130. var childDto = new BomChildExamineDto();
  131. childDto.level = level;
  132. childDto.bom_id = dto.bom_id;
  133. childDto.bom_child_id = c.Id;
  134. childDto.id = help.NextId();
  135. childDto.parent_id = dto.id;
  136. childDto.item_id = icitem.Id;
  137. childDto.item_name = icitem.name;
  138. childDto.item_code = icitem.number;
  139. childDto.num = childNum;
  140. childDto.num_order = order_num;
  141. childDto.model = icitem.model;
  142. childDto.unit = c.unit;
  143. childDto.erp_cls = icitem.erp_cls;
  144. childDto.erp_cls_name = icitem.erp_cls_name;
  145. childDto.backflush = c.backflush;
  146. childDto.qty = c.qty.Value;
  147. childDto.isbom = 0;
  148. childDto.is_replace = c.is_replace;
  149. childDto.haveicsubs = c.haveicsubs;
  150. childDto.substitute_code = c.substitute_code;
  151. childDto.icitem_ids = c.icitem_ids;
  152. childDto.type = type;
  153. childDto.item_number = icitem.number;
  154. returnlist.Add(childDto);
  155. }
  156. }
  157. idx++;
  158. }
  159. }
  160. /// <summary>
  161. /// BOM替代关系预处理
  162. /// </summary>
  163. /// <param name="returnlist"></param>
  164. /// <param name="bomlist"></param>
  165. /// <param name="bomchildlist"></param>
  166. /// <param name="icitemlist"></param>
  167. /// <param name="sublist"></param>
  168. /// <param name="suballlist"></param>
  169. /// <param name="subdtllist"></param>
  170. public void BomSubstitute(List<BomChildExamineDto> returnlist, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, List<ic_substitute> sublist, List<ic_substitute_all> suballlist, List<ic_substitute_all_dtl> subdtllist)
  171. {
  172. List<long> childidList = new List<long>();
  173. int type = 1;
  174. List<BomChildExamineDto> addlist = new List<BomChildExamineDto>();
  175. //除顶级外,其他层级关系全带出来。生成平铺
  176. foreach (var item in returnlist)
  177. {
  178. //最顶级、虚拟件
  179. if (item.level == 1 || item.erp_cls == 4 || childidList.Contains(item.bom_child_id.GetValueOrDefault()))
  180. {
  181. continue;
  182. }
  183. //有替代关系
  184. if (item.haveicsubs != 1)
  185. {
  186. continue;
  187. }
  188. if (!string.IsNullOrEmpty(item.icitem_ids))
  189. {
  190. long cid = 1;
  191. var cids = item.icitem_ids.Split(',');
  192. foreach (var c in cids)
  193. {
  194. if (long.TryParse(c, out cid))
  195. {
  196. childidList.Add(cid);
  197. }
  198. }
  199. }
  200. //找到当前物料的替代群组关系集
  201. var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
  202. if (sl != null)
  203. {
  204. var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
  205. foreach (var sal in sall)
  206. {
  207. var sadl = subdtllist.Where(s => s.substitute_allid == sal.Id).ToList();
  208. foreach (var dtl in sadl)
  209. {
  210. if (sal.main_material.GetValueOrDefault() != 1)
  211. {
  212. //递归将替代关系组装出来。
  213. SubstitutePretreatment(sl, sal, dtl, item, addlist, icitemlist, bomlist, bomchildlist, type);
  214. }
  215. else
  216. {
  217. //将主料赋值上属性
  218. var dtlitem = returnlist.Find(s => s.item_id == dtl.icitem_id && s.level == item.level);
  219. dtlitem.substitute_all_num = sal.order_num;//群组优先级
  220. }
  221. }
  222. }
  223. }
  224. }
  225. returnlist.AddRange(addlist);
  226. }
  227. /// <summary>
  228. /// 替代关系递归组装出来
  229. /// </summary>
  230. /// <param name="sal"></param>
  231. /// <param name="dtl"></param>
  232. /// <param name="toDto"></param>
  233. /// <param name="returnlist"></param>
  234. /// <param name="icitemlist"></param>
  235. /// <param name="bomlist"></param>
  236. /// <param name="bomchildlist"></param>
  237. public void SubstitutePretreatment(ic_substitute sl, ic_substitute_all sal, ic_substitute_all_dtl dtl, BomChildExamineDto toDto, List<BomChildExamineDto> returnlist, List<ic_item> icitemlist, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, int type)
  238. {
  239. //如果dtl对应的icitem是BOM,还需要向下继续展开。
  240. //List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  241. var dto = new BomChildExamineDto();
  242. var bom = bomlist.Where(s => s.icitem_id == dtl.icitem_id).FirstOrDefault();
  243. var icitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
  244. if (icitem == null)
  245. {
  246. return;
  247. }
  248. dto.id = help.NextId();
  249. dto.level = toDto.level;
  250. dto.parent_id = toDto.parent_id;
  251. dto.item_id = icitem.Id;
  252. dto.item_name = icitem.name;
  253. dto.item_code = icitem.number;
  254. dto.num = toDto.num;
  255. dto.num_order = toDto.num_order;
  256. dto.model = icitem.model;
  257. dto.unit = icitem.unit;
  258. dto.erp_cls = icitem.erp_cls;
  259. dto.erp_cls_name = icitem.erp_cls_name;
  260. dto.backflush = toDto.backflush;
  261. //dto.qty = toDto.qty;
  262. //dto.replace_amount = dtl.replace_amount.Value;
  263. dto.qty = dtl.replace_amount == null ? 1 : dtl.replace_amount.Value;
  264. dto.is_replace = 0;
  265. dto.haveicsubs = 0;
  266. dto.substitute_code = "";
  267. dto.icitem_ids = "";
  268. dto.substitute_strategy = sl.substitute_strategy == null ? 0 : sl.substitute_strategy.Value;//替代策略
  269. dto.substitute_mode = sl.substitute_mode == null ? 0 : sl.substitute_mode.Value;//替代方式
  270. dto.type = type;
  271. dto.substitute_all_num = sal.order_num;//群组优先级
  272. if (bom != null)
  273. {
  274. dto.bom_id = bom.Id;
  275. dto.qty = dtl.replace_amount == null ? 1 : dtl.replace_amount.Value;
  276. dto.isbom = 1;
  277. dto.is_replace = 0;
  278. dto.haveicsubs = 0;
  279. dto.substitute_code = "";
  280. dto.icitem_ids = "";
  281. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  282. }
  283. else
  284. {
  285. dto.bom_id = null;
  286. dto.isbom = 0;
  287. returnlist.Add(dto);
  288. }
  289. }
  290. }
  291. }