ResourceExamineAppService.cs 23 KB

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