using RIZO.Model.MES.recipe;
using RIZO.Service.MES.recipe.IService;
using Infrastructure.Attribute;
using MDM.Attribute;
using MDM.Model.Process;
using MDM.Models.Flow;
using MDM.Models.Process;
using MDM.Models.Session;
using MDM.Service;
using MDM.Services.Session.IService;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DOAN.Model.MES.product;
namespace MDM.Services.Session
{
///
/// 产品配方关联表Service业务层处理
///
[AppService(ServiceType = typeof(ISessionManagerService), ServiceLifetime = LifeTime.Transient)]
public class SessionManagerService : Service.BaseService, ISessionManagerService
{
///
/// 开工申请
///
/// 过程码
///
public int StartWorkApply(string ProcessCode, string WorkOrder)
{
//校验重复开工
bool isRepeat = Context.Queryable().Where(it => it.Workorder == WorkOrder && it.ProductSN == ProcessCode).Any();
if (isRepeat)
{
return -1;
}
//根据工单号查询工艺路线
ProWorkorder workorder = Context.Queryable()
.Where(it => it.Workorder == WorkOrder).First();
ProcessRouting processRouting = Context.Queryable()
.Where(it => it.FkProductMaterialCode == workorder.ProductionCode)
.First();
ProductLifecycle lifecycle = new ProductLifecycle();
lifecycle.Workorder = WorkOrder;
lifecycle.ProductSN = ProcessCode;
lifecycle.LineCode = workorder.LineCode;
lifecycle.GroupCode = workorder.GroupCode;
lifecycle.ProductStatus = 1;
lifecycle.RoutingCode = processRouting.RoutingCode ?? "";
lifecycle.ProductStartTime = DateTime.Now;
lifecycle.CreatedTime = DateTime.Now;
return Context.Insertable(lifecycle).ExecuteCommand();
}
///
/// 进站申请
///
///
///
///
public InStationApplyResult InStationApply(string OperationCode, string ProcessCode)
{
// 查询本工序的 入站流程
if (string.IsNullOrEmpty(OperationCode) && string.IsNullOrEmpty(ProcessCode))
{
return InStationApplyResult.InvalidParameters;
}
var lifecycle = Context.Queryable()
.Where(it => it.ProductSN == ProcessCode)
.First();
if (lifecycle == null)
{
return InStationApplyResult.ProductNotStartWork;
}
ProcessOperationFlow operationFlow = Context.Queryable()
.Where(it => it.FkRoutingCode == lifecycle.RoutingCode && it.FkOperationCode == OperationCode)
.Where(it => it.FlowTypeCode.Contains("intbound_flow"))
.First();
if (operationFlow != null)
{
// 已知程序集和类名
string assemblyName = "MDM.Services.Flows";
string className = "CommonFlowService"; // 具体的类名
// 加载程序集并获取指定类型
Assembly assembly = Assembly.Load(assemblyName);
Type targetType = assembly.GetType($"{assemblyName}.{className}");
if (targetType != null)
{
// 获取类型中带有RIZOFlow特性的方法
var methods = targetType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (var method in methods)
{
var rizoFlow = method.GetCustomAttribute();
if (rizoFlow != null && rizoFlow.FlowType == operationFlow.FlowTypeCode)
{
// 找到匹配的方法并调用
var instance = Activator.CreateInstance(targetType);
var result = method.Invoke(instance, new object[] { OperationCode, ProcessCode });
// 处理返回结果
if (result is InStationApplyResult applyResult)
{
return applyResult;
}
}
}
}
}
return InStationApplyResult.UnknownStatus;
}
///
/// 中间操作流程规则申请
///
///
///
///
public List MiddleProcessFlowDistribution(string operationCode, string ProcessCode)
{
var lifecycle = Context.Queryable()
.Where(it => it.ProductSN == ProcessCode)
.First();
ProWorkorder proWorkorder = Context.Queryable().Where(it => it.Workorder == lifecycle.Workorder).First();
ProcessOperationWorkstationMapping processOperationWorkstationMapping = Context.Queryable().Where(it => it.FkRoutingCode == lifecycle.RoutingCode && it.FkOperationCode == operationCode && it.FkProductlinebodyCode == proWorkorder.LineCode).First();
List resultList = new List();
//TODO 获取这个工序的中间操作流程
List operationFlows = Context.Queryable().Where(it => !it.FlowTypeCode.Contains("bound_flow")).ToList();
if (operationFlows != null && operationFlows.Count() > 0)
{
// 已知程序集和类名
string assemblyName = "MDM.Services.Flows";
string className = "CommonFlowService"; // 具体的类名
// 加载程序集并获取指定类型
Assembly assembly = Assembly.Load(assemblyName);
Type targetType = assembly.GetType($"{assemblyName}.{className}");
if (targetType != null)
{
// 获取类型中带有RIZOFlow特性的方法
var methods = targetType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (var method in methods)
{
var rizoFlow = method.GetCustomAttribute();
foreach (var operationFlow in operationFlows)
{
if (rizoFlow != null && rizoFlow.FlowType == operationFlow.FlowTypeCode)
{
// 找到匹配的方法并调用
var instance = Activator.CreateInstance(targetType);
// 获取方法的参数信息
var parameters = method.GetParameters();
//工序流程
if (parameters.Length == 3)
{
var result = method.Invoke(instance, new object[] { lifecycle.RoutingCode, operationCode, rizoFlow.FlowCode });
if (result is string list)
{
resultList.Add(list);
}
}
// 工位流程
if (parameters.Length == 5)
{
var result = method.Invoke(instance, new object[] { lifecycle.RoutingCode, operationCode, rizoFlow.FlowCode, processOperationWorkstationMapping.FkProductlinebodyCode, processOperationWorkstationMapping.FkWorkstationCode });
if (result is string list)
{
resultList.Add(list);
}
}
}
}
}
}
}
return resultList;
}
///
/// 出站申请
///
///
///
///
public OutStationApplyResult OutStationApply(string OperationCode, string ProcessCode)
{
// 查询本工序的 出站流程
if (string.IsNullOrEmpty(OperationCode) && string.IsNullOrEmpty(ProcessCode))
{
return OutStationApplyResult.InvalidParameters;
}
var lifecycle = Context.Queryable()
.Where(it => it.ProductSN == ProcessCode)
.First();
if (lifecycle == null)
{
return OutStationApplyResult.ProductNotStartWork;
}
ProcessOperationFlow operationFlow = Context.Queryable()
.Where(it => it.FkRoutingCode == lifecycle.RoutingCode && it.FkOperationCode == OperationCode)
.Where(it => it.FlowTypeCode.Contains("outbound_flow"))
.First();
if (operationFlow != null)
{
// 已知程序集和类名
string assemblyName = "MDM.Services.Flows";
string className = "CommonFlowService"; // 具体的类名
// 加载程序集并获取指定类型
Assembly assembly = Assembly.Load(assemblyName);
Type targetType = assembly.GetType($"{assemblyName}.{className}");
if (targetType != null)
{
// 获取类型中带有RIZOFlow特性的方法
var methods = targetType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (var method in methods)
{
var rizoFlow = method.GetCustomAttribute();
if (rizoFlow != null && rizoFlow.FlowType == operationFlow.FlowTypeCode)
{
// 找到匹配的方法并调用
var instance = Activator.CreateInstance(targetType);
var result = method.Invoke(instance, new object[] { OperationCode, ProcessCode });
// 处理返回结果
if (result is OutStationApplyResult applyResult)
{
return applyResult;
}
}
}
}
}
return OutStationApplyResult.UnknownStatus;
}
///
/// 完成工单申请
///
///
///
///
public int FinishWorkApply(string ProcessCode, string WorkOrder)
{
//校验重复完工
bool isRepeat = Context.Queryable()
.Where(it => it.Workorder == WorkOrder && it.ProductSN == ProcessCode)
.Where(it => it.ProductStatus == 2)
.Any();
if (isRepeat)
{
return -1;
}
return Context.Updateable()
.SetColumns(it => new ProductLifecycle()
{
ProductStatus = 2,
ProductFinishTime = DateTime.Now,
UpdatedTime = DateTime.Now
})
.Where(it => it.ProductSN == ProcessCode && it.Workorder == WorkOrder)
.ExecuteCommand();
}
///
/// 返工申请
///
///
///
///
public int ReWorkApply(string ProcessCode, string WorkOrder)
{
//校验重复返工
bool isRepeat = Context.Queryable()
.Where(it => it.Workorder == WorkOrder && it.ProductSN == ProcessCode)
.Where(it => it.ProductStatus == 3)
.Any();
if (isRepeat)
{
return -1;
}
return Context.Updateable()
.SetColumns(it => new ProductLifecycle()
{
ProductStatus = 3,
ProductFinishTime = DateTime.Now,
UpdatedTime = DateTime.Now
})
.Where(it => it.ProductSN == ProcessCode && it.Workorder == WorkOrder)
.ExecuteCommand();
}
}
}