using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static WIDESEA_Comm.Wheel_trace;
namespace WIDESEA_WCS
{
public class Wheel_info_trace
{
///
/// 查询车轮上线
///
///
public static void Wheel_online(PLCClient client)
{
VOLContext context = new VOLContext();
Idt_info_traceRepository traceRepository = new dt_info_traceRepository(context);
IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(context);
PropertyInfo[] propertyInfos = typeof(Wheel_online).GetProperties();
var groups = client.itemGroups.Where(x => x.opratortype == "trace_online").OrderBy(x => x.Methods).ThenBy(x => x.name).GroupBy(x => x.Methods).ToList();
foreach (var key in groups)
{
try
{
Wheel_online StackerDBItem = new Wheel_online();
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.Name == "Name")
{
StackerDBItem.Name = key.Key;
}
else
{
DBItemGroup group = key.FirstOrDefault(x => x.name == propertyInfo.Name);
if (group != null)
{
var readData = DBExtension.Read(group, client);
switch (propertyInfo.Name)
{
case "oi_on":
StackerDBItem.oi_on = Convert.ToByte(readData);
break;
case "o_wp_id":
StackerDBItem.o_wp_id = Convert.ToString(readData);
break;
case "o_online_time":
StackerDBItem.o_online_time = readData.ToString();
break;
}
}
}
}
#region 替换老PCS时使用
if (StackerDBItem.oi_on == 1)
{
if (string.IsNullOrEmpty(StackerDBItem.o_wp_id)) throw new Exception($"{StackerDBItem.Name}:SN号为空");
if (string.IsNullOrEmpty(StackerDBItem.o_online_time)) throw new Exception($"{StackerDBItem.Name}:上线时间为空");
var info = traceRepository.Find(x => x.trace_SN == StackerDBItem.o_wp_id).FirstOrDefault();
if (info != null) throw new Exception();
info = new dt_info_trace();
info.trace_SN = StackerDBItem.o_wp_id;
var time = DateTime.ParseExact(StackerDBItem.o_online_time, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
//var time1 = DateTime.ParseExact(StackerDBItem.o_online_time, "yyyyMMddHHmmss", new CultureInfo("zh-CN", true));
info.trace_status = "待加工";
info.trace_online_time = time;
info.trace_line_number = StackerDBItem.Name;
var Work = workinfoRepository.Find(x => x.SN == info.trace_SN && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
if (Work != null)
{
info.trace_workOrder = Work.workOrder;
info.trace_productName = Work.productName;
info.trace_drawingNo = Work.drawingNo;
info.trace_heatBatchID = Work.heatBatchID;
info.trace_heatID = Work.heatID;
info.trace_billetID = Work.billetID.ToString();
}
traceRepository.Add(info, true);
client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//信号复位
}
#endregion
}
catch (Exception ex)
{
//client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//信号复位
}
}
}
///
/// 查询车轮加工
///
///
public static void Wheel_machine(PLCClient client)
{
VOLContext context = new VOLContext();
Idt_info_traceRepository traceRepository = new dt_info_traceRepository(context);
PropertyInfo[] propertyInfos = typeof(Wheel_Machine).GetProperties();
var groups = client.itemGroups.Where(x => x.opratortype == "trace_Machine").OrderBy(x => x.Methods).ThenBy(x => x.name).GroupBy(x => x.Methods).ToList();
foreach (var key in groups)
{
try
{
Wheel_Machine StackerDBItem = new Wheel_Machine();
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.Name == "Name")
{
StackerDBItem.Name = key.Key;
}
else
{
DBItemGroup group = key.FirstOrDefault(x => x.name == propertyInfo.Name);
if (group != null)
{
var readData = DBExtension.Read(group, client);
switch (propertyInfo.Name)
{
case "oi_on":
StackerDBItem.oi_on = Convert.ToByte(readData);
break;
case "o_wp_id":
StackerDBItem.o_wp_id = Convert.ToString(readData);
break;
case "o_mach_start_time":
StackerDBItem.o_mach_start_time = readData.ToString();
break;
case "o_mach_finish_time":
StackerDBItem.o_mach_finish_time = readData.ToString();
break;
case "o_operator":
StackerDBItem.o_operator = readData.ToString();
break;
case "o_shift":
StackerDBItem.o_shift = readData.ToString();
break;
case "o_wp_status":
StackerDBItem.o_wp_status = Convert.ToInt16(readData);
break;
case "o_wp_process":
StackerDBItem.o_wp_process = Convert.ToInt16(readData);
break;
}
}
}
}
#region 替换老PCS时使用
if (StackerDBItem.oi_on == 1)
{
if (string.IsNullOrEmpty(StackerDBItem.o_wp_id)) throw new Exception($"{StackerDBItem.Name}:SN号为空");
var info = traceRepository.Find(x => x.trace_SN == StackerDBItem.o_wp_id).FirstOrDefault();
if (info != null)
{
if (!string.IsNullOrEmpty(StackerDBItem.o_mach_start_time))
info.trace_mach_start_time = DateTime.ParseExact(StackerDBItem.o_mach_start_time, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
if (!string.IsNullOrEmpty(StackerDBItem.o_mach_finish_time))
info.trace_mach_finish_time = DateTime.ParseExact(StackerDBItem.o_mach_finish_time, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
if (!string.IsNullOrEmpty(StackerDBItem.o_operator)) info.trace_operator = StackerDBItem.o_operator;
if (!string.IsNullOrEmpty(StackerDBItem.o_shift)) info.trace_shift = info.trace_shift;
info.trace_mach_statu = StackerDBItem.o_wp_status;
info.trace_process = StackerDBItem.o_wp_process;
info.trace_mach_number = StackerDBItem.Name;
traceRepository.Update(info, x => new { x.trace_mach_start_time, x.trace_mach_finish_time, x.trace_operator, x.trace_shift, x.trace_mach_statu, x.trace_process, x.trace_mach_number }, true);
}
client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//信号复位
}
#endregion
}
catch (Exception ex)
{
//client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//信号复位
}
}
}
///
/// 查询车轮下线
///
///
public static void Wheel_offline(PLCClient client)
{
VOLContext context = new VOLContext();
Idt_info_traceRepository traceRepository = new dt_info_traceRepository(context);
PropertyInfo[] propertyInfos = typeof(Wheel_offline).GetProperties();
var groups = client.itemGroups.Where(x => x.opratortype == "trace_offline").OrderBy(x => x.Methods).ThenBy(x => x.name).GroupBy(x => x.Methods).ToList();
foreach (var key in groups)
{
try
{
Wheel_offline StackerDBItem = new Wheel_offline();
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.Name == "Name")
{
StackerDBItem.Name = key.Key;
}
else
{
DBItemGroup group = key.FirstOrDefault(x => x.name == propertyInfo.Name);
if (group != null)
{
var readData = DBExtension.Read(group, client);
switch (propertyInfo.Name)
{
case "oi_on":
StackerDBItem.oi_on = Convert.ToByte(readData);
break;
case "o_wp_id":
StackerDBItem.o_wp_id = Convert.ToString(readData);
break;
case "o_offline_time":
StackerDBItem.o_offline_time = readData.ToString();
break;
case "o_direction":
StackerDBItem.o_direction = Convert.ToByte(readData);
break;
}
}
}
}
#region 替换老PCS时使用
if (StackerDBItem.oi_on == 1)
{
if (string.IsNullOrEmpty(StackerDBItem.o_wp_id)) throw new Exception($"{StackerDBItem.Name}:SN号为空");
var info = traceRepository.Find(x => x.trace_SN == StackerDBItem.o_wp_id).FirstOrDefault();
if (info != null)
{
if (!string.IsNullOrEmpty(StackerDBItem.o_offline_time))
info.trace_offline_time = DateTime.ParseExact(StackerDBItem.o_offline_time, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
info.trace_direction = StackerDBItem.o_direction.ToString();
//info.trace_line_number = info.trace_line_number + StackerDBItem.Name;
traceRepository.Update(info, x => new { x.trace_offline_time, x.trace_direction }, true);
}
client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//信号复位
}
#endregion
}
catch (Exception ex)
{
//client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//信号复位
}
}
}
}
}