You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

681 lines
32 KiB
C#

1 year ago
using Admin.Core.IRepository;
using Admin.Core.IService;
using Admin.Core.Model;
using System.Threading.Tasks;
using Admin.Core.Model.ViewModels;
using System.Linq;
using System.Collections.Generic;
using log4net;
using Admin.Core.PlcServer;
1 year ago
using System;
10 months ago
using Admin.Core.Common;
using NPOI.OpenXmlFormats.Dml.Diagram;
1 year ago
namespace Admin.Core.Service
{
public class SolventServices : ISolventServices
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(SolventServices));
private readonly IRT_planRepository _planRepository;
private readonly IPmt_recipeRepository _recipeRepository;
private readonly IPmt_weighRepository _weighRepository;
private readonly IPmt_materialRepository _materialRepository;
private readonly IHw_SolventErrorPreventionReportRepository _solventErrorPreventionReportRepository;
private readonly IHw_weighRepository _hwWeighRepository;
private readonly IHw_PumpRepository _hwPumpRepository;
private readonly IHw_CylinderRepository _hwCylinderRepository;
int execNum = 0;
#region 构造函数
public SolventServices(IRT_planRepository planRepository, IPmt_recipeRepository recipeRepository, IPmt_weighRepository weighRepository,
IPmt_materialRepository materialRepository, IHw_SolventErrorPreventionReportRepository solventErrorPreventionReportRepository,
IHw_weighRepository hwWeighRepository, IHw_PumpRepository hwPumpRepository, IHw_CylinderRepository hwCylinderRepository)
{
_planRepository = planRepository;
_recipeRepository = recipeRepository;
_weighRepository = weighRepository;
_materialRepository = materialRepository;
_solventErrorPreventionReportRepository = solventErrorPreventionReportRepository;
_hwWeighRepository = hwWeighRepository;
_hwPumpRepository = hwPumpRepository;
_hwCylinderRepository = hwCylinderRepository;
}
#endregion
#region 拉缸类型验证成功后执行PCL开始称量
/// <summary>
/// //扫描拉缸容器条码后验证成功执行PLC 启动
/// </summary>
1 year ago
/// <param name="view">验证成功后传递过来相关数据</param>
1 year ago
/// <returns>是否执行成功!</returns>
10 months ago
public async Task<MessageModel<bool>> ExecPlcState(VerifyView view)
1 year ago
{
10 months ago
MessageModel<bool> result = new MessageModel<bool>();
1 year ago
try
{
10 months ago
if (view.Weighbridge == "1号地磅")
1 year ago
{
{
10 months ago
var s7 = PlcConnect.Instance;
var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3);
if (rtPlanList.Count > 0)
1 year ago
{
10 months ago
if (string.IsNullOrEmpty(view.Plan_Id))
{
10 months ago
var r = rtPlanList.FirstOrDefault();
view.Plan_Id = r.Plan_Id;
}
var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == view.Plan_Id);
List<Hw_SolventErrorPreventionReport> list = new List<Hw_SolventErrorPreventionReport>();
if (view.BarCodeList!=null)
1 year ago
{
10 months ago
foreach (var item in view.BarCodeList)
12 months ago
{
10 months ago
string[] str = item.Split("-");
Hw_SolventErrorPreventionReport report = new Hw_SolventErrorPreventionReport()
{
PlanId = view.Plan_Id,
Batch = rtPlan.Batch,
ProductName = rtPlan.ProductName,
FinishedNum = s7.ReadInt16("DB110.DBW1758.0").Content,
RMatCode = view.RMatCode,
MaterialID = view.MaterialID,
MaterialName = view.MaterialName,
Weight = view.MaterialWeight,
WholeMaterial = view.WholeMaterial,
Material_InventoryCode = str[0],
Material_BatchNumber = str[1],
Material_SerialCode = str[2],
BarCodeStr = item,
CreateTime = DateTime.Now
};
list.Add(report);
12 months ago
}
10 months ago
int i = await _solventErrorPreventionReportRepository.AddAsync(list);
}
#region MyRegion
//if (s7.ReadInt16("DB110.DBW8").Content != 250)
//{
//var hwWeighList = await _hwWeighRepository.GetWaiteMaterial(view.Plan_Id);
//if (hwWeighList != null)
//{
// if (hwWeighList.Count() > (rtPlan.Exec_Num + 1))
// {
// rtPlan.Exec_Num = 1;
// }
// else if (hwWeighList.Count() == (rtPlan.Exec_Num + 1))
// {
// rtPlan.Exec_Num = rtPlan.Exec_Num + 1;
// }
// else
// {
// rtPlan.Exec_Num = 1;
// }
//}
//else
//{
// rtPlan.Exec_Num = 0;
//}
//var num = s7.ReadInt16("DB110.DBW1758.0").Content;
//if (num == 0)
//{
// rtPlan.Exec_Num = 1;
//}
//else
//{
// rtPlan.Exec_Num = num;
//}
////rtPlan.Exec_Num = plcList.plc.ReadInt16("DB110.DBW1758.0");
//保存本次数据
#endregion
var cylinderList = _hwCylinderRepository.Query();
if (cylinderList.Count == 0)
{
result.success = false;
result.msg = "未获查询到拉缸信息";
return result;
}
if (view.CylinderBarCode.Trim() == "00000000")
{
rtPlan.CylinderName = "00000000";
rtPlan.CylinderBarCode = "00000000";
await _planRepository.UpdateExecDb(rtPlan);//更新数据库
result.success = true;
result.msg = "更新拉缸信息成功!";
int rate = Convert.ToInt32(view.Rate * 100);
var writeRate = s7.Write("DB110.DBW10.0", short.Parse(rate.ToString()));//传入固率
log.Info($"1号地磅 传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}");
var barCode = s7.Write("DB110.DBW1768.0", short.Parse("1")); //写入启动称量信号
log.Info($"1号地磅 写入BarCode:{barCode.IsSuccess},结果:{barCode.Message}");
if (barCode.IsSuccess && writeRate.IsSuccess)
1 year ago
{
10 months ago
result.success = true;
result.msg = "更新计划信息成功!称量开始!";
}
else
{
result.success = false;
result.msg = $"计划执行失败,请检查网络是否联通![传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}]<>[开始信号:{barCode.IsSuccess},结果:{barCode.Message}]";
}
}
else
{
Hw_Cylinder hw_Cylinder = cylinderList.FirstOrDefault(d => d.BarCode == view.CylinderBarCode.Trim());
if (hw_Cylinder == null)
{
result.success = false;
result.msg = $"未查询到拉缸信息!拉缸二维码:【{view.CylinderBarCode}】";
}
else
{
rtPlan.CylinderName = hw_Cylinder.Name;
rtPlan.CylinderBarCode = hw_Cylinder.BarCode;
log.Info(view.CylinderBarCode);
await _planRepository.UpdateExecDb(rtPlan);//更新数据库
int rate = Convert.ToInt32(view.Rate * 100);
var writeRate = s7.Write("DB110.DBW10.0", short.Parse(rate.ToString()));//传入固率
log.Info($"1号地磅 传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}");
var barCode = s7.Write("DB110.DBW1768.0", short.Parse("1")); //写入启动称量信号
log.Info($"1号地磅 写入BarCode:{barCode.IsSuccess},结果:{barCode.Message}");
if (barCode.IsSuccess && writeRate.IsSuccess)
12 months ago
{
10 months ago
result.success = true;
result.msg = "更新计划信息成功!称量开始!";
12 months ago
}
else
{
10 months ago
result.success = false;
result.msg = $"计划执行失败,请检查网络是否联通![传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}]<>[开始信号:{barCode.IsSuccess},结果:{barCode.Message}]";
12 months ago
}
1 year ago
}
}
}
1 year ago
}
10 months ago
}
if (view.Weighbridge == "2号地磅")
{
1 year ago
{
10 months ago
var s7 = PlcConnect.Instance;
1 year ago
10 months ago
var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3);
if (rtPlanList.Count > 0)
1 year ago
{
10 months ago
if (string.IsNullOrEmpty(view.Plan_Id))
{
10 months ago
var r = rtPlanList.FirstOrDefault();
view.Plan_Id = r.Plan_Id;
}
var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == view.Plan_Id);
List<Hw_SolventErrorPreventionReport> list = new List<Hw_SolventErrorPreventionReport>();
if (view.BarCodeList != null)
1 year ago
{
10 months ago
foreach (var item in view.BarCodeList)
1 year ago
{
10 months ago
string[] str = item.Split("-");
Hw_SolventErrorPreventionReport report = new Hw_SolventErrorPreventionReport()
12 months ago
{
10 months ago
PlanId = view.Plan_Id,
Batch = rtPlan.Batch,
ProductName = rtPlan.ProductName,
FinishedNum = s7.ReadInt16("DB111.DBW1758.0").Content,
RMatCode = view.RMatCode,
MaterialID = view.MaterialID,
MaterialName = view.MaterialName,
Weight = view.MaterialWeight,
WholeMaterial = view.WholeMaterial,
Material_InventoryCode = str[0],
Material_BatchNumber = str[1],
Material_SerialCode = str[2],
BarCodeStr = item,
CreateTime = DateTime.Now
};
list.Add(report);
}
int i = await _solventErrorPreventionReportRepository.AddAsync(list);
}
12 months ago
10 months ago
#region MyRegion
//if (s7.ReadInt16("DB111.DBW8").Content != 250)
//{
//var hwWeighList = await _hwWeighRepository.GetWaiteMaterial(view.Plan_Id);
//if (hwWeighList != null)
//{
// if (hwWeighList.Count() > (rtPlan.Exec_Num + 1))
// {
// rtPlan.Exec_Num = 1;
// }
// else if (hwWeighList.Count() == (rtPlan.Exec_Num + 1))
// {
// rtPlan.Exec_Num = rtPlan.Exec_Num + 1;
// }
// else
// {
// rtPlan.Exec_Num = 1;
// }
//}
//else
//{
// rtPlan.Exec_Num = 0;
//}
//var num = s7.ReadInt16("DB111.DBW1758.0").Content;
//if (num == 0)
//{
// rtPlan.Exec_Num = 1;
//}
//else
//{
// rtPlan.Exec_Num = num;
//}
////rtPlan.Exec_Num = plcList.plc.ReadInt16("DB111.DBW1758.0");
//保存本次数据
#endregion
var cylinderList = _hwCylinderRepository.Query();
if (cylinderList.Count == 0)
{
result.success = false;
result.msg = "未获查询到拉缸信息";
return result;
}
if (view.CylinderBarCode.Trim() == "00000000")
{
rtPlan.CylinderName = "00000000";
rtPlan.CylinderBarCode = "00000000";
await _planRepository.UpdateExecDb(rtPlan);//更新数据库
result.success = true;
result.msg = "更新拉缸信息成功!";
int rate = Convert.ToInt32(view.Rate * 100);
var writeRate = s7.Write("DB111.DBW10.0", short.Parse(rate.ToString()));//传入固率
log.Info($"2号地磅 传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}");
var barCode = s7.Write("DB111.DBW1768.0", short.Parse("1")); //写入启动称量信号
log.Info($"2号地磅 写入BarCode:{barCode.IsSuccess},结果:{barCode.Message}");
if (barCode.IsSuccess && writeRate.IsSuccess)
{
result.success = true;
result.msg = "更新计划信息成功!称量开始!";
}
else
{
result.success = false;
result.msg = $"计划执行失败,请检查网络是否联通![传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}]<>[开始信号:{barCode.IsSuccess},结果:{barCode.Message}]";
}
}
else
{
Hw_Cylinder hw_Cylinder = cylinderList.FirstOrDefault(d => d.BarCode == view.CylinderBarCode.Trim());
if (hw_Cylinder == null)
{
result.success = false;
result.msg = $"未查询到拉缸信息!拉缸二维码:【{view.CylinderBarCode}】";
}
else
{
1 year ago
rtPlan.CylinderName = hw_Cylinder.Name;
rtPlan.CylinderBarCode = hw_Cylinder.BarCode;
log.Info(view.CylinderBarCode);
await _planRepository.UpdateExecDb(rtPlan);//更新数据库
10 months ago
int rate = Convert.ToInt32(view.Rate * 100);
var writeRate = s7.Write("DB111.DBW10.0", short.Parse(rate.ToString()));//传入固率
log.Info($"2号地磅 传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}");
var barCode = s7.Write("DB111.DBW1768.0", short.Parse("1")); //写入启动称量信号
log.Info($"2号地磅 写入BarCode:{barCode.IsSuccess},结果:{barCode.Message}");
if (barCode.IsSuccess && writeRate.IsSuccess)
{
result.success = true;
result.msg = "更新计划信息成功!称量开始!";
}
else
{
result.success = false;
result.msg = $"计划执行失败,请检查网络是否联通![传入固含:{writeRate.IsSuccess},结果:{writeRate.Message}]<>[开始信号:{barCode.IsSuccess},结果:{barCode.Message}]";
}
1 year ago
}
}
}
}
1 year ago
}
10 months ago
return result;
1 year ago
}
catch (System.Exception ex)
{
log.Info(ex.Message);
10 months ago
result.success = false;
result.msg = $"执行数据异常:{ex.Message}";
return result;
1 year ago
}
}
#endregion
#region 泵投料釜比对信息
/// <summary>
/// 泵投料釜比对信息
/// </summary>
/// <param name="planId">计划ID</param>
/// <param name="pumpCode">泵Code</param>
/// <param name="cylinderCode">投料泵Code</param>
/// <returns></returns>
public async Task<bool> ExecState(string planId, string pumpCode, string cylinderCode)
{
try
{
10 months ago
var s7 = PlcConnect.Instance;
1 year ago
var result = await _planRepository.QueryAsync(d => d.Plan_Id == planId && d.PumpBarCode == pumpCode && d.CylinderBarCode == cylinderCode);
if (result.Count > 0)
{
10 months ago
#region 写入PLC
10 months ago
var pumpList = _hwPumpRepository.Query();
if (pumpList.Count == 0) { return false; }
var pump = pumpList.FirstOrDefault(d => d.BarCode == pumpCode);
if (pump == null) { return false; }
switch (pump.RowNum)
{
10 months ago
case 1:
s7.Write("DB110.DBW282.0",short.Parse("1"));
break;
case 2:
s7.Write("DB110.DBW294.0", short.Parse("1"));
break;
case 3:
s7.Write("DB110.DBW306.0", short.Parse("1"));
break;
case 4:
s7.Write("DB110.DBW318.0", short.Parse("1"));
break;
case 5:
s7.Write("DB110.DBW330.0", short.Parse("1"));
break;
case 6:
s7.Write("DB110.DBW342.0", short.Parse("1"));
break;
case 7:
s7.Write("DB110.DBW354.0", short.Parse("1"));
break;
case 8:
s7.Write("DB110.DBW366.0", short.Parse("1"));
break;
case 9:
s7.Write("DB110.DBW 378.0", short.Parse("1"));
break;
default:
break;
}
10 months ago
#endregion
return true;
1 year ago
}
else
{
#region 写入PLC
10 months ago
var pumpList = _hwPumpRepository.Query();
if (pumpList.Count > 0) { return false; }
var pump = pumpList.FirstOrDefault(d => d.BarCode == pumpCode);
if (pump == null) { return false; }
switch (pump.RowNum)
1 year ago
{
10 months ago
case 1:
s7.Write("DB110.DBW282.0",short.Parse("2"));
break;
case 2:
s7.Write("DB110.DBW294.0", short.Parse("2"));
break;
case 3:
s7.Write("DB110.DBW306.0", short.Parse("2"));
break;
case 4:
s7.Write("DB110.DBW318.0", short.Parse("2"));
break;
case 5:
s7.Write("DB110.DBW330.0", short.Parse("2"));
break;
case 6:
s7.Write("DB110.DBW342.0", short.Parse("2"));
break;
case 7:
s7.Write("DB110.DBW354.0", short.Parse("2"));
break;
case 8:
s7.Write("DB110.DBW366.0", short.Parse("2"));
break;
case 9:
s7.Write("DB110.DBW 378.0", short.Parse("2"));
break;
default:
break;
}
10 months ago
#endregion
return true;
1 year ago
}
}
10 months ago
catch (System.Exception ex)
1 year ago
{
10 months ago
log.Error(ex.Message);
1 year ago
return false;
}
}
#endregion
#region 拉缸查询
/// <summary>
/// 拉缸查询
/// </summary>
/// <param name="code"></param>
1 year ago
/// <param name="weighbridge"></param>
1 year ago
/// <returns></returns>
public async Task<WaitePlanView> GetByCylinderCode(string code, string weighbridge)
{
try
{
1 year ago
var list = await _planRepository.GetDownLoadPlan(code, weighbridge);
if (list.Count == 0) return null;
var plan = list.FirstOrDefault();
var hwWeighList = await _hwWeighRepository.GetWaiteMaterial(plan.Plan_Id);
if (hwWeighList == null) return null;
10 months ago
if (weighbridge == "1号地磅")
1 year ago
{
10 months ago
var s7 = PlcConnect.Instance;
int planNum = s7.ReadInt16("DB110.DBW2.0").Content;//执行批次
if (plan.Plan_Num > planNum)
1 year ago
{
10 months ago
var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3);
var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == plan.Plan_Id);
12 months ago
10 months ago
int materialNo = s7.ReadInt16("DB110.DBW1758").Content;//当前执行物料
if (materialNo == 0) materialNo = 1;
var hwWeigh = hwWeighList.FirstOrDefault(d => d.MID == materialNo);
if (hwWeigh == null) return null;
1 year ago
10 months ago
return hwWeigh;
1 year ago
}
1 year ago
else
{
10 months ago
return null;
1 year ago
}
1 year ago
}
else
{
10 months ago
var s7 = PlcConnect.Instance;
int planNum = s7.ReadInt16("DB111.DBW2.0").Content;//执行批次
10 months ago
if (plan.Plan_Num > planNum)
{
var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3);
var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == plan.Plan_Id);
execNum = rtPlan.Exec_Num;
int materialNo = s7.ReadInt16("DB111.DBW1758").Content;//当前执行物料
10 months ago
var hwWeigh = hwWeighList.FirstOrDefault(d => d.MID == materialNo);
if (hwWeigh == null) return null;
return hwWeigh;
}
else
{
return null;
}
1 year ago
}
10 months ago
}
catch (System.Exception ex)
{
log.Info(ex.Message);
1 year ago
return null;
}
10 months ago
}
#endregion
#region 整包物料查询
/// <summary>
/// 整包物料查询
/// </summary>
/// <param name="code">整包物料编码</param>
/// <param name="weighbridge">地磅</param>
/// <returns></returns>
public async Task<PlanView> GetByZbCode(string code, string weighbridge)
{
try
{
string str1 = string.Empty;
if (!code.Contains("kg"))
{
string[] str = code.Split("-");
str1= str[0];
}
else
{
str1= code;
}
var list = await _planRepository.GetDownLoadPlan(str1, weighbridge);
if (list.Count == 0) return null;
var plan = list.FirstOrDefault(d=>d.Plan_State==3);
if (weighbridge == "1号地磅")
{
var s7 = PlcConnect.Instance;
var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3);
var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == plan.Plan_Id);
int materialNo = s7.ReadInt16("DB110.DBW1758").Content;//当前执行物料
if (materialNo == 0) materialNo = 1;
var planView = list.FirstOrDefault(d => d.MID == materialNo&& d.Plan_State == 3);
if (planView == null) return null;
return planView;
}
else
{
var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3);
var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == plan.Plan_Id);
var s7 = PlcConnect.Instance;
int materialNo = s7.ReadInt16("DB110.DBW1758").Content;//当前执行物料
if (materialNo == 0) materialNo = 1;
var planView = list.FirstOrDefault(d => d.MID == materialNo);
if (planView == null) return null;
return planView;
}
}
1 year ago
catch (System.Exception ex)
{
log.Info(ex.Message);
return null;
}
}
#endregion
#region 拉缸与泵匹配
/// <summary>
/// 泵查询——计划已经完成后
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
public async Task<PumpAndCylinderView> GetByPumpCode(string code)
{
try
{
List<RT_plan> planList = (await _planRepository.QueryAsync(d => d.Plan_State == 8)).OrderByDescending(d => d.End_Date).ToList();
if (planList.Count == 0) return null;
var plan = planList.FirstOrDefault(d => d.PumpBarCode == code);
if (plan == null) return null;
PumpAndCylinderView view = new PumpAndCylinderView()
{
Plan_Id= plan.Plan_Id,
CylinderName=plan.CylinderName,
CylinderBarCode=plan.CylinderBarCode,
PumpName = plan.PumpName,
PumpBarCode = plan.PumpBarCode
};
return view;
}
catch (System.Exception ex)
{
throw;
}
}
#endregion
#region 获取所有溶剂计划
/// <summary>
/// 获取所有溶剂计划
/// </summary>
/// <returns></returns>
public async Task<List<SolventPlanView>> GetSolventPlanInfo()
{
List<SolventPlanView> list = new List<SolventPlanView>();
List<RT_plan> plan = await _planRepository.QueryAsync();
plan.ForEach(x =>
{
SolventPlanView view = new SolventPlanView();
view.Plan_Id = x.Plan_Id;
view.Plan_Num = x.Plan_Num;
view.Real_Num = x.Real_Num;
view.Plan_Serial = x.Plan_Serial;
view.Plan_Date = x.Plan_Date;
view.Plan_State = x.Plan_State;
view.Plan_StateText = x.Plan_StateText;
view.Plan_TotalError = x.Plan_TotalError;
view.Plan_TotalWeight = x.Plan_TotalWeight;
view.Recipe_Code = x.Recipe_Code;
view.Recipe_Name = x.Recipe_Name;
list.Add(view);
});
return list;
10 months ago
}
1 year ago
#endregion
10 months ago
1 year ago
}
}