using HslCommunication.Profinet.Siemens; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.Entity.PptPlan; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Mesnac.Action.ChemicalWeighing.DBHelper { public class PlanDbHelper { #region 根据计划号获取当前正在执行的计划任务 /// /// 根据计划号获取当前正在执行的计划任务 /// /// /// /// /// public static List GetExecPlan(int serial) { try { SiemensS7Net plc = PlcConnect.Instance; string strSql = string.Empty; //处理任务取消,但是当前任务会在执行完当前物料称量结束后才能 if (GetPlanInfo().Count() > 0) { strSql = $@"select 0 Id,p.Dosing_Id,p.Plan_Id,p.Plan_Serial,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Shift_Id,p.Shift_Class,p.Plan_State,p.Plan_StateText,p.Plan_TotalWeight,p.Plan_TotalError, r.Total_Weight,r.Total_Error,m.Material_ID as MaterialID,m.Material_name,m.Bin_Serial as Station,m.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,p.IsPrenatalTest,ISNULL(p.FlagNum,0) as FlagNum from xl_plan p left join xl_recipe r on p.Recipe_ID=r.ID left join xl_weigh w on r.ID=w.Recipe_ID left join (select b.Bin_Serial,b.Bin_Name,b.Bin_Code,b.Material_ID,m.Material_name from Pmt_Bin b left join xl_material m on m.ID=b.Material_ID) m on w.Bin_Serial=m.Bin_Serial where p.Plan_State=3 "; IFreeSql fsql = FreeHelper.Instance; Console.WriteLine($"正在执行的计划:{strSql}"); var list = fsql.Select().WithSql(strSql).ToList(); Console.WriteLine($"正在执行的计划:{list.Count}"); return list; } else { if (plc.ReadInt16("DB104.DBX4.1").Content == 1) { strSql = @"select 0 Id,p.Dosing_Id,p.Plan_Id,p.Plan_Serial,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Shift_Id,p.Shift_Class,p.Plan_State,p.Plan_StateText,p.Plan_TotalWeight,p.Plan_TotalError, r.Total_Weight,r.Total_Error,m.Material_ID as MaterialID,m.Material_name,m.Bin_Serial as Station,m.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,p.IsPrenatalTest,ISNULL(p.FlagNum,0) as FlagNum from (select * from xl_plan where Plan_State=7 and Start_Date=(select Max(Start_Date) from xl_plan where Plan_State=7) ) p left join xl_recipe r on p.Recipe_ID=r.ID left join xl_weigh w on r.ID=w.Recipe_ID left join (select b.Bin_Serial,b.Bin_Name,b.Bin_Code,b.Material_ID,m.Material_name from Pmt_Bin b left join xl_material m on m.ID=b.Material_ID) m on w.Bin_Serial=m.Bin_Serial"; IFreeSql fsql = FreeHelper.Instance; Console.WriteLine($"正在执行的计划:{strSql}"); var list = fsql.Select().WithSql(strSql).ToList(); Console.WriteLine($"正在执行的计划:{list.Count}"); return list; } } return null; } catch (Exception ex) { Console.WriteLine($"查询执行计划:{ex.Message}"); return null; } } public static List GetPlanInfo() { try { string sql = @"select 0 Id,p.Dosing_Id,p.Plan_Id,p.Plan_Serial,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Shift_Id,p.Shift_Class,p.Plan_State,p.Plan_StateText,p.Plan_TotalWeight,p.Plan_TotalError, r.Total_Weight,r.Total_Error,m.Material_ID as MaterialID,m.Material_name,m.Bin_Serial as Station,m.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,p.IsPrenatalTest,0 FlagNum from xl_plan p left join xl_recipe r on p.Recipe_ID=r.ID left join xl_weigh w on r.ID=w.Recipe_ID left join (select b.Bin_Serial,b.Bin_Name,b.Bin_Code,b.Material_ID,m.Material_name from Pmt_Bin b left join xl_material m on m.ID=b.Material_ID) m on w.Bin_Serial=m.Bin_Serial where p.Plan_State=3 "; IFreeSql fsql = FreeHelper.Instance; Console.WriteLine($"验证正在执行的计划1"); var list = fsql.Select().WithSql(sql).ToList(); Console.WriteLine($"验证正在执行的计划:{list.Count}"); var planList = list.OrderBy(d => d.Plan_Serial); return planList.ToList(); } catch (Exception ex) { Console.WriteLine($"验证正在执行的计划:{ex.Message}"); return null; } } #endregion #region 获取LR_plan /// /// 获取LR_plan /// /// 计划号 /// 返回符合条件List public static LR_plan GetLrPlan(string planID, int realNum, int flagNum) { try { string strSql = "SELECT * FROM LR_plan where Plan_Id = @Plan_Id and Real_Num=@Real_Num and FlagNum=@flagNum"; IFreeSql fsql = FreeHelper.Instance; LR_plan list = fsql.Select().WithSql(strSql, new { Plan_Id = planID, Real_Num = realNum, flagNum = flagNum }).ToOne(); return list; } catch (Exception ex) { Console.WriteLine($"查询LR_plan表数据失败:{ex.Message}"); return null; } } #endregion #region 产前检测——更新计划完成数量 /// /// 更新计划完成数量 /// /// 计划号 /// 完成数量 /// public static bool UpdateCheckPlanCompletedQuantity(Xl_DownLoadPlan dp, int tcheckBatch, int state, DateTime endTime, double totalWeight) { try { IFreeSql fsql = FreeHelper.Instance; int result = fsql.Update().Set(a => new xl_plan { Real_Num = tcheckBatch, Plan_TotalWeight = totalWeight, Plan_State = state, Plan_StateText = StateToShow(state), End_Date = endTime.ToString("yyyy-MM-dd HH:mm:ss") }).Where(a => a.Plan_Id == dp.Plan_Id).ExecuteAffrows(); if (result > 0) return true; else return false; } catch (Exception ex) { Console.WriteLine($"产前检测更新xl_plan数据失败:{ex.Message}"); return false; } } #endregion #region 将计划运行状态转换为文字进行显示 /// /// 将计划运行状态转换为文字进行显示 /// /// 计划状态代码 /// 显示的文字信息 public static string StateToShow(int stateNum) { string reStr = null; if (stateNum == 3) { reStr = "正在运行"; } else if (stateNum == 4) { reStr = "已下传"; } else if (stateNum == 5) { reStr = "未启动"; } else if (stateNum == 7) { reStr = "已终止"; } else if (stateNum == 8) { reStr = "已完成"; } return reStr; } #endregion #region 根据ID获取料仓 /// /// 获取所有物料对象集合 /// /// public static Pmt_Bin GetFristBinInfo(int Id) { IFreeSql fsql = FreeHelper.Instance; Pmt_Bin bin = fsql.Select().Where(d => d.Bin_Serial == Id).ToOne(); return bin; } #endregion #region 更新计划详情 public static bool UpdatePlanDetail(Xl_DownLoadPlan item, LR_plan lrPlan, LR_weigh lrWeigh, Pmt_Bin bin, Hw_BinAlarm alarm, int key) { try { IFreeSql fsql = FreeHelper.Instance; if (item.IsPrenatalTest == 1) { if (key == 1) { fsql.Transaction(() => { var affrows = fsql.Insert(lrPlan).ExecuteAffrows(); //判断是否插入成功 if (affrows < 1) throw new Exception("存盘计划详细数据失败!"); //抛出异常,回滚事务,事务退出 affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); if (affrows < 1) throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 }); } else { fsql.Transaction(() => { var affrows = fsql.Insert(lrPlan).ExecuteAffrows(); //判断是否插入成功 if (affrows < 1) throw new Exception("存盘计划详细数据失败!"); //抛出异常,回滚事务,事务退出 lrWeigh.MainId = affrows; affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); if (affrows < 1) throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 }); } } else { if (key == 1) { fsql.Transaction(() => { var affrows = Convert.ToInt32(fsql.Insert(lrPlan).ExecuteIdentity()); //判断是否插入成功 if (affrows < 1) throw new Exception("存盘计划详细数据失败!"); //抛出异常,回滚事务,事务退出 lrWeigh.MainId = affrows; affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); if (affrows < 1) throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 affrows = fsql.Update(bin.Bin_Serial).Set(a => a.BinWeight, bin.BinWeight).ExecuteAffrows(); if (affrows < 1) throw new Exception("保存库存实时数据失败!");//抛出异常,回滚事务,事务退出 affrows = fsql.Insert(alarm).ExecuteAffrows(); if (affrows < 1) throw new Exception("物料出库存盘数据失败!");//抛出异常,回滚事务,事务退出 }); } else { fsql.Transaction(() => { var affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); if (affrows < 1) throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 bin.Equip_Code = "00"; affrows = fsql.Update(bin.Bin_Serial).Set(a => a.BinWeight, bin.BinWeight).ExecuteAffrows(); if (affrows < 1) throw new Exception("保存库存实时数据失败!");//抛出异常,回滚事务,事务退出 affrows = fsql.Insert(alarm).ExecuteAffrows(); if (affrows < 1) throw new Exception("物料出库存盘数据失败!");//抛出异常,回滚事务,事务退出 }); } } return true; } catch (Exception ex) { return false; } } #endregion #region 保存执行计划 public static bool UpdateCheckLrPlanCompletedQuantity(Xl_DownLoadPlan dp, int tcheckBatch, int state, DateTime endTime, double totalWeight, int flagNum) { try { IFreeSql fsql = FreeHelper.Instance; int result = fsql.Update().Set(a => new LR_plan { Real_Num = tcheckBatch, Total_Weight = decimal.Parse(totalWeight.ToString()), Plan_State = state, End_Date = endTime.ToString("yyyy-MM-dd HH:mm:ss") }).Where(a => a.Plan_Id == dp.Plan_Id && a.FlagNum == flagNum).ExecuteAffrows(); if (result > 0) return true; else return false; } catch (Exception) { return false; } } #endregion } }