zhuangpei-mesbackend/DOAN.Service/MES/mm/MmPreparantTaskService.cs
2024-09-09 09:03:55 +08:00

353 lines
13 KiB
C#

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.Repository;
using DOAN.Service.group.IService;
using DOAN.Service.MES.mm.IService;
using Infrastructure.Attribute;
using Mapster;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.IdentityModel.Tokens;
using NPOI.SS.Formula.Functions;
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<MmPreparationTask>, IMmPreparantTaskService
{
public List<BaseWorkRoute> GetProcessRouteList()
{
return Context.Queryable<BaseWorkRoute>().Where(it => it.Status == 1).ToList();
}
public List<ProWorkorder> GetWorkOrder(DateTime searchDate, string route_code)
{
searchDate = searchDate.ToLocalTime().Date;
return Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == searchDate)
.WhereIF(!string.IsNullOrEmpty(route_code), it => it.FkRouteCode == route_code)
.ToList();
}
/// <summary>
/// 获取任务
/// </summary>
/// <param name="workorder"></param>
/// <returns></returns>
public List<MmPreparationTaskDto3> GetTaskList(string workorder)
{
List<MmPreparationTaskDto3> MmPreparationTaskList = Context.Queryable<MmPreparationTask>().Where(it => it.FkWorkorder == workorder).ToList().Adapt<List<MmPreparationTaskDto3>>();
if (MmPreparationTaskList != null && MmPreparationTaskList.Count() > 0)
{
// 算工单 子物料需求总数
ProWorkorder workorder1 = Context.Queryable<ProWorkorder>().Where(it => it.Workorder == workorder).First();
List<BaseMaterialBom> subMaterialReq = Context.Queryable<BaseMaterialBom>()
.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<MmTaskMaterialInfo>().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<MmTaskMaterialInfoDto> GetTaskMaterialInfo(string task_code)
{
return Context.Queryable<MmTaskMaterialInfo>().Where(it => it.FkTaskCode == task_code).ToList().Adapt<List<MmTaskMaterialInfoDto>>();
}
public List<MmTaskMaterialInfoANDBOmDto> GetTaskMaterialBOMContrast(string task_code)
{
// 获取bom表的总物料需求
var query = Context.Queryable<MmPreparationTask>().Where(it => it.TaskCode == task_code);
List<MmTaskMaterialInfoANDBOmDto> result = new List<MmTaskMaterialInfoANDBOmDto>();
List<BaseMaterialBomDto2> TotalMaterialRequirements = null;
ProWorkorder proworkorder = Context.Queryable(query).LeftJoin<ProWorkorder>((q, w) => q.FkWorkorder == w.Workorder).Select((q, w) => w).First();
if (proworkorder != null)
{
var query2 = Context.Queryable<BaseMaterialBom>().Where(it => it.InvCode == proworkorder.ProductionCode);
TotalMaterialRequirements = Context.Queryable(query2).LeftJoin<BaseMaterialList>((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<MmPreparationTask>()
.LeftJoin<MmTaskMaterialInfo>((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<MmPreparationTask>()
.LeftJoin<MmTaskMaterialInfo>((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;
}
/// <summary>
/// 新增任务
/// </summary>
/// <param name="workorder"></param>
/// <returns></returns>
public int AddNewTask(string workorder, string CreatedBy)
{
int result = 0;
int Max = Context.Queryable<MmPreparationTask>().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;
}
/// <summary>
/// 删除任务
/// </summary>
/// <param name="task_code"></param>
/// <returns></returns>
public int DeleteTask(string task_code)
{
int result = 0;
UseTran2(() =>
{
result += Context.Deleteable<MmTaskMaterialInfo>().Where(it => it.FkTaskCode == task_code).ExecuteCommand();
result += Context.Deleteable<MmPreparationTask>().Where(it => it.TaskCode == task_code).ExecuteCommand();
});
return result;
}
public int ModifyTaskNum(MmTaskMaterialInfoDto2 parm, string name)
{
int result = 0;
MmTaskMaterialInfo info = new MmTaskMaterialInfo();
info.Id = XueHua;
info.FkTaskCode = parm.FkTaskCode;
info.MaterialCode = parm.MaterialCode;
BaseMaterialList material = null;
if (parm.MaterialCode != null)
{
material = Context.Queryable<BaseMaterialList>().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;
}
/// <summary>
/// 表格形式查询工单 任务及其详情
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<FormsWorkoderAndTaskInfo> TableQuerytaskInfo(FormsWorkoderAndTaskQuery parm)
{
var predicate = Expressionable.Create<ProWorkorder>()
.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.FkRouteCode == parm.route_code)
.AndIF(!string.IsNullOrEmpty(parm.Workorder), it => it.Workorder.Contains(parm.Workorder));
var query1 = Context.Queryable<ProWorkorder>().Where(predicate.ToExpression());
//查询工单绑定的任务
return Context.Queryable(query1).LeftJoin<MmPreparationTask>((q, t) => q.Workorder == t.FkWorkorder)
.LeftJoin<MmTaskMaterialInfo>((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.FkRouteCode,
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<FormsWorkoderAndTaskInfo, FormsWorkoderAndTaskInfo>(parm);
}
public int SwitchTaskPreparation(string task_code, int preparation_status)
{
return Context.Updateable<MmPreparationTask>().Where(it => it.TaskCode == task_code)
.SetColumns(it => it.PreparationStatus == preparation_status)
.ExecuteCommand();
}
/// <summary>
/// 备料大屏
/// </summary>
/// <param name="toHandelDate"></param>
/// <returns></returns>
public MaterialPreparationLargeScreenDto MaterialPreparationLargeScreen(DateTime toHandelDate)
{
return null;
}
}
}