ResourceExamineAppService.cs 32 KB

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