ResourceExamineAppService.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Dynamic.Core;
  6. using System.Threading.Tasks;
  7. using Volo.Abp.Application.Dtos;
  8. using Volo.Abp.Domain.Repositories;
  9. using Business.Models;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Business.Permissions;
  12. using XCZ;
  13. using Business.ResourceExamineManagement.Dto;
  14. using Bussiness.Model.MES.IC;
  15. using AutoMapper.Internal.Mappers;
  16. using Bussiness.Model.Tech;
  17. using Bussiness.Model.Production;
  18. using Business.Core.MongoDBHelper;
  19. using Business.Core.Utilities;
  20. using Hangfire.Storage.Monitoring;
  21. using Business.BookManagement.Dto;
  22. using Volo.Abp.ObjectMapping;
  23. using Volo.Abp.Application.Services;
  24. using ZstdSharp.Unsafe;
  25. using System.Transactions;
  26. using NUglify.JavaScript.Syntax;
  27. using System.Linq.Expressions;
  28. using XCZ.Extensions;
  29. using System.ComponentModel;
  30. using System.Reflection.Emit;
  31. using NUglify.Helpers;
  32. using Microsoft.AspNetCore.SignalR.Protocol;
  33. using XCZ.Extensions;
  34. using System.ComponentModel.Design;
  35. using Volo.Abp.Validation.StringValues;
  36. using System.Runtime.CompilerServices;
  37. using MongoDB.Driver;
  38. namespace Business.ResourceExamineManagement
  39. {
  40. /// <summary>
  41. /// 资源检查
  42. /// </summary>
  43. //[Authorize(BusinessPermissions.ResourceExamine.Default)]
  44. public class ResourceExamineAppService : ApplicationService, IResourceExamineAppService
  45. {
  46. #region 服务
  47. /// <summary>
  48. /// 工艺路径
  49. /// </summary>
  50. private readonly IMongoDB<mes_technique> _mes_technique;
  51. /// <summary>
  52. /// 工序
  53. /// </summary>
  54. private readonly IMongoDB<mes_process> _mes_process;
  55. /// <summary>
  56. /// 工艺关联工序
  57. /// </summary>
  58. private readonly IMongoDB<mes_tech_process> _mes_tech_process;
  59. /// <summary>
  60. /// 工作日历
  61. /// </summary>
  62. private readonly IMongoDB<mes_work_calendar> _mes_work_calendar;
  63. /// <summary>
  64. /// 工作日历明细
  65. /// </summary>
  66. private readonly IMongoDB<mes_work_calendar_list> _mes_work_calendar_list;
  67. /// <summary>
  68. /// 工艺工序关联工位
  69. /// </summary>
  70. private readonly IMongoDB<mes_tech_proc_workshop> _mes_tech_proc_workshop;
  71. /// <summary>
  72. /// 排程占用记录
  73. /// </summary>
  74. private readonly IMongoDB<mes_schedule_occupy> _mes_schedule_occupy;
  75. /// <summary>
  76. /// 物料详情
  77. /// </summary>
  78. private readonly IMongoDB<ic_item> _ic_item;
  79. private IBasicRepository<ic_item, long> _mysql_ic_item;
  80. /// <summary>
  81. /// 物料BOM
  82. /// </summary>
  83. private readonly IMongoDB<ic_bom> _ic_bom;
  84. private IBasicRepository<ic_bom, long> _mysql_ic_bom;
  85. /// <summary>
  86. /// 物料BOM明细
  87. /// </summary>
  88. private readonly IMongoDB<ic_bom_child> _ic_bom_child;
  89. private IBasicRepository<ic_bom_child, long> _mysql_ic_bom_child;
  90. /// <summary>
  91. /// 物料库存表
  92. /// </summary>
  93. private readonly IMongoDB<ic_item_stock> _ic_item_stock;
  94. /// <summary>
  95. /// 物料质检表
  96. /// </summary>
  97. private readonly IMongoDB<ic_check> _ic_check;
  98. /// <summary>
  99. /// 替代群组
  100. /// </summary>
  101. private readonly IMongoDB<ic_substitute> _ic_substitute;
  102. /// <summary>
  103. /// 替代群组
  104. /// </summary>
  105. private readonly IMongoDB<ic_substitute_all> _ic_substitute_all;
  106. /// <summary>
  107. /// 替代群组
  108. /// </summary>
  109. private readonly IMongoDB<ic_substitute_all_dtl> _ic_substitute_all_dtl;
  110. /// <summary>
  111. /// 生产工单主表
  112. /// </summary>
  113. private readonly IMongoDB<mes_morder> _mes_morder;
  114. /// <summary>
  115. /// 在制工单占用记录表
  116. /// </summary>
  117. private readonly IMongoDB<mes_mooccupy> _mes_mooccupy;
  118. /// <summary>
  119. /// 销售工单
  120. /// </summary>
  121. private readonly IMongoDB<crm_seorder> _crm_seorder;
  122. #endregion
  123. /// <summary>
  124. /// 构造函数
  125. /// </summary>
  126. /// <param name="icitem"></param>
  127. /// <param name="icbom"></param>
  128. public ResourceExamineAppService(
  129. IMongoDB<mes_technique> mes_technique,
  130. IMongoDB<mes_process> mes_process,
  131. IMongoDB<mes_tech_process> mes_tech_process,
  132. IMongoDB<mes_tech_proc_workshop> mes_tech_proc_workshop,
  133. IMongoDB<ic_item> ic_item,
  134. IMongoDB<ic_bom> ic_bom,
  135. IMongoDB<ic_bom_child> ic_bom_child,
  136. IMongoDB<ic_item_stock> ic_item_stock,
  137. IMongoDB<ic_check> ic_check,
  138. IMongoDB<ic_substitute> ic_substitute,
  139. IMongoDB<ic_substitute_all> ic_substitute_all,
  140. IMongoDB<ic_substitute_all_dtl> ic_substitute_all_dtl,
  141. IMongoDB<mes_morder> mes_morder,
  142. IMongoDB<mes_mooccupy> mes_mooccupy,
  143. IBasicRepository<ic_item, long> mysql_ic_item,
  144. IBasicRepository<ic_bom, long> mysql_ic_bom,
  145. IBasicRepository<ic_bom_child, long> mysql_ic_bom_child
  146. )
  147. {
  148. _mes_technique = mes_technique;
  149. _mes_process = mes_process;
  150. _mes_tech_process = mes_tech_process;
  151. _mes_tech_proc_workshop = mes_tech_proc_workshop;
  152. _ic_item = ic_item;
  153. _ic_bom = ic_bom;
  154. _ic_bom_child = ic_bom_child;
  155. _ic_item_stock = ic_item_stock;
  156. _ic_check = ic_check;
  157. _ic_substitute = ic_substitute;
  158. _ic_substitute_all = ic_substitute_all;
  159. _ic_substitute_all_dtl = ic_substitute_all_dtl;
  160. _mes_morder = mes_morder;
  161. _mysql_ic_item = mysql_ic_item;
  162. _mysql_ic_bom = mysql_ic_bom;
  163. _mysql_ic_bom_child = mysql_ic_bom_child;
  164. }
  165. /// <summary>
  166. /// mongoDB示例方法,后期删除
  167. /// </summary>
  168. /// <returns></returns>
  169. public async Task test()
  170. {
  171. //多条插入
  172. List<mes_technique> infos = new List<mes_technique>();
  173. mes_technique info;
  174. for (int i = 0; i < 3; i++)
  175. {
  176. info = new mes_technique();
  177. info.GenerateNewId();
  178. info.tech_name = "多条" + i;
  179. info.level = i;
  180. infos.Add(info);
  181. }
  182. await _mes_technique.InsertMany(infos);
  183. var info2 = _mes_technique.GetAll().Result;
  184. var a = new PschedDto();
  185. a.count = info2.Count;
  186. //获取数据
  187. var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
  188. //更新数据
  189. info1.tech_name = "更新***";
  190. var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
  191. //根据条件查询数据
  192. Expression<Func<mes_technique, bool>> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
  193. var info3 = await _mes_technique.GetManyByCondition(filter);
  194. }
  195. /// <summary>
  196. /// 资源检查
  197. /// </summary>
  198. /// <param name="input"></param>
  199. /// <returns></returns>
  200. /// <exception cref="NotImplementedException"></exception>
  201. public async Task<PschedDto> ReceiveResult(SeorderentryDto input)
  202. {
  203. //测试代码
  204. //await test();
  205. await ProductiveExamine(1733221167209762816, 100, 1000);
  206. return null;
  207. throw new NotImplementedException();
  208. }
  209. /// <summary>
  210. /// 产能计算
  211. /// </summary>
  212. /// <param name="tech_id">工艺路径主键</param>
  213. /// <param name="packages">需要生产产品件数</param>
  214. /// <param name="quantity">需要生产数量</param>
  215. public async Task<DateTime> ProductiveExamine(long tech_id, int packages, int quantity)
  216. {
  217. if (packages <= 0 || quantity <= 0)
  218. {
  219. throw new NotImplementedException("产能计算参数有误!");
  220. }
  221. #region 1、数据准备
  222. //1.1、获取工艺路径数据
  223. mes_technique tech = _mes_technique.GetOneByID(tech_id).Result;
  224. //1.2、获取工艺路径关联工序数据
  225. List<mes_tech_process> tech_Processes = _mes_tech_process.GetManyByCondition(x => x.tech_id == tech_id).Result;
  226. //1.3、获取当前工艺路径下的工序数据
  227. FilterDefinition<mes_process> filter = Builders<mes_process>.Filter.In(s => s.Id, tech_Processes.Select(m => m.proc_id).ToList());
  228. List<mes_process> process = _mes_process.GetManyByIds(filter).Result;
  229. //1.3、获取工艺工序关联工位信息
  230. FilterDefinition<mes_tech_proc_workshop> filter1 = Builders<mes_tech_proc_workshop>.Filter.In(s => s.tech_proc_id, tech_Processes.Select(m => m.Id).ToList());
  231. List<mes_tech_proc_workshop> tech_Proc_Workshops = _mes_tech_proc_workshop.GetManyByIds(filter1).Result;
  232. //1.4、获取工位占用情况
  233. //List<mes_schedule_occupy> schedule_Occupies = _mes_schedule_occupy.GetManyByCondition().Result;
  234. #endregion
  235. #region 计算产能,得到耗时
  236. /*产能计算有两种模式:1-UPH;2-节拍时间
  237. *1、根据工艺路径设置的效率计算层级,来获取参与产能计算的所有工序/子工序
  238. *2、根据当前工序设置的计算模式UPH/节拍时间来计算当前工序生产所需时间T1
  239. *3、当前工序生产所需时间T1*效率系数Rate得到实际的生产所需时间T2(流水线不需要*效率系数)
  240. *4、当前工序实际生产所需时间T2+当前工序前置准备时间得到当前工序生产所需总时间
  241. *5、所有工序生产总时间加起来得到生产所需总时间
  242. */
  243. //1、获取参与计算的所有工序/子工序
  244. var tech_pro_list = tech_Processes.Where(p => p.level == tech.level).ToList();
  245. //生产总时间(分钟)
  246. decimal sumTimes = 0.00m;
  247. //记录按照CT参与计算的工序的父级Id
  248. List<long> exists = new List<long>();
  249. foreach (var item in tech_pro_list)
  250. {
  251. //数据校验
  252. //获取当前工序
  253. var curProcess = process.FirstOrDefault(p => p.Id == item.proc_id);
  254. if (item.type == 1 && item.ct == 0)
  255. {
  256. throw new NotImplementedException(string.Format("工艺[{0}]下的工序[{1}]节拍时间设置有误,请调整!", tech.tech_name, curProcess.proc_name));
  257. }
  258. if (item.type == 2 && item.uph == 0)
  259. {
  260. throw new NotImplementedException(string.Format("工艺[{0}]下的工序[{1}]UPH设置有误,请调整!", tech.tech_name, curProcess.proc_name));
  261. }
  262. if (item.parentprocid != null)//存在父子级关系
  263. {
  264. //判断当前父级是否已经参与过计算
  265. if (exists.Contains(item.parentprocid.Value))
  266. {
  267. continue;
  268. }
  269. //记录父级id
  270. exists.Add(item.parentprocid.Value);
  271. //获取同一父级下的子工序
  272. var childs = tech_Processes.Where(p => p.parentprocid == item.parentprocid).ToList();
  273. decimal maxTime = 0.00m;//记录同一父级下最大耗时
  274. decimal time = 0.00m;
  275. //2、两种计算模式UPH/节拍时间
  276. if (item.type == 2)//UPH(每小时生产个数),取同一父级下最长耗时
  277. {
  278. foreach (var chd in childs)
  279. {
  280. //当前工序消耗时间
  281. time = quantity / item.uph.Value * 60 * item.effect_ratio.Value;
  282. if (time > maxTime)
  283. {
  284. maxTime = time;
  285. }
  286. }
  287. sumTimes += maxTime;
  288. continue;
  289. }
  290. //3、根据节拍时间计算
  291. //sum(CT)
  292. decimal sumCT = childs.Sum(p => p.ct.Value);
  293. //max(CT)
  294. decimal maxCT = childs.OrderByDescending(p => p.ct).FirstOrDefault().ct.Value;
  295. //sum(LT)
  296. decimal sumLT = childs.Sum(p => p.readytime.Value);
  297. //当前流水线消耗时间
  298. sumTimes += (packages - 1) * maxCT + sumCT + sumLT;
  299. }
  300. else
  301. {//不存在父子级关系
  302. //2、两种计算模式UPH/节拍时间
  303. if (item.type == 2)//uph
  304. {
  305. //当前工序消耗时间
  306. sumTimes += quantity / item.uph.Value * 60 * item.effect_ratio.Value + item.readytime.Value;
  307. //计算完成,进行下一次循环
  308. continue;
  309. }
  310. //节拍时间:流水线,从流水线最后工序开始计算
  311. if (item.nextprocid != null)
  312. {
  313. //下-工序Id不为null,说明不是流水线最终工序,进行下一次循环
  314. continue;
  315. }
  316. List<mes_tech_process> allProcess = new List<mes_tech_process>();
  317. //递归,获取最终工序的上一工序
  318. GetPreProcess(tech_Processes, item, allProcess);
  319. //sum(CT)
  320. decimal sumCT = allProcess.Sum(p => p.ct.Value);
  321. //max(CT)
  322. decimal maxCT = allProcess.OrderByDescending(p => p.ct).FirstOrDefault().ct.Value;
  323. //sum(LT)
  324. decimal sumLT = allProcess.Sum(p => p.readytime.Value);
  325. //当前流水线消耗时间
  326. sumTimes += (packages - 1) * maxCT + sumCT + sumLT;
  327. }
  328. }
  329. #endregion
  330. return DateTime.Now.AddDays(1).AddMinutes((double)sumTimes);
  331. }
  332. /// <summary>
  333. /// 递归获取工序
  334. /// </summary>
  335. /// <param name="tech_Processes"></param>
  336. /// <param name="item"></param>
  337. /// <returns></returns>
  338. private void GetPreProcess(List<mes_tech_process> tech_Processes, mes_tech_process item, List<mes_tech_process> allProcess)
  339. {
  340. allProcess.Add(item);
  341. //获取上一工序
  342. var preProc = tech_Processes.FirstOrDefault(p => p.nextprocid == item.proc_id);
  343. if (preProc != null)
  344. {
  345. GetPreProcess(tech_Processes, preProc, allProcess);
  346. }
  347. }
  348. /// <summary>
  349. /// 检查在制工单
  350. /// </summary>
  351. /// <param name="bomNumber">Bom编码</param>
  352. /// <param name="Quantity">需要数量</param>
  353. /// <param name="EndDate">交付日期</param>
  354. /// <param name="OrderId">销售订单ID</param>
  355. /// <returns></returns>
  356. public async Task<List<mes_morder>> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate, long OrderId)
  357. {
  358. if (string.IsNullOrEmpty(bomNumber) || Quantity != null)
  359. {
  360. //TODO:入参异常;
  361. throw new NotImplementedException("BOM编码或需求数量不能为空!");
  362. }
  363. //根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。
  364. //TODO:工单类型;
  365. Expression<Func<mes_morder, bool>> filter = x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭"
  366. && x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false;
  367. var morderList = await _mes_morder.GetManyByCondition(filter);
  368. //TODO:获取销售订单信息
  369. //工单已被占用后要与占用表关联查询...减去占用量后 剩下生产数量可供下个销售工单使用。
  370. Expression<Func<mes_mooccupy, bool>> mooccupyfilter = x => x.moo_state == 1 && x.IsDeleted == false;
  371. var mes_mooccupyList = await _mes_mooccupy.GetManyByCondition(mooccupyfilter);
  372. //首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。
  373. //当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。
  374. var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate &&
  375. (x.morder_production_number - x.inventory_number) > Quantity).ToList();
  376. if (morderDataList.Count > 0)
  377. {
  378. //存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。
  379. var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault();
  380. //生成mes_mooccupy工单占用表数据,代表此工单被某个销售订单已占用。
  381. mes_mooccupy mes_Mooccupy = new mes_mooccupy();
  382. mes_Mooccupy.GenerateNewId();
  383. mes_Mooccupy.moo_id_type = "分配";
  384. mes_Mooccupy.moo_id_billid = 0;//销售订单ID
  385. mes_Mooccupy.fbill_no = "0";//销售订单号
  386. mes_Mooccupy.fentry_id = 0;//销售订单行
  387. mes_Mooccupy.fitem_name = string.Empty;//物料名称
  388. mes_Mooccupy.fitem_number = bomNumber;
  389. mes_Mooccupy.fmodel = string.Empty;//规格型号
  390. mes_Mooccupy.moo_moid = morder.Id;
  391. mes_Mooccupy.moo_mo = morder.morder_no;
  392. mes_Mooccupy.moo_qty = Quantity;
  393. mes_Mooccupy.moo_stime = DateTime.Now;
  394. mes_Mooccupy.moo_etime = DateTime.Now;//日期来源需确定
  395. mes_Mooccupy.moo_state = 1;
  396. mes_Mooccupy.moo_cbr = string.Empty;
  397. //mes_Mooccupy.moo_ctime = ;
  398. mes_Mooccupy.moo_creason = string.Empty;
  399. mes_Mooccupy.tenant_id = 0;
  400. await _mes_mooccupy.InsertOne(mes_Mooccupy);
  401. }
  402. else
  403. {
  404. // 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量
  405. var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList();
  406. if (morderListData.Count == 0)
  407. {
  408. //TODO:后期处理无在制工单返回内容
  409. throw new NotImplementedException("无可用在制工单!");
  410. }
  411. List<mes_morder> mes_Morders = new List<mes_morder>();
  412. List<mes_mooccupy> mes_Mooccupies = new List<mes_mooccupy>();
  413. decimal? number = Quantity;
  414. foreach (var item in morderListData)
  415. {
  416. //查询出工单已占用总数量
  417. var mes_mooccupy = mes_mooccupyList.Where(x => x.moo_moid == item.Id).ToList();
  418. //生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。
  419. mes_mooccupy mes_Mooccupy = new mes_mooccupy();
  420. mes_Mooccupy.GenerateNewId();
  421. mes_Mooccupy.moo_id_type = "分配";
  422. mes_Mooccupy.moo_id_billid = 0;//销售订单ID
  423. mes_Mooccupy.fbill_no = "0";//销售订单号
  424. mes_Mooccupy.fentry_id = 0;//销售订单行
  425. mes_Mooccupy.fitem_name = string.Empty;//物料名称
  426. mes_Mooccupy.fitem_number = bomNumber;
  427. mes_Mooccupy.fmodel = string.Empty;//规格型号
  428. mes_Mooccupy.moo_moid = item.Id;
  429. mes_Mooccupy.moo_mo = item.morder_no;
  430. //占用量=生产计划数量-入库数量-已被占用数量
  431. mes_Mooccupy.moo_qty = item.morder_production_number - item.inventory_number - mes_mooccupy.Sum(x => x.moo_qty);
  432. mes_Mooccupy.moo_stime = DateTime.Now;
  433. mes_Mooccupy.moo_etime = DateTime.Now;//日期来源需确定
  434. mes_Mooccupy.moo_state = 1;
  435. mes_Mooccupy.moo_cbr = string.Empty;
  436. //mes_Mooccupy.moo_ctime = ;
  437. mes_Mooccupy.moo_creason = string.Empty;
  438. mes_Mooccupy.tenant_id = 0;
  439. mes_Mooccupies.Add(mes_Mooccupy);
  440. //需求数量-占用量后小于或等于0 停止循环占用工单
  441. if (number - mes_Mooccupy.moo_qty <= 0)
  442. {
  443. break;
  444. }
  445. }
  446. }
  447. return morderList;
  448. #region 注释
  449. /* var morderList = _mes_morder.GetAll().Result;
  450. //查询生产工单类型为计划工单并且工单类型为备库工单 并且BOM编码一致数据,订单状态为 初始或下达工单
  451. var morderTypeList = morderList.Where(x => x.work_order_type == "备库工单" && x.morder_type == "计划工单" &&
  452. x.bom_number == bomNumber && (x.morder_state == "初始" || x.morder_state == "下达")).ToList();
  453. //计划数量-需求数量=剩余可用数量 -->使用计划结束时间排序取第一条满足条件工单
  454. // List =>数量满足Quantity && 生成工单结束日期最早
  455. var SatisfyQuantityList = morderTypeList.Where(x => (x.morder_production_number.Value - x.need_number.Value) >= Quantity).
  456. ToList().OrderBy(x => x.moentry_etime).FirstOrDefault();
  457. if (SatisfyQuantityList == null)
  458. {
  459. }
  460. else
  461. {
  462. }*/
  463. #endregion
  464. }
  465. /// <summary>
  466. /// BOM预处理
  467. /// </summary>
  468. /// <param name="orderid"></param>
  469. /// <param name="BomId"></param>
  470. /// <param name="Quantity"></param>
  471. public void BomPretreatment(long? orderid, long? BomId, int Quantity)
  472. {
  473. if (orderid == null)
  474. {
  475. //throw new bu
  476. }
  477. if (BomId == null)
  478. {
  479. //throw new bu
  480. }
  481. //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
  482. var help = new SnowFlake();
  483. var bomlist = _ic_bom.GetAll().Result;
  484. var bomchildlist = _ic_bom_child.GetAll().Result;
  485. var icitemlist = _ic_item.GetAll().Result;
  486. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  487. var dto = new BomChildExamineDto();
  488. dto.bom_id = BomId.Value;
  489. dto.level = 1;
  490. dto.id = help.NextId();
  491. dto.parent_id = help.NextId();
  492. dto.qty = 1;
  493. dto.num = "1";
  494. dto.isbom = 1;
  495. dto.is_replace = 0;
  496. dto.haveicsubs = 0;
  497. dto.substitute_code = "";
  498. dto.icitem_ids = "";
  499. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist);
  500. }
  501. /// <summary>
  502. /// BOM预处理层级组装
  503. /// </summary>
  504. /// <param name="bomlist"></param>
  505. /// <param name="bomchildlist"></param>
  506. /// <param name="icitemlist"></param>
  507. /// <param name="dto"></param>
  508. /// <param name="returnlist"></param>
  509. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist)
  510. {
  511. int level = dto.level++;//初始化定义level层级
  512. var help = new SnowFlake();
  513. var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
  514. var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
  515. if (bom == null || item == null)
  516. {
  517. }
  518. //var dto = new BomChildExamineDto();
  519. //dto.level = level;
  520. //dto.bom_id = bomId;
  521. //dto.id = help.NextId();
  522. //dto.parent_id = parent_id;
  523. dto.item_id = bom.icitem_id;
  524. dto.item_name = bom.item_name;
  525. dto.item_code = bom.item_number;
  526. dto.model = item.model;
  527. dto.unit = bom.unit;
  528. dto.erp_cls = item.erp_cls;
  529. dto.erp_cls_name = item.erp_cls_name;
  530. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  531. returnlist.Add(dto);
  532. var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
  533. int idx = 1;
  534. foreach (var c in childlist)
  535. {
  536. string childNum = dto.num + "." + idx.ToString();
  537. var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
  538. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  539. if (c.is_bom == 1)
  540. {
  541. var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
  542. if (childBom != null)
  543. {
  544. var cdto = new BomChildExamineDto();
  545. cdto.id = help.NextId();
  546. cdto.level = level;
  547. cdto.parent_id = dto.id;
  548. cdto.qty = c.qty.Value;
  549. cdto.backflush = c.backflush;
  550. cdto.num = childNum;
  551. cdto.isbom = 1;
  552. cdto.is_replace = c.is_replace;
  553. cdto.haveicsubs = c.haveicsubs;
  554. cdto.substitute_code = c.substitute_code;
  555. cdto.icitem_ids = c.icitem_ids;
  556. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist);
  557. }
  558. }
  559. else
  560. {
  561. if (icitem != null)
  562. {
  563. var childDto = new BomChildExamineDto();
  564. childDto.level = level++;
  565. childDto.bom_id = dto.bom_id;
  566. childDto.id = help.NextId();
  567. childDto.parent_id = dto.id;
  568. childDto.item_id = icitem.Id;
  569. childDto.item_name = icitem.name;
  570. childDto.item_code = icitem.number;
  571. childDto.num = childNum;
  572. childDto.model = icitem.model;
  573. childDto.unit = c.unit;
  574. childDto.erp_cls = item.erp_cls;
  575. childDto.erp_cls_name = item.erp_cls_name;
  576. childDto.backflush = c.backflush;
  577. childDto.qty = c.qty.Value;
  578. childDto.isbom = 0;
  579. childDto.is_replace = c.is_replace;
  580. childDto.haveicsubs = c.haveicsubs;
  581. childDto.substitute_code = c.substitute_code;
  582. childDto.icitem_ids = c.icitem_ids;
  583. returnlist.Add(childDto);
  584. }
  585. }
  586. idx++;
  587. }
  588. }
  589. /// <summary>
  590. /// BOM替代关系预处理
  591. /// </summary>
  592. public void BomSubstitute(List<BomChildExamineDto> returnlist, List<ic_item> icitemlist)
  593. {
  594. var sublist = _ic_substitute.GetManyByCondition(s => s.substitute_code.IsIn(returnlist.Select(c => c.substitute_code))).Result.ToList();
  595. var suballlist = _ic_substitute_all.GetManyByCondition(s => s.substitute_id.IsIn(sublist.Select(c => c.Id))).Result.ToList();
  596. var subdtllist = _ic_substitute_all_dtl.GetManyByCondition(s => s.substitute_allid.IsIn(suballlist.Select(c => c.Id))).Result.ToList();
  597. List<long> dicIds = new List<long>();
  598. var help = new SnowFlake();
  599. //除顶级外,其他层级关系全带出来。生成平铺
  600. foreach (var item in returnlist)
  601. {
  602. //最顶级、虚拟件
  603. if (item.level != 1 && item.erp_cls != 4)
  604. {
  605. //有替代关系
  606. if (item.haveicsubs == 1)
  607. {
  608. /*//群组替代
  609. if (item.is_replace == 1)
  610. {
  611. if (!string.IsNullOrEmpty(item.icitem_ids))
  612. {
  613. var ids = item.icitem_ids.Split(',');
  614. foreach (var i in ids)
  615. {
  616. long id = 0;
  617. if (long.TryParse(i, out id))
  618. {
  619. dicIds.Add(id);
  620. }
  621. }
  622. }
  623. }*/
  624. var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
  625. if (sl != null)
  626. {
  627. var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
  628. foreach (var sal in sall)
  629. {
  630. var sadl = subdtllist.Where(s => s.substitute_allid == sal.Id).ToList();
  631. foreach (var dtl in sadl)
  632. {
  633. SubstitutePretreatment(sal, dtl, item, returnlist, icitemlist);
  634. //var sitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
  635. //if (sitem != null)
  636. //{
  637. // var childDto = new BomChildExamineDto();
  638. // childDto.level = item.level;
  639. // childDto.bom_id = item.bom_id;
  640. // childDto.id = help.NextId();
  641. // childDto.parent_id = item.parent_id;
  642. // childDto.item_id = sitem.Id;
  643. // childDto.item_name = sitem.name;
  644. // childDto.item_code = sitem.number;
  645. // childDto.num = item.num;
  646. // childDto.model = sitem.model;
  647. // childDto.unit = sitem.unit;
  648. // childDto.erp_cls = sitem.erp_cls;
  649. // childDto.erp_cls_name = sitem.erp_cls_name;
  650. // childDto.backflush = item.backflush;
  651. // childDto.replace_amount = dtl.use_amount.Value;
  652. // childDto.replace_amount = dtl.replace_amount.Value;
  653. // childDto.isbom = 0;
  654. // childDto.is_replace = c.is_replace;
  655. // childDto.haveicsubs = c.haveicsubs;
  656. // childDto.substitute_code = c.substitute_code;
  657. // childDto.icitem_ids = c.icitem_ids;
  658. // returnlist.Add(childDto);
  659. //}
  660. }
  661. }
  662. }
  663. }
  664. }
  665. }
  666. }
  667. public void SubstitutePretreatment(ic_substitute_all sal, ic_substitute_all_dtl dtl, BomChildExamineDto toDto, List<BomChildExamineDto> returnlist, List<ic_item> icitemlist)
  668. {
  669. //生成sal层级dto,是toDto的替代关系,类似R6-K50的BOM的层级1.1.1
  670. //循环生成dtl层级dto,明细
  671. //如果dtl对应的icitem是BOM,还需要向下继续展开。
  672. }
  673. /// <summary>
  674. /// 计算库存量
  675. /// </summary>
  676. /// <param name="returnlist"></param>
  677. public void BomStock(List<BomChildExamineDto> returnlist, long factoryid)
  678. {
  679. returnlist = returnlist.OrderBy(s => s.num).ToList();
  680. //取物料库存记录
  681. var stocklist = _ic_item_stock.GetManyByCondition(s => s.icitem_id.IsIn(returnlist.Select(c => c.item_id)) && s.factory_id == factoryid).Result;
  682. //取当前物料质检记录
  683. //var checklist = _ic_check.GetManyByCondition(s => s.icitem_id.IsIn(returnlist.Select(c => c.item_id)) && s.istest.GetValueOrDefault() != 1).Result.ToList();
  684. List<long> idlist = new List<long>();
  685. foreach (var item in returnlist)
  686. {
  687. if (item.erp_cls != 4)//虚拟件不计算
  688. {
  689. var sl = stocklist.Where(s => s.icitem_id == item.item_id).ToList();
  690. //库存=物料库存+VMI库存+质检未入库。
  691. item.sqty = sl.Sum(s => s.sqty).GetValueOrDefault();
  692. //质检记录没加入,暂时不考虑。
  693. //VMI仓储信息
  694. }
  695. }
  696. }
  697. }
  698. }