using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
using Aucma.Core.HwPLc;
using Microsoft.Extensions.DependencyInjection;
using NetTaste;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Aucma.Core.SheetMetal.Business
{
///
/// 同步PLC处理
///
public class SyncModelHandle
{
protected readonly ISmSyncModelServices? _smSyncModelServices;
public HwPLc.PlcModel obj_backPanel = null;
public HwPLc.PlcModel obj_sidePanel = null;
public SyncModelHandle() {
_smSyncModelServices = App.ServiceProvider.GetService();
}
///
/// 获取所有设置的钣金前板型号
///
///
public async Task QuerySidePanelMode()
{
Thread.Sleep(5000);
obj_sidePanel = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("SidePanelPlc"));
if (obj_sidePanel == null) { return; }
if (obj_sidePanel.plc.IsConnected)
{
Console.WriteLine("===============采集前板型号开始===============");
byte[] info = obj_sidePanel.plc.Read("D7000", 3000);
if (info == null)
{
Thread.Sleep(1000);
return;
}
int j = 1;
string mode = string.Empty;
List addList = new List();
List updateList = new List();
var smSyncModelList = _smSyncModelServices.QueryAsync().Result;
for (int i = 1; i <= 600; i++)
{
if (i % 2 == 0)
{
//设置型号
mode = Encoding.ASCII.GetString(info.Skip(i * 10).Take(10).ToArray()).Replace("\0", "").Trim();
if (string.IsNullOrEmpty(mode)) return;
if (smSyncModelList.Count == 0 || smSyncModelList == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode;
sm.SyncTime = DateTime.Now;
sm.CreateTime = DateTime.Now;
sm.Category = "0";
addList.Add(sm);
}
else
{
var obj = smSyncModelList.First(d => d.SmProductId == j.ToString()&& d.Category=="0" );
if (obj == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode;
sm.SyncTime = DateTime.Now;
sm.Category = "0";
addList.Add(sm);
}
if (string.IsNullOrEmpty(obj.SmModel) && string.IsNullOrEmpty(mode.Trim())) continue;
if (obj.SmModel.Equals(mode)) continue;
else
{
if (obj.SmModel.Equals(mode))
{
obj.SmProductId = j.ToString();
obj.SmModel = mode;
obj.SyncTime = DateTime.Now;
obj.Category = "0";
updateList.Add(obj);
}
}
}
Console.WriteLine($"型号{j}:{mode}");
j++;
}
}
if (addList != null || addList.Count > 0) await _smSyncModelServices.AddAsync(addList);
if (updateList != null || updateList.Count > 0) await _smSyncModelServices.UpdateAsync(updateList);
Console.WriteLine("===============采集结束===============");
}
}
///
/// 获取所有设置的钣金后板型号
///
///
public async Task QueryBackPanelMode()
{
Thread.Sleep(5000);
obj_backPanel = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("BackPanelPLC"));
if (obj_backPanel == null) { return; }
if (obj_backPanel.plc.IsConnected)
{
Console.WriteLine("===============采集后板开始===============");
byte[] info = obj_sidePanel.plc.Read("D7000", 3000);
if (info == null)
{
Thread.Sleep(1000);
return;
}
int j = 1;
string mode = string.Empty;
List addList = new List();
List updateList = new List();
var smSyncModelList = _smSyncModelServices.QueryAsync().Result;
for (int i = 1; i <= 600; i++)
{
if (i % 2 == 0)
{
//设置型号
mode = Encoding.ASCII.GetString(info.Skip(i * 10).Take(10).ToArray()).Replace("\0","").Trim();
if (smSyncModelList.Count == 0 || smSyncModelList == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode.Trim();
sm.SyncTime = DateTime.Now;
sm.CreateTime = DateTime.Now;
sm.Category = "1";
addList.Add(sm);
}
else
{
var obj = smSyncModelList.First(d => d.SmProductId == j.ToString() && d.Category == "1");
if (obj == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode.Trim();
sm.SyncTime = DateTime.Now;
sm.Category ="1";
addList.Add(sm);
}
else
{
if (string.IsNullOrEmpty(obj.SmModel) && string.IsNullOrEmpty(mode)) continue;
if (obj.SmModel.Equals(mode)) continue;
else
{
obj.SmProductId = j.ToString();
obj.SmModel = mode.Trim();
obj.SyncTime = DateTime.Now;
obj.Category = "1";
updateList.Add(obj);
}
}
}
Console.WriteLine($"型号{j}:{mode}");
j++;
}
}
if (addList.Count > 0) await _smSyncModelServices.AddAsync(addList);
if (updateList.Count > 0) await _smSyncModelServices.UpdateAsync(updateList);
Console.WriteLine("===============采集后板结束===============");
}
}
}
}