using DOAN.Model.MES.base_; using DOAN.Model; using DOAN.Model.MES.product; using DOAN.Model.MES.product.Dto; using DOAN.Service.MES.product.IService; using Infrastructure.Attribute; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DOAN.Repository; using DOAN.Model.MES.base_.Dto; using DOAN.Model.MES.mm; using Mapster; using Microsoft.AspNetCore.Authentication; namespace DOAN.Service.MES.product { /// /// 报工表Service业务层处理 /// [AppService(ServiceType = typeof(IProReportworkService), ServiceLifetime = LifeTime.Transient)] public class ProReportworkService : BaseService, IProReportworkService { /// /// 查询报工表列表 /// /// /// public PagedInfo GetList(ProReportworkQueryDto parm) { if (parm.TimeRange != null && parm.TimeRange.Length == 2) { parm.TimeRange[0] = parm.TimeRange[0].Date; // parm.TimeRange[1] = parm.TimeRange[1].Date.AddDays(1); parm.TimeRange[1] = parm.TimeRange[1].Date; } var predicate = Expressionable.Create() .AndIF(!string.IsNullOrEmpty(parm.FkWorkorder), (w, r) => w.Workorder.Contains(parm.FkWorkorder)) .AndIF(!string.IsNullOrEmpty(parm.GroupCode), (w, r) => w.GroupCode == parm.GroupCode) .AndIF(!string.IsNullOrEmpty(parm.LineCode), (w, r) => w.LineCode == parm.LineCode) .AndIF(parm.WarehouseconfirmationNum > 0, (w, r) => r.QualifiedNumber > 0) .AndIF( parm.TimeRange != null && parm.TimeRange.Length == 2 && parm.TimeRange[0] > DateTime.MinValue, (w, r) => w.WorkorderDate >= parm.TimeRange[0]) .AndIF( parm.TimeRange != null && parm.TimeRange.Length == 2 && parm.TimeRange[1] > DateTime.MinValue, (w, r) => w.WorkorderDate <= parm.TimeRange[1]) .AndIF(parm.Status > 0, (w, r) => w.Status == parm.Status) ; var response = Context.Queryable() .LeftJoin((w, r) => w.Workorder == r.FkWorkorder) .Where(predicate.ToExpression()) .Select((w, r) => new ProReportworkDto { Id = r.Id, ProductionCode = w.ProductionCode, ProductionName = w.ProductionName, Specification = w.Specification, QualifiedNumber = r.QualifiedNumber, UnqualifiedNumber = r.UnqualifiedNumber, ReworkNumber = r.ReworkNumber, ScrapNumber = r.ScrapNumber, Remark = r.Remark, Difference = r.QualifiedNumber - r.FinishedNum ?? 0, FkWorkorder = w.Workorder, GroupCode = w.GroupCode, LineCode = w.LineCode, DispatchNum = w.DeliveryNum ?? 0, Priority = w.Priority, Status = w.Status, Beat = w.Beat }, true).ToPage_NO_Convert(parm); return response; } /// /// 获取详情 /// /// /// public ProReportwork GetInfo(string Id) { var response = Queryable() .Where(x => x.Id == Id) .First(); return response; } /// /// 添加报工表 /// /// /// public ProReportwork AddProReportwork(ProReportwork model) { model.Id = XueHua; #region 线边库 库存扣除 if (string.IsNullOrEmpty(model.FkWorkorder)) { linesideInventoryDeductions(model.FkWorkorder, 1); } #endregion return Context.Insertable(model).ExecuteReturnEntity(); } /// /// 修改报工表 /// /// /// public int UpdateProReportwork(ProReportwork model) { // 更新线边库 if (model.FinishedNum != null&&string.IsNullOrEmpty(model.FkWorkorder)) { linesideInventoryRecovery(model.FkWorkorder, model.CreatedBy); linesideInventoryDeductions(model.FkWorkorder, model.FinishedNum??0); } //var response = Update(w => w.Id == model.Id, it => new ProReportwork() //{ // FkWorkorder = model.FkWorkorder, // DispatchNum = model.DispatchNum, // FinishedNum = model.FinishedNum, // GroupCode = model.GroupCode, // LineCode = model.LineCode, // CreatedBy = model.CreatedBy, // CreatedTime = model.CreatedTime, // UpdatedBy = model.UpdatedBy, // UpdatedTime = model.UpdatedTime, //}); //return response; return Update(model, true); } public List UserExport(DateTime handleDate) { // DateTime AddhandleDate = handleDate.AddDays(1); return Context.Queryable() .LeftJoin((w, r) => w.Workorder == r.FkWorkorder) .Where((w, r) => w.WorkorderDate == handleDate) .Select((w, r) => new ProReportworkDto() { Id = r.Id, ProductionCode = w.ProductionCode, ProductionName = w.ProductionName, Specification = w.Specification, QualifiedNumber = r.QualifiedNumber, UnqualifiedNumber = r.UnqualifiedNumber, ReworkNumber = r.ReworkNumber, ScrapNumber = r.ScrapNumber, Remark = r.Remark, Difference = r.QualifiedNumber - r.FinishedNum ?? 0, FkWorkorder = w.Workorder, GroupCode = w.GroupCode, LineCode = w.LineCode, DispatchNum = w.DeliveryNum ?? 0, Priority = w.Priority, Status = w.Status, Beat = w.Beat }).ToList(); } /// /// 如果没有id 传入工单 手动生成报工记录 /// /// /// public string ManualGenerationOfReportWork(string workorder, string CreatedBy) { bool isEXsit = Context.Queryable().Where(it => it.FkWorkorder == workorder).Any(); if (isEXsit) { return "-1"; } ProWorkorder handleWorkorder = Context.Queryable().Where(it => it.Workorder == workorder).First(); ProReportwork proReportwork = new ProReportwork(); proReportwork.Id = XueHua; proReportwork.FkWorkorder = workorder; proReportwork.DispatchNum = handleWorkorder.DeliveryNum; proReportwork.FinishedNum = null; proReportwork.GroupCode = handleWorkorder.GroupCode; proReportwork.LineCode = handleWorkorder.LineCode; proReportwork.CreatedBy = CreatedBy; proReportwork.CreatedTime = DateTime.Now; ProReportwork proReportwork1 = Context.Insertable(proReportwork).ExecuteReturnEntity(); return proReportwork1.Id; } /// /// 线边库 库存扣除 /// /// private void linesideInventoryDeductions(string workorder, int reportNum) { var selected_workorder = Context.Queryable().Where(it => it.Workorder == workorder).First(); if (selected_workorder == null) { return; } //TODO 获取实际报工数量 作为线边库库存 出库 // 查询这个工单的BOM List SubMaterialList = Context.Queryable() .Where(it => it.InvCode == selected_workorder.ProductionCode) .ToList(); if (SubMaterialList.Count > 0) { List InsertedSubMaterialOutboundAndInboundList = new List(); List needReduceLinesidebarInventories = new List(); // 添加出库记录 // 扣除库存记录 string[] SubMaterialArray = SubMaterialList.Select(it => it.InvCode).ToArray(); //线边库存 List SideInventoryList = Context.Queryable() .Where(it => it.LineCode == selected_workorder.LineCode) .Where(it => SubMaterialArray.Contains(it.MaterialCode)).ToList(); foreach (var subMaterial in SubMaterialList) { //物料在线边库存 if (SideInventoryList.Where(it => it.MaterialCode == subMaterial.SubInvCode).Any()) { // 记录需要扣除的线边库存 MmLinesidebarInventory sideInventory = new MmLinesidebarInventory(); sideInventory.LineCode = selected_workorder.LineCode; sideInventory.MaterialCode = subMaterial.SubInvCode; var seleced = SideInventoryList.Where(it => it.MaterialCode == subMaterial.SubInvCode).First(); sideInventory.LogicQuantity = seleced.LogicQuantity - reportNum * Convert.ToDecimal(subMaterial.Iusequantity); //库存------需要扣减的数量 needReduceLinesidebarInventories.Add(sideInventory); MmLinesidebarInventoryOutboundAndInbound outbound = new MmLinesidebarInventoryOutboundAndInbound(); outbound.Id = XueHua; outbound.LineCode = selected_workorder.LineCode; outbound.MaterialCode = subMaterial.SubInvCode; outbound.MaterialName = subMaterial.SubInvName; outbound.Action = 1; outbound.WorkOrder = workorder; outbound.Quantity = reportNum * Convert.ToDecimal(subMaterial.Iusequantity); outbound.Remark = $"(正常)触摸屏报工,工单号为{workorder}"; outbound.CreatedBy = "kanban"; outbound.CreatedTime = DateTime.Now; InsertedSubMaterialOutboundAndInboundList.Add(outbound); } else { MmLinesidebarInventoryOutboundAndInbound outbound = new MmLinesidebarInventoryOutboundAndInbound(); outbound.Id = XueHua; outbound.LineCode = selected_workorder.LineCode; outbound.MaterialCode = subMaterial.SubInvCode; outbound.MaterialName = subMaterial.SubInvName; outbound.Action = -1; outbound.WorkOrder = workorder; outbound.Quantity = reportNum * Convert.ToDecimal(subMaterial.Iusequantity); outbound.Remark = $"(异常)触摸屏报工,工单号为{workorder},物料不在这个线边库"; outbound.CreatedBy = "kanban"; outbound.CreatedTime = DateTime.Now; InsertedSubMaterialOutboundAndInboundList.Add(outbound); } } UseTran2(() => { Context.Updateable(needReduceLinesidebarInventories).UpdateColumns(it => new { it.LogicQuantity }) .WhereColumns(it => new { it.LineCode, it.MaterialCode }).ExecuteCommand(); Context.Insertable(InsertedSubMaterialOutboundAndInboundList).ExecuteCommand(); }); } } private void linesideInventoryRecovery(string workorder, string CreatedBy) { // 取消报工 出库 var cancal = Context.Queryable() .Where(it => it.WorkOrder == workorder).OrderByDescending(it => it.CreatedTime).First(); // 取消 报工库存日志 MmLinesidebarInventoryOutboundAndInbound log = new MmLinesidebarInventoryOutboundAndInbound(); log.Id = XueHua; log.LineCode = cancal.LineCode; log.MaterialCode = cancal.MaterialCode; log.MaterialName = cancal.MaterialName; log.Action = 2; log.Quantity = cancal.Quantity; log.Remark = $"取消这个工单{cancal.WorkOrder}的报工消耗,恢复库存;{cancal.Remark}"; log.CreatedBy = CreatedBy; log.CreatedTime = DateTime.Now; UseTran2(() => { // 增加库存 Context.Updateable() .Where(it => it.LineCode == cancal.LineCode) .Where(it => it.MaterialCode == cancal.MaterialCode) .SetColumns(it => it.LogicQuantity == it.LogicQuantity + cancal.Quantity); Context.Insertable(log).ExecuteCommand(); }); } } }