ResourceExamineAppService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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.Design;
  30. using Volo.Abp.Validation.StringValues;
  31. using System.Runtime.CompilerServices;
  32. using MongoDB.Driver;
  33. namespace Business.ResourceExamineManagement
  34. {
  35. /// <summary>
  36. /// 资源检查
  37. /// </summary>
  38. //[Authorize(BusinessPermissions.ResourceExamine.Default)]
  39. public class ResourceExamineAppService : ApplicationService, IResourceExamineAppService
  40. {
  41. #region 服务
  42. /// <summary>
  43. /// 工艺路径
  44. /// </summary>
  45. private readonly IMongoDB<mes_technique> _mes_technique;
  46. /// <summary>
  47. /// 工序
  48. /// </summary>
  49. private readonly IMongoDB<mes_process> _mes_process;
  50. /// <summary>
  51. /// 工艺关联工序
  52. /// </summary>
  53. private readonly IMongoDB<mes_tech_process> _mes_tech_process;
  54. /// <summary>
  55. /// 工作日历
  56. /// </summary>
  57. private readonly IMongoDB<mes_work_calendar> _mes_work_calendar;
  58. /// <summary>
  59. /// 工作日历明细
  60. /// </summary>
  61. private readonly IMongoDB<mes_work_calendar_list> _mes_work_calendar_list;
  62. /// <summary>
  63. /// 工艺工序关联工位
  64. /// </summary>
  65. private readonly IMongoDB<mes_tech_proc_workshop> _mes_tech_proc_workshop;
  66. /// <summary>
  67. /// 排程占用记录
  68. /// </summary>
  69. private readonly IMongoDB<mes_schedule_occupy> _mes_schedule_occupy;
  70. /// <summary>
  71. /// 物料详情
  72. /// </summary>
  73. private readonly IMongoDB<ic_item> _ic_item;
  74. private IBasicRepository<ic_item, long> _mysql_ic_item;
  75. /// <summary>
  76. /// 物料BOM
  77. /// </summary>
  78. private readonly IMongoDB<ic_bom> _ic_bom;
  79. private IBasicRepository<ic_bom, long> _mysql_ic_bom;
  80. /// <summary>
  81. /// 物料BOM明细
  82. /// </summary>
  83. private readonly IMongoDB<ic_bom_child> _ic_bom_child;
  84. private IBasicRepository<ic_bom_child, long> _mysql_ic_bom_child;
  85. /// <summary>
  86. /// 物料库存表
  87. /// </summary>
  88. private readonly IMongoDB<ic_item_stock> _ic_item_stock;
  89. /// <summary>
  90. /// 生产工单主表
  91. /// </summary>
  92. private readonly IMongoDB<mes_morder> _mes_morder;
  93. #endregion
  94. /// <summary>
  95. /// 构造函数
  96. /// </summary>
  97. /// <param name="icitem"></param>
  98. /// <param name="icbom"></param>
  99. public ResourceExamineAppService(
  100. IMongoDB<mes_technique> mes_technique,
  101. IMongoDB<mes_process> mes_process,
  102. IMongoDB<mes_tech_process> mes_tech_process,
  103. IMongoDB<mes_tech_proc_workshop> mes_tech_proc_workshop,
  104. IMongoDB<ic_item> ic_item,
  105. IMongoDB<ic_bom> ic_bom,
  106. IMongoDB<ic_bom_child> ic_bom_child,
  107. IMongoDB<ic_item_stock> ic_item_stock,
  108. IMongoDB<mes_morder> mes_morder,
  109. IBasicRepository<ic_item, long> mysql_ic_item,
  110. IBasicRepository<ic_bom, long> mysql_ic_bom,
  111. IBasicRepository<ic_bom_child, long> mysql_ic_bom_child
  112. )
  113. {
  114. _mes_technique = mes_technique;
  115. _mes_process = mes_process;
  116. _mes_tech_process = mes_tech_process;
  117. _mes_tech_proc_workshop = mes_tech_proc_workshop;
  118. _ic_item = ic_item;
  119. _ic_bom = ic_bom;
  120. _ic_bom_child = ic_bom_child;
  121. _ic_item_stock = ic_item_stock;
  122. _mes_morder = mes_morder;
  123. _mysql_ic_item = mysql_ic_item;
  124. _mysql_ic_bom = mysql_ic_bom;
  125. _mysql_ic_bom_child = mysql_ic_bom_child;
  126. }
  127. /// <summary>
  128. /// mongoDB示例方法,后期删除
  129. /// </summary>
  130. /// <returns></returns>
  131. public async Task test()
  132. {
  133. //多条插入
  134. List<mes_technique> infos = new List<mes_technique>();
  135. mes_technique info;
  136. for (int i = 0; i < 3; i++)
  137. {
  138. info = new mes_technique();
  139. info.GenerateNewId();
  140. info.tech_name = "多条" + i;
  141. info.level = i;
  142. infos.Add(info);
  143. }
  144. await _mes_technique.InsertMany(infos);
  145. var info2 = _mes_technique.GetAll().Result;
  146. var a = new PschedDto();
  147. a.count = info2.Count;
  148. //获取数据
  149. var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
  150. //更新数据
  151. info1.tech_name = "更新***";
  152. var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
  153. //根据条件查询数据
  154. Expression<Func<mes_technique, bool>> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
  155. var info3 = await _mes_technique.GetManyByCondition(filter);
  156. }
  157. /// <summary>
  158. /// 资源检查
  159. /// </summary>
  160. /// <param name="input"></param>
  161. /// <returns></returns>
  162. /// <exception cref="NotImplementedException"></exception>
  163. public async Task<PschedDto> ReceiveResult(SeorderentryDto input)
  164. {
  165. //测试代码
  166. //await test();
  167. await ProductiveExamine(1733221167209762816,100,1000);
  168. return null;
  169. throw new NotImplementedException();
  170. }
  171. /// <summary>
  172. /// 产能计算
  173. /// </summary>
  174. /// <param name="tech_id">工艺路径主键</param>
  175. /// <param name="packages">需要生产产品件数</param>
  176. /// <param name="quantity">需要生产数量</param>
  177. public async Task<DateTime> ProductiveExamine(long tech_id, int packages, int quantity)
  178. {
  179. if (packages <=0 ||quantity <= 0)
  180. {
  181. throw new NotImplementedException("产能计算参数有误!");
  182. }
  183. #region 1、数据准备
  184. //1.1、获取工艺路径数据
  185. mes_technique tech = _mes_technique.GetOneByID(tech_id).Result;
  186. //1.2、获取工艺路径关联工序数据
  187. List<mes_tech_process> tech_Processes = _mes_tech_process.GetManyByCondition(x => x.tech_id == tech_id).Result;
  188. //1.3、获取当前工艺路径下的工序数据
  189. FilterDefinition<mes_process> filter = Builders<mes_process>.Filter.In(s => s.Id, tech_Processes.Select(m => m.proc_id).ToList());
  190. List<mes_process> process = _mes_process.GetManyByIds(filter).Result;
  191. //1.3、获取工艺工序关联工位信息
  192. 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());
  193. List<mes_tech_proc_workshop> tech_Proc_Workshops = _mes_tech_proc_workshop.GetManyByIds(filter1).Result;
  194. //1.4、获取工位占用情况
  195. //List<mes_schedule_occupy> schedule_Occupies = _mes_schedule_occupy.GetManyByCondition().Result;
  196. #endregion
  197. #region 计算产能,得到耗时
  198. /*产能计算有两种模式:1-UPH;2-节拍时间
  199. *1、根据工艺路径设置的效率计算层级,来获取参与产能计算的所有工序/子工序
  200. *2、根据当前工序设置的计算模式UPH/节拍时间来计算当前工序生产所需时间T1
  201. *3、当前工序生产所需时间T1*效率系数Rate得到实际的生产所需时间T2(流水线不需要*效率系数)
  202. *4、当前工序实际生产所需时间T2+当前工序前置准备时间得到当前工序生产所需总时间
  203. *5、所有工序生产总时间加起来得到生产所需总时间
  204. */
  205. //1、获取参与计算的所有工序/子工序
  206. var tech_pro_list = tech_Processes.Where(p => p.level == tech.level).ToList();
  207. //生产总时间(分钟)
  208. decimal sumTimes = 0.00m;
  209. //记录按照CT参与计算的工序的父级Id
  210. List<long> exists = new List<long>();
  211. foreach (var item in tech_pro_list)
  212. {
  213. //数据校验
  214. //获取当前工序
  215. var curProcess = process.FirstOrDefault(p => p.Id == item.proc_id);
  216. if (item.type == 1 && item.ct == 0)
  217. {
  218. throw new NotImplementedException(string.Format("工艺[{0}]下的工序[{1}]节拍时间设置有误,请调整!",tech.tech_name, curProcess.proc_name));
  219. }
  220. if (item.type == 2 && item.uph == 0)
  221. {
  222. throw new NotImplementedException(string.Format("工艺[{0}]下的工序[{1}]UPH设置有误,请调整!", tech.tech_name, curProcess.proc_name));
  223. }
  224. if (item.parentprocid != null)//存在父子级关系
  225. {
  226. //判断当前父级是否已经参与过计算
  227. if (exists.Contains(item.parentprocid.Value))
  228. {
  229. continue;
  230. }
  231. //记录父级id
  232. exists.Add(item.parentprocid.Value);
  233. //获取同一父级下的子工序
  234. var childs = tech_Processes.Where(p => p.parentprocid == item.parentprocid).ToList();
  235. decimal maxTime = 0.00m;//记录同一父级下最大耗时
  236. decimal time = 0.00m;
  237. //2、两种计算模式UPH/节拍时间
  238. if (item.type == 2)//UPH(每小时生产个数),取同一父级下最长耗时
  239. {
  240. foreach (var chd in childs)
  241. {
  242. //当前工序消耗时间
  243. time = quantity / item.uph.Value * 60 * item.effect_ratio.Value;
  244. if (time > maxTime)
  245. {
  246. maxTime = time;
  247. }
  248. }
  249. sumTimes += maxTime;
  250. continue;
  251. }
  252. //3、根据节拍时间计算
  253. //sum(CT)
  254. decimal sumCT = childs.Sum(p => p.ct.Value);
  255. //max(CT)
  256. decimal maxCT = childs.OrderByDescending(p => p.ct).FirstOrDefault().ct.Value;
  257. //sum(LT)
  258. decimal sumLT = childs.Sum(p => p.readytime.Value);
  259. //当前流水线消耗时间
  260. sumTimes += (packages - 1) * maxCT + sumCT + sumLT;
  261. }
  262. else{//不存在父子级关系
  263. //2、两种计算模式UPH/节拍时间
  264. if (item.type == 2)//uph
  265. {
  266. //当前工序消耗时间
  267. sumTimes += quantity / item.uph.Value * 60 * item.effect_ratio.Value + item.readytime.Value;
  268. //计算完成,进行下一次循环
  269. continue;
  270. }
  271. //节拍时间:流水线,从流水线最后工序开始计算
  272. if (item.nextprocid != null)
  273. {
  274. //下-工序Id不为null,说明不是流水线最终工序,进行下一次循环
  275. continue;
  276. }
  277. List<mes_tech_process> allProcess = new List<mes_tech_process>();
  278. //递归,获取最终工序的上一工序
  279. GetPreProcess(tech_Processes, item, allProcess);
  280. //sum(CT)
  281. decimal sumCT = allProcess.Sum(p => p.ct.Value);
  282. //max(CT)
  283. decimal maxCT = allProcess.OrderByDescending(p => p.ct).FirstOrDefault().ct.Value;
  284. //sum(LT)
  285. decimal sumLT = allProcess.Sum(p => p.readytime.Value);
  286. //当前流水线消耗时间
  287. sumTimes += (packages - 1) * maxCT + sumCT + sumLT;
  288. }
  289. }
  290. #endregion
  291. return DateTime.Now.AddDays(1).AddMinutes((double)sumTimes);
  292. }
  293. /// <summary>
  294. /// 递归获取工序
  295. /// </summary>
  296. /// <param name="tech_Processes"></param>
  297. /// <param name="item"></param>
  298. /// <returns></returns>
  299. private void GetPreProcess(List<mes_tech_process> tech_Processes, mes_tech_process item, List<mes_tech_process> allProcess)
  300. {
  301. allProcess.Add(item);
  302. //获取上一工序
  303. var preProc = tech_Processes.FirstOrDefault(p => p.nextprocid == item.proc_id);
  304. if (preProc != null) {
  305. GetPreProcess(tech_Processes, preProc, allProcess);
  306. }
  307. }
  308. /// <summary>
  309. /// 检查在制工单
  310. /// </summary>
  311. /// <param name="bomNumber">Bom编码</param>
  312. /// <param name="Quantity">需要数量</param>
  313. /// <param name="EndDate">交付日期</param>
  314. /// <returns></returns>
  315. public async Task<List<mes_morder>> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate)
  316. {
  317. if (string.IsNullOrEmpty(bomNumber) || Quantity != null)
  318. {
  319. //TODO:入参异常;
  320. throw new NotImplementedException("BOM编码或需求数量不能为空!");
  321. }
  322. //根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。
  323. //TODO:工单类型;
  324. Expression<Func<mes_morder, bool>> filter = x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭"
  325. && x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false;
  326. var morderList = await _mes_morder.GetManyByCondition(filter);
  327. //首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。
  328. //当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。
  329. var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate &&
  330. (x.morder_production_number - x.inventory_number) > Quantity).ToList();
  331. if (morderDataList.Count > 0)
  332. {
  333. //存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。
  334. var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault();
  335. //生成mes_mooccupy工单占用表数据,代表此工单被某个销售订单已占用。
  336. }
  337. else
  338. {
  339. // 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量
  340. var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList();
  341. if (morderListData.Count == 0)
  342. {
  343. //TODO:后期处理无在制工单返回内容
  344. throw new NotImplementedException("无可用在制工单!");
  345. }
  346. List<mes_morder> mes_Morders = new List<mes_morder>();
  347. decimal? number = Quantity;
  348. foreach (var item in morderListData)
  349. {
  350. number = number - (item.morder_production_number - item.inventory_number);
  351. if (number < 0)
  352. {
  353. mes_Morders.Add(item);
  354. }
  355. else
  356. {
  357. break;
  358. }
  359. }
  360. //生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。
  361. }
  362. return morderList;
  363. #region 注释
  364. /* var morderList = _mes_morder.GetAll().Result;
  365. //查询生产工单类型为计划工单并且工单类型为备库工单 并且BOM编码一致数据,订单状态为 初始或下达工单
  366. var morderTypeList = morderList.Where(x => x.work_order_type == "备库工单" && x.morder_type == "计划工单" &&
  367. x.bom_number == bomNumber && (x.morder_state == "初始" || x.morder_state == "下达")).ToList();
  368. //计划数量-需求数量=剩余可用数量 -->使用计划结束时间排序取第一条满足条件工单
  369. // List =>数量满足Quantity && 生成工单结束日期最早
  370. var SatisfyQuantityList = morderTypeList.Where(x => (x.morder_production_number.Value - x.need_number.Value) >= Quantity).
  371. ToList().OrderBy(x => x.moentry_etime).FirstOrDefault();
  372. if (SatisfyQuantityList == null)
  373. {
  374. }
  375. else
  376. {
  377. }*/
  378. #endregion
  379. }
  380. public void BomPretreatment(long? orderid, long? BomId, int Quantity)
  381. {
  382. if (orderid == null)
  383. {
  384. //throw new bu
  385. }
  386. if (BomId == null)
  387. {
  388. //throw new bu
  389. }
  390. //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
  391. var help = new SnowFlake();
  392. var bomlist = _ic_bom.GetAll().Result;
  393. var bomchildlist = _ic_bom_child.GetAll().Result;
  394. var icitemlist = _ic_item.GetAll().Result;
  395. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  396. var dto = new BomChildExamineDto();
  397. dto.bom_id = BomId.Value;
  398. dto.level = 1;
  399. dto.id = help.NextId();
  400. dto.parent_id = help.NextId();
  401. dto.qty = 1;
  402. dto.num = "1";
  403. dto.isbom = 1;
  404. dto.is_replace = 0;
  405. dto.haveicsubs = 0;
  406. dto.substitute_code = "";
  407. dto.icitem_ids = "";
  408. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist);
  409. }
  410. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist)
  411. {
  412. int level = dto.level++;//初始化定义level层级
  413. var help = new SnowFlake();
  414. var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
  415. var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
  416. if (bom == null || item == null)
  417. {
  418. }
  419. //var dto = new BomChildExamineDto();
  420. //dto.level = level;
  421. //dto.bom_id = bomId;
  422. //dto.id = help.NextId();
  423. //dto.parent_id = parent_id;
  424. dto.item_id = bom.icitem_id;
  425. dto.item_name = bom.item_name;
  426. dto.item_code = bom.item_number;
  427. dto.model = item.model;
  428. dto.unit = bom.unit;
  429. dto.erp_cls = item.erp_cls;
  430. dto.erp_cls_name = item.erp_cls_name;
  431. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  432. returnlist.Add(dto);
  433. var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
  434. int idx = 1;
  435. foreach (var c in childlist)
  436. {
  437. string childNum = dto.num + "." + idx.ToString();
  438. var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
  439. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  440. if (c.is_bom == 1)
  441. {
  442. var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
  443. if (childBom != null)
  444. {
  445. var cdto = new BomChildExamineDto();
  446. cdto.id = help.NextId();
  447. cdto.level = level;
  448. cdto.parent_id = dto.id;
  449. cdto.qty = c.qty.Value;
  450. cdto.backflush = c.backflush;
  451. cdto.num = childNum;
  452. cdto.isbom = 1;
  453. cdto.is_replace = c.is_replace;
  454. cdto.haveicsubs = c.haveicsubs;
  455. cdto.substitute_code = c.substitute_code;
  456. cdto.icitem_ids = c.icitem_ids;
  457. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist);
  458. }
  459. }
  460. else
  461. {
  462. if (icitem != null)
  463. {
  464. var childDto = new BomChildExamineDto();
  465. childDto.level = level++;
  466. childDto.bom_id = dto.bom_id;
  467. childDto.id = help.NextId();
  468. childDto.parent_id = dto.id;
  469. childDto.item_id = icitem.Id;
  470. childDto.item_name = icitem.name;
  471. childDto.item_code = icitem.number;
  472. childDto.num = childNum;
  473. childDto.model = icitem.model;
  474. childDto.unit = c.unit;
  475. childDto.erp_cls = item.erp_cls;
  476. childDto.erp_cls_name = item.erp_cls_name;
  477. childDto.backflush = c.backflush;
  478. childDto.qty = c.qty.Value;
  479. childDto.isbom = 0;
  480. childDto.is_replace = c.is_replace;
  481. childDto.haveicsubs = c.haveicsubs;
  482. childDto.substitute_code = c.substitute_code;
  483. childDto.icitem_ids = c.icitem_ids;
  484. returnlist.Add(childDto);
  485. }
  486. }
  487. idx++;
  488. }
  489. }
  490. /// <summary>
  491. /// 计算库存量
  492. /// </summary>
  493. /// <param name="returnlist"></param>
  494. public void BomStock(List<BomChildExamineDto> returnlist)
  495. {
  496. returnlist = returnlist.OrderBy(s => s.num).ToList();
  497. List<long> idlist = new List<long>();
  498. foreach (var item in returnlist)
  499. {
  500. }
  501. }
  502. }
  503. }