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; using System; using NPOI.SS.Formula.Functions; 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开始称量 /// /// //扫描拉缸(容器)条码后验证成功,执行PLC 启动 /// /// 验证成功后传递过来相关数据 /// 是否执行成功! public async Task ExecPlcState(VerifyView view) { try { var plcList = PlcHelper.siemensList.SingleOrDefault(d => d.EquipName.Equals("溶剂PLC")); if (plcList.plc.IsConnected) { if (view.Weighbridge == "1号地磅") { int rate = Convert.ToInt32(view.Rate * 100); bool rr=plcList.plc.WriteInt16("DB110.DBW10.0", rate.ToString());//传入固率 bool barCode = plcList.plc.WriteInt16("DB110.DBW1768.0", "1"); // bool matCode = plcList.plc.WriteInt16("DB110.DBW482.0", "1");//执行启动 if (barCode) { //log.Info(view.Rate + "&" + rate); Hw_SolventErrorPreventionReport report = new Hw_SolventErrorPreventionReport() { PlanId = view.Plan_Id, FinishedNum = plcList.plc.ReadInt16("DB110.DBW1758.0"), RMatCode = view.RMatCode, MaterialID = view.MaterialID, MaterialName = view.MaterialName, Weight = view.MaterialWeight, WholeMaterial = view.WholeMaterial }; var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3); if (rtPlanList.Count > 0) { var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == view.Plan_Id); if (plcList.plc.ReadInt16("DB110.DBW8") != 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; // } //} rtPlan.Exec_Num = plcList.plc.ReadInt16("DB110.DBW1758.0"); //保存本次数据 int i = await _solventErrorPreventionReportRepository.AddAsync(report); var cylinderList = _hwCylinderRepository.Query(); if (cylinderList.Count == 0) { return false; } Hw_Cylinder hw_Cylinder = cylinderList.FirstOrDefault(d => d.BarCode == view.CylinderBarCode.Trim()); rtPlan.CylinderName = hw_Cylinder.Name; rtPlan.CylinderBarCode = hw_Cylinder.BarCode; log.Info(view.CylinderBarCode); await _planRepository.UpdateExecDb(rtPlan);//更新数据库 return true; } } //等待 return false; } return false; } else if (view.Weighbridge == "2号地磅") { int rate = Convert.ToInt32(view.Rate * 100); plcList.plc.WriteInt16("DB110.DBW10.0", rate.ToString());//传入固率 bool barCode = plcList.plc.WriteInt16("DB110.DBW1768.0", "1"); // bool matCode = plcList.plc.WriteInt16("DB110.DBW482.0", "1");//执行启动 if (barCode) { Hw_SolventErrorPreventionReport report = new Hw_SolventErrorPreventionReport() { PlanId = view.Plan_Id, FinishedNum = plcList.plc.ReadInt16("DB110.DBW1758.0"), RMatCode = view.RMatCode, MaterialID = view.MaterialID, MaterialName = view.MaterialName, Weight = view.MaterialWeight, WholeMaterial = view.WholeMaterial }; var rtPlanList = await _planRepository.QueryAsync(d => d.Plan_State == 3); if (rtPlanList.Count > 0) { var rtPlan = rtPlanList.FirstOrDefault(d => d.Plan_Id == view.Plan_Id); if (plcList.plc.ReadInt16("DB110.DBW8") != 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; // } //} rtPlan.Exec_Num = plcList.plc.ReadInt16("DB110.DBW1758.0"); //保存本次数据 int i = await _solventErrorPreventionReportRepository.AddAsync(report); var cylinderList = _hwCylinderRepository.Query(); if (cylinderList.Count == 0) { return false; } Hw_Cylinder hw_Cylinder = cylinderList.FirstOrDefault(d => d.BarCode == view.CylinderBarCode.Trim()); rtPlan.CylinderName = hw_Cylinder.Name; rtPlan.CylinderBarCode = hw_Cylinder.BarCode; log.Info(view.CylinderBarCode); await _planRepository.UpdateExecDb(rtPlan);//更新数据库 return true; } } //等待 return false; } return false; } return false; } else { return true; } } catch (System.Exception ex) { log.Info(ex.Message); return false; } } #endregion #region 泵投料釜比对信息 /// /// 泵投料釜比对信息 /// /// 计划ID /// 泵Code /// 投料泵Code /// public async Task ExecState(string planId, string pumpCode, string cylinderCode) { try { var result = await _planRepository.QueryAsync(d => d.Plan_Id == planId && d.PumpBarCode == pumpCode && d.CylinderBarCode == cylinderCode); if (result.Count > 0) { var plcList = PlcHelper.siemensList.SingleOrDefault(d => d.EquipName.Equals("溶剂PLC")); if (plcList.plc.IsConnected) { #region 写入PLC 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) { case 1: plcList.plc.WriteInt16("DB110.DBW282.0", "1"); break; case 2: plcList.plc.WriteInt16("DB110.DBW294.0", "1"); break; case 3: plcList.plc.WriteInt16("DB110.DBW306.0", "1"); break; case 4: plcList.plc.WriteInt16("DB110.DBW318.0", "1"); break; case 5: plcList.plc.WriteInt16("DB110.DBW330.0", "1"); break; case 6: plcList.plc.WriteInt16("DB110.DBW342.0", "1"); break; case 7: plcList.plc.WriteInt16("DB110.DBW354.0", "1"); break; case 8: plcList.plc.WriteInt16("DB110.DBW366.0", "1"); break; case 9: plcList.plc.WriteInt16("DB110.DBW 378.0", "1"); break; default: break; } #endregion return true; } else { return false; } } else { #region 写入PLC var plcList = PlcHelper.siemensList.SingleOrDefault(d => d.EquipName.Equals("溶剂PLC")); if (plcList.plc.IsConnected) { 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) { case 1: plcList.plc.WriteInt16("DB110.DBW282.0", "2"); break; case 2: plcList.plc.WriteInt16("DB110.DBW294.0", "2"); break; case 3: plcList.plc.WriteInt16("DB110.DBW306.0", "2"); break; case 4: plcList.plc.WriteInt16("DB110.DBW318.0", "2"); break; case 5: plcList.plc.WriteInt16("DB110.DBW330.0", "2"); break; case 6: plcList.plc.WriteInt16("DB110.DBW342.0", "2"); break; case 7: plcList.plc.WriteInt16("DB110.DBW354.0", "2"); break; case 8: plcList.plc.WriteInt16("DB110.DBW366.0", "2"); break; case 9: plcList.plc.WriteInt16("DB110.DBW 378.0", "2"); break; default: break; } #endregion return true; } else { return false; } } } catch (System.Exception) { return false; } } #endregion #region 拉缸查询 /// /// 拉缸查询 /// /// /// /// public async Task GetByCylinderCode(string code, string weighbridge) { try { 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; var plcList = PlcHelper.siemensList.SingleOrDefault(d => d.EquipName.Equals("溶剂PLC")); if (plcList.plc.IsConnected) { if (weighbridge== "1号地磅") { int planNum = plcList.plc.ReadInt16("DB110.DBW2.0");//执行批次 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 = plcList.plc.ReadInt16("DB110.DBW1758");//当前执行物料 if (materialNo == 0) materialNo = 1; var hwWeigh = hwWeighList.FirstOrDefault(d => d.MID == materialNo); if (hwWeigh == null) return null; return hwWeigh; } else { //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 = plcList.plc.ReadInt16("DB110.DBW478.0");//当前执行物料 //var hwWeigh = hwWeighList.FirstOrDefault(d => d.MID == materialNo); //if (hwWeigh == null) return null; return null; } } else { int planNum = plcList.plc.ReadInt16("DB110.DBW2.0");//执行批次 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 = plcList.plc.ReadInt16("DB110.DBW1758");//当前执行物料 var hwWeigh = hwWeighList.FirstOrDefault(d => d.MID == materialNo); if (hwWeigh == null) return null; return hwWeigh; } else { //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 = plcList.plc.ReadInt16("DB110.DBW478.0");//当前执行物料 //var hwWeigh = hwWeighList.FirstOrDefault(d => d.MID == materialNo); //if (hwWeigh == null) return null; return null; } } } else { log.Error("PLC连接失败!"); } return null; } catch (System.Exception ex) { log.Info(ex.Message); return null; } } #endregion #region 拉缸与泵匹配 /// /// 泵查询——计划已经完成后 /// /// /// /// public async Task GetByPumpCode(string code) { try { List 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 获取所有溶剂计划 /// /// 获取所有溶剂计划 /// /// public async Task> GetSolventPlanInfo() { List list = new List(); List 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; } #endregion } }