ResourceExamineAppService.cs 31 KB

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