ResourceExamineAppService.cs 19 KB

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