ResourceExamineAppService.cs 41 KB

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