ResourceExamineAppService.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. using Hangfire.Annotations;
  40. namespace Business.ResourceExamineManagement
  41. {
  42. /// <summary>
  43. /// 资源检查
  44. /// </summary>
  45. //[Authorize(BusinessPermissions.ResourceExamine.Default)]
  46. public class ResourceExamineAppService : ApplicationService, IResourceExamineAppService
  47. {
  48. #region 服务
  49. /// <summary>
  50. /// 工艺路径
  51. /// </summary>
  52. private readonly IMongoDB<mes_technique> _mes_technique;
  53. private IRepository<mes_technique, long> _mysql_mes_technique;
  54. /// <summary>
  55. /// 工序
  56. /// </summary>
  57. private readonly IMongoDB<mes_process> _mes_process;
  58. /// <summary>
  59. /// 工艺关联工序
  60. /// </summary>
  61. private readonly IMongoDB<mes_tech_process> _mes_tech_process;
  62. /// <summary>
  63. /// 工作日历
  64. /// </summary>
  65. private readonly IMongoDB<mes_work_calendar> _mes_work_calendar;
  66. /// <summary>
  67. /// 工作日历明细
  68. /// </summary>
  69. private readonly IMongoDB<mes_work_calendar_list> _mes_work_calendar_list;
  70. /// <summary>
  71. /// 工艺工序关联工位
  72. /// </summary>
  73. private readonly IMongoDB<mes_tech_proc_workshop> _mes_tech_proc_workshop;
  74. /// <summary>
  75. /// 排程占用记录
  76. /// </summary>
  77. private readonly IMongoDB<mes_schedule_occupy> _mes_schedule_occupy;
  78. /// <summary>
  79. /// 物料占用记录
  80. /// </summary>
  81. private readonly IMongoDB<ic_item_stockoccupy> _ic_item_stockoccupy;
  82. /// <summary>
  83. /// 物料详情
  84. /// </summary>
  85. private readonly IMongoDB<ic_item> _ic_item;
  86. private IRepository<ic_item, long> _mysql_ic_item;
  87. /// <summary>
  88. /// 物料BOM
  89. /// </summary>
  90. private readonly IMongoDB<ic_bom> _ic_bom;
  91. private IRepository<ic_bom, long> _mysql_ic_bom;
  92. /// <summary>
  93. /// 物料BOM明细
  94. /// </summary>
  95. private readonly IMongoDB<ic_bom_child> _ic_bom_child;
  96. private IRepository<ic_bom_child, long> _mysql_ic_bom_child;
  97. /// <summary>
  98. /// 物料库存表
  99. /// </summary>
  100. private readonly IMongoDB<ic_item_stock> _ic_item_stock;
  101. private IRepository<ic_item_stock, long> _mysql_ic_item_stock;
  102. /// <summary>
  103. /// 物料质检表
  104. /// </summary>
  105. private readonly IMongoDB<ic_check> _ic_check;
  106. /// <summary>
  107. /// 替代群组
  108. /// </summary>
  109. private readonly IMongoDB<ic_substitute> _ic_substitute;
  110. /// <summary>
  111. /// 替代群组
  112. /// </summary>
  113. private readonly IMongoDB<ic_substitute_all> _ic_substitute_all;
  114. /// <summary>
  115. /// 替代群组
  116. /// </summary>
  117. private readonly IMongoDB<ic_substitute_all_dtl> _ic_substitute_all_dtl;
  118. /// <summary>
  119. /// 生产工单主表
  120. /// </summary>
  121. private readonly IMongoDB<mes_morder> _mes_morder;
  122. /// <summary>
  123. /// 生产工单子表
  124. /// </summary>
  125. private readonly IMongoDB<mes_moentry> _mes_moentry;
  126. /// <summary>
  127. /// 在制工单占用记录表
  128. /// </summary>
  129. private readonly IMongoDB<mes_mooccupy> _mes_mooccupy;
  130. /// <summary>
  131. /// 销售工单
  132. /// </summary>
  133. private readonly IRepository<crm_seorder> _crm_seorder;
  134. /// <summary>
  135. /// 销售工单
  136. /// </summary>
  137. private readonly IRepository<crm_seorderentry> _crm_seorderentry;
  138. #endregion
  139. /// <summary>
  140. /// 构造函数
  141. /// </summary>
  142. /// <param name="icitem"></param>
  143. /// <param name="icbom"></param>
  144. public ResourceExamineAppService(
  145. IMongoDB<mes_technique> mes_technique,
  146. IMongoDB<mes_process> mes_process,
  147. IMongoDB<mes_tech_process> mes_tech_process,
  148. IMongoDB<mes_tech_proc_workshop> mes_tech_proc_workshop,
  149. IMongoDB<ic_item> ic_item,
  150. IMongoDB<ic_bom> ic_bom,
  151. IMongoDB<ic_bom_child> ic_bom_child,
  152. IMongoDB<ic_item_stock> ic_item_stock,
  153. IMongoDB<ic_check> ic_check,
  154. //IMongoDB<ic_substitute> ic_substitute,
  155. //IMongoDB<ic_substitute_all> ic_substitute_all,
  156. //IMongoDB<ic_substitute_all_dtl> ic_substitute_all_dtl,
  157. IMongoDB<mes_morder> mes_morder,
  158. IMongoDB<mes_moentry> mes_moentry,
  159. IMongoDB<mes_mooccupy> mes_mooccupy,
  160. IMongoDB<ic_item_stockoccupy> ic_item_stockoccupy,
  161. IRepository<ic_item, long> mysql_ic_item,
  162. IRepository<ic_bom, long> mysql_ic_bom,
  163. IRepository<ic_bom_child, long> mysql_ic_bom_child,
  164. IRepository<mes_technique, long> mysql_mes_technique,
  165. IRepository<crm_seorder, long> mysql_crm_seorder,
  166. IRepository<crm_seorderentry, long> mysql_crm_seorderentry,
  167. IRepository<ic_item_stock, long> mysql_ic_item_stock
  168. )
  169. {
  170. _mes_technique = mes_technique;
  171. _mes_process = mes_process;
  172. _mes_tech_process = mes_tech_process;
  173. _mes_tech_proc_workshop = mes_tech_proc_workshop;
  174. _ic_item = ic_item;
  175. _ic_bom = ic_bom;
  176. _ic_bom_child = ic_bom_child;
  177. _ic_item_stock = ic_item_stock;
  178. _ic_check = ic_check;
  179. //_ic_substitute = ic_substitute;
  180. //_ic_substitute_all = ic_substitute_all;
  181. //_ic_substitute_all_dtl = ic_substitute_all_dtl;
  182. _mes_morder = mes_morder;
  183. _mes_moentry = mes_moentry;
  184. _mes_mooccupy = mes_mooccupy;
  185. _ic_item_stockoccupy = ic_item_stockoccupy;
  186. _mysql_ic_item = mysql_ic_item;
  187. _mysql_ic_bom = mysql_ic_bom;
  188. _mysql_ic_bom_child = mysql_ic_bom_child;
  189. _crm_seorder = mysql_crm_seorder;
  190. _crm_seorderentry = mysql_crm_seorderentry;
  191. _mysql_mes_technique = mysql_mes_technique;
  192. _mysql_ic_item_stock = mysql_ic_item_stock;
  193. }
  194. /// <summary>
  195. /// mongoDB示例方法,后期删除
  196. /// </summary>
  197. /// <returns></returns>
  198. public async Task test()
  199. {
  200. //多条插入
  201. List<mes_technique> infos = new List<mes_technique>();
  202. mes_technique info;
  203. for (int i = 0; i < 3; i++)
  204. {
  205. info = new mes_technique();
  206. info.GenerateNewId();
  207. info.tech_name = "多条" + i;
  208. info.level = i;
  209. infos.Add(info);
  210. }
  211. await _mes_technique.InsertMany(infos);
  212. var info2 = _mes_technique.GetAll().Result;
  213. var a = new PschedDto();
  214. a.count = info2.Count;
  215. //获取数据
  216. var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
  217. //更新数据
  218. info1.tech_name = "更新***";
  219. var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
  220. //根据条件查询数据
  221. Expression<Func<mes_technique, bool>> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
  222. var info3 = await _mes_technique.GetManyByCondition(filter);
  223. }
  224. /// <summary>
  225. /// 资源检查
  226. /// </summary>
  227. /// <param name="input"></param>
  228. /// <returns></returns>
  229. /// <exception cref="NotImplementedException"></exception>
  230. public async Task<PschedDto> ReceiveResult(SeorderentryDto input)
  231. {
  232. //数据库快照-同步mysql库数据到mongoDB中
  233. await SyncData();
  234. //生成当前计算bangid
  235. //SnowFlake snow = new SnowFlake();
  236. //long bangid = snow.NextId();
  237. ////产能检查
  238. //await ProductiveExamine(1733221167209762816, 100);
  239. return null;
  240. throw new NotImplementedException();
  241. }
  242. /// <summary>
  243. /// 数据库快照
  244. /// </summary>
  245. /// <returns></returns>
  246. public async Task SyncData()
  247. {
  248. //同步物料库存数据
  249. var icitemStokc = _mysql_ic_item_stock.GetQueryableAsync().Result.ToListAsync().Result;
  250. if (icitemStokc.Count > 0)
  251. {
  252. await _ic_item_stock.InsertMany(icitemStokc);
  253. }
  254. }
  255. /// <summary>
  256. /// 产能计算
  257. /// </summary>
  258. /// <param name="tech_id">工艺路径主键</param>
  259. /// <param name="packages">需要生产产品件数</param>
  260. public async Task<DateTime> ProductiveExamine(long tech_id, int packages)
  261. {
  262. if (packages <= 0)
  263. {
  264. throw new NotImplementedException("产能计算参数有误!");
  265. }
  266. #region 1、数据准备
  267. //1.1、获取工艺路径数据
  268. mes_technique tech = await _mes_technique.GetOneByID(tech_id);
  269. //1.2、获取工艺路径关联工序数据
  270. List<mes_tech_process> tech_Processes = await _mes_tech_process.GetManyByCondition(x => x.tech_id == tech_id);
  271. //1.3、获取当前工艺路径下的工序数据
  272. FilterDefinition<mes_process> filter = Builders<mes_process>.Filter.In(s => s.Id, tech_Processes.Select(m => m.proc_id).ToList());
  273. List<mes_process> process = await _mes_process.GetManyByIds(filter);
  274. //1.3、获取工艺工序关联工位信息
  275. 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());
  276. List<mes_tech_proc_workshop> tech_Proc_Workshops = await _mes_tech_proc_workshop.GetManyByIds(filter1);
  277. #endregion
  278. #region 计算产能,得到耗时
  279. decimal sumTimes = 0.00m;//总耗时(分钟)
  280. //工序需要等待时间记录
  281. List<StartTimeDto> starts = new List<StartTimeDto>();
  282. //1、获取工艺路径下的第一层级工序
  283. List<mes_tech_process> fistChilds = tech_Processes.Where(p => p.parentprocid == tech_id).OrderBy(m => m.Id).ToList();
  284. if (fistChilds.Count == 0)
  285. {
  286. throw new NotImplementedException("当前工艺路径没有配置工序,请调整!");
  287. }
  288. decimal curTakeTime = 0.00m;//当前工序耗时(分钟)
  289. //添加第一个工序需要等待时间记录
  290. StartTimeDto dto;
  291. foreach (var chd in fistChilds)
  292. {
  293. dto = new StartTimeDto();
  294. if (chd.nextprocid == null)//最后一个工序
  295. {
  296. //计算最后一个工序耗时
  297. curTakeTime = CalcTakeTime(chd, packages);
  298. }
  299. else
  300. {
  301. curTakeTime = CalcTakeTime(chd, chd.lq.Value);
  302. }
  303. sumTimes += curTakeTime;
  304. //添加耗时记录
  305. dto.tech_id = tech_id;
  306. dto.proc_id = chd.proc_id;
  307. dto.nextproc_id = chd.nextprocid;
  308. dto.wait_time = curTakeTime;
  309. starts.Add(dto);
  310. }
  311. #endregion
  312. return DateTime.Now.AddDays(1).AddMinutes((double)sumTimes);
  313. }
  314. /// <summary>
  315. /// 计算当前工序前置准备时间
  316. /// </summary>
  317. /// <param name="proc"></param>
  318. /// <param name="quantity">LeadQuantity to Start Next</param>
  319. /// <returns></returns>
  320. private decimal CalcTakeTime(mes_tech_process proc, decimal quantity)
  321. {
  322. decimal takeTime = 0.00m;//当前工序前置准备时间(分钟)
  323. if (proc.wctype == 1)//人工型:数量/uph(一小时生产数量)*60(小时转换为分钟)/wsinuse(工位数)
  324. {
  325. takeTime = quantity / proc.uph.Value * 60 / proc.wsinuse.Value;
  326. return takeTime;
  327. }
  328. else if (proc.wctype == 2)//流水线型:数量*ct(生产一件所需时间)
  329. {
  330. takeTime = quantity * proc.ct.Value;
  331. return takeTime;
  332. }
  333. else if (proc.wctype == 3)//设备型:向上取整(数量/一次可加工数量)*ct(老化一次所需时间)
  334. {
  335. takeTime = Math.Ceiling(quantity / proc.upe.Value) * proc.ct.Value;
  336. return takeTime;
  337. }
  338. return takeTime;
  339. }
  340. /// <summary>
  341. /// 生成主工单
  342. /// </summary>
  343. /// <param name="BomNumber">Bom编码</param>
  344. /// <param name="Quantity">需要数量</param>
  345. /// <param name="DeliverDate">交付日期</param>
  346. /// <param name="seorderentry_id">销售订单ID</param>
  347. /// <returns></returns>
  348. public async Task GenerateMorder(long seorderentry_id)
  349. {
  350. //需要生成的物料,需要生产的数量,对应销售订单。
  351. //1.库存、在制工单检查完成后 当前BOM需要自制时 产生工单。
  352. //2.每一个销售订单行对应一个工单。
  353. //查询销售订单子表数据
  354. var seorderentry = await _crm_seorderentry.FindAsync(x => x.Id == seorderentry_id);
  355. //获取销售订单信息
  356. var seorder = await _crm_seorder.FindAsync(x => x.Id == seorderentry.seorder_id);
  357. mes_morder mes_Morder = new mes_morder();
  358. mes_Morder.GenerateNewId();
  359. mes_Morder.morder_type = "销售工单";
  360. //mes_Morder.morder_icitem_type
  361. mes_Morder.work_order_type = "常规工单";
  362. mes_Morder.morder_state = "初始";
  363. mes_Morder.morder_no = "";//TODO:编码规则暂时没有
  364. mes_Morder.fms_number = seorderentry.fms_number;
  365. mes_Morder.bom_number = seorderentry.bom_number;
  366. mes_Morder.fmodel = seorderentry.specification;
  367. mes_Morder.moentry_startup_status = 1;
  368. mes_Morder.tenant_id = 0;
  369. mes_Morder.product_code = seorderentry.item_number;
  370. mes_Morder.product_name = seorderentry.item_name;
  371. mes_Morder.project_name = seorder.project_name;
  372. mes_Morder.planner_num = seorderentry.planner_no;
  373. mes_Morder.planner_name = seorderentry.planner_name;
  374. mes_Morder.morder_date = DateTime.Now.Date;
  375. //TODO:可删除主表字段
  376. mes_Morder.unit = seorderentry.unit;
  377. mes_Morder.morder_production_number = seorderentry.qty;
  378. mes_Morder.need_number = seorderentry.qty;
  379. mes_Morder.remaining_number = 0;
  380. await _mes_morder.InsertOne(mes_Morder);
  381. mes_moentry mes_Moentry = new mes_moentry();
  382. mes_Moentry.GenerateNewId();
  383. mes_Moentry.moentry_moid = mes_Morder.Id;
  384. mes_Moentry.moentry_mono = mes_Morder.morder_no;
  385. mes_Moentry.soentry_id = seorderentry.Id;
  386. mes_Moentry.fbill_no = seorderentry.bill_no;
  387. mes_Moentry.fentry_id = seorderentry.entry_seq.Value;
  388. mes_Moentry.unit = seorderentry.unit;
  389. mes_Moentry.morder_production_number = seorderentry.qty;
  390. mes_Moentry.need_number = seorderentry.qty;
  391. mes_Moentry.remaining_number = 0;
  392. await _mes_moentry.InsertOne(mes_Moentry);
  393. }
  394. /// <summary>
  395. /// 检查成品库存
  396. /// </summary>
  397. /// <param name="seorderentry_id">销售订单子表ID</param>
  398. /// <returns></returns>
  399. public async Task<bool> CheckFinishedProductInventory(long seorderentry_id)
  400. {
  401. //获取销售订单子表
  402. var seorderentry = await _crm_seorderentry.FindAsync(x => x.Id == seorderentry_id);
  403. if (string.IsNullOrEmpty(seorderentry.bom_number))
  404. {
  405. return false;
  406. }
  407. //物料BOM
  408. var ic_bom = _ic_bom.GetManyByCondition(x => x.bom_number == seorderentry.bom_number).Result.FirstOrDefault();
  409. //物料库存表
  410. var ic_Item_Stocks = await _ic_item_stock.GetManyByCondition(x => x.icitem_id == ic_bom.icitem_id);
  411. if (seorderentry.qty <= ic_Item_Stocks.Sum(x => x.sqty))
  412. {
  413. return true;
  414. }
  415. else
  416. {
  417. return false;
  418. }
  419. }
  420. /// <summary>
  421. /// 检查在制工单
  422. /// </summary>
  423. /// <param name="bomNumber">Bom编码</param>
  424. /// <param name="Quantity">需要数量</param>
  425. /// <param name="DeliverDate">交付日期</param>
  426. /// <param name="seorderentry_id">销售订单子表ID</param>
  427. /// <returns></returns>
  428. public async Task<List<mes_morder>> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate, long seorderentry_id)
  429. {
  430. if (string.IsNullOrEmpty(bomNumber) || Quantity != null)
  431. {
  432. //TODO:入参异常;
  433. throw new NotImplementedException("BOM编码或需求数量不能为空!");
  434. }
  435. //根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。
  436. //TODO:工单类型;
  437. var morderList = await _mes_morder.GetManyByCondition(x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭"
  438. && x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false);
  439. //获取销售订单信息
  440. //var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  441. var seorderentry = await _crm_seorderentry.FindAsync(x => x.Id == seorderentry_id);
  442. //获取物料详情
  443. var mysql_ic_item = await _mysql_ic_item.FindAsync(x => x.number == bomNumber);
  444. //工单已被占用后要与占用表关联查询...减去占用量后 剩下生产数量可供下个销售工单使用。
  445. var mes_mooccupyList = await _mes_mooccupy.GetManyByCondition(x => x.moo_state == 1 && x.IsDeleted == false);
  446. //首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。
  447. //当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。
  448. var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate &&
  449. (x.morder_production_number - x.inventory_number) > Quantity).ToList();
  450. if (morderDataList.Count > 0)
  451. {
  452. //存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。
  453. var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault();
  454. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == morder.Id).ToList();
  455. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, morder, mooccupies);
  456. await _mes_mooccupy.InsertOne(mes_Mooccupy);
  457. }
  458. else
  459. {
  460. // 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量
  461. var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList();
  462. if (morderListData.Count == 0)
  463. {
  464. //TODO:后期处理无在制工单返回内容
  465. throw new NotImplementedException("无可用在制工单!");
  466. }
  467. List<mes_mooccupy> mes_Mooccupies = new List<mes_mooccupy>();
  468. decimal? number = Quantity;
  469. foreach (var item in morderListData)
  470. {
  471. //查询出工单已占用总数量
  472. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == item.Id).ToList();
  473. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, item, mooccupies);
  474. mes_Mooccupies.Add(mes_Mooccupy);
  475. //需求数量-占用量后小于或等于0 停止循环占用工单
  476. if (number - mes_Mooccupy.moo_qty <= 0)
  477. {
  478. break;
  479. }
  480. }
  481. }
  482. return morderList;
  483. }
  484. /// <summary>
  485. /// 拼接工单占用表
  486. /// </summary>
  487. /// <param name="seorderentry">销售订单子表</param>
  488. /// <param name="mysql_ic_item">物料详情表</param>
  489. /// <param name="item">工单表</param>
  490. /// <param name="mes_mooccupy">占用工单表</param>
  491. /// <returns></returns>
  492. public mes_mooccupy GetMooccupies(crm_seorderentry seorderentry, ic_item mysql_ic_item, mes_morder item, List<mes_mooccupy> mes_mooccupy)
  493. {
  494. decimal? Sumqty = 0;
  495. if (mes_mooccupy.Count > 0)
  496. {
  497. Sumqty = mes_mooccupy.Sum(x => x.moo_qty);
  498. }
  499. //生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。
  500. mes_mooccupy mes_Mooccupy = new mes_mooccupy();
  501. mes_Mooccupy.GenerateNewId();
  502. mes_Mooccupy.moo_id_type = "分配";
  503. mes_Mooccupy.moo_id_billid = seorderentry.seorder_id;//销售订单ID
  504. mes_Mooccupy.fbill_no = seorderentry.bill_no;//销售订单号
  505. mes_Mooccupy.fentry_id = seorderentry.entry_seq.Value;//销售订单行
  506. mes_Mooccupy.fitem_name = mysql_ic_item.name;//物料名称
  507. mes_Mooccupy.fitem_number = mysql_ic_item.number;
  508. mes_Mooccupy.fmodel = mysql_ic_item.model;//规格型号
  509. mes_Mooccupy.moo_moid = item.Id;
  510. mes_Mooccupy.moo_mo = item.morder_no;
  511. //占用量=生产计划数量-入库数量-已被占用数量
  512. mes_Mooccupy.moo_qty = item.morder_production_number - item.inventory_number - Sumqty;
  513. mes_Mooccupy.moo_stime = DateTime.Now;
  514. mes_Mooccupy.moo_etime = DateTime.Now;//日期来源需确定
  515. mes_Mooccupy.moo_state = 1;
  516. mes_Mooccupy.moo_cbr = string.Empty;
  517. //mes_Mooccupy.moo_ctime = ;
  518. mes_Mooccupy.moo_creason = string.Empty;
  519. mes_Mooccupy.tenant_id = 0;//TODO:企业ID =集团 或公司ID
  520. return mes_Mooccupy;
  521. }
  522. /// <summary>
  523. /// BOM预处理
  524. /// </summary>
  525. /// <param name="orderid"></param>
  526. /// <param name="BomId"></param>
  527. /// <param name="Quantity"></param>
  528. public void BomPretreatment(long? orderid, long? BomId, int Quantity)
  529. {
  530. if (orderid == null)
  531. {
  532. //throw new bu
  533. }
  534. if (BomId == null)
  535. {
  536. //throw new bu
  537. }
  538. //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
  539. var help = new SnowFlake();
  540. var bomlist = _ic_bom.GetAll().Result;
  541. var bomchildlist = _ic_bom_child.GetAll().Result;
  542. var icitemlist = _ic_item.GetAll().Result;
  543. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  544. var dto = new BomChildExamineDto();
  545. dto.bom_id = BomId.Value;
  546. dto.level = 1;
  547. dto.id = help.NextId();
  548. dto.parent_id = help.NextId();
  549. dto.qty = 1;
  550. dto.num = "1";
  551. dto.isbom = 1;
  552. dto.is_replace = 0;
  553. dto.haveicsubs = 0;
  554. dto.substitute_code = "";
  555. dto.icitem_ids = "";
  556. int type = 0;
  557. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  558. }
  559. /// <summary>
  560. /// BOM预处理层级组装
  561. /// </summary>
  562. /// <param name="bomlist"></param>
  563. /// <param name="bomchildlist"></param>
  564. /// <param name="icitemlist"></param>
  565. /// <param name="dto"></param>
  566. /// <param name="returnlist"></param>
  567. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist, int type)
  568. {
  569. int level = dto.level++;//初始化定义level层级
  570. var help = new SnowFlake();
  571. var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
  572. var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
  573. if (bom == null || item == null)
  574. {
  575. }
  576. dto.item_id = bom.icitem_id;
  577. dto.item_name = bom.item_name;
  578. dto.item_code = bom.item_number;
  579. dto.model = item.model;
  580. dto.unit = bom.unit;
  581. dto.erp_cls = item.erp_cls;
  582. dto.erp_cls_name = item.erp_cls_name;
  583. dto.type = type;
  584. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  585. returnlist.Add(dto);
  586. var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
  587. int idx = 1;
  588. foreach (var c in childlist)
  589. {
  590. string childNum = dto.num + "." + idx.ToString();
  591. var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
  592. var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
  593. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  594. if (childBom != null)
  595. {
  596. var cdto = new BomChildExamineDto();
  597. cdto.id = help.NextId();
  598. cdto.level = level;
  599. cdto.parent_id = dto.id;
  600. cdto.bom_child_id = c.Id;
  601. cdto.qty = c.qty.Value;
  602. cdto.backflush = c.backflush;
  603. cdto.num = childNum;
  604. cdto.isbom = 1;
  605. cdto.is_replace = c.is_replace;
  606. cdto.haveicsubs = c.haveicsubs;
  607. cdto.substitute_code = c.substitute_code;
  608. cdto.icitem_ids = c.icitem_ids;
  609. cdto.type = type;
  610. //递归寻找子级
  611. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist, type);
  612. }
  613. else
  614. {
  615. if (icitem != null)
  616. {
  617. var childDto = new BomChildExamineDto();
  618. childDto.level = level++;
  619. childDto.bom_id = dto.bom_id;
  620. childDto.bom_child_id = c.Id;
  621. childDto.id = help.NextId();
  622. childDto.parent_id = dto.id;
  623. childDto.item_id = icitem.Id;
  624. childDto.item_name = icitem.name;
  625. childDto.item_code = icitem.number;
  626. childDto.num = childNum;
  627. childDto.model = icitem.model;
  628. childDto.unit = c.unit;
  629. childDto.erp_cls = item.erp_cls;
  630. childDto.erp_cls_name = item.erp_cls_name;
  631. childDto.backflush = c.backflush;
  632. childDto.qty = c.qty.Value;
  633. childDto.isbom = 0;
  634. childDto.is_replace = c.is_replace;
  635. childDto.haveicsubs = c.haveicsubs;
  636. childDto.substitute_code = c.substitute_code;
  637. childDto.icitem_ids = c.icitem_ids;
  638. childDto.type = type;
  639. returnlist.Add(childDto);
  640. }
  641. }
  642. idx++;
  643. }
  644. }
  645. /// <summary>
  646. /// BOM替代关系预处理
  647. /// </summary>
  648. public void BomSubstitute(List<BomChildExamineDto> returnlist, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist)
  649. {
  650. var sublist = _ic_substitute.GetManyByCondition(s => s.substitute_code.IsIn(returnlist.Select(c => c.substitute_code))).Result.ToList();
  651. var suballlist = _ic_substitute_all.GetManyByCondition(s => s.substitute_id.IsIn(sublist.Select(c => c.Id))).Result.ToList();
  652. var subdtllist = _ic_substitute_all_dtl.GetManyByCondition(s => s.substitute_allid.IsIn(suballlist.Select(c => c.Id))).Result.ToList();
  653. List<long> childidList = new List<long>();
  654. var help = new SnowFlake();
  655. int type = 1;
  656. //除顶级外,其他层级关系全带出来。生成平铺
  657. foreach (var item in returnlist)
  658. {
  659. //最顶级、虚拟件
  660. if (item.level != 1 && item.erp_cls != 4 && !childidList.Contains(item.bom_child_id.GetValueOrDefault()))
  661. {
  662. //有替代关系
  663. if (item.haveicsubs == 1)
  664. {
  665. if (!string.IsNullOrEmpty(item.icitem_ids))
  666. {
  667. long cid = 1;
  668. var cids = item.icitem_ids.Split(',');
  669. foreach (var c in cids)
  670. {
  671. if (long.TryParse(c, out cid))
  672. {
  673. childidList.Add(cid);
  674. }
  675. }
  676. }
  677. //找到当前物料的替代群组关系集
  678. var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
  679. if (sl != null)
  680. {
  681. var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
  682. foreach (var sal in sall)
  683. {
  684. var sadl = subdtllist.Where(s => s.substitute_allid == sal.Id).ToList();
  685. foreach (var dtl in sadl)
  686. {
  687. if (dtl.ismain == 0)
  688. {
  689. //递归将替代关系组装出来。
  690. SubstitutePretreatment(sl, sal, dtl, item, returnlist, icitemlist, bomlist, bomchildlist, type);
  691. }
  692. }
  693. }
  694. }
  695. }
  696. }
  697. }
  698. }
  699. /// <summary>
  700. /// 替代关系递归组装出来
  701. /// </summary>
  702. /// <param name="sal"></param>
  703. /// <param name="dtl"></param>
  704. /// <param name="toDto"></param>
  705. /// <param name="returnlist"></param>
  706. /// <param name="icitemlist"></param>
  707. /// <param name="bomlist"></param>
  708. /// <param name="bomchildlist"></param>
  709. 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)
  710. {
  711. //循环生成dtl层级dto,明细
  712. //如果dtl对应的icitem是BOM,还需要向下继续展开。
  713. var help = new SnowFlake();
  714. //List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  715. var dto = new BomChildExamineDto();
  716. var bom = bomlist.WhereIf(true, s => s.icitem_id == dtl.icitem_id).FirstOrDefault();
  717. var icitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
  718. if (icitem == null)
  719. {
  720. return;
  721. }
  722. dto.id = help.NextId();
  723. dto.level = toDto.level;
  724. dto.parent_id = toDto.parent_id;
  725. dto.item_id = icitem.Id;
  726. dto.item_name = icitem.name;
  727. dto.item_code = icitem.number;
  728. dto.num = toDto.num;
  729. dto.model = icitem.model;
  730. dto.unit = icitem.unit;
  731. dto.erp_cls = icitem.erp_cls;
  732. dto.erp_cls_name = icitem.erp_cls_name;
  733. dto.backflush = toDto.backflush;
  734. dto.qty = toDto.qty;
  735. dto.replace_amount = dtl.replace_amount.Value;
  736. dto.is_replace = 0;
  737. dto.haveicsubs = 0;
  738. dto.substitute_code = "";
  739. dto.icitem_ids = "";
  740. dto.substitute_strategy = sl.substitute_strategy.Value;//替代策略
  741. dto.substitute_mode = sl.substitute_mode.Value;//替代方式
  742. dto.type = type;
  743. dto.substitute_all_num = sal.order_num;//群组优先级
  744. if (bom != null)
  745. {
  746. dto.bom_id = bom.Id;
  747. dto.qty = dtl.replace_amount.Value;
  748. dto.isbom = 1;
  749. dto.is_replace = 0;
  750. dto.haveicsubs = 0;
  751. dto.substitute_code = "";
  752. dto.icitem_ids = "";
  753. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  754. }
  755. else
  756. {
  757. dto.bom_id = null;
  758. dto.isbom = 0;
  759. returnlist.Add(dto);
  760. }
  761. }
  762. /// <summary>
  763. /// 计算物料库存量
  764. /// </summary>
  765. /// <param name="returnlist"></param>
  766. public void BomStock(List<BomChildExamineDto> returnlist, long bangid, long orderid, long orderentryid, long factoryid)
  767. {
  768. returnlist = returnlist.OrderBy(s => s.num).ToList();
  769. //取物料库存记录
  770. FilterDefinition<ic_item_stock> filter = Builders<ic_item_stock>.Filter.In(s => s.icitem_id, returnlist.Select(c => c.item_id).ToList());
  771. var stocklist = _ic_item_stock.GetManyByIds(filter).Result;
  772. //取当前订单的物料库存占用记录
  773. var occupylist = _ic_item_stockoccupy.GetManyByCondition(p => p.bang_id == bangid && p.order_id == orderid).Result;
  774. //计算剩余库存
  775. foreach (var item in returnlist)
  776. {
  777. if (item.erp_cls == 4)//虚拟件不计算
  778. {
  779. continue;
  780. }
  781. //非虚拟件
  782. //当前物料的库存数量
  783. decimal stockQty = stocklist.Where(s => s.icitem_id == item.item_id).Sum(p => p.sqty.GetValueOrDefault());
  784. //获取当前订单其他订单行当前物料的占用数量
  785. decimal otherStockQty = occupylist.Where(s => s.icitem_id == item.item_id).Sum(p => p.quantity);
  786. //当前订单行物料库存情况
  787. item.sqty = stockQty - otherStockQty;
  788. }
  789. }
  790. /// <summary>
  791. /// 替代关系检查计算
  792. /// </summary>
  793. public void CalcIcitemSubstitute(List<BomChildExamineDto> returnlist, int count)
  794. {
  795. returnlist = returnlist.OrderBy(s => s.num).ToList();
  796. //1.如果主料够的时候,不需要显示替代料的平铺视图,如果主料不够,显示替代料的平铺视图。
  797. //2.替代策略和替代方式,影响到的是甲乙组概念,替代按主料有限,取代按组的优先级。A与B的替代,则A和B各自会存在一个组。
  798. List<long> calcIds = new List<long>();
  799. //先处理下最顶级的产品需要数量
  800. returnlist[0].needCount = returnlist[0].qty * count;
  801. returnlist[0].satisfyNum = returnlist[0].sqty;
  802. returnlist[0].lack_qty = returnlist[0].needCount - returnlist[0].satisfyNum;
  803. returnlist[0].is_show = true;
  804. foreach (var item in returnlist)
  805. {
  806. //循环平铺整个资源检查的物料库存情况、缺料情况
  807. CaclMaterialShortage(returnlist, item, count);
  808. }
  809. foreach (var item in returnlist)
  810. {
  811. //替代件不计算,替代件通过标准件的替代关系,去计算需要使用哪些物料
  812. if (item.type == 1)
  813. {
  814. continue;
  815. }
  816. CaclBomChildUseShortage(returnlist, item);
  817. }
  818. }
  819. /// <summary>
  820. /// 物料计算
  821. /// </summary>
  822. /// <param name="returnlist"></param>
  823. /// <param name="item"></param>
  824. /// <param name="count"></param>
  825. public void CaclBomChildUseShortage(List<BomChildExamineDto> returnlist, BomChildExamineDto item)
  826. {
  827. //判断是否是BOM,如果是BOM,还需要向下展开
  828. var bomchild = returnlist.Where(s => s.parent_id == item.id && s.type == 0).ToList();
  829. if (bomchild.Count > 0)
  830. {
  831. foreach (var child in bomchild)
  832. {
  833. CaclBomChildUseShortage(returnlist, item);
  834. }
  835. }
  836. var tolist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num).ToList();
  837. //有替代关系
  838. if (item.haveicsubs == 1)
  839. {
  840. //首先判断标准件库存是否满足。
  841. //如果是BOM,也需要向下展开,看子物料是否满足。
  842. //不满足的情况下,则需要展开替代关系,根据替代策略和替代方式,来判定替代件的库存。
  843. //假设子BOM有替代关系,则子BOM的子物料,也是有替代关系存在的,需要展开来看。
  844. //1.假设标准件库存满足,则不计算替代件关系。
  845. //2.如果标准件不满足,则替代件开始计算并展示。
  846. //注意:不管是否需要替代件,标准件都需要展开。
  847. var sublist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num && s.level == item.level).OrderBy(c => c.substitute_all_num).ToList();
  848. //sublist找出当前含替代关系的标准件和替代件。
  849. if (item.substitute_mode == 0)
  850. {
  851. //替代
  852. }
  853. else
  854. {
  855. //取代
  856. }
  857. }
  858. else
  859. {
  860. //无替代关系
  861. if (item.stock_state == 0)
  862. {
  863. item.use_qty = item.needCount;
  864. item.is_show = true;
  865. item.stock_state = 0;
  866. }
  867. else if (item.stock_state == 1)
  868. {
  869. // 缺料数量 item.lack_qty
  870. // 使用数量将库存全部使用了 item.use_qty = item.sqty;
  871. if (item.erp_cls == 2 || item.erp_cls == 3)
  872. {
  873. //委外、外购
  874. //生成委外工单、采购申请单
  875. //得到单据到货时间。
  876. //todo:初步设置为7天到货期,后期根据实际业务来补充修改。
  877. item.kitting_time = DateTime.Now.AddDays(7);
  878. }
  879. else if (item.erp_cls == 1)
  880. {
  881. //自制
  882. //调用产能计算,得到物料自制后的齐套时间。
  883. //todo:初步设置为7天完成,等沟通调用方法,来修改此处。
  884. item.kitting_time = DateTime.Now.AddDays(7);
  885. }
  886. }
  887. }
  888. }
  889. /// <summary>
  890. /// 平铺物料缺料情况,展示所有主料+替代料的库存情况、缺料情况
  891. /// </summary>
  892. /// <param name="returnlist"></param>
  893. /// <param name="item"></param>
  894. /// <param name="count"></param>
  895. public void CaclMaterialShortage(List<BomChildExamineDto> returnlist, BomChildExamineDto item, int count)
  896. {
  897. var parent = returnlist.Find(s => s.id == item.parent_id);
  898. //当前物料总共需要数量
  899. item.needCount = parent.needCount * item.qty;
  900. item.satisfyNum = item.sqty;//5
  901. item.lack_qty = item.needCount - item.satisfyNum;
  902. //如果不满足,计算子物料,或者计算替代料
  903. if (item.lack_qty < 0)
  904. {
  905. //库存满足
  906. item.stock_state = 0;
  907. item.lack_qty = 0;
  908. }
  909. else
  910. {
  911. //找出自己的子集,存在子集则是BOM,不存在子集,则自己非BOM。
  912. var childList = returnlist.Where(s => s.parent_id == item.id && s.type == item.type).ToList();
  913. if (childList.Count > 0)
  914. {
  915. //自己是BOM
  916. foreach (var child in childList)
  917. {
  918. //循环子集判断库存
  919. CaclMaterialShortage(returnlist, child, count);
  920. }
  921. }
  922. else
  923. {
  924. item.stock_state = 1;
  925. }
  926. }
  927. }
  928. }
  929. }