using Mesnac.Action.ChemicalWeighing.DBHelper;
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
{
public class SolventPlanHelper
{
#region 根据计划号获取下发计划任务——地磅
///
/// 根据计划号获取下发计划任务
///
///
///
///
///
public static List GetPlan()
{
try
{
string strSql = @"select w.Weight_Id MID, 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.ID as MaterialID,m.Material_name,b.Bin_Serial as Station,b.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,w.IsRate,w.TheoryRate,p.Exec_Num
from RT_plan p left join Pmt_recipe r on p.Recipe_ID=r.ID
left join Pmt_weigh w on r.ID=w.Recipe_ID
left join Pmt_material m on w.Material_ID=m.ID
left join Pmt_Bin b on m.ID=b.Material_ID where p.Weighbridge='1号地磅' and p.Plan_State=3 ";
IFreeSql fsql = FreeHelper.Instance;
List list = fsql.Select().WithSql(strSql).ToList();
var planList = list.OrderBy(d => d.MID);
return list;
}
catch (Exception ex)
{
Console.WriteLine($"获取下发计划失败:{ex.Message}");
return null;
}
}
#endregion
#region 根据计划号获取下发计划任务——地磅2
///
/// 根据计划号获取下发计划任务
///
///
///
///
///
public static List GetPlan2()
{
try
{
string strSql = @"select w.Weight_Id MID, 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.ID as MaterialID,m.Material_name,b.Bin_Serial as Station,b.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,w.IsRate,w.TheoryRate,p.Exec_Num
from RT_plan p left join Pmt_recipe r on p.Recipe_ID=r.ID
left join Pmt_weigh w on r.ID=w.Recipe_ID
left join Pmt_material m on w.Material_ID=m.ID
left join Pmt_Bin b on m.ID=b.Material_ID where p.Weighbridge='2号地磅' and p.Plan_State=3 ";
IFreeSql fsql = FreeHelper.Instance;
List list = fsql.Select().WithSql(strSql).ToList();
var planList = list.OrderBy(d => d.MID);
return list;
}
catch (Exception ex)
{
Console.WriteLine($"获取下发计划失败:{ex.Message}");
return null;
}
}
#endregion
#region 获取LR_plan
///
/// 获取RT_Plan
///
/// 计划号
/// 返回符合条件List
public static Hw_plan GetRtPlan(string planID, int realNum)
{
IFreeSql fsql = FreeHelper.Instance;
string strSql = "SELECT * FROM Hw_plan where Plan_Id = @Plan_Id and Real_Num=@Real_Num";
Hw_plan entity = fsql.Select().Where(d => d.Plan_Id == planID&& d.Real_Num== realNum).ToOne();
return entity;
}
#endregion
#region 更新计划完成数
///
/// 更新计划完成数UpdateRealNum
///
/// 计划编号
/// 完成数
public static bool UpdatePlan(string planID, int realNum, int state, decimal totalWeight, decimal totalError)
{
try
{
IFreeSql fsql = FreeHelper.Instance;
int result = fsql.Update().Set(a => new RT_plan
{
Real_Num = realNum,
Plan_State = state,
Plan_StateText = StateToShow(state),
End_Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Plan_TotalWeight = totalWeight,
Plan_TotalError = totalError,
Exec_Num = 0,
IF_FLAG = 1
}).Where(a => a.Plan_Id == planID).ExecuteAffrows();
if (result < 1)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("更新计划完成数异常:" + ex.Message);
return false;
}
}
#endregion
#region 更新计划完成数
///
/// 更新计划完成数UpdateRealNum
///
/// 计划编号
/// 完成数
public static bool UpdateRealNum(string planID, int realNum, decimal totalWeight, decimal totalError)
{
try
{
IFreeSql fsql = FreeHelper.Instance;
int result = fsql.Update().Set(a => new RT_plan
{
Real_Num = realNum,
Plan_TotalWeight = totalWeight,
Plan_TotalError = totalError,
Exec_Num = 1
}).Where(a => a.Plan_Id == planID).ExecuteAffrows();
if (result < 1)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("更新计划完成数异常:" + ex.Message, ex);
return false;
}
}
#endregion
public static bool SaveToSolvenDB(Hw_plan plan, Hw_weigh weigh)
{
try
{
IFreeSql fsql = FreeHelper.Instance;
fsql.Transaction(() =>
{
var affrows = fsql.Insert(plan).ExecuteAffrows();
//判断是否插入成功
if (affrows < 1)
throw new Exception("溶剂存盘计划详细数据失败!"); //抛出异常,回滚事务,事务退出
affrows = fsql.Insert(weigh).ExecuteAffrows();
if (affrows < 1)
throw new Exception("溶剂存盘物料详细数据失败!");//抛出异常,回滚事务,事务退出
});
return true;
}
catch (Exception ex)
{
Console.WriteLine($"保存盘物料计划数据失败!异常原因:{ex.Message}");
return false;
}
}
public static bool SaveToSolvenDB2(Hw_plan plan, Hw_weigh weigh)
{
try
{
IFreeSql fsql = FreeHelper.Instance;
var affrows = fsql.Insert(weigh).ExecuteAffrows();
if (affrows < 1)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine($"保存物料详细数据失败!异常原因:{ex.Message}");
return false;
}
}
#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
}
}