using DOAN.Model; using DOAN.Model.MES.base_; using DOAN.Model.MES.base_.Dto; using DOAN.Model.MES.mm; using DOAN.Model.MES.mm.Dto; using DOAN.Model.MES.product; using DOAN.Model.Mobile.Dto; using DOAN.Repository; using DOAN.Service.group.IService; using DOAN.Service.MES.base_; using DOAN.Service.MES.mm.IService; using Infrastructure.Attribute; using Mapster; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Routing; using Microsoft.IdentityModel.Tokens; using NPOI.SS.Formula.Functions; using NPOI.Util; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DOAN.Service.MES.mm { [AppService(ServiceType = typeof(IMmPreparantTaskService), ServiceLifetime = LifeTime.Transient)] public class MmPreparantTaskService : BaseService, IMmPreparantTaskService { public List GetProcessRouteList() { return Context.Queryable().Where(it => it.Status == 1).ToList(); } public List GetWorkOrder(DateTime searchDate, string route_code) { searchDate = searchDate.ToLocalTime().Date; return Context.Queryable().Where(it => it.WorkorderDate == searchDate) .WhereIF(!string.IsNullOrEmpty(route_code), it => it.LineCode == route_code) .ToList(); } /// /// 获取任务 /// /// /// public List GetTaskList(string workorder) { List MmPreparationTaskList = Context.Queryable().Where(it => it.FkWorkorder == workorder).ToList().Adapt>(); if (MmPreparationTaskList != null && MmPreparationTaskList.Count() > 0) { // 算工单 子物料需求总数 ProWorkorder workorder1 = Context.Queryable().Where(it => it.Workorder == workorder).First(); List subMaterialReq = Context.Queryable() .Where(it => it.InvCode == workorder1.ProductionCode).ToList(); float MaterialRequire_totals = workorder1.DeliveryNum.Value * subMaterialReq.Sum(it => float.Parse(it.Iusequantity)); float sum = 0; foreach (var item in MmPreparationTaskList) { //计算每个任务 配料数量总数 decimal task_Quantity = Context.Queryable().Where(it => it.FkTaskCode == item.TaskCode).Sum(it => it.Quantity); // 计算百分比 float percentage = ((float)task_Quantity / MaterialRequire_totals) * 100; // 保留两位小数 float roundedPercentage = (float)Math.Round(percentage, 1); sum += roundedPercentage; item.Percentage = (float)Math.Round(sum, 1); } } return MmPreparationTaskList; } public List GetTaskMaterialInfo(string task_code) { return Context.Queryable().Where(it => it.FkTaskCode == task_code).ToList().Adapt>(); } public List GetTaskMaterialBOMContrast(string task_code) { // 获取bom表的总物料需求 var query = Context.Queryable().Where(it => it.TaskCode == task_code); List result = new List(); List TotalMaterialRequirements = null; ProWorkorder proworkorder = Context.Queryable(query).LeftJoin((q, w) => q.FkWorkorder == w.Workorder).Select((q, w) => w).First(); if (proworkorder != null) { var query2 = Context.Queryable().Where(it => it.InvCode == proworkorder.ProductionCode); TotalMaterialRequirements = Context.Queryable(query2).LeftJoin((bom, list) => bom.SubInvCode == list.Code) .Select((bom, list) => new BaseMaterialBomDto2() { Specification = list.Specification, Unit = list.Unit, }, true).ToList(); if (TotalMaterialRequirements != null && TotalMaterialRequirements .Count() > 0) { foreach (var item in TotalMaterialRequirements) { float num = float.Parse(item.Iusequantity) * proworkorder.DeliveryNum.Value; item.Iusequantity = num.ToString(); MmTaskMaterialInfoANDBOmDto result_item = new MmTaskMaterialInfoANDBOmDto(); result_item.MaterialCode = item.SubInvCode; result_item.MaterialName = item.SubInvName; result_item.BOM_require_Quantity = decimal.Parse(item.Iusequantity); result_item.Unit = item.Unit; result_item.Specification = item.Specification; result.Add(result_item); } } } if (result.Count() > 0) { // 已经满足的物料总需求 var configuredList = Context.Queryable() .LeftJoin((t, i) => t.TaskCode == i.FkTaskCode) .Where((t, i) => t.FkWorkorder == proworkorder.Workorder) .GroupBy((t, i) => i.MaterialCode) .Select((t, i) => new { MaterialCode = i.MaterialCode, configured_all_Quantity = SqlFunc.AggregateSum(i.Quantity), }).ToList(); // 其中本次任务满足的物料需求 var configuredList2 = Context.Queryable() .LeftJoin((t, i) => t.TaskCode == i.FkTaskCode) .Where((t, i) => t.TaskCode == task_code) .GroupBy((t, i) => i.MaterialCode) .Select((t, i) => new { MaterialCode = i.MaterialCode, task_Quantity = SqlFunc.AggregateSum(i.Quantity) }).ToList(); foreach (var item in result) { foreach (var configured in configuredList) { if (item.MaterialCode == configured.MaterialCode) { item.configured_all_Quantity = configured.configured_all_Quantity; } } foreach (var configured in configuredList2) { if (item.MaterialCode == configured.MaterialCode) { item.task_Quantity = configured.task_Quantity; } } } } return result; } /// /// 新增任务 /// /// /// public int AddNewTask(string workorder, string CreatedBy) { int result = 0; int Max = Context.Queryable().Where(it => it.FkWorkorder == workorder).Max(it => it.SerialNum); MmPreparationTask preparationTask = new MmPreparationTask(); preparationTask.Id = XueHua; preparationTask.SerialNum = Max + 1; preparationTask.TaskCode = workorder + "_" + preparationTask.SerialNum; preparationTask.FkWorkorder = workorder; preparationTask.PreparationStatus = 1; preparationTask.CreatedBy = CreatedBy; preparationTask.CreatedTime = DateTime.Now; result = Context.Insertable(preparationTask).ExecuteCommand(); return result; } /// /// 删除任务 /// /// /// public int DeleteTask(string task_code) { //TODO 取消入库 //CancelInbound(task_code); int result1 = 0; UseTran2(() => { result1 += Context.Deleteable().Where(it => it.FkTaskCode == task_code).ExecuteCommand(); result1 += Context.Deleteable().Where(it => it.TaskCode == task_code).ExecuteCommand(); }); return result1; } public int ModifyTaskNum(MmTaskMaterialInfoDto2 parm, string name) { //TODO 先取消入库数后,新增入库 //TODO 取消入库 //TODO 这个地方不就修改个数量,怎么扯一大坨 int result = 0; MmTaskMaterialInfo info = new MmTaskMaterialInfo(); info.Id = XueHua; info.FkTaskCode = parm.FkTaskCode; info.MaterialCode = parm.MaterialCode; BaseMaterialList material = null; if (string.IsNullOrEmpty(parm.MaterialCode)) { material = Context.Queryable().Where(it => it.Code == parm.MaterialCode).First(); } info.MaterialName = material?.Name; info.Specification = material?.Specification; info.Quantity = parm.Quantity; info.Unit = material?.Unit; info.CreatedBy = name; info.CreatedTime = DateTime.Now; info.UpdatedBy = name; info.UpdatedTime = DateTime.Now; var x = Context.Storageable(info) .WhereColumns(it => new { it.FkTaskCode, it.MaterialCode }).ToStorage(); result += x.AsInsertable.ExecuteCommand(); result += x.AsUpdateable.IgnoreColumns(z => new { z.Id, z.CreatedBy, z.CreatedTime, z.MaterialName, z.Specification, z.Unit }).ExecuteCommand(); return result; } /// /// 表格形式查询工单 任务及其详情 /// /// /// public PagedInfo TableQuerytaskInfo(FormsWorkoderAndTaskQuery parm) { var predicate = Expressionable.Create() .AndIF(parm.searchDate != null && parm.searchDate.Length >= 2 && parm.searchDate[0] > DateTime.MinValue, it => it.WorkorderDate >= parm.searchDate[0].ToLocalTime().Date) .AndIF(parm.searchDate != null && parm.searchDate.Length >= 2 && parm.searchDate[1] > DateTime.MinValue, it => it.WorkorderDate >= parm.searchDate[1].ToLocalTime().Date) .AndIF(!string.IsNullOrEmpty(parm.route_code), it => it.LineCode == parm.route_code) .AndIF(!string.IsNullOrEmpty(parm.Workorder), it => it.Workorder.Contains(parm.Workorder)); var query1 = Context.Queryable().Where(predicate.ToExpression()); //查询工单绑定的任务 return Context.Queryable(query1).LeftJoin((q, t) => q.Workorder == t.FkWorkorder) .LeftJoin((q, t, i) => t.TaskCode == i.FkTaskCode) .Select((q, t, i) => new FormsWorkoderAndTaskInfo() { Id = t.Id, SerialNum = t.SerialNum, TaskCode = t.TaskCode, FkWorkorder = q.Workorder, RouteCode = q.LineCode, DeliveryNum = q.DeliveryNum, ProductionName = q.ProductionName, ProductionCode = q.ProductionCode, CustomCode = q.CustomCode, WorkorderDate = q.WorkorderDate, FkTaskCode = i.FkTaskCode, PreparationStatus = t.PreparationStatus, CreatedBy = t.CreatedBy, CreatedTime = t.CreatedTime, UpdatedTime = t.UpdatedTime, UpdatedBy = t.UpdatedBy, SubId = i.Id, MaterialCode = i.MaterialCode, MaterialName = i.MaterialName, Specification = i.Specification, Quantity = i.Quantity, Unit = i.Unit, SubCreatedBy = i.CreatedBy, SubCreatedTime = i.CreatedTime, SubUpdatedBy = i.UpdatedBy, SubUpdatedTime = i.UpdatedTime, }).ToPage(parm); } public PagedInfo TableQuerytaskInfoBYline(FormsWorkoderAndTaskQuery2 parm) { var predicte1 = Expressionable.Create() .AndIF(parm.searchDate != null && parm.searchDate.Length >= 2 && parm.searchDate[0] > DateTime.MinValue, it => it.TaskDate >= parm.searchDate[0].ToLocalTime().Date) .AndIF(parm.searchDate != null && parm.searchDate.Length >= 2 && parm.searchDate[1] > DateTime.MinValue, it => it.TaskDate <= parm.searchDate[1].ToLocalTime().Date) .AndIF(!string.IsNullOrEmpty(parm.route_code), it => it.LineCode == parm.route_code); var query1 = Context.Queryable().Where(predicte1.ToExpression()); return Context.Queryable(query1).LeftJoin((q, i) => q.TaskCode == i.FkTaskCode) .OrderBy((q, i) => q.TaskDate) .OrderBy((q, i) => q.LineCode) .OrderBy((q, i) => q.SerialNum) .Select((q, i) => new FormLineAndTaskAndTaskInfo { Id = q.Id, SerialNum = q.SerialNum, TaskCode = q.TaskCode, LineCode = q.LineCode, TaskDate = q.TaskDate, PreparationStatus = q.PreparationStatus, MaterialCode = i.MaterialCode, MaterialName = i.MaterialName, Specification = i.Specification, Quantity = i.Quantity, Unit = i.Unit, CreatedBy = i.CreatedBy, CreatedTime = i.CreatedTime, UpdatedBy = i.UpdatedBy, UpdatedTime = i.UpdatedTime, }).ToPage(parm); } public List ExportPreparantTask(FormsWorkoderAndTaskQuery2 parm) { var predicte1 = Expressionable.Create() .AndIF(parm.searchDate != null && parm.searchDate.Length >= 2 && parm.searchDate[0] > DateTime.MinValue, it => it.TaskDate >= parm.searchDate[0].ToLocalTime().Date) .AndIF(parm.searchDate != null && parm.searchDate.Length >= 2 && parm.searchDate[1] > DateTime.MinValue, it => it.TaskDate <= parm.searchDate[1].ToLocalTime().Date) .AndIF(!string.IsNullOrEmpty(parm.route_code), it => it.LineCode == parm.route_code); var query1 = Context.Queryable().Where(predicte1.ToExpression()); return Context.Queryable(query1).LeftJoin((q, i) => q.TaskCode == i.FkTaskCode) .OrderBy((q, i) => q.TaskDate) .OrderBy((q, i) => q.LineCode) .OrderBy((q, i) => q.SerialNum) .Select((q, i) => new FormLineAndTaskAndTaskInfo { Id = q.Id, SerialNum = q.SerialNum, TaskCode = q.TaskCode, LineCode = q.LineCode, TaskDate = q.TaskDate, PreparationStatus = q.PreparationStatus, MaterialCode = i.MaterialCode, MaterialName = i.MaterialName, Specification = i.Specification, Quantity = i.Quantity, Unit = i.Unit, CreatedBy = i.CreatedBy, CreatedTime = i.CreatedTime, UpdatedBy = i.UpdatedBy, UpdatedTime = i.UpdatedTime, }).ToList(); } public int SwitchTaskPreparation(string task_code, int preparation_status) { return Context.Updateable().Where(it => it.TaskCode == task_code) .SetColumns(it => it.PreparationStatus == preparation_status) .ExecuteCommand(); } /// /// 备料大屏 /// /// /// public List MaterialPreparationLargeScreen(DateTime toHandelDate) { List screen = new List(); //TODO 查询工单 toHandelDate = toHandelDate.ToLocalTime().Date; var query = Context.Queryable().Where(it => it.WorkorderDate == toHandelDate); var repsonse = Context.Queryable(query).LeftJoin((q, r) => q.LineCode == r.Code) .Select((q, r) => new { searchDate = q.WorkorderDate, RouteName = r.Name, RouteCode = r.Code, WorkOrder = q.Workorder, }).ToList(); //TODO 根据工单号 查询每个工单号进度 var result = repsonse.GroupBy(it => it.RouteCode).Select(g => new { RouteCode = g.Key, searchDate = g.Max(k => k.searchDate), RouteName = g.Max(k => k.RouteName), workorderList = g.Select(k => k.WorkOrder).ToArray() }).ToList(); screen = result.Adapt>(); if (screen.Count > 0) { foreach (var item in screen) { item.materialPreparationProgresses = SearchMaterialPreparationProgress(item.workorderList); } } return screen; } /// /// 根据工单号 查询每个工单号进度 /// /// /// private List SearchMaterialPreparationProgress(string[] Workorders) { List screenDtos = new List(); if (Workorders.Length > 0) { foreach (var Workorder in Workorders) { MaterialPreparationProgress progress = new MaterialPreparationProgress(); //TODO 查询工单总任务数 progress.WorkOrder = Workorder; progress.Preparation_all_num = Context.Queryable().Where(it => it.FkWorkorder == Workorder) .Count(); progress.Preparationed_num = Context.Queryable().Where(it => it.FkWorkorder == Workorder) .Where(it => it.PreparationStatus == 2) .Count(); if (progress.Preparationed_num == progress.Preparation_all_num) { progress.PreparationStatus = 2; } else if (progress.Preparationed_num < progress.Preparation_all_num) { progress.PreparationStatus = 1; } if (progress.Preparation_all_num == 0) { progress.PreparationStatus = 0; } screenDtos.Add(progress); } } return screenDtos; } } }