using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Domain.Repositories; using Business.Models; using Microsoft.AspNetCore.Authorization; using Business.Permissions; using XCZ; using Business.ResourceExamineManagement.Dto; using Bussiness.Model.MES.IC; using AutoMapper.Internal.Mappers; using Bussiness.Model.Tech; using Bussiness.Model.Production; using Business.Core.MongoDBHelper; using Business.Core.Utilities; using Hangfire.Storage.Monitoring; using Business.BookManagement.Dto; using Volo.Abp.ObjectMapping; using Volo.Abp.Application.Services; using ZstdSharp.Unsafe; using System.Transactions; using NUglify.JavaScript.Syntax; using System.Linq.Expressions; using XCZ.Extensions; using System.ComponentModel.Design; using Volo.Abp.Validation.StringValues; using System.Runtime.CompilerServices; using MongoDB.Driver; namespace Business.ResourceExamineManagement { /// /// 资源检查 /// //[Authorize(BusinessPermissions.ResourceExamine.Default)] public class ResourceExamineAppService : ApplicationService, IResourceExamineAppService { #region 服务 /// /// 工艺路径 /// private readonly IMongoDB _mes_technique; /// /// 工序 /// private readonly IMongoDB _mes_process; /// /// 工艺关联工序 /// private readonly IMongoDB _mes_tech_process; /// /// 工作日历 /// private readonly IMongoDB _mes_work_calendar; /// /// 工作日历明细 /// private readonly IMongoDB _mes_work_calendar_list; /// /// 工艺工序关联工位 /// private readonly IMongoDB _mes_tech_proc_workshop; /// /// 排程占用记录 /// private readonly IMongoDB _mes_schedule_occupy; /// /// 物料详情 /// private readonly IMongoDB _ic_item; private IBasicRepository _mysql_ic_item; /// /// 物料BOM /// private readonly IMongoDB _ic_bom; private IBasicRepository _mysql_ic_bom; /// /// 物料BOM明细 /// private readonly IMongoDB _ic_bom_child; private IBasicRepository _mysql_ic_bom_child; /// /// 物料库存表 /// private readonly IMongoDB _ic_item_stock; /// /// 生产工单主表 /// private readonly IMongoDB _mes_morder; /// /// 在制工单占用记录表 /// private readonly IMongoDB _mes_mooccupy; #endregion /// /// 构造函数 /// /// /// public ResourceExamineAppService( IMongoDB mes_technique, IMongoDB mes_process, IMongoDB mes_tech_process, IMongoDB mes_tech_proc_workshop, IMongoDB ic_item, IMongoDB ic_bom, IMongoDB ic_bom_child, IMongoDB ic_item_stock, IMongoDB mes_morder, IMongoDB mes_mooccupy, IBasicRepository mysql_ic_item, IBasicRepository mysql_ic_bom, IBasicRepository mysql_ic_bom_child ) { _mes_technique = mes_technique; _mes_process = mes_process; _mes_tech_process = mes_tech_process; _mes_tech_proc_workshop = mes_tech_proc_workshop; _ic_item = ic_item; _ic_bom = ic_bom; _ic_bom_child = ic_bom_child; _ic_item_stock = ic_item_stock; _mes_morder = mes_morder; _mysql_ic_item = mysql_ic_item; _mysql_ic_bom = mysql_ic_bom; _mysql_ic_bom_child = mysql_ic_bom_child; } /// /// mongoDB示例方法,后期删除 /// /// public async Task test() { //多条插入 List infos = new List(); mes_technique info; for (int i = 0; i < 3; i++) { info = new mes_technique(); info.GenerateNewId(); info.tech_name = "多条" + i; info.level = i; infos.Add(info); } await _mes_technique.InsertMany(infos); var info2 = _mes_technique.GetAll().Result; var a = new PschedDto(); a.count = info2.Count; //获取数据 var info1 = await _mes_technique.GetOneByID((long)1732029975067480064); //更新数据 info1.tech_name = "更新***"; var rlt = await _mes_technique.UpdateOne(info1, info1.Id); //根据条件查询数据 Expression> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0"; var info3 = await _mes_technique.GetManyByCondition(filter); } /// /// 资源检查 /// /// /// /// public async Task ReceiveResult(SeorderentryDto input) { //测试代码 //await test(); await ProductiveExamine(1733221167209762816,100,1000); return null; throw new NotImplementedException(); } /// /// 产能计算 /// /// 工艺路径主键 /// 需要生产产品件数 /// 需要生产数量 public async Task ProductiveExamine(long tech_id, int packages, int quantity) { if (packages <=0 ||quantity <= 0) { throw new NotImplementedException("产能计算参数有误!"); } #region 1、数据准备 //1.1、获取工艺路径数据 mes_technique tech = _mes_technique.GetOneByID(tech_id).Result; //1.2、获取工艺路径关联工序数据 List tech_Processes = _mes_tech_process.GetManyByCondition(x => x.tech_id == tech_id).Result; //1.3、获取当前工艺路径下的工序数据 FilterDefinition filter = Builders.Filter.In(s => s.Id, tech_Processes.Select(m => m.proc_id).ToList()); List process = _mes_process.GetManyByIds(filter).Result; //1.3、获取工艺工序关联工位信息 FilterDefinition filter1 = Builders.Filter.In(s => s.tech_proc_id, tech_Processes.Select(m => m.Id).ToList()); List tech_Proc_Workshops = _mes_tech_proc_workshop.GetManyByIds(filter1).Result; //1.4、获取工位占用情况 //List schedule_Occupies = _mes_schedule_occupy.GetManyByCondition().Result; #endregion #region 计算产能,得到耗时 /*产能计算有两种模式:1-UPH;2-节拍时间 *1、根据工艺路径设置的效率计算层级,来获取参与产能计算的所有工序/子工序 *2、根据当前工序设置的计算模式UPH/节拍时间来计算当前工序生产所需时间T1 *3、当前工序生产所需时间T1*效率系数Rate得到实际的生产所需时间T2(流水线不需要*效率系数) *4、当前工序实际生产所需时间T2+当前工序前置准备时间得到当前工序生产所需总时间 *5、所有工序生产总时间加起来得到生产所需总时间 */ //1、获取参与计算的所有工序/子工序 var tech_pro_list = tech_Processes.Where(p => p.level == tech.level).ToList(); //生产总时间(分钟) decimal sumTimes = 0.00m; //记录按照CT参与计算的工序的父级Id List exists = new List(); foreach (var item in tech_pro_list) { //数据校验 //获取当前工序 var curProcess = process.FirstOrDefault(p => p.Id == item.proc_id); if (item.type == 1 && item.ct == 0) { throw new NotImplementedException(string.Format("工艺[{0}]下的工序[{1}]节拍时间设置有误,请调整!",tech.tech_name, curProcess.proc_name)); } if (item.type == 2 && item.uph == 0) { throw new NotImplementedException(string.Format("工艺[{0}]下的工序[{1}]UPH设置有误,请调整!", tech.tech_name, curProcess.proc_name)); } if (item.parentprocid != null)//存在父子级关系 { //判断当前父级是否已经参与过计算 if (exists.Contains(item.parentprocid.Value)) { continue; } //记录父级id exists.Add(item.parentprocid.Value); //获取同一父级下的子工序 var childs = tech_Processes.Where(p => p.parentprocid == item.parentprocid).ToList(); decimal maxTime = 0.00m;//记录同一父级下最大耗时 decimal time = 0.00m; //2、两种计算模式UPH/节拍时间 if (item.type == 2)//UPH(每小时生产个数),取同一父级下最长耗时 { foreach (var chd in childs) { //当前工序消耗时间 time = quantity / item.uph.Value * 60 * item.effect_ratio.Value; if (time > maxTime) { maxTime = time; } } sumTimes += maxTime; continue; } //3、根据节拍时间计算 //sum(CT) decimal sumCT = childs.Sum(p => p.ct.Value); //max(CT) decimal maxCT = childs.OrderByDescending(p => p.ct).FirstOrDefault().ct.Value; //sum(LT) decimal sumLT = childs.Sum(p => p.readytime.Value); //当前流水线消耗时间 sumTimes += (packages - 1) * maxCT + sumCT + sumLT; } else{//不存在父子级关系 //2、两种计算模式UPH/节拍时间 if (item.type == 2)//uph { //当前工序消耗时间 sumTimes += quantity / item.uph.Value * 60 * item.effect_ratio.Value + item.readytime.Value; //计算完成,进行下一次循环 continue; } //节拍时间:流水线,从流水线最后工序开始计算 if (item.nextprocid != null) { //下-工序Id不为null,说明不是流水线最终工序,进行下一次循环 continue; } List allProcess = new List(); //递归,获取最终工序的上一工序 GetPreProcess(tech_Processes, item, allProcess); //sum(CT) decimal sumCT = allProcess.Sum(p => p.ct.Value); //max(CT) decimal maxCT = allProcess.OrderByDescending(p => p.ct).FirstOrDefault().ct.Value; //sum(LT) decimal sumLT = allProcess.Sum(p => p.readytime.Value); //当前流水线消耗时间 sumTimes += (packages - 1) * maxCT + sumCT + sumLT; } } #endregion return DateTime.Now.AddDays(1).AddMinutes((double)sumTimes); } /// /// 递归获取工序 /// /// /// /// private void GetPreProcess(List tech_Processes, mes_tech_process item, List allProcess) { allProcess.Add(item); //获取上一工序 var preProc = tech_Processes.FirstOrDefault(p => p.nextprocid == item.proc_id); if (preProc != null) { GetPreProcess(tech_Processes, preProc, allProcess); } } /// /// 检查在制工单 /// /// Bom编码 /// 需要数量 /// 交付日期 /// public async Task> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate) { if (string.IsNullOrEmpty(bomNumber) || Quantity != null) { //TODO:入参异常; throw new NotImplementedException("BOM编码或需求数量不能为空!"); } //根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。 //TODO:工单类型; Expression> filter = x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭" && x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false; var morderList = await _mes_morder.GetManyByCondition(filter); //首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。 //工单已被占用后要与占用表关联查询...减去占用量后 剩下生产数量可供下个销售工单使用。 Expression> mooccupyfilter = x => x.moo_state == 1 && x.IsDeleted == false; var mes_mooccupyList = await _mes_mooccupy.GetManyByCondition(mooccupyfilter); //morderList = morderList.Where(x => x.Id.IsIn(mes_mooccupyList.Select(p => p.moo_moid))).ToList(); //当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。 var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate && (x.morder_production_number - x.inventory_number) > Quantity).ToList(); if (morderDataList.Count > 0) { //存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。 var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault(); //生成mes_mooccupy工单占用表数据,代表此工单被某个销售订单已占用。 mes_mooccupy mes_Mooccupy = new mes_mooccupy(); mes_Mooccupy.GenerateNewId(); mes_Mooccupy.moo_id_type = "分配"; mes_Mooccupy.moo_id_billid = 0;//销售订单ID mes_Mooccupy.fbill_no = "0";//销售订单号 mes_Mooccupy.fentry_id = 0;//销售订单行 mes_Mooccupy.fitem_name = string.Empty;//物料名称 mes_Mooccupy.fitem_number = bomNumber; mes_Mooccupy.fmodel = string.Empty;//规格型号 mes_Mooccupy.moo_moid = morder.Id; mes_Mooccupy.moo_mo = morder.morder_no; mes_Mooccupy.moo_qty = Quantity; mes_Mooccupy.moo_stime = DateTime.Now; mes_Mooccupy.moo_etime = DateTime.Now;//日期来源需确定 mes_Mooccupy.moo_state = 1; mes_Mooccupy.moo_cbr = string.Empty; //mes_Mooccupy.moo_ctime = ; mes_Mooccupy.moo_creason = string.Empty; mes_Mooccupy.tenant_id = 0; await _mes_mooccupy.InsertOne(mes_Mooccupy); } else { // 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量 var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList(); if (morderListData.Count == 0) { //TODO:后期处理无在制工单返回内容 throw new NotImplementedException("无可用在制工单!"); } List mes_Morders = new List(); List mes_Mooccupies = new List(); decimal? number = Quantity; foreach (var item in morderListData) { //查询出工单已占用总数量 var mes_mooccupy = mes_mooccupyList.Where(x => x.moo_moid == item.Id).ToList(); // 需要数量-(生产计划数量-入库数量-已被占用数量) number = number - (item.morder_production_number - item.inventory_number - mes_mooccupy.Sum(x => x.moo_qty)); if (number <= 0) { mes_Morders.Add(item); } else { mes_Morders.Add(item); break; } } //生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。 } return morderList; #region 注释 /* var morderList = _mes_morder.GetAll().Result; //查询生产工单类型为计划工单并且工单类型为备库工单 并且BOM编码一致数据,订单状态为 初始或下达工单 var morderTypeList = morderList.Where(x => x.work_order_type == "备库工单" && x.morder_type == "计划工单" && x.bom_number == bomNumber && (x.morder_state == "初始" || x.morder_state == "下达")).ToList(); //计划数量-需求数量=剩余可用数量 -->使用计划结束时间排序取第一条满足条件工单 // List =>数量满足Quantity && 生成工单结束日期最早 var SatisfyQuantityList = morderTypeList.Where(x => (x.morder_production_number.Value - x.need_number.Value) >= Quantity). ToList().OrderBy(x => x.moentry_etime).FirstOrDefault(); if (SatisfyQuantityList == null) { } else { }*/ #endregion } public void BomPretreatment(long? orderid, long? BomId, int Quantity) { if (orderid == null) { //throw new bu } if (BomId == null) { //throw new bu } //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList(); var help = new SnowFlake(); var bomlist = _ic_bom.GetAll().Result; var bomchildlist = _ic_bom_child.GetAll().Result; var icitemlist = _ic_item.GetAll().Result; List returnlist = new List(); var dto = new BomChildExamineDto(); dto.bom_id = BomId.Value; dto.level = 1; dto.id = help.NextId(); dto.parent_id = help.NextId(); dto.qty = 1; dto.num = "1"; dto.isbom = 1; dto.is_replace = 0; dto.haveicsubs = 0; dto.substitute_code = ""; dto.icitem_ids = ""; GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist); } public void GetBomList(List bomlist, List bomchildlist, List icitemlist, BomChildExamineDto dto, List returnlist) { int level = dto.level++;//初始化定义level层级 var help = new SnowFlake(); var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault(); var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault(); if (bom == null || item == null) { } //var dto = new BomChildExamineDto(); //dto.level = level; //dto.bom_id = bomId; //dto.id = help.NextId(); //dto.parent_id = parent_id; dto.item_id = bom.icitem_id; dto.item_name = bom.item_name; dto.item_code = bom.item_number; dto.model = item.model; dto.unit = bom.unit; dto.erp_cls = item.erp_cls; dto.erp_cls_name = item.erp_cls_name; //var bdto = ObjectMapper.Map(bom); returnlist.Add(dto); var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList(); int idx = 1; foreach (var c in childlist) { string childNum = dto.num + "." + idx.ToString(); var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault(); //如果此明细查的到BOM信息,则代表此child是一个子BOM。 if (c.is_bom == 1) { var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault(); if (childBom != null) { var cdto = new BomChildExamineDto(); cdto.id = help.NextId(); cdto.level = level; cdto.parent_id = dto.id; cdto.qty = c.qty.Value; cdto.backflush = c.backflush; cdto.num = childNum; cdto.isbom = 1; cdto.is_replace = c.is_replace; cdto.haveicsubs = c.haveicsubs; cdto.substitute_code = c.substitute_code; cdto.icitem_ids = c.icitem_ids; GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist); } } else { if (icitem != null) { var childDto = new BomChildExamineDto(); childDto.level = level++; childDto.bom_id = dto.bom_id; childDto.id = help.NextId(); childDto.parent_id = dto.id; childDto.item_id = icitem.Id; childDto.item_name = icitem.name; childDto.item_code = icitem.number; childDto.num = childNum; childDto.model = icitem.model; childDto.unit = c.unit; childDto.erp_cls = item.erp_cls; childDto.erp_cls_name = item.erp_cls_name; childDto.backflush = c.backflush; childDto.qty = c.qty.Value; childDto.isbom = 0; childDto.is_replace = c.is_replace; childDto.haveicsubs = c.haveicsubs; childDto.substitute_code = c.substitute_code; childDto.icitem_ids = c.icitem_ids; returnlist.Add(childDto); } } idx++; } } /// /// 计算库存量 /// /// public void BomStock(List returnlist) { returnlist = returnlist.OrderBy(s => s.num).ToList(); List idlist = new List(); foreach (var item in returnlist) { } } } }