ResourceExamineAppService.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. using Volo.Abp.Validation.Localization;
  39. using Hangfire.Annotations;
  40. namespace Business.ResourceExamineManagement
  41. {
  42. /// <summary>
  43. /// 资源检查
  44. /// </summary>
  45. //[Authorize(BusinessPermissions.ResourceExamine.Default)]
  46. public class ResourceExamineAppService : ApplicationService, IResourceExamineAppService
  47. {
  48. #region 服务
  49. /// <summary>
  50. /// 工艺路径
  51. /// </summary>
  52. private readonly IMongoDB<mes_technique> _mes_technique;
  53. private IRepository<mes_technique, long> _mysql_mes_technique;
  54. /// <summary>
  55. /// 工序
  56. /// </summary>
  57. private readonly IMongoDB<mes_process> _mes_process;
  58. /// <summary>
  59. /// 工艺关联工序
  60. /// </summary>
  61. private readonly IMongoDB<mes_tech_process> _mes_tech_process;
  62. /// <summary>
  63. /// 工作日历
  64. /// </summary>
  65. private readonly IMongoDB<mes_work_calendar> _mes_work_calendar;
  66. /// <summary>
  67. /// 工作日历明细
  68. /// </summary>
  69. private readonly IMongoDB<mes_work_calendar_list> _mes_work_calendar_list;
  70. /// <summary>
  71. /// 工艺工序关联工位
  72. /// </summary>
  73. private readonly IMongoDB<mes_tech_proc_workshop> _mes_tech_proc_workshop;
  74. /// <summary>
  75. /// 排程占用记录
  76. /// </summary>
  77. private readonly IMongoDB<mes_schedule_occupy> _mes_schedule_occupy;
  78. /// <summary>
  79. /// 物料占用记录
  80. /// </summary>
  81. private readonly IMongoDB<ic_item_stockoccupy> _ic_item_stockoccupy;
  82. /// <summary>
  83. /// 物料详情
  84. /// </summary>
  85. private readonly IMongoDB<ic_item> _ic_item;
  86. private IRepository<ic_item, long> _mysql_ic_item;
  87. /// <summary>
  88. /// 物料BOM
  89. /// </summary>
  90. private readonly IMongoDB<ic_bom> _ic_bom;
  91. private IRepository<ic_bom, long> _mysql_ic_bom;
  92. /// <summary>
  93. /// 物料BOM明细
  94. /// </summary>
  95. private readonly IMongoDB<ic_bom_child> _ic_bom_child;
  96. private IRepository<ic_bom_child, long> _mysql_ic_bom_child;
  97. /// <summary>
  98. /// 物料库存表
  99. /// </summary>
  100. private readonly IMongoDB<ic_item_stock> _ic_item_stock;
  101. /// <summary>
  102. /// 物料质检表
  103. /// </summary>
  104. private readonly IMongoDB<ic_check> _ic_check;
  105. /// <summary>
  106. /// 替代群组
  107. /// </summary>
  108. private readonly IMongoDB<ic_substitute> _ic_substitute;
  109. /// <summary>
  110. /// 替代群组
  111. /// </summary>
  112. private readonly IMongoDB<ic_substitute_all> _ic_substitute_all;
  113. /// <summary>
  114. /// 替代群组
  115. /// </summary>
  116. private readonly IMongoDB<ic_substitute_all_dtl> _ic_substitute_all_dtl;
  117. /// <summary>
  118. /// 生产工单主表
  119. /// </summary>
  120. private readonly IMongoDB<mes_morder> _mes_morder;
  121. /// <summary>
  122. /// 在制工单占用记录表
  123. /// </summary>
  124. private readonly IMongoDB<mes_mooccupy> _mes_mooccupy;
  125. /// <summary>
  126. /// 销售工单
  127. /// </summary>
  128. private readonly IRepository<crm_seorder> _crm_seorder;
  129. /// <summary>
  130. /// 销售工单
  131. /// </summary>
  132. private readonly IRepository<crm_seorderentry> _crm_seorderentry;
  133. #endregion
  134. /// <summary>
  135. /// 构造函数
  136. /// </summary>
  137. /// <param name="icitem"></param>
  138. /// <param name="icbom"></param>
  139. public ResourceExamineAppService(
  140. IMongoDB<mes_technique> mes_technique,
  141. IMongoDB<mes_process> mes_process,
  142. IMongoDB<mes_tech_process> mes_tech_process,
  143. IMongoDB<mes_tech_proc_workshop> mes_tech_proc_workshop,
  144. IMongoDB<ic_item> ic_item,
  145. IMongoDB<ic_bom> ic_bom,
  146. IMongoDB<ic_bom_child> ic_bom_child,
  147. IMongoDB<ic_item_stock> ic_item_stock,
  148. IMongoDB<ic_check> ic_check,
  149. //IMongoDB<ic_substitute> ic_substitute,
  150. //IMongoDB<ic_substitute_all> ic_substitute_all,
  151. //IMongoDB<ic_substitute_all_dtl> ic_substitute_all_dtl,
  152. IMongoDB<mes_morder> mes_morder,
  153. IMongoDB<mes_mooccupy> mes_mooccupy,
  154. IMongoDB<ic_item_stockoccupy> ic_item_stockoccupy,
  155. IRepository<ic_item, long> mysql_ic_item,
  156. IRepository<ic_bom, long> mysql_ic_bom,
  157. IRepository<ic_bom_child, long> mysql_ic_bom_child,
  158. IRepository<mes_technique, long> mysql_mes_technique,
  159. IRepository<crm_seorder, long> mysql_crm_seorder,
  160. IRepository<crm_seorderentry, long> mysql_crm_seorderentry
  161. )
  162. {
  163. _mes_technique = mes_technique;
  164. _mes_process = mes_process;
  165. _mes_tech_process = mes_tech_process;
  166. _mes_tech_proc_workshop = mes_tech_proc_workshop;
  167. _ic_item = ic_item;
  168. _ic_bom = ic_bom;
  169. _ic_bom_child = ic_bom_child;
  170. _ic_item_stock = ic_item_stock;
  171. _ic_check = ic_check;
  172. //_ic_substitute = ic_substitute;
  173. //_ic_substitute_all = ic_substitute_all;
  174. //_ic_substitute_all_dtl = ic_substitute_all_dtl;
  175. _mes_morder = mes_morder;
  176. _mes_mooccupy = mes_mooccupy;
  177. _ic_item_stockoccupy = ic_item_stockoccupy;
  178. _mysql_ic_item = mysql_ic_item;
  179. _mysql_ic_bom = mysql_ic_bom;
  180. _mysql_ic_bom_child = mysql_ic_bom_child;
  181. _mysql_mes_technique = mysql_mes_technique;
  182. }
  183. /// <summary>
  184. /// mongoDB示例方法,后期删除
  185. /// </summary>
  186. /// <returns></returns>
  187. public async Task test()
  188. {
  189. //多条插入
  190. List<mes_technique> infos = new List<mes_technique>();
  191. mes_technique info;
  192. for (int i = 0; i < 3; i++)
  193. {
  194. info = new mes_technique();
  195. info.GenerateNewId();
  196. info.tech_name = "多条" + i;
  197. info.level = i;
  198. infos.Add(info);
  199. }
  200. await _mes_technique.InsertMany(infos);
  201. var info2 = _mes_technique.GetAll().Result;
  202. var a = new PschedDto();
  203. a.count = info2.Count;
  204. //获取数据
  205. var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
  206. //更新数据
  207. info1.tech_name = "更新***";
  208. var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
  209. //根据条件查询数据
  210. Expression<Func<mes_technique, bool>> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
  211. var info3 = await _mes_technique.GetManyByCondition(filter);
  212. }
  213. /// <summary>
  214. /// 资源检查
  215. /// </summary>
  216. /// <param name="input"></param>
  217. /// <returns></returns>
  218. /// <exception cref="NotImplementedException"></exception>
  219. public async Task<PschedDto> ReceiveResult(SeorderentryDto input)
  220. {
  221. //数据库快照-同步mysql库数据到mongoDB中
  222. await SyncData();
  223. //生成当前计算bangid
  224. //SnowFlake snow = new SnowFlake();
  225. //long bangid = snow.NextId();
  226. ////产能检查
  227. //await ProductiveExamine(1733221167209762816, 100);
  228. return null;
  229. throw new NotImplementedException();
  230. }
  231. /// <summary>
  232. /// 数据库快照
  233. /// </summary>
  234. /// <returns></returns>
  235. public async Task SyncData()
  236. {
  237. var query = _mysql_mes_technique.GetQueryableAsync().Result.ToListAsync().Result;
  238. await _mes_technique.InsertMany(query);
  239. //var dt1 = DateTime.Now;
  240. //var a = await _mysql_ic_bom.GetQueryableAsync();
  241. //var query1 =await a.ToListAsync();
  242. //await _ic_bom.InsertMany(query1);
  243. //var dt2 = DateTime.Now;
  244. //var c = 3;
  245. }
  246. /// <summary>
  247. /// 产能计算
  248. /// </summary>
  249. /// <param name="tech_id">工艺路径主键</param>
  250. /// <param name="packages">需要生产产品件数</param>
  251. public async Task<DateTime> ProductiveExamine(long tech_id, int packages)
  252. {
  253. if (packages <= 0)
  254. {
  255. throw new NotImplementedException("产能计算参数有误!");
  256. }
  257. #region 1、数据准备
  258. //1.1、获取工艺路径数据
  259. mes_technique tech = await _mes_technique.GetOneByID(tech_id);
  260. //1.2、获取工艺路径关联工序数据
  261. List<mes_tech_process> tech_Processes = await _mes_tech_process.GetManyByCondition(x => x.tech_id == tech_id);
  262. //1.3、获取当前工艺路径下的工序数据
  263. FilterDefinition<mes_process> filter = Builders<mes_process>.Filter.In(s => s.Id, tech_Processes.Select(m => m.proc_id).ToList());
  264. List<mes_process> process = await _mes_process.GetManyByIds(filter);
  265. //1.3、获取工艺工序关联工位信息
  266. 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());
  267. List<mes_tech_proc_workshop> tech_Proc_Workshops = await _mes_tech_proc_workshop.GetManyByIds(filter1);
  268. #endregion
  269. #region 计算产能,得到耗时
  270. decimal sumTimes = 0.00m;//总耗时(分钟)
  271. //工序需要等待时间记录
  272. List<StartTimeDto> starts = new List<StartTimeDto>();
  273. //1、获取工艺路径下的第一层级工序
  274. List<mes_tech_process> fistChilds = tech_Processes.Where(p => p.parentprocid == tech_id).OrderBy(m => m.Id).ToList();
  275. if (fistChilds.Count == 0)
  276. {
  277. throw new NotImplementedException("当前工艺路径没有配置工序,请调整!");
  278. }
  279. decimal curTakeTime = 0.00m;//当前工序耗时(分钟)
  280. //添加第一个工序需要等待时间记录
  281. StartTimeDto dto;
  282. foreach (var chd in fistChilds)
  283. {
  284. dto = new StartTimeDto();
  285. if (chd.nextprocid == null)//最后一个工序
  286. {
  287. //计算最后一个工序耗时
  288. curTakeTime = CalcTakeTime(chd, packages);
  289. }
  290. else
  291. {
  292. curTakeTime = CalcTakeTime(chd, chd.lq.Value);
  293. }
  294. sumTimes += curTakeTime;
  295. //添加耗时记录
  296. dto.tech_id = tech_id;
  297. dto.proc_id = chd.proc_id;
  298. dto.nextproc_id = chd.nextprocid;
  299. dto.wait_time = curTakeTime;
  300. starts.Add(dto);
  301. }
  302. #endregion
  303. return DateTime.Now.AddDays(1).AddMinutes((double)sumTimes);
  304. }
  305. /// <summary>
  306. /// 计算当前工序前置准备时间
  307. /// </summary>
  308. /// <param name="proc"></param>
  309. /// <param name="quantity">LeadQuantity to Start Next</param>
  310. /// <returns></returns>
  311. private decimal CalcTakeTime(mes_tech_process proc, decimal quantity)
  312. {
  313. decimal takeTime = 0.00m;//当前工序前置准备时间(分钟)
  314. if (proc.wctype == 1)//人工型:数量/uph(一小时生产数量)*60(小时转换为分钟)/wsinuse(工位数)
  315. {
  316. takeTime = quantity / proc.uph.Value * 60 / proc.wsinuse.Value;
  317. return takeTime;
  318. }
  319. else if (proc.wctype == 2)//流水线型:数量*ct(生产一件所需时间)
  320. {
  321. takeTime = quantity * proc.ct.Value;
  322. return takeTime;
  323. }
  324. else if (proc.wctype == 3)//设备型:向上取整(数量/一次可加工数量)*ct(老化一次所需时间)
  325. {
  326. takeTime = Math.Ceiling(quantity / proc.upe.Value) * proc.ct.Value;
  327. return takeTime;
  328. }
  329. return takeTime;
  330. }
  331. /// <summary>
  332. /// 生成主工单
  333. /// </summary>
  334. /// <param name="BomNumber">Bom编码</param>
  335. /// <param name="Quantity">需要数量</param>
  336. /// <param name="DeliverDate">交付日期</param>
  337. /// <param name="OrderId">销售订单ID</param>
  338. /// <returns></returns>
  339. public async Task GenerateMorder(string BomNumber, decimal? Quantity, DateTime DeliverDate, long OrderId)
  340. {
  341. //需要生成的物料,需要生产的数量,对应销售订单。
  342. //1.库存、在制工单检查完成后 当前BOM需要自制时 产生工单。
  343. //2.每一个销售订单行对应一个工单。
  344. if (string.IsNullOrEmpty(BomNumber) || Quantity == null)
  345. {
  346. //TODO: 参数异常 直接返回
  347. }
  348. //获取销售订单信息
  349. //var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  350. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  351. }
  352. /// <summary>
  353. /// 检查成品库存
  354. /// </summary>
  355. /// <param name="OrderId">销售订单ID</param>
  356. /// <returns></returns>
  357. public async Task<bool> CheckFinishedProductInventory(long OrderId)
  358. {
  359. //获取销售订单信息
  360. var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  361. //获取销售订单子表
  362. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  363. if (string.IsNullOrEmpty(seorderentry.bom_number))
  364. {
  365. return false;
  366. }
  367. Expression<Func<ic_item_stock, bool>> filter = x => x.icitem_id == long.Parse(seorderentry.bom_number);
  368. var ic_Item_Stocks = await _ic_item_stock.GetManyByCondition(filter);
  369. if (seorderentry.qty <= ic_Item_Stocks.Sum(x => x.sqty))
  370. {
  371. return true;
  372. }
  373. else
  374. {
  375. return false;
  376. }
  377. }
  378. /// <summary>
  379. /// 检查在制工单
  380. /// </summary>
  381. /// <param name="bomNumber">Bom编码</param>
  382. /// <param name="Quantity">需要数量</param>
  383. /// <param name="DeliverDate">交付日期</param>
  384. /// <param name="OrderId">销售订单ID</param>
  385. /// <returns></returns>
  386. public async Task<List<mes_morder>> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate, long OrderId)
  387. {
  388. if (string.IsNullOrEmpty(bomNumber) || Quantity != null)
  389. {
  390. //TODO:入参异常;
  391. throw new NotImplementedException("BOM编码或需求数量不能为空!");
  392. }
  393. //根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。
  394. //TODO:工单类型;
  395. Expression<Func<mes_morder, bool>> filter = x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭"
  396. && x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false;
  397. var morderList = await _mes_morder.GetManyByCondition(filter);
  398. //获取销售订单信息
  399. //var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  400. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  401. //获取物料详情
  402. var mysql_ic_item = await _mysql_ic_item.FindAsync(x => x.number == bomNumber);
  403. //工单已被占用后要与占用表关联查询...减去占用量后 剩下生产数量可供下个销售工单使用。
  404. Expression<Func<mes_mooccupy, bool>> mooccupyfilter = x => x.moo_state == 1 && x.IsDeleted == false;
  405. var mes_mooccupyList = await _mes_mooccupy.GetManyByCondition(mooccupyfilter);
  406. //首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。
  407. //当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。
  408. var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate &&
  409. (x.morder_production_number - x.inventory_number) > Quantity).ToList();
  410. if (morderDataList.Count > 0)
  411. {
  412. //存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。
  413. var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault();
  414. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == morder.Id).ToList();
  415. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, morder, mooccupies);
  416. await _mes_mooccupy.InsertOne(mes_Mooccupy);
  417. }
  418. else
  419. {
  420. // 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量
  421. var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList();
  422. if (morderListData.Count == 0)
  423. {
  424. //TODO:后期处理无在制工单返回内容
  425. throw new NotImplementedException("无可用在制工单!");
  426. }
  427. List<mes_mooccupy> mes_Mooccupies = new List<mes_mooccupy>();
  428. decimal? number = Quantity;
  429. foreach (var item in morderListData)
  430. {
  431. //查询出工单已占用总数量
  432. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == item.Id).ToList();
  433. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, item, mooccupies);
  434. mes_Mooccupies.Add(mes_Mooccupy);
  435. //需求数量-占用量后小于或等于0 停止循环占用工单
  436. if (number - mes_Mooccupy.moo_qty <= 0)
  437. {
  438. break;
  439. }
  440. }
  441. }
  442. return morderList;
  443. }
  444. /// <summary>
  445. /// 拼接工单占用表
  446. /// </summary>
  447. /// <param name="seorderentry">销售订单子表</param>
  448. /// <param name="mysql_ic_item">物料详情表</param>
  449. /// <param name="item">工单表</param>
  450. /// <param name="mes_mooccupy">占用工单表</param>
  451. /// <returns></returns>
  452. public mes_mooccupy GetMooccupies(crm_seorderentry seorderentry, ic_item mysql_ic_item, mes_morder item, List<mes_mooccupy> mes_mooccupy)
  453. {
  454. decimal? Sumqty = 0;
  455. if (mes_mooccupy.Count > 0)
  456. {
  457. Sumqty = mes_mooccupy.Sum(x => x.moo_qty);
  458. }
  459. //生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。
  460. mes_mooccupy mes_Mooccupy = new mes_mooccupy();
  461. mes_Mooccupy.GenerateNewId();
  462. mes_Mooccupy.moo_id_type = "分配";
  463. mes_Mooccupy.moo_id_billid = seorderentry.seorder_id;//销售订单ID
  464. mes_Mooccupy.fbill_no = seorderentry.bill_no;//销售订单号
  465. mes_Mooccupy.fentry_id = seorderentry.entry_seq.Value;//销售订单行
  466. mes_Mooccupy.fitem_name = mysql_ic_item.name;//物料名称
  467. mes_Mooccupy.fitem_number = mysql_ic_item.number;
  468. mes_Mooccupy.fmodel = mysql_ic_item.model;//规格型号
  469. mes_Mooccupy.moo_moid = item.Id;
  470. mes_Mooccupy.moo_mo = item.morder_no;
  471. //占用量=生产计划数量-入库数量-已被占用数量
  472. mes_Mooccupy.moo_qty = item.morder_production_number - item.inventory_number - Sumqty;
  473. mes_Mooccupy.moo_stime = DateTime.Now;
  474. mes_Mooccupy.moo_etime = DateTime.Now;//日期来源需确定
  475. mes_Mooccupy.moo_state = 1;
  476. mes_Mooccupy.moo_cbr = string.Empty;
  477. //mes_Mooccupy.moo_ctime = ;
  478. mes_Mooccupy.moo_creason = string.Empty;
  479. mes_Mooccupy.tenant_id = 0;//TODO:企业ID =集团 或公司ID
  480. return mes_Mooccupy;
  481. }
  482. /// <summary>
  483. /// BOM预处理
  484. /// </summary>
  485. /// <param name="orderid"></param>
  486. /// <param name="BomId"></param>
  487. /// <param name="Quantity"></param>
  488. public void BomPretreatment(long? orderid, long? BomId, int Quantity)
  489. {
  490. if (orderid == null)
  491. {
  492. //throw new bu
  493. }
  494. if (BomId == null)
  495. {
  496. //throw new bu
  497. }
  498. //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
  499. var help = new SnowFlake();
  500. var bomlist = _ic_bom.GetAll().Result;
  501. var bomchildlist = _ic_bom_child.GetAll().Result;
  502. var icitemlist = _ic_item.GetAll().Result;
  503. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  504. var dto = new BomChildExamineDto();
  505. dto.bom_id = BomId.Value;
  506. dto.level = 1;
  507. dto.id = help.NextId();
  508. dto.parent_id = help.NextId();
  509. dto.qty = 1;
  510. dto.num = "1";
  511. dto.isbom = 1;
  512. dto.is_replace = 0;
  513. dto.haveicsubs = 0;
  514. dto.substitute_code = "";
  515. dto.icitem_ids = "";
  516. int type = 0;
  517. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  518. }
  519. /// <summary>
  520. /// BOM预处理层级组装
  521. /// </summary>
  522. /// <param name="bomlist"></param>
  523. /// <param name="bomchildlist"></param>
  524. /// <param name="icitemlist"></param>
  525. /// <param name="dto"></param>
  526. /// <param name="returnlist"></param>
  527. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist, int type)
  528. {
  529. int level = dto.level++;//初始化定义level层级
  530. var help = new SnowFlake();
  531. var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
  532. var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
  533. if (bom == null || item == null)
  534. {
  535. }
  536. dto.item_id = bom.icitem_id;
  537. dto.item_name = bom.item_name;
  538. dto.item_code = bom.item_number;
  539. dto.model = item.model;
  540. dto.unit = bom.unit;
  541. dto.erp_cls = item.erp_cls;
  542. dto.erp_cls_name = item.erp_cls_name;
  543. dto.type = type;
  544. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  545. returnlist.Add(dto);
  546. var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
  547. int idx = 1;
  548. foreach (var c in childlist)
  549. {
  550. string childNum = dto.num + "." + idx.ToString();
  551. var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
  552. var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
  553. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  554. if (childBom != null)
  555. {
  556. var cdto = new BomChildExamineDto();
  557. cdto.id = help.NextId();
  558. cdto.level = level;
  559. cdto.parent_id = dto.id;
  560. cdto.bom_child_id = c.Id;
  561. cdto.qty = c.qty.Value;
  562. cdto.backflush = c.backflush;
  563. cdto.num = childNum;
  564. cdto.isbom = 1;
  565. cdto.is_replace = c.is_replace;
  566. cdto.haveicsubs = c.haveicsubs;
  567. cdto.substitute_code = c.substitute_code;
  568. cdto.icitem_ids = c.icitem_ids;
  569. cdto.type = type;
  570. //递归寻找子级
  571. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist, type);
  572. }
  573. else
  574. {
  575. if (icitem != null)
  576. {
  577. var childDto = new BomChildExamineDto();
  578. childDto.level = level++;
  579. childDto.bom_id = dto.bom_id;
  580. childDto.bom_child_id = c.Id;
  581. childDto.id = help.NextId();
  582. childDto.parent_id = dto.id;
  583. childDto.item_id = icitem.Id;
  584. childDto.item_name = icitem.name;
  585. childDto.item_code = icitem.number;
  586. childDto.num = childNum;
  587. childDto.model = icitem.model;
  588. childDto.unit = c.unit;
  589. childDto.erp_cls = item.erp_cls;
  590. childDto.erp_cls_name = item.erp_cls_name;
  591. childDto.backflush = c.backflush;
  592. childDto.qty = c.qty.Value;
  593. childDto.isbom = 0;
  594. childDto.is_replace = c.is_replace;
  595. childDto.haveicsubs = c.haveicsubs;
  596. childDto.substitute_code = c.substitute_code;
  597. childDto.icitem_ids = c.icitem_ids;
  598. childDto.type = type;
  599. returnlist.Add(childDto);
  600. }
  601. }
  602. idx++;
  603. }
  604. }
  605. /// <summary>
  606. /// BOM替代关系预处理
  607. /// </summary>
  608. public void BomSubstitute(List<BomChildExamineDto> returnlist, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist)
  609. {
  610. var sublist = _ic_substitute.GetManyByCondition(s => s.substitute_code.IsIn(returnlist.Select(c => c.substitute_code))).Result.ToList();
  611. var suballlist = _ic_substitute_all.GetManyByCondition(s => s.substitute_id.IsIn(sublist.Select(c => c.Id))).Result.ToList();
  612. var subdtllist = _ic_substitute_all_dtl.GetManyByCondition(s => s.substitute_allid.IsIn(suballlist.Select(c => c.Id))).Result.ToList();
  613. List<long> childidList = new List<long>();
  614. var help = new SnowFlake();
  615. int type = 1;
  616. //除顶级外,其他层级关系全带出来。生成平铺
  617. foreach (var item in returnlist)
  618. {
  619. //最顶级、虚拟件
  620. if (item.level != 1 && item.erp_cls != 4 && !childidList.Contains(item.bom_child_id.GetValueOrDefault()))
  621. {
  622. //有替代关系
  623. if (item.haveicsubs == 1)
  624. {
  625. if (!string.IsNullOrEmpty(item.icitem_ids))
  626. {
  627. long cid = 1;
  628. var cids = item.icitem_ids.Split(',');
  629. foreach (var c in cids)
  630. {
  631. if (long.TryParse(c, out cid))
  632. {
  633. childidList.Add(cid);
  634. }
  635. }
  636. }
  637. //找到当前物料的替代群组关系集
  638. var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
  639. if (sl != null)
  640. {
  641. var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
  642. foreach (var sal in sall)
  643. {
  644. var sadl = subdtllist.Where(s => s.substitute_allid == sal.Id).ToList();
  645. foreach (var dtl in sadl)
  646. {
  647. if (dtl.ismain == 0)
  648. {
  649. //递归将替代关系组装出来。
  650. SubstitutePretreatment(sl, sal, dtl, item, returnlist, icitemlist, bomlist, bomchildlist, type);
  651. }
  652. }
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. /// <summary>
  660. /// 替代关系递归组装出来
  661. /// </summary>
  662. /// <param name="sal"></param>
  663. /// <param name="dtl"></param>
  664. /// <param name="toDto"></param>
  665. /// <param name="returnlist"></param>
  666. /// <param name="icitemlist"></param>
  667. /// <param name="bomlist"></param>
  668. /// <param name="bomchildlist"></param>
  669. 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)
  670. {
  671. //循环生成dtl层级dto,明细
  672. //如果dtl对应的icitem是BOM,还需要向下继续展开。
  673. var help = new SnowFlake();
  674. //List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  675. var dto = new BomChildExamineDto();
  676. var bom = bomlist.WhereIf(true, s => s.icitem_id == dtl.icitem_id).FirstOrDefault();
  677. var icitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
  678. if (icitem == null)
  679. {
  680. return;
  681. }
  682. dto.id = help.NextId();
  683. dto.level = toDto.level;
  684. dto.parent_id = toDto.parent_id;
  685. dto.item_id = icitem.Id;
  686. dto.item_name = icitem.name;
  687. dto.item_code = icitem.number;
  688. dto.num = toDto.num;
  689. dto.model = icitem.model;
  690. dto.unit = icitem.unit;
  691. dto.erp_cls = icitem.erp_cls;
  692. dto.erp_cls_name = icitem.erp_cls_name;
  693. dto.backflush = toDto.backflush;
  694. dto.qty = toDto.qty;
  695. dto.replace_amount = dtl.replace_amount.Value;
  696. dto.is_replace = 0;
  697. dto.haveicsubs = 0;
  698. dto.substitute_code = "";
  699. dto.icitem_ids = "";
  700. dto.substitute_strategy = sl.substitute_strategy.Value;//替代策略
  701. dto.substitute_mode = sl.substitute_mode.Value;//替代方式
  702. dto.type = type;
  703. dto.substitute_all_num = sal.order_num;//群组优先级
  704. if (bom != null)
  705. {
  706. dto.bom_id = bom.Id;
  707. dto.qty = dtl.replace_amount.Value;
  708. dto.isbom = 1;
  709. dto.is_replace = 0;
  710. dto.haveicsubs = 0;
  711. dto.substitute_code = "";
  712. dto.icitem_ids = "";
  713. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  714. }
  715. else
  716. {
  717. dto.bom_id = null;
  718. dto.isbom = 0;
  719. returnlist.Add(dto);
  720. }
  721. }
  722. /// <summary>
  723. /// 计算物料库存量
  724. /// </summary>
  725. /// <param name="returnlist"></param>
  726. public void BomStock(List<BomChildExamineDto> returnlist, long bangid, long orderid, long orderentryid, long factoryid)
  727. {
  728. returnlist = returnlist.OrderBy(s => s.num).ToList();
  729. //取物料库存记录
  730. FilterDefinition<ic_item_stock> filter = Builders<ic_item_stock>.Filter.In(s => s.icitem_id, returnlist.Select(c => c.item_id).ToList());
  731. var stocklist = _ic_item_stock.GetManyByIds(filter).Result;
  732. //取当前订单的物料库存占用记录
  733. var occupylist = _ic_item_stockoccupy.GetManyByCondition(p => p.bang_id == bangid && p.order_id == orderid).Result;
  734. //计算剩余库存
  735. foreach (var item in returnlist)
  736. {
  737. if (item.erp_cls == 4)//虚拟件不计算
  738. {
  739. continue;
  740. }
  741. //非虚拟件
  742. //当前物料的库存数量
  743. decimal stockQty = stocklist.Where(s => s.icitem_id == item.item_id).Sum(p => p.sqty.GetValueOrDefault());
  744. //获取当前订单其他订单行当前物料的占用数量
  745. decimal otherStockQty = occupylist.Where(s => s.icitem_id == item.item_id).Sum(p => p.quantity);
  746. //当前订单行物料库存情况
  747. item.sqty = stockQty - otherStockQty;
  748. }
  749. }
  750. /// <summary>
  751. /// 替代关系检查计算
  752. /// </summary>
  753. public void CalcIcitemSubstitute(List<BomChildExamineDto> returnlist,int count)
  754. {
  755. returnlist = returnlist.OrderBy(s => s.num).ToList();
  756. //1.如果主料够的时候,不需要显示替代料的平铺视图,如果主料不够,显示替代料的平铺视图。
  757. //2.替代策略和替代方式,影响到的是甲乙组概念,替代按主料有限,取代按组的优先级。A与B的替代,则A和B各自会存在一个组。
  758. List<long> calcIds = new List<long>();
  759. //先处理下最顶级的产品需要数量
  760. returnlist[0].needCount = returnlist[0].qty * count;
  761. returnlist[0].satisfyNum = returnlist[0].sqty;
  762. returnlist[0].lack_qty = returnlist[0].needCount - returnlist[0].satisfyNum;
  763. returnlist[0].is_show = true;
  764. foreach (var item in returnlist)
  765. {
  766. //循环平铺整个资源检查的物料库存情况、缺料情况
  767. CaclMaterialShortage(returnlist,item, count);
  768. }
  769. foreach (var item in returnlist)
  770. {
  771. //替代件不计算,替代件通过标准件的替代关系,去计算需要使用哪些物料
  772. if (item.type == 1)
  773. {
  774. continue;
  775. }
  776. CaclBomChildUseShortage(returnlist, item);
  777. }
  778. }
  779. /// <summary>
  780. /// 物料计算
  781. /// </summary>
  782. /// <param name="returnlist"></param>
  783. /// <param name="item"></param>
  784. /// <param name="count"></param>
  785. public void CaclBomChildUseShortage(List<BomChildExamineDto> returnlist, BomChildExamineDto item)
  786. {
  787. //判断是否是BOM,如果是BOM,还需要向下展开
  788. var bomchild = returnlist.Where(s => s.parent_id == item.id && s.type == 0).ToList();
  789. if (bomchild.Count > 0)
  790. {
  791. foreach (var child in bomchild)
  792. {
  793. CaclBomChildUseShortage(returnlist, item);
  794. }
  795. }
  796. var tolist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num).ToList();
  797. //有替代关系
  798. if (item.haveicsubs == 1)
  799. {
  800. //首先判断标准件库存是否满足。
  801. //如果是BOM,也需要向下展开,看子物料是否满足。
  802. //不满足的情况下,则需要展开替代关系,根据替代策略和替代方式,来判定替代件的库存。
  803. //假设子BOM有替代关系,则子BOM的子物料,也是有替代关系存在的,需要展开来看。
  804. //1.假设标准件库存满足,则不计算替代件关系。
  805. //2.如果标准件不满足,则替代件开始计算并展示。
  806. //注意:不管是否需要替代件,标准件都需要展开。
  807. var sublist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num && s.level == item.level).OrderBy(c => c.substitute_all_num).ToList();
  808. //sublist找出当前含替代关系的标准件和替代件。
  809. if (item.substitute_mode == 0)
  810. {
  811. //替代
  812. }
  813. else {
  814. //取代
  815. }
  816. }
  817. else
  818. {
  819. //无替代关系
  820. if (item.stock_state==0)
  821. {
  822. item.use_qty = item.needCount;
  823. item.is_show = true;
  824. item.stock_state = 0;
  825. }
  826. else if (item.stock_state == 1)
  827. {
  828. // 缺料数量 item.lack_qty
  829. // 使用数量将库存全部使用了 item.use_qty = item.sqty;
  830. if (item.erp_cls == 2 || item.erp_cls == 3)
  831. {
  832. //委外、外购
  833. //生成委外工单、采购申请单
  834. //得到单据到货时间。
  835. //todo:初步设置为7天到货期,后期根据实际业务来补充修改。
  836. item.kitting_time = DateTime.Now.AddDays(7);
  837. }
  838. else if (item.erp_cls == 1)
  839. {
  840. //自制
  841. //调用产能计算,得到物料自制后的齐套时间。
  842. //todo:初步设置为7天完成,等沟通调用方法,来修改此处。
  843. item.kitting_time = DateTime.Now.AddDays(7);
  844. }
  845. }
  846. }
  847. }
  848. /// <summary>
  849. /// 平铺物料缺料情况,展示所有主料+替代料的库存情况、缺料情况
  850. /// </summary>
  851. /// <param name="returnlist"></param>
  852. /// <param name="item"></param>
  853. /// <param name="count"></param>
  854. public void CaclMaterialShortage(List<BomChildExamineDto> returnlist, BomChildExamineDto item, int count)
  855. {
  856. var parent = returnlist.Find(s => s.id == item.parent_id);
  857. //当前物料总共需要数量
  858. item.needCount = parent.needCount * item.qty;
  859. item.satisfyNum = item.sqty;//5
  860. item.lack_qty = item.needCount - item.satisfyNum;
  861. //如果不满足,计算子物料,或者计算替代料
  862. if (item.lack_qty < 0)
  863. {
  864. //库存满足
  865. item.stock_state = 0;
  866. item.lack_qty = 0;
  867. }
  868. else
  869. {
  870. //找出自己的子集,存在子集则是BOM,不存在子集,则自己非BOM。
  871. var childList = returnlist.Where(s => s.parent_id == item.id && s.type == item.type).ToList();
  872. if (childList.Count > 0)
  873. {
  874. //自己是BOM
  875. foreach (var child in childList)
  876. {
  877. //循环子集判断库存
  878. CaclMaterialShortage(returnlist, child, count);
  879. }
  880. }
  881. else
  882. {
  883. item.stock_state = 1;
  884. }
  885. }
  886. }
  887. }
  888. }