using Admin.Core.IService; using Admin.Core.Model; using Aucma.Core.HwPLc; using log4net; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; namespace Aucma.Core.BoxFoam.Business { /// /// 新箱体发泡班组信息切换 /// public class TeamSwitchBusiness { private readonly log4net.ILog log = LogManager.GetLogger(typeof(TeamSwitchBusiness)); private readonly IBoxFoamPlanServices _boxFoamPlanServices; private readonly IBoxFoamDataServices _oldBoxFoamDataServices; private readonly IBoxFoamDataRecordServices _boxFoamDataRecordServices; public TeamSwitchBusiness() { _boxFoamPlanServices = App.ServiceProvider.GetService(); _oldBoxFoamDataServices = App.ServiceProvider.GetService(); _boxFoamDataRecordServices = App.ServiceProvider.GetService(); } public void Init() { DayShiftInfoCut(); NightShiftInfoCut(); } /// /// 白班信息切换 /// private void DayShiftInfoCut() { DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 08, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += DayShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); } private void DayShiftTimerCallback(object sender, ElapsedEventArgs e) { Console.WriteLine("切换为白班!"); UpdateShiftInfo(1); #region 重新定义Timer,进行第二天执行 DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 08, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = (System.Timers.Timer)sender; timer.Elapsed += DayShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); #endregion } /// /// 夜班班组切换 /// private void NightShiftInfoCut() { DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 20, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += NightShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); } private void NightShiftTimerCallback(object sender, ElapsedEventArgs e) { Console.WriteLine("切换为夜班!"); UpdateShiftInfo(2); #region 重新定义Timer,进行第二天执行 DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 20, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = (System.Timers.Timer)sender; timer.Elapsed += NightShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); #endregion } /// /// 班组切换 /// /// 班组类型:1-白班;2-夜班 private void UpdateShiftInfo(int shiftType) { log.Warn($"{(shiftType == 1 ? "白班" : "夜班")}班组切换,开始处理切换逻辑"); Console.WriteLine($"{(shiftType == 1 ? "白班" : "夜班")}班组切换,开始处理切换逻辑"); try { #region 240325 liulb 添加清空夹具产量功能; Delete By wenjy 2024-03-30 删除清空产量逻辑 //下发PLC清空产量信号 //var obj1 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine1Plc")); //if (obj1 != null) //{ // if (obj1.plc.IsConnected) // { // var flag = obj1.plc.WriteBool("M6.0", true);//换班 数据清空 // Thread.Sleep(500); // obj1.plc.WriteBool("M6.0", false);//数据清空后复位 // Console.WriteLine($"【{DateTime.Now}】班组切换清空1~6区产量数据:{(flag == true ? "成功" : "失败")}"); // log.Warn($"班组切换清空1~6区产量数据:{(flag == true ? "成功" : "失败")}"); // } //} //var obj2 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine2Plc")); //if (obj2 != null) //{ // if (obj2.plc.IsConnected) // { // bool flag = obj2.plc.WriteBool("M6.0", true);//换班 数据清空 // Thread.Sleep(500); // obj2.plc.WriteBool("M6.0", false);//数据清空后复位 // Console.WriteLine($"【{DateTime.Now}】班组切换清空7~12区产量数据:{(flag == true ? "成功" : "失败")}"); // log.Warn($"班组切换清空7~12区产量数据:{(flag == true ? "成功" : "失败")}"); // } //} #endregion #region Add By wenjy 2024-03-30 10:56:00 清空PLC产量,写true保持500毫秒后写false ClearPlcOutPut(true); Thread.Sleep(3000); ClearPlcOutPut(false); #endregion //小时统计报表数据存至记录表,清空小时统计报表 var hourDataList = _oldBoxFoamDataServices.Query(x => x.ProductLineCode == "CX_02" && x.StationCode == "1005"); List hourRecordList = new List(); if (hourDataList != null) { foreach (var item in hourDataList) { BoxFoamDataRecord hourRecord = new BoxFoamDataRecord(); hourRecord.OrderCode = item.OrderCode; hourRecord.ProductLineCode = item.ProductLineCode; hourRecord.Fixturestatus = item.Production; hourRecord.CuringTimeSettingValue = item.CuringTimeSettingValue; hourRecord.ActualValue = item.ActualValue; hourRecord.AnHour = item.AnHour; hourRecord.TwoHour = item.TwoHour; hourRecord.ThreeHour = item.ThreeHour; hourRecord.FourHour = item.FourHour; hourRecord.FiveHour = item.FiveHour; hourRecord.SixHour = item.SixHour; hourRecord.SevenHour = item.SevenHour; hourRecord.EightHour = item.EightHour; hourRecord.NineHour = item.NineHour; hourRecord.TenHour = item.TenHour; hourRecord.ElevenHour = item.ElevenHour; hourRecord.TwelveHour = item.TwelveHour; hourRecord.BoxFixturesideplate = item.BoxFixturesideplate; hourRecord.BoxClosebetatemperature = item.BoxClosebetatemperature; hourRecord.BoxBeat = item.BoxBeat; hourRecord.CreateTime = DateTime.Now; hourRecordList.Add(hourRecord); bool deleteFlag = _oldBoxFoamDataServices.DeleteAsync(item).Result; if (deleteFlag) { log.Warn($"{item.Fixtureboxtype}:小时数据清除成功"); } else { log.Warn($"{item.Fixtureboxtype}:小时数据清除失败"); } } } int addFlag = _boxFoamDataRecordServices.AddAsync(hourRecordList).Result; if (addFlag > 0) { log.Warn("班组切换小时数据记录保存成功"); } else { log.Warn("班组切换小时数据记录保存失败"); } //当班计划,班组计划设为0 var planInfoList = _boxFoamPlanServices.QueryAsync(x => x.ProductLineCode == "CX_02" && x.StationCode == "1005" && x.ShiftType == 1).Result; if (planInfoList != null) { foreach (var plan in planInfoList) { plan.ShiftType = 0; bool updateFlag = _boxFoamPlanServices.UpdateAsync(plan).Result; if (updateFlag) { log.Warn($"计划:{plan.OrderCode},班组切换标识更新成功"); if (plan.PlanAmount > plan.CompleteAmount) { plan.PlanAmount = plan.PlanAmount - plan.CompleteAmount; plan.CompleteAmount = 0; plan.ShiftType = 1; int saveFlag = _boxFoamPlanServices.AddAsync(plan).Result; if (saveFlag > 0) { log.Warn($"计划:{plan.OrderCode},剩余计划:{plan.PlanAmount},未完成进行顺延,执行成功"); } else { log.Warn($"计划:{plan.OrderCode},剩余计划:{plan.PlanAmount},未完成进行顺延,执行失败"); } } } else { log.Warn($"计划:{plan.OrderCode},班组切换标识更新失败"); } } } } catch (Exception ex) { log.Warn($"{(shiftType == 1 ? "白班" : "夜班")}班组切换,切换逻辑处理异常:{ex.Message}"); } } /// /// 清空PLC产量数据 /// /// private void ClearPlcOutPut(bool value) { var obj1 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine1Plc")); if (obj1 != null) { if (obj1.plc.IsConnected) { var flag = obj1.plc.WriteBool("M6.0", value);//换班 数据清空 log.Warn($"班组切换清空A区产量数据写{value}:{(flag == true ? "成功" : "失败")}"); Console.WriteLine($"班组切换清空A区产量数据写{value}:{(flag == true ? "成功" : "失败")}"); } } var obj2 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine2Plc")); if (obj2 != null) { if (obj2.plc.IsConnected) { bool flag = obj2.plc.WriteBool("M6.0", value);//换班 数据清空 log.Warn($"班组切换清空B区产量数据写{value}:{(flag == true ? "成功" : "失败")}"); Console.WriteLine($"班组切换清空B区产量数据写{value}:{(flag == true ? "成功" : "失败")}"); } } } } }