ResourceExamineAppService.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 XCZ.Extensions;
  34. using System.ComponentModel.Design;
  35. using Volo.Abp.Validation.StringValues;
  36. using System.Runtime.CompilerServices;
  37. using MongoDB.Driver;
  38. using Volo.Abp.Validation.Localization;
  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. /// <summary>
  101. /// 物料质检表
  102. /// </summary>
  103. private readonly IMongoDB<ic_check> _ic_check;
  104. /// <summary>
  105. /// 替代群组
  106. /// </summary>
  107. private readonly IMongoDB<ic_substitute> _ic_substitute;
  108. /// <summary>
  109. /// 替代群组
  110. /// </summary>
  111. private readonly IMongoDB<ic_substitute_all> _ic_substitute_all;
  112. /// <summary>
  113. /// 替代群组
  114. /// </summary>
  115. private readonly IMongoDB<ic_substitute_all_dtl> _ic_substitute_all_dtl;
  116. /// <summary>
  117. /// 生产工单主表
  118. /// </summary>
  119. private readonly IMongoDB<mes_morder> _mes_morder;
  120. /// <summary>
  121. /// 在制工单占用记录表
  122. /// </summary>
  123. private readonly IMongoDB<mes_mooccupy> _mes_mooccupy;
  124. /// <summary>
  125. /// 销售工单
  126. /// </summary>
  127. private readonly IRepository<crm_seorder> _crm_seorder;
  128. /// <summary>
  129. /// 销售工单
  130. /// </summary>
  131. private readonly IRepository<crm_seorderentry> _crm_seorderentry;
  132. #endregion
  133. /// <summary>
  134. /// 构造函数
  135. /// </summary>
  136. /// <param name="icitem"></param>
  137. /// <param name="icbom"></param>
  138. public ResourceExamineAppService(
  139. IMongoDB<mes_technique> mes_technique,
  140. IMongoDB<mes_process> mes_process,
  141. IMongoDB<mes_tech_process> mes_tech_process,
  142. IMongoDB<mes_tech_proc_workshop> mes_tech_proc_workshop,
  143. IMongoDB<ic_item> ic_item,
  144. IMongoDB<ic_bom> ic_bom,
  145. IMongoDB<ic_bom_child> ic_bom_child,
  146. IMongoDB<ic_item_stock> ic_item_stock,
  147. IMongoDB<ic_check> ic_check,
  148. //IMongoDB<ic_substitute> ic_substitute,
  149. //IMongoDB<ic_substitute_all> ic_substitute_all,
  150. //IMongoDB<ic_substitute_all_dtl> ic_substitute_all_dtl,
  151. IMongoDB<mes_morder> mes_morder,
  152. IMongoDB<mes_mooccupy> mes_mooccupy,
  153. IMongoDB<ic_item_stockoccupy> ic_item_stockoccupy,
  154. IRepository<ic_item, long> mysql_ic_item,
  155. IRepository<ic_bom, long> mysql_ic_bom,
  156. IRepository<ic_bom_child, long> mysql_ic_bom_child,
  157. IRepository<mes_technique, long> mysql_mes_technique,
  158. IRepository<crm_seorder, long> mysql_crm_seorder,
  159. IRepository<crm_seorderentry, long> mysql_crm_seorderentry
  160. )
  161. {
  162. _mes_technique = mes_technique;
  163. _mes_process = mes_process;
  164. _mes_tech_process = mes_tech_process;
  165. _mes_tech_proc_workshop = mes_tech_proc_workshop;
  166. _ic_item = ic_item;
  167. _ic_bom = ic_bom;
  168. _ic_bom_child = ic_bom_child;
  169. _ic_item_stock = ic_item_stock;
  170. _ic_check = ic_check;
  171. //_ic_substitute = ic_substitute;
  172. //_ic_substitute_all = ic_substitute_all;
  173. //_ic_substitute_all_dtl = ic_substitute_all_dtl;
  174. _mes_morder = mes_morder;
  175. _mes_mooccupy = mes_mooccupy;
  176. _ic_item_stockoccupy = ic_item_stockoccupy;
  177. _mysql_ic_item = mysql_ic_item;
  178. _mysql_ic_bom = mysql_ic_bom;
  179. _mysql_ic_bom_child = mysql_ic_bom_child;
  180. _mysql_mes_technique = mysql_mes_technique;
  181. }
  182. /// <summary>
  183. /// mongoDB示例方法,后期删除
  184. /// </summary>
  185. /// <returns></returns>
  186. public async Task test()
  187. {
  188. //多条插入
  189. List<mes_technique> infos = new List<mes_technique>();
  190. mes_technique info;
  191. for (int i = 0; i < 3; i++)
  192. {
  193. info = new mes_technique();
  194. info.GenerateNewId();
  195. info.tech_name = "多条" + i;
  196. info.level = i;
  197. infos.Add(info);
  198. }
  199. await _mes_technique.InsertMany(infos);
  200. var info2 = _mes_technique.GetAll().Result;
  201. var a = new PschedDto();
  202. a.count = info2.Count;
  203. //获取数据
  204. var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
  205. //更新数据
  206. info1.tech_name = "更新***";
  207. var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
  208. //根据条件查询数据
  209. Expression<Func<mes_technique, bool>> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
  210. var info3 = await _mes_technique.GetManyByCondition(filter);
  211. }
  212. /// <summary>
  213. /// 资源检查
  214. /// </summary>
  215. /// <param name="input"></param>
  216. /// <returns></returns>
  217. /// <exception cref="NotImplementedException"></exception>
  218. public async Task<PschedDto> ReceiveResult(SeorderentryDto input)
  219. {
  220. //数据库快照-同步mysql库数据到mongoDB中
  221. await SyncData();
  222. //生成当前计算bangid
  223. //SnowFlake snow = new SnowFlake();
  224. //long bangid = snow.NextId();
  225. ////产能检查
  226. //await ProductiveExamine(1733221167209762816, 100);
  227. return null;
  228. throw new NotImplementedException();
  229. }
  230. /// <summary>
  231. /// 数据库快照
  232. /// </summary>
  233. /// <returns></returns>
  234. public async Task SyncData()
  235. {
  236. //var query = _mysql_mes_technique.GetQueryableAsync().Result.ToListAsync().Result;
  237. //await _mes_technique.InsertMany(query);
  238. var dt1 = DateTime.Now;
  239. var query = _mysql_ic_bom_child.GetQueryableAsync().Result.ToListAsync().Result;
  240. await _ic_bom_child.InsertMany(query);
  241. var dt2 = DateTime.Now;
  242. var c = 3;
  243. }
  244. /// <summary>
  245. /// 产能计算
  246. /// </summary>
  247. /// <param name="tech_id">工艺路径主键</param>
  248. /// <param name="packages">需要生产产品件数</param>
  249. public async Task<DateTime> ProductiveExamine(long tech_id, int packages)
  250. {
  251. if (packages <= 0)
  252. {
  253. throw new NotImplementedException("产能计算参数有误!");
  254. }
  255. #region 1、数据准备
  256. //1.1、获取工艺路径数据
  257. mes_technique tech = await _mes_technique.GetOneByID(tech_id);
  258. //1.2、获取工艺路径关联工序数据
  259. List<mes_tech_process> tech_Processes = await _mes_tech_process.GetManyByCondition(x => x.tech_id == tech_id);
  260. //1.3、获取当前工艺路径下的工序数据
  261. FilterDefinition<mes_process> filter = Builders<mes_process>.Filter.In(s => s.Id, tech_Processes.Select(m => m.proc_id).ToList());
  262. List<mes_process> process = await _mes_process.GetManyByIds(filter);
  263. //1.3、获取工艺工序关联工位信息
  264. 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());
  265. List<mes_tech_proc_workshop> tech_Proc_Workshops = await _mes_tech_proc_workshop.GetManyByIds(filter1);
  266. #endregion
  267. #region 计算产能,得到耗时
  268. decimal sumTimes = 0.00m;//总耗时(分钟)
  269. //工序需要等待时间记录
  270. List<StartTimeDto> starts = new List<StartTimeDto>();
  271. //1、获取工艺路径下的第一层级工序
  272. List<mes_tech_process> fistChilds = tech_Processes.Where(p => p.parentprocid == tech_id).OrderBy(m => m.Id).ToList();
  273. if (fistChilds.Count == 0)
  274. {
  275. throw new NotImplementedException("当前工艺路径没有配置工序,请调整!");
  276. }
  277. decimal curTakeTime = 0.00m;//当前工序耗时(分钟)
  278. //添加第一个工序需要等待时间记录
  279. StartTimeDto dto;
  280. foreach (var chd in fistChilds)
  281. {
  282. dto = new StartTimeDto();
  283. if (chd.nextprocid == null)//最后一个工序
  284. {
  285. //计算最后一个工序耗时
  286. curTakeTime = CalcTakeTime(chd, packages);
  287. }
  288. else
  289. {
  290. curTakeTime = CalcTakeTime(chd, chd.lq.Value);
  291. }
  292. sumTimes += curTakeTime;
  293. //添加耗时记录
  294. dto.tech_id = tech_id;
  295. dto.proc_id = chd.proc_id;
  296. dto.nextproc_id = chd.nextprocid;
  297. dto.wait_time = curTakeTime;
  298. starts.Add(dto);
  299. }
  300. #endregion
  301. return DateTime.Now.AddDays(1).AddMinutes((double)sumTimes);
  302. }
  303. /// <summary>
  304. /// 计算当前工序前置准备时间
  305. /// </summary>
  306. /// <param name="proc"></param>
  307. /// <param name="quantity">LeadQuantity to Start Next</param>
  308. /// <returns></returns>
  309. private decimal CalcTakeTime(mes_tech_process proc, decimal quantity)
  310. {
  311. decimal takeTime = 0.00m;//当前工序前置准备时间(分钟)
  312. if (proc.wctype == 1)//人工型:数量/uph(一小时生产数量)*60(小时转换为分钟)/wsinuse(工位数)
  313. {
  314. takeTime = quantity / proc.uph.Value * 60 / proc.wsinuse.Value;
  315. return takeTime;
  316. }
  317. else if (proc.wctype == 2)//流水线型:数量*ct(生产一件所需时间)
  318. {
  319. takeTime = quantity * proc.ct.Value;
  320. return takeTime;
  321. }
  322. else if (proc.wctype == 3)//设备型:向上取整(数量/一次可加工数量)*ct(老化一次所需时间)
  323. {
  324. takeTime = Math.Ceiling(quantity / proc.upe.Value) * proc.ct.Value;
  325. return takeTime;
  326. }
  327. return takeTime;
  328. }
  329. /// <summary>
  330. /// 生成主工单
  331. /// </summary>
  332. /// <param name="BomNumber">Bom编码</param>
  333. /// <param name="Quantity">需要数量</param>
  334. /// <param name="DeliverDate">交付日期</param>
  335. /// <param name="OrderId">销售订单ID</param>
  336. /// <returns></returns>
  337. public async Task GenerateMorder(string BomNumber, decimal? Quantity, DateTime DeliverDate, long OrderId)
  338. {
  339. //需要生成的物料,需要生产的数量,对应销售订单。
  340. //1.库存、在制工单检查完成后 当前BOM需要自制时 产生工单。
  341. //2.每一个销售订单行对应一个工单。
  342. if (string.IsNullOrEmpty(BomNumber) || Quantity == null)
  343. {
  344. //TODO: 参数异常 直接返回
  345. }
  346. //获取销售订单信息
  347. //var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  348. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  349. }
  350. /// <summary>
  351. /// 检查成品库存
  352. /// </summary>
  353. /// <param name="OrderId">销售订单ID</param>
  354. /// <returns></returns>
  355. public async Task<bool> CheckFinishedProductInventory(long OrderId)
  356. {
  357. //获取销售订单信息
  358. var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  359. //获取销售订单子表
  360. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  361. if (string.IsNullOrEmpty(seorderentry.bom_number))
  362. {
  363. return false;
  364. }
  365. Expression<Func<ic_item_stock, bool>> filter = x => x.icitem_id == long.Parse(seorderentry.bom_number);
  366. var ic_Item_Stocks = await _ic_item_stock.GetManyByCondition(filter);
  367. if (seorderentry.qty <= ic_Item_Stocks.Sum(x => x.sqty))
  368. {
  369. return true;
  370. }
  371. else
  372. {
  373. return false;
  374. }
  375. }
  376. /// <summary>
  377. /// 检查在制工单
  378. /// </summary>
  379. /// <param name="bomNumber">Bom编码</param>
  380. /// <param name="Quantity">需要数量</param>
  381. /// <param name="DeliverDate">交付日期</param>
  382. /// <param name="OrderId">销售订单ID</param>
  383. /// <returns></returns>
  384. public async Task<List<mes_morder>> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate, long OrderId)
  385. {
  386. if (string.IsNullOrEmpty(bomNumber) || Quantity != null)
  387. {
  388. //TODO:入参异常;
  389. throw new NotImplementedException("BOM编码或需求数量不能为空!");
  390. }
  391. //根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。
  392. //TODO:工单类型;
  393. Expression<Func<mes_morder, bool>> filter = x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭"
  394. && x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false;
  395. var morderList = await _mes_morder.GetManyByCondition(filter);
  396. //获取销售订单信息
  397. //var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  398. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  399. //获取物料详情
  400. var mysql_ic_item = await _mysql_ic_item.FindAsync(x => x.number == bomNumber);
  401. //工单已被占用后要与占用表关联查询...减去占用量后 剩下生产数量可供下个销售工单使用。
  402. Expression<Func<mes_mooccupy, bool>> mooccupyfilter = x => x.moo_state == 1 && x.IsDeleted == false;
  403. var mes_mooccupyList = await _mes_mooccupy.GetManyByCondition(mooccupyfilter);
  404. //首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。
  405. //当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。
  406. var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate &&
  407. (x.morder_production_number - x.inventory_number) > Quantity).ToList();
  408. if (morderDataList.Count > 0)
  409. {
  410. //存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。
  411. var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault();
  412. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == morder.Id).ToList();
  413. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, morder, mooccupies);
  414. await _mes_mooccupy.InsertOne(mes_Mooccupy);
  415. }
  416. else
  417. {
  418. // 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量
  419. var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList();
  420. if (morderListData.Count == 0)
  421. {
  422. //TODO:后期处理无在制工单返回内容
  423. throw new NotImplementedException("无可用在制工单!");
  424. }
  425. List<mes_mooccupy> mes_Mooccupies = new List<mes_mooccupy>();
  426. decimal? number = Quantity;
  427. foreach (var item in morderListData)
  428. {
  429. //查询出工单已占用总数量
  430. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == item.Id).ToList();
  431. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, item, mooccupies);
  432. mes_Mooccupies.Add(mes_Mooccupy);
  433. //需求数量-占用量后小于或等于0 停止循环占用工单
  434. if (number - mes_Mooccupy.moo_qty <= 0)
  435. {
  436. break;
  437. }
  438. }
  439. }
  440. return morderList;
  441. }
  442. /// <summary>
  443. /// 拼接工单占用表
  444. /// </summary>
  445. /// <param name="seorderentry">销售订单子表</param>
  446. /// <param name="mysql_ic_item">物料详情表</param>
  447. /// <param name="item">工单表</param>
  448. /// <param name="mes_mooccupy">占用工单表</param>
  449. /// <returns></returns>
  450. public mes_mooccupy GetMooccupies(crm_seorderentry seorderentry, ic_item mysql_ic_item, mes_morder item, List<mes_mooccupy> mes_mooccupy)
  451. {
  452. decimal? Sumqty = 0;
  453. if (mes_mooccupy.Count > 0)
  454. {
  455. Sumqty = mes_mooccupy.Sum(x => x.moo_qty);
  456. }
  457. //生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。
  458. mes_mooccupy mes_Mooccupy = new mes_mooccupy();
  459. mes_Mooccupy.GenerateNewId();
  460. mes_Mooccupy.moo_id_type = "分配";
  461. mes_Mooccupy.moo_id_billid = seorderentry.seorder_id;//销售订单ID
  462. mes_Mooccupy.fbill_no = seorderentry.bill_no;//销售订单号
  463. mes_Mooccupy.fentry_id = seorderentry.entry_seq.Value;//销售订单行
  464. mes_Mooccupy.fitem_name = mysql_ic_item.name;//物料名称
  465. mes_Mooccupy.fitem_number = mysql_ic_item.number;
  466. mes_Mooccupy.fmodel = mysql_ic_item.model;//规格型号
  467. mes_Mooccupy.moo_moid = item.Id;
  468. mes_Mooccupy.moo_mo = item.morder_no;
  469. //占用量=生产计划数量-入库数量-已被占用数量
  470. mes_Mooccupy.moo_qty = item.morder_production_number - item.inventory_number - Sumqty;
  471. mes_Mooccupy.moo_stime = DateTime.Now;
  472. mes_Mooccupy.moo_etime = DateTime.Now;//日期来源需确定
  473. mes_Mooccupy.moo_state = 1;
  474. mes_Mooccupy.moo_cbr = string.Empty;
  475. //mes_Mooccupy.moo_ctime = ;
  476. mes_Mooccupy.moo_creason = string.Empty;
  477. mes_Mooccupy.tenant_id = 0;//TODO:企业ID =集团 或公司ID
  478. return mes_Mooccupy;
  479. }
  480. /// <summary>
  481. /// BOM预处理
  482. /// </summary>
  483. /// <param name="orderid"></param>
  484. /// <param name="BomId"></param>
  485. /// <param name="Quantity"></param>
  486. public void BomPretreatment(long? orderid, long? BomId, int Quantity)
  487. {
  488. if (orderid == null)
  489. {
  490. //throw new bu
  491. }
  492. if (BomId == null)
  493. {
  494. //throw new bu
  495. }
  496. //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
  497. var help = new SnowFlake();
  498. var bomlist = _ic_bom.GetAll().Result;
  499. var bomchildlist = _ic_bom_child.GetAll().Result;
  500. var icitemlist = _ic_item.GetAll().Result;
  501. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  502. var dto = new BomChildExamineDto();
  503. dto.bom_id = BomId.Value;
  504. dto.level = 1;
  505. dto.id = help.NextId();
  506. dto.parent_id = help.NextId();
  507. dto.qty = 1;
  508. dto.num = "1";
  509. dto.isbom = 1;
  510. dto.is_replace = 0;
  511. dto.haveicsubs = 0;
  512. dto.substitute_code = "";
  513. dto.icitem_ids = "";
  514. int type = 0;
  515. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  516. }
  517. /// <summary>
  518. /// BOM预处理层级组装
  519. /// </summary>
  520. /// <param name="bomlist"></param>
  521. /// <param name="bomchildlist"></param>
  522. /// <param name="icitemlist"></param>
  523. /// <param name="dto"></param>
  524. /// <param name="returnlist"></param>
  525. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist, int type)
  526. {
  527. int level = dto.level++;//初始化定义level层级
  528. var help = new SnowFlake();
  529. var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
  530. var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
  531. if (bom == null || item == null)
  532. {
  533. }
  534. dto.item_id = bom.icitem_id;
  535. dto.item_name = bom.item_name;
  536. dto.item_code = bom.item_number;
  537. dto.model = item.model;
  538. dto.unit = bom.unit;
  539. dto.erp_cls = item.erp_cls;
  540. dto.erp_cls_name = item.erp_cls_name;
  541. dto.type = type;
  542. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  543. returnlist.Add(dto);
  544. var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
  545. int idx = 1;
  546. foreach (var c in childlist)
  547. {
  548. string childNum = dto.num + "." + idx.ToString();
  549. var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
  550. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  551. if (c.is_bom == 1)
  552. {
  553. var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
  554. if (childBom != null)
  555. {
  556. var cdto = new BomChildExamineDto();
  557. cdto.id = help.NextId();
  558. cdto.level = level;
  559. cdto.parent_id = dto.id;
  560. cdto.qty = c.qty.Value;
  561. cdto.backflush = c.backflush;
  562. cdto.num = childNum;
  563. cdto.isbom = 1;
  564. cdto.is_replace = c.is_replace;
  565. cdto.haveicsubs = c.haveicsubs;
  566. cdto.substitute_code = c.substitute_code;
  567. cdto.icitem_ids = c.icitem_ids;
  568. cdto.type = type;
  569. //递归寻找子级
  570. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist, type);
  571. }
  572. }
  573. else
  574. {
  575. if (icitem != null)
  576. {
  577. var childDto = new BomChildExamineDto();
  578. childDto.level = level++;
  579. childDto.bom_id = dto.bom_id;
  580. childDto.id = help.NextId();
  581. childDto.parent_id = dto.id;
  582. childDto.item_id = icitem.Id;
  583. childDto.item_name = icitem.name;
  584. childDto.item_code = icitem.number;
  585. childDto.num = childNum;
  586. childDto.model = icitem.model;
  587. childDto.unit = c.unit;
  588. childDto.erp_cls = item.erp_cls;
  589. childDto.erp_cls_name = item.erp_cls_name;
  590. childDto.backflush = c.backflush;
  591. childDto.qty = c.qty.Value;
  592. childDto.isbom = 0;
  593. childDto.is_replace = c.is_replace;
  594. childDto.haveicsubs = c.haveicsubs;
  595. childDto.substitute_code = c.substitute_code;
  596. childDto.icitem_ids = c.icitem_ids;
  597. childDto.type = type;
  598. returnlist.Add(childDto);
  599. }
  600. }
  601. idx++;
  602. }
  603. }
  604. /// <summary>
  605. /// BOM替代关系预处理
  606. /// </summary>
  607. public void BomSubstitute(List<BomChildExamineDto> returnlist, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist)
  608. {
  609. var sublist = _ic_substitute.GetManyByCondition(s => s.substitute_code.IsIn(returnlist.Select(c => c.substitute_code))).Result.ToList();
  610. var suballlist = _ic_substitute_all.GetManyByCondition(s => s.substitute_id.IsIn(sublist.Select(c => c.Id))).Result.ToList();
  611. var subdtllist = _ic_substitute_all_dtl.GetManyByCondition(s => s.substitute_allid.IsIn(suballlist.Select(c => c.Id))).Result.ToList();
  612. List<long> dicIds = new List<long>();
  613. var help = new SnowFlake();
  614. int type = 1;
  615. //除顶级外,其他层级关系全带出来。生成平铺
  616. foreach (var item in returnlist)
  617. {
  618. //最顶级、虚拟件
  619. if (item.level != 1 && item.erp_cls != 4)
  620. {
  621. //有替代关系
  622. if (item.haveicsubs == 1)
  623. {
  624. //找到当前物料的替代群组关系集
  625. var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
  626. if (sl != null)
  627. {
  628. var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
  629. foreach (var sal in sall)
  630. {
  631. var sadl = subdtllist.Where(s => s.substitute_allid == sal.Id).ToList();
  632. foreach (var dtl in sadl)
  633. {
  634. //递归将替代关系组装出来。
  635. SubstitutePretreatment(sl, sal, dtl, item, returnlist, icitemlist, bomlist, bomchildlist, type);
  636. }
  637. }
  638. }
  639. }
  640. }
  641. }
  642. }
  643. /// <summary>
  644. /// 替代关系递归组装出来
  645. /// </summary>
  646. /// <param name="sal"></param>
  647. /// <param name="dtl"></param>
  648. /// <param name="toDto"></param>
  649. /// <param name="returnlist"></param>
  650. /// <param name="icitemlist"></param>
  651. /// <param name="bomlist"></param>
  652. /// <param name="bomchildlist"></param>
  653. 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)
  654. {
  655. //循环生成dtl层级dto,明细
  656. //如果dtl对应的icitem是BOM,还需要向下继续展开。
  657. var help = new SnowFlake();
  658. //List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  659. var dto = new BomChildExamineDto();
  660. var bom = bomlist.WhereIf(true, s => s.icitem_id == dtl.icitem_id).FirstOrDefault();
  661. var icitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
  662. if (icitem == null)
  663. {
  664. return;
  665. }
  666. dto.id = help.NextId();
  667. dto.level = toDto.level;
  668. dto.parent_id = toDto.parent_id;
  669. dto.item_id = icitem.Id;
  670. dto.item_name = icitem.name;
  671. dto.item_code = icitem.number;
  672. dto.num = toDto.num;
  673. dto.model = icitem.model;
  674. dto.unit = icitem.unit;
  675. dto.erp_cls = icitem.erp_cls;
  676. dto.erp_cls_name = icitem.erp_cls_name;
  677. dto.backflush = toDto.backflush;
  678. dto.qty = toDto.qty;
  679. dto.replace_amount = dtl.replace_amount.Value;
  680. dto.is_replace = 0;
  681. dto.haveicsubs = 0;
  682. dto.substitute_code = "";
  683. dto.icitem_ids = "";
  684. dto.substitute_strategy = sl.substitute_strategy.Value;
  685. dto.substitute_mode = sl.substitute_mode.Value;
  686. dto.type = type;
  687. if (bom != null)
  688. {
  689. dto.bom_id = bom.Id;
  690. dto.qty = dtl.replace_amount.Value;
  691. dto.isbom = 1;
  692. dto.is_replace = 0;
  693. dto.haveicsubs = 0;
  694. dto.substitute_code = "";
  695. dto.icitem_ids = "";
  696. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  697. }
  698. else
  699. {
  700. dto.bom_id = null;
  701. dto.isbom = 0;
  702. returnlist.Add(dto);
  703. }
  704. }
  705. /// <summary>
  706. /// 计算物料库存量
  707. /// </summary>
  708. /// <param name="returnlist"></param>
  709. public void BomStock(List<BomChildExamineDto> returnlist, long bangid, long orderid, long orderentryid, long factoryid)
  710. {
  711. returnlist = returnlist.OrderBy(s => s.num).ToList();
  712. //取物料库存记录
  713. FilterDefinition<ic_item_stock> filter = Builders<ic_item_stock>.Filter.In(s => s.icitem_id, returnlist.Select(c => c.item_id).ToList());
  714. var stocklist = _ic_item_stock.GetManyByIds(filter).Result;
  715. //取当前订单的物料库存占用记录
  716. var occupylist = _ic_item_stockoccupy.GetManyByCondition(p => p.bang_id == bangid && p.order_id == orderid).Result;
  717. //计算剩余库存
  718. foreach (var item in returnlist)
  719. {
  720. if (item.erp_cls == 4)//虚拟件不计算
  721. {
  722. continue;
  723. }
  724. //非虚拟件
  725. //当前物料的库存数量
  726. decimal stockQty = stocklist.Where(s => s.icitem_id == item.item_id).Sum(p => p.sqty.GetValueOrDefault());
  727. //获取当前订单其他订单行当前物料的占用数量
  728. decimal otherStockQty = occupylist.Where(s => s.icitem_id == item.item_id).Sum(p => p.quantity);
  729. //当前订单行物料库存情况
  730. item.sqty = stockQty - otherStockQty;
  731. }
  732. }
  733. }
  734. }