ResourceExamineAppService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. 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_tech_process> _mes_tech_process;
  50. /// <summary>
  51. /// 工作日历
  52. /// </summary>
  53. private readonly IMongoDB<mes_work_calendar> _mes_work_calendar;
  54. /// <summary>
  55. /// 工作日历明细
  56. /// </summary>
  57. private readonly IMongoDB<mes_work_calendar_list> _mes_work_calendar_list;
  58. /// <summary>
  59. /// 工艺工序关联工位
  60. /// </summary>
  61. private readonly IMongoDB<mes_tech_proc_workshop> _mes_tech_proc_workshop;
  62. /// <summary>
  63. /// 排程占用记录
  64. /// </summary>
  65. private readonly IMongoDB<mes_schedule_occupy> _mes_schedule_occupy;
  66. /// <summary>
  67. /// 物料详情
  68. /// </summary>
  69. private readonly IMongoDB<ic_item> _ic_item;
  70. private IBasicRepository<ic_item, long> _mysql_ic_item;
  71. /// <summary>
  72. /// 物料BOM
  73. /// </summary>
  74. private readonly IMongoDB<ic_bom> _ic_bom;
  75. private IBasicRepository<ic_bom, long> _mysql_ic_bom;
  76. /// <summary>
  77. /// 物料BOM明细
  78. /// </summary>
  79. private readonly IMongoDB<ic_bom_child> _ic_bom_child;
  80. private IBasicRepository<ic_bom_child, long> _mysql_ic_bom_child;
  81. /// <summary>
  82. /// 物料库存表
  83. /// </summary>
  84. private readonly IMongoDB<ic_item_stock> _ic_item_stock;
  85. /// <summary>
  86. /// 物料质检表
  87. /// </summary>
  88. private readonly IMongoDB<ic_check> _ic_check;
  89. /// <summary>
  90. /// 替代群组
  91. /// </summary>
  92. private readonly IMongoDB<ic_substitute> _ic_substitute;
  93. /// <summary>
  94. /// 替代群组
  95. /// </summary>
  96. private readonly IMongoDB<ic_substitute_all> _ic_substitute_all;
  97. /// <summary>
  98. /// 替代群组
  99. /// </summary>
  100. private readonly IMongoDB<ic_substitute_all_dtl> _ic_substitute_all_dtl;
  101. /// <summary>
  102. /// 生产工单主表
  103. /// </summary>
  104. private readonly IMongoDB<mes_morder> _mes_morder;
  105. #endregion
  106. /// <summary>
  107. /// 构造函数
  108. /// </summary>
  109. /// <param name="icitem"></param>
  110. /// <param name="icbom"></param>
  111. public ResourceExamineAppService(
  112. IMongoDB<mes_technique> mes_technique,
  113. IMongoDB<ic_item> ic_item,
  114. IMongoDB<ic_bom> ic_bom,
  115. IMongoDB<ic_bom_child> ic_bom_child,
  116. IMongoDB<ic_item_stock> ic_item_stock,
  117. IMongoDB<ic_check> ic_check,
  118. IMongoDB<ic_substitute> ic_substitute,
  119. IMongoDB<ic_substitute_all> ic_substitute_all,
  120. IMongoDB<ic_substitute_all_dtl> ic_substitute_all_dtl,
  121. IMongoDB<mes_morder> mes_morder,
  122. IBasicRepository<ic_item, long> mysql_ic_item,
  123. IBasicRepository<ic_bom, long> mysql_ic_bom,
  124. IBasicRepository<ic_bom_child, long> mysql_ic_bom_child
  125. )
  126. {
  127. _mes_technique = mes_technique;
  128. _ic_item = ic_item;
  129. _ic_bom = ic_bom;
  130. _ic_bom_child = ic_bom_child;
  131. _ic_item_stock = ic_item_stock;
  132. _ic_check = ic_check;
  133. _ic_substitute = ic_substitute;
  134. _ic_substitute_all = ic_substitute_all;
  135. _ic_substitute_all_dtl = ic_substitute_all_dtl;
  136. _mes_morder = mes_morder;
  137. _mysql_ic_item = mysql_ic_item;
  138. _mysql_ic_bom = mysql_ic_bom;
  139. _mysql_ic_bom_child = mysql_ic_bom_child;
  140. }
  141. /// <summary>
  142. /// mongoDB示例方法,后期删除
  143. /// </summary>
  144. /// <returns></returns>
  145. public async Task test()
  146. {
  147. //多条插入
  148. List<mes_technique> infos = new List<mes_technique>();
  149. mes_technique info;
  150. for (int i = 0; i < 3; i++)
  151. {
  152. info = new mes_technique();
  153. info.GenerateNewId();
  154. info.tech_name = "多条" + i;
  155. info.level = i;
  156. infos.Add(info);
  157. }
  158. await _mes_technique.InsertMany(infos);
  159. var info2 = _mes_technique.GetAll().Result;
  160. var a = new PschedDto();
  161. a.count = info2.Count;
  162. //获取数据
  163. var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
  164. //更新数据
  165. info1.tech_name = "更新***";
  166. var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
  167. //根据条件查询数据
  168. Expression<Func<mes_technique, bool>> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
  169. var info3 = await _mes_technique.GetManyByCondition(filter);
  170. }
  171. /// <summary>
  172. /// 资源检查
  173. /// </summary>
  174. /// <param name="input"></param>
  175. /// <returns></returns>
  176. /// <exception cref="NotImplementedException"></exception>
  177. public async Task<PschedDto> ReceiveResult(SeorderentryDto input)
  178. {
  179. return null;
  180. throw new NotImplementedException();
  181. }
  182. /// <summary>
  183. /// 产能计算
  184. /// </summary>
  185. /// <param name="BomId">Bom主键</param>
  186. /// <param name="Quantity">需要数量</param>
  187. public async Task ProductiveExamine(long? BomId, int Quantity)
  188. {
  189. }
  190. /// <summary>
  191. /// 检查在制工单
  192. /// </summary>
  193. /// <param name="bomNumber">Bom编码</param>
  194. /// <param name="Quantity">需要数量</param>
  195. /// <returns></returns>
  196. public async Task CheckMorder(string bomNumber, decimal? Quantity, DateTime EndDate)
  197. {
  198. if (string.IsNullOrEmpty(bomNumber) || Quantity < 0)
  199. {
  200. //TODO:入参异常;
  201. //throw new NotImplementedException();
  202. }
  203. var morderList = _mes_morder.GetAll().Result;
  204. var morderTypeList = morderList.Where(x => x.work_order_type == "备库工单" && x.morder_type == "计划工单" && x.bom_number == bomNumber && (x.morder_state == "初始" || x.morder_state == "下达")).ToList();
  205. //查询生产工单类型为计划工单并且工单类型为备库工单 并且BOM编码一致数据
  206. // 订单状态为 初始或下达工单
  207. var SatisfyQuantityList = morderTypeList.Where(x => x.morder_production_number >= Quantity).
  208. ToList().OrderBy(x => x.moentry_sys_etime).FirstOrDefault();
  209. if (SatisfyQuantityList == null)
  210. {
  211. }
  212. else
  213. {
  214. }
  215. // List =>数量满足Quantity && 生成工单结束日期最早
  216. }
  217. /// <summary>
  218. /// BOM预处理
  219. /// </summary>
  220. /// <param name="orderid"></param>
  221. /// <param name="BomId"></param>
  222. /// <param name="Quantity"></param>
  223. public void BomPretreatment(long? orderid, long? BomId, int Quantity)
  224. {
  225. if (orderid == null)
  226. {
  227. //throw new bu
  228. }
  229. if (BomId == null)
  230. {
  231. //throw new bu
  232. }
  233. //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
  234. var help = new SnowFlake();
  235. var bomlist = _ic_bom.GetAll().Result;
  236. var bomchildlist = _ic_bom_child.GetAll().Result;
  237. var icitemlist = _ic_item.GetAll().Result;
  238. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  239. var dto = new BomChildExamineDto();
  240. dto.bom_id = BomId.Value;
  241. dto.level = 1;
  242. dto.id = help.NextId();
  243. dto.parent_id = help.NextId();
  244. dto.qty = 1;
  245. dto.num = "1";
  246. dto.isbom = 1;
  247. dto.is_replace = 0;
  248. dto.haveicsubs = 0;
  249. dto.substitute_code = "";
  250. dto.icitem_ids = "";
  251. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist);
  252. }
  253. /// <summary>
  254. /// BOM预处理层级组装
  255. /// </summary>
  256. /// <param name="bomlist"></param>
  257. /// <param name="bomchildlist"></param>
  258. /// <param name="icitemlist"></param>
  259. /// <param name="dto"></param>
  260. /// <param name="returnlist"></param>
  261. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist)
  262. {
  263. int level = dto.level++;//初始化定义level层级
  264. var help = new SnowFlake();
  265. var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
  266. var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
  267. if (bom == null || item == null)
  268. {
  269. }
  270. //var dto = new BomChildExamineDto();
  271. //dto.level = level;
  272. //dto.bom_id = bomId;
  273. //dto.id = help.NextId();
  274. //dto.parent_id = parent_id;
  275. dto.item_id = bom.icitem_id;
  276. dto.item_name = bom.item_name;
  277. dto.item_code = bom.item_number;
  278. dto.model = item.model;
  279. dto.unit = bom.unit;
  280. dto.erp_cls = item.erp_cls;
  281. dto.erp_cls_name = item.erp_cls_name;
  282. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  283. returnlist.Add(dto);
  284. var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
  285. int idx = 1;
  286. foreach (var c in childlist)
  287. {
  288. string childNum = dto.num + "." + idx.ToString();
  289. var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
  290. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  291. if (c.is_bom == 1)
  292. {
  293. var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
  294. if (childBom != null)
  295. {
  296. var cdto = new BomChildExamineDto();
  297. cdto.id = help.NextId();
  298. cdto.level = level;
  299. cdto.parent_id = dto.id;
  300. cdto.qty = c.qty.Value;
  301. cdto.backflush = c.backflush;
  302. cdto.num = childNum;
  303. cdto.isbom = 1;
  304. cdto.is_replace = c.is_replace;
  305. cdto.haveicsubs = c.haveicsubs;
  306. cdto.substitute_code = c.substitute_code;
  307. cdto.icitem_ids = c.icitem_ids;
  308. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist);
  309. }
  310. }
  311. else
  312. {
  313. if (icitem != null)
  314. {
  315. var childDto = new BomChildExamineDto();
  316. childDto.level = level++;
  317. childDto.bom_id = dto.bom_id;
  318. childDto.id = help.NextId();
  319. childDto.parent_id = dto.id;
  320. childDto.item_id = icitem.Id;
  321. childDto.item_name = icitem.name;
  322. childDto.item_code = icitem.number;
  323. childDto.num = childNum;
  324. childDto.model = icitem.model;
  325. childDto.unit = c.unit;
  326. childDto.erp_cls = item.erp_cls;
  327. childDto.erp_cls_name = item.erp_cls_name;
  328. childDto.backflush = c.backflush;
  329. childDto.qty = c.qty.Value;
  330. childDto.isbom = 0;
  331. childDto.is_replace = c.is_replace;
  332. childDto.haveicsubs = c.haveicsubs;
  333. childDto.substitute_code = c.substitute_code;
  334. childDto.icitem_ids = c.icitem_ids;
  335. returnlist.Add(childDto);
  336. }
  337. }
  338. idx++;
  339. }
  340. }
  341. /// <summary>
  342. /// BOM替代关系预处理
  343. /// </summary>
  344. public void BomSubstitute(List<BomChildExamineDto> returnlist, List<ic_item> icitemlist)
  345. {
  346. var sublist = _ic_substitute.GetManyByCondition(s => s.substitute_code.IsIn(returnlist.Select(c => c.substitute_code))).Result.ToList();
  347. var suballlist =_ic_substitute_all.GetManyByCondition(s => s.substitute_id.IsIn(sublist.Select(c => c.Id))).Result.ToList();
  348. var subdtllist = _ic_substitute_all_dtl.GetManyByCondition(s => s.substitute_allid.IsIn(suballlist.Select(c => c.Id))).Result.ToList();
  349. List<long> dicIds = new List<long>();
  350. var help = new SnowFlake();
  351. //除顶级外,其他层级关系全带出来。生成平铺
  352. foreach (var item in returnlist)
  353. {
  354. //最顶级、虚拟件
  355. if (item.level != 1 && item.erp_cls!=4)
  356. {
  357. //有替代关系
  358. if (item.haveicsubs == 1)
  359. {
  360. /*//群组替代
  361. if (item.is_replace == 1)
  362. {
  363. if (!string.IsNullOrEmpty(item.icitem_ids))
  364. {
  365. var ids = item.icitem_ids.Split(',');
  366. foreach (var i in ids)
  367. {
  368. long id = 0;
  369. if (long.TryParse(i, out id))
  370. {
  371. dicIds.Add(id);
  372. }
  373. }
  374. }
  375. }*/
  376. var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
  377. if (sl != null)
  378. {
  379. var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
  380. foreach (var sal in sall)
  381. {
  382. var sadl = subdtllist.Where(s => s.substitute_allid== sal.Id).ToList();
  383. foreach (var dtl in sadl)
  384. {
  385. SubstitutePretreatment(sal, dtl, item, returnlist, icitemlist);
  386. //var sitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
  387. //if (sitem != null)
  388. //{
  389. // var childDto = new BomChildExamineDto();
  390. // childDto.level = item.level;
  391. // childDto.bom_id = item.bom_id;
  392. // childDto.id = help.NextId();
  393. // childDto.parent_id = item.parent_id;
  394. // childDto.item_id = sitem.Id;
  395. // childDto.item_name = sitem.name;
  396. // childDto.item_code = sitem.number;
  397. // childDto.num = item.num;
  398. // childDto.model = sitem.model;
  399. // childDto.unit = sitem.unit;
  400. // childDto.erp_cls = sitem.erp_cls;
  401. // childDto.erp_cls_name = sitem.erp_cls_name;
  402. // childDto.backflush = item.backflush;
  403. // childDto.replace_amount = dtl.use_amount.Value;
  404. // childDto.replace_amount = dtl.replace_amount.Value;
  405. // childDto.isbom = 0;
  406. // childDto.is_replace = c.is_replace;
  407. // childDto.haveicsubs = c.haveicsubs;
  408. // childDto.substitute_code = c.substitute_code;
  409. // childDto.icitem_ids = c.icitem_ids;
  410. // returnlist.Add(childDto);
  411. //}
  412. }
  413. }
  414. }
  415. }
  416. }
  417. }
  418. }
  419. public void SubstitutePretreatment(ic_substitute_all sal, ic_substitute_all_dtl dtl,BomChildExamineDto toDto,List<BomChildExamineDto> returnlist, List<ic_item> icitemlist)
  420. {
  421. //生成sal层级dto,是toDto的替代关系,类似R6-K50的BOM的层级1.1.1
  422. //循环生成dtl层级dto,明细
  423. //如果dtl对应的icitem是BOM,还需要向下继续展开。
  424. }
  425. /// <summary>
  426. /// 计算库存量
  427. /// </summary>
  428. /// <param name="returnlist"></param>
  429. public void BomStock(List<BomChildExamineDto> returnlist,long factoryid)
  430. {
  431. returnlist = returnlist.OrderBy(s => s.num).ToList();
  432. //取物料库存记录
  433. var stocklist = _ic_item_stock.GetManyByCondition(s => s.icitem_id.IsIn(returnlist.Select(c => c.item_id)) && s.factory_id == factoryid).Result;
  434. //取当前物料质检记录
  435. //var checklist = _ic_check.GetManyByCondition(s => s.icitem_id.IsIn(returnlist.Select(c => c.item_id)) && s.istest.GetValueOrDefault() != 1).Result.ToList();
  436. List<long> idlist = new List<long>();
  437. foreach (var item in returnlist)
  438. {
  439. if (item.erp_cls != 4)//虚拟件不计算
  440. {
  441. var sl = stocklist.Where(s => s.icitem_id == item.item_id).ToList();
  442. //库存=物料库存+VMI库存+质检未入库。
  443. item.sqty = sl.Sum(s => s.sqty).GetValueOrDefault();
  444. //质检记录没加入,暂时不考虑。
  445. //VMI仓储信息
  446. }
  447. }
  448. }
  449. }
  450. }