using Aucma.Scada.Model.domain;
using HighWayIot.Config;
using HighWayIot.Log4net;
using HighWayIot.Repository.service;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Aucma.Scada.Business
{
///
/// 组装计划业务逻辑
///
public sealed class AssemblyPlanBusiness
{
#region 单例实现
private static readonly Lazy lazy = new Lazy(() => new AssemblyPlanBusiness());
public static AssemblyPlanBusiness Instance
{
get
{
return lazy.Value;
}
}
#endregion
#region 对象引用
private LogHelper logHelper = LogHelper.Instance;
private AppConfig appConfig = AppConfig.Instance;
private RegisterServices registerServices = RegisterServices.Instance;
#endregion
#region 接口引用
///
/// 生产计划
///
private IProductPlanInfoService _productPlanInfoService;
///
/// 执行计划
///
private IExecutePlanInfoService _executePlanInfoService;
///
/// BOM信息
///
private IBaseBomInfoService _bomInfoService;
///
/// 货道明细
///
private IBaseSpaceDetailService _spaceDetailService;
///
/// 货道信息
///
private IBaseSpaceInfoService _spaceInfoService;
private IRealTaskInfoService _realTaskInfoService;
#endregion
#region 委托事件
///
/// 刷新执行计划
///
///
public delegate void RefreshExecutePlanInfo(List executePlanInfos);
public event RefreshExecutePlanInfo RefreshExecutePlanInfoEvent;
///
/// 下传执行计划
///
///
public delegate Task NextPassExecutePlanInfo(ExecutePlanInfo planInfo);
public event NextPassExecutePlanInfo NextPassExecutePlanInfoEvent;
#endregion
private AssemblyPlanBusiness()
{
_productPlanInfoService = registerServices.GetService();
_executePlanInfoService = registerServices.GetService();
_bomInfoService = registerServices.GetService();
_spaceDetailService = registerServices.GetService();
_spaceInfoService = registerServices.GetService();
_realTaskInfoService = registerServices.GetService();
}
///
/// 根据产线工位编号获取生产计划
///
///
///
public List GetProductPlanInfosByProductLineCode(string orderCode)
{
List productPlanInfos = null;
try
{
productPlanInfos = _productPlanInfoService.GetProductPlanInfosByProductLineCode(appConfig.stationCode);
if (productPlanInfos.Count > 0)
{
if (!string.IsNullOrEmpty(orderCode))
{
productPlanInfos = productPlanInfos.Where(x => x.orderCode.Contains(orderCode)).ToList();
}
}
}
catch (Exception ex)
{
logHelper.Error("根据产线工位编号获取生产计划异常", ex);
}
return productPlanInfos;
}
///
/// 根据产线工位编号获取执行计划
///
///
public List GetEexecutePlanInfosByProductLineCode()
{
List planInfos = null;
try
{
planInfos = _executePlanInfoService.GetExecutePlanInfosByProductLineCode(appConfig.stationCode).OrderBy(x => x.executeOrder).ToList();
if (planInfos != null)
{
//for(int i = 0; i < planInfos.Count; i++)
//{
// planInfos[i].executeOrder = i + 1;
//}
//_executePlanInfoService.UpdateRangeExecutePlanInfo(planInfos);
planInfos = planInfos.Where(x => x.executeStatus != 3).ToList();
RefreshExecutePlanInfoEvent?.Invoke(planInfos);
}
}
catch (Exception ex)
{
logHelper.Error("根据产线工位编号获取执行计划异常", ex);
}
return planInfos;
}
///
/// 判断库存
///
/// 生产计划
/// 下达数量
/// BOM类型
///
///
public bool JudgmentStock(ProductPlanInfo planInfo, int transmitAmount, string materialType, string storeCode)
{
bool result = false;
try
{
BaseBomInfo baseBomInfo = null;
if (appConfig.shellStoreCode == storeCode)
{
baseBomInfo = _bomInfoService.GetBomInfoByMaterialCode(planInfo.materialCode);
}
else
{
baseBomInfo = _bomInfoService.GetChildenBomInfoByMaterialCode(planInfo.materialCode, materialType);
}
if (baseBomInfo != null)
{
materialType = baseBomInfo.materialCode;
// var planInfos = _executePlanInfoService.GetExecutePlanInfosByProductLineCode(appConfig.stationCode);
var spaceInfo = _spaceInfoService.GetBaseSpaceInfosByMaterialType(storeCode, materialType);
if (spaceInfo != null)
{
int sumStock = spaceInfo.Sum(x => x.spaceStock);
// if (planInfos != null)
// {
// int sumPlanAmount = planInfos.Sum(x => x.planAmount - x.completeAmount);
if (sumStock >= transmitAmount)
{
result = true;
}
// }
}
}
}
catch (Exception ex)
{
logHelper.Error("计划下达判断库存异常", ex);
}
return result;
}
///
/// 根据生产计划下达执行计划
///
///
///
public bool PlanTransmitByProductPlan(string productPlanCode, int transmitAmount)
{
bool result = false;
try
{
var productPlanInfo = _productPlanInfoService.GetProductPlanByPlanCode(productPlanCode);
if (productPlanInfo != null)
{
ExecutePlanInfo executePlanInfo = new ExecutePlanInfo();
executePlanInfo.executePlanCode = System.Guid.NewGuid().ToString("N");
executePlanInfo.productPlanCode = productPlanInfo.planCode;
executePlanInfo.productLineCode = productPlanInfo.productLineCode;
executePlanInfo.orderCode = productPlanInfo.orderCode;
executePlanInfo.materialCode = productPlanInfo.materialCode;
executePlanInfo.materialName = productPlanInfo.materialName;
executePlanInfo.executeOrder = _executePlanInfoService.GetExecuteOrderByProductLineCode(appConfig.stationCode);
executePlanInfo.executeStatus = 1;
executePlanInfo.planAmount = transmitAmount;
executePlanInfo.createdTime = DateTime.Now;
result = _executePlanInfoService.InsertExecutePlanInfo(executePlanInfo);
if (result)
{
GetEexecutePlanInfosByProductLineCode();
}
}
}
catch (Exception ex)
{
logHelper.Error("根据生产计划下达执行计划异常", ex);
}
return result;
}
///
/// 执行计划上移
///
///
public void ExecutePlanInfo_MoveUp(List executePlanInfos, string executePlanCode)
{
List executePlans = new List();
if (executePlanInfos != null)
{
ExecutePlanInfo planInfo = executePlanInfos.Where(x => x.executePlanCode == executePlanCode).First();
int executeOrder = planInfo.executeOrder;
int planIndex = executePlanInfos.IndexOf(planInfo);
if (planIndex != -1 && planIndex != 0)
{
var lastPlanInfo = executePlanInfos[planIndex - 1];
planInfo.executeOrder = lastPlanInfo.executeOrder;
lastPlanInfo.executeOrder = executeOrder;
executePlans.Add(planInfo);
executePlans.Add(lastPlanInfo);
var result = _executePlanInfoService.UpdateRangeExecutePlanInfo(executePlans);
if (result)
{
GetEexecutePlanInfosByProductLineCode();
}
}
}
}
///
/// 执行计划下移
///
///
public void ExecutePlanInfo_MoveDown(List executePlanInfos, string executePlanCode)
{
List executePlans = new List();
if (executePlanInfos != null)
{
ExecutePlanInfo planInfo = executePlanInfos.Where(x => x.executePlanCode == executePlanCode).First();
int executeOrder = planInfo.executeOrder;
int planIndex = executePlanInfos.IndexOf(planInfo);
if (planIndex != executePlanInfos.Count - 1)
{
var lastPlanInfo = executePlanInfos[planIndex + 1];
planInfo.executeOrder = lastPlanInfo.executeOrder;
lastPlanInfo.executeOrder = executeOrder;
executePlans.Add(planInfo);
executePlans.Add(lastPlanInfo);
var result = _executePlanInfoService.UpdateRangeExecutePlanInfo(executePlans);
if (result)
{
GetEexecutePlanInfosByProductLineCode();
}
}
}
}
///
/// 根据执行计划编号删除执行计划
///
///
///
public bool ExecutePlanInfo_Delete(string planCode)
{
bool result = _executePlanInfoService.DeleteExecutePlanInfoByPlanCode(planCode);
if (result)
{
GetEexecutePlanInfosByProductLineCode();
}
return result;
}
///
/// 下传计划
///
///
public ExecutePlanInfo ExecutePlanInfo_NextPass(string planCode, string nowPlanCode)
{
ExecutePlanInfo planInfo = null;
try
{
List planInfos = new List();
if (!string.IsNullOrEmpty(nowPlanCode))
{
ExecutePlanInfo nowPlanInfo = _executePlanInfoService.GetExecutePlanInfoByPlanCode(nowPlanCode);
if (nowPlanInfo != null)
{
nowPlanInfo.executeStatus = 1;
planInfos.Add(nowPlanInfo);
}
}
//获取执行计划
planInfo = _executePlanInfoService.GetExecutePlanInfoByPlanCode(planCode);
if (planInfo != null)
{
//传给出库
NextPassExecutePlanInfoEvent?.Invoke(planInfo);
planInfo.executeStatus = 2;
planInfo.beginTime = DateTime.Now;
planInfos.Add(planInfo);
}
else
{
logHelper.Info($"执行计划下传失败,执行计划编号:{planCode}未获取到执行计划");
return planInfo;
}
if (planInfos.Count > 0)
{
var result = _executePlanInfoService.UpdateRangeExecutePlanInfo(planInfos);
if (result)
{
GetEexecutePlanInfosByProductLineCode();
}
}
}
catch (Exception ex)
{
logHelper.Error("计划下传异常", ex);
}
return planInfo;
}
///
/// 获取小时产量
///
///
public List GetHourAmount()
{
return _executePlanInfoService.GetStationHourAmount(appConfig.stationCode);
}
///
/// 获取物料型号统计
///
///
public List GetMaterialStats()
{
return _executePlanInfoService.GetStationMaterialStats(appConfig.stationCode);
}
public bool autoFlag = false;
///
/// 自动下发执行计划
///
public void AutomaticDownpass()
{
//获取当前创建的执行计划
while (autoFlag)
{
try
{
var planInfos = _executePlanInfoService.GetExecutePlanInfosByProductLineCode(appConfig.stationCode);
if (planInfos != null)
{
planInfos = planInfos.Where(x => x.executeStatus == 1).ToList();
if(planInfos.Count > 0)
{
planInfos = planInfos.OrderBy(x => x.createdTime).ToList();
}
foreach (var planInfo in planInfos)
{
ExecutePlanInfo_NextPass(planInfo.executePlanCode, planInfo.executePlanCode);
Task.Delay(2000).Wait();
bool isFlase = true;
do
{
if (!autoFlag)
{
isFlase = autoFlag;
break;
}
var taksInfos = _realTaskInfoService.GetTaskInfosByStoreCode(new string[] { appConfig.shellStoreCode, appConfig.linerStoreCode }, 2);
if(taksInfos != null)
{
taksInfos = taksInfos.Where(x=>x.taskStatus!=3).ToList();
if (taksInfos.Count == 0)
{
var prodPlanInfo = _productPlanInfoService.GetProductPlanByPlanCode(planInfo.productPlanCode);
if (prodPlanInfo != null)
{
if (prodPlanInfo.planAmount > prodPlanInfo.completeAmount)
{
if ((prodPlanInfo.planAmount - prodPlanInfo.completeAmount) >= planInfo.planAmount)
{
//创建新计划
PlanTransmitByProductPlan(planInfo.productPlanCode, planInfo.planAmount);
// 删除老计划
_executePlanInfoService.DeleteExecutePlanInfoByPlanCode(planInfo.executePlanCode);
}
else
{
//创建新计划
PlanTransmitByProductPlan(planInfo.productPlanCode, prodPlanInfo.planAmount - prodPlanInfo.completeAmount);
// 删除老计划
_executePlanInfoService.DeleteExecutePlanInfoByPlanCode(planInfo.executePlanCode);
}
}
else
{
var prodPlanInfos = _productPlanInfoService.GetProductPlanInfosByProductLineCode(appConfig.stationCode);
if (prodPlanInfos != null)
{
prodPlanInfos = prodPlanInfos.Where(x => x.materialCode == planInfo.materialCode && x.planAmount > x.completeAmount).ToList();
prodPlanInfo = prodPlanInfos.First();
if ((prodPlanInfo.planAmount - prodPlanInfo.completeAmount) >= planInfo.planAmount)
{
//创建新计划
PlanTransmitByProductPlan(planInfo.productPlanCode, planInfo.planAmount);
// 删除老计划
_executePlanInfoService.DeleteExecutePlanInfoByPlanCode(planInfo.executePlanCode);
}
else
{
//创建新计划
PlanTransmitByProductPlan(planInfo.productPlanCode, prodPlanInfo.planAmount - prodPlanInfo.completeAmount);
// 删除老计划
_executePlanInfoService.DeleteExecutePlanInfoByPlanCode(planInfo.executePlanCode);
}
}
}
}
isFlase = false;
}
}
Task.Delay(2000).Wait();
} while (isFlase);
}
}
Task.Delay(5000).Wait();
}
catch (Exception ex)
{
Console.WriteLine($"组装计划自动执行异常:{ex.Message}");
}
}
}
}
}