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.

467 lines
20 KiB
C#

using DevExpress.Utils.Drawing.Helpers;
using Mesnac.Action.ChemicalWeighing.DBHelper;
using Mesnac.Action.ChemicalWeighing.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mesnac.Action.ChemicalWeighing
{
public class RjHelper
{
public static void DownRjPlan(DataTable dt,int MainId)
{
var list = DataTableHelper.DataTableToList(dt);
List<Hw_WareHouse_Sub> subList = new List<Hw_WareHouse_Sub>();
List<Pmt_recipe> recipeList = new List<Pmt_recipe>();
list.ForEach((a) =>
{
Hw_WareHouse_Sub sb = new Hw_WareHouse_Sub();
sb.MainId = MainId;
sb.PId = a.PId;
sb.ProductName = a.ProductName;
a.Plan_State = 3;//待下发
sb.MaterialID = a.Material_Code;
sb.Material_Code = a.Material_Code;
sb.MaterialName = a.MaterialName;
sb.MaterialType = a.MaterialType;
sb.SetWeight = Convert.ToDouble(a.SetWeight);
sb.SetError = Convert.ToDouble(a.SetError);
sb.Difference = a.Difference;
sb.Fixed = a.Fixed == "" || a.Fixed == null ? false : true;
sb.FixedRatio = a.FixedRatio == ""|| a.FixedRatio == null ? 0.00 : Convert.ToDouble(a.FixedRatio);
sb.Weighbridge = a.Weighbridge;
sb.PumpCode = a.PumpCode;
sb.Recipe = a.Recipe;
sb.Remark = a.Remark;
sb.CreateTime = DateTime.Now;
subList.Add(sb);
});
//查询溶剂数据
var xlList = subList.Where(d => d.MaterialType.Contains("溶剂"));
var xt = (from d in xlList
select new
{
d.PId,
d.ProductName,
d.MaterialType,
d.Recipe,
d.Weighbridge,
d.PumpCode
}).Distinct();
foreach (var item in xt)
{
try
{
#region 配方
Pmt_recipe recipe = new Pmt_recipe();
recipe.ID = Guid.NewGuid().ToString("N");
recipe.Equip_Code = "01";
recipe.Recipe_Code = item.Recipe;
recipe.Version = "1";
recipe.Recipe_Name = item.Recipe;
recipe.Recipe_Verify = 1;
recipe.Total_Weight = 0;
recipe.Total_Error = 0;
recipe.CreateDateTime = DateTime.Now;
#endregion
#region 物料
var repiceList = xlList.Where(d => item.PId.Equals(d.PId) && item.ProductName.Equals(d.ProductName) && item.Recipe.Equals(d.Recipe));
int i = 1;
List<Pmt_weigh> weighList = new List<Pmt_weigh>();
foreach (var sub in repiceList)
{
Pmt_weigh material = new Pmt_weigh();
material.ID = Guid.NewGuid().ToString("N");
material.Equip_Code = "01";
material.Recipe_ID = recipe.ID;
material.Weight_Id = i;
if (sub.Material_Code == null | sub.Material_Code == "")
{
throw new Exception("未查询到物料!");
}
var obj = GetMaterial(sub.Material_Code);
if (obj == null)
{
throw new Exception("未查询到当前物料!");
}
material.Material_ID = obj.ID;
material.Set_Weight = Convert.ToDecimal(sub.SetWeight);
material.Set_Error = Convert.ToDecimal(sub.SetError);
recipe.Total_Weight = recipe.Total_Weight + material.Set_Weight;
recipe.Total_Error = recipe.Total_Error + material.Set_Error;
if (sub.Fixed)
{
material.IsRate = true;
material.TheoryRate = Convert.ToDecimal(sub.FixedRatio);
}
else
{
material.IsRate =false;
material.TheoryRate = 0;
}
weighList.Add(material);
i++;
}
#endregion
#region 计划
RT_plan plan = new RT_plan();
plan.Dosing_Id = 0;
plan.Equip_Code = "01";
plan.Batch = item.PId;
plan.ProductName = item.ProductName;
int serialNum = 0;
int rtPlanSNum = GetNextSerialNum(); //xl_plan序号
int lrPlanSNum = GetNextSerialNumFromLRPlan(DateTime.Now); //Hw_Plan序号
if (rtPlanSNum >= lrPlanSNum)
{
serialNum = rtPlanSNum;
}
else
{
serialNum = lrPlanSNum;
}
string planID = CommHelper.GenerateNextPlanIDNew(DateTime.Now, 1, serialNum); //计划号
plan.Plan_Id = planID;
plan.Plan_Serial = serialNum;
plan.Recipe_ID = recipe.ID;
plan.Recipe_Code = item.Recipe;
plan.Recipe_Name = item.Recipe;
plan.Version = "1";
plan.Mixer_Line = null;
plan.Recipe_Type = 0;
plan.Shift_Id = "1";
plan.Shift_Class = "";
plan.Plan_Num = 1;
plan.Real_Num = 0;
plan.Duration_Time = 0;
plan.End_Date = null;
plan.Weight_Man = null;
plan.Stock_Man = null;
plan.Plan_Batch = null;
plan.Plan_State = (int)PlanState.UnStart;
plan.Plan_StateText = CommHelper.StateToShow((int)PlanState.UnStart);
plan.Plan_Date = String.Format("{0:yyyy-MM-dd}", DateTime.Now);
plan.IF_FLAG = 1;
if (string.IsNullOrEmpty(item.Weighbridge))
{
throw new Exception("地磅不可为空!");
}
plan.Weighbridge = item.Weighbridge;
plan.PumpBarCode = item.PumpCode;
if (item.PumpCode==null|| item.PumpCode=="")
{
throw new Exception("投料泵为空!");
}
plan.PumpName = GetPumpInfo(item.PumpCode).Name;
#endregion
if (recipe == null)
{
throw new Exception("配方数据不可为空!");
}
if (weighList == null)
{
throw new Exception("配方物料数据不可为空!");
}
if (plan == null)
{
throw new Exception("计划数据不可为空!");
}
if (repiceList == null)
{
throw new Exception("反应釜计划数据不可为空!");
}
bool r = SaveRecipe(recipe, weighList, plan, repiceList.ToList());
if (r == false)
{
string msg = $"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!";
MessageBox.Show(msg, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
//Console.WriteLine($"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!");
}
//Console.WriteLine($"{item.PId}——{item.ProductName}——{item.Recipe};状态:{r}");
}
catch (Exception ex)
{
string msg = $"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}";
MessageBox.Show(msg, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
public static void DownZBPlan(DataTable dt,int MainId)
{
var list = DataTableHelper.DataTableToList(dt);
List<Hw_WareHouse_Sub> subList = new List<Hw_WareHouse_Sub>();
List<Pmt_recipe> recipeList = new List<Pmt_recipe>();
list.ForEach((a) =>
{
Hw_WareHouse_Sub sb = new Hw_WareHouse_Sub();
sb.MainId = MainId;
sb.PId = a.PId;
sb.ProductName = a.ProductName;
a.Plan_State = 3;//待下发
sb.MaterialID = a.Material_Code;
sb.Material_Code = a.Material_Code;
sb.MaterialName = a.MaterialName;
sb.MaterialType = a.MaterialType;
sb.SetWeight = Convert.ToDouble(a.SetWeight);
sb.SetError = Convert.ToDouble(a.SetError);
sb.Difference = a.Difference;
sb.PumpCode = a.PumpCode;
sb.Weighbridge = a.Weighbridge;
sb.Recipe = a.Recipe;
sb.Remark = a.Remark;
sb.CreateTime = DateTime.Now;
subList.Add(sb);
});
//查询数据
var xlList = subList.Where(d => d.MaterialType.Contains("整包"));
var xt = (from d in xlList
select new
{
d.PId,
d.ProductName,
d.MaterialType,
d.Recipe,
d.Weighbridge,
}).Distinct();
foreach (var item in xt)
{
try
{
#region 配方
Pmt_recipe recipe = new Pmt_recipe();
recipe.ID = Guid.NewGuid().ToString("N");
recipe.Equip_Code = "01";
recipe.Recipe_Code = item.Recipe;
recipe.Version = "1";
recipe.Recipe_Name = item.Recipe;
recipe.Recipe_Verify = 1;
recipe.Total_Weight = 0;
recipe.Total_Error = 0;
recipe.CreateDateTime = DateTime.Now;
#endregion
#region 物料
var repiceList = xlList.Where(d => item.PId.Equals(d.PId) && item.ProductName.Equals(d.ProductName) && item.Recipe.Equals(d.Recipe));
int i = 1;
List<Pmt_weigh> weighList = new List<Pmt_weigh>();
foreach (var sub in repiceList)
{
Pmt_weigh material = new Pmt_weigh();
material.ID = Guid.NewGuid().ToString("N");
material.Equip_Code = "01";
material.Recipe_ID = recipe.ID;
material.Weight_Id = i;
if (sub.Material_Code == null | sub.Material_Code == "")
{
throw new Exception("未查询到物料!");
}
var obj = GetMaterial(sub.Material_Code);
if (obj == null)
{
throw new Exception("未查询到当前物料!");
}
material.Material_ID = obj.ID;
material.Set_Weight = Convert.ToDecimal(sub.SetWeight);
material.Set_Error = Convert.ToDecimal(sub.SetError);
recipe.Total_Weight = recipe.Total_Weight + material.Set_Weight;
recipe.Total_Error = recipe.Total_Error + material.Set_Error;
weighList.Add(material);
i++;
}
#endregion
#region 计划
RT_plan plan = new RT_plan();
plan.Dosing_Id = 0;
plan.Equip_Code = "01";
plan.Batch = item.PId;
plan.ProductName = item.ProductName;
int serialNum = 0;
int rtPlanSNum = GetNextSerialNum(); //xl_plan序号
int lrPlanSNum = GetNextSerialNumFromLRPlan(DateTime.Now); //Hw_Plan序号
if (rtPlanSNum >= lrPlanSNum)
{
serialNum = rtPlanSNum;
}
else
{
serialNum = lrPlanSNum;
}
string planID = CommHelper.GenerateNextPlanIDNew(DateTime.Now, 1, serialNum); //计划号
plan.Plan_Id = planID;
plan.Plan_Serial = serialNum;
plan.Recipe_ID = recipe.ID;
plan.Recipe_Code = item.Recipe;
plan.Recipe_Name = item.Recipe;
plan.Version = "1";
plan.Mixer_Line = null;
plan.Recipe_Type = 0;
plan.Shift_Id = "1";
plan.Shift_Class = "";
plan.Plan_Num = 1;
plan.Real_Num = 0;
plan.Duration_Time = 0;
plan.End_Date = null;
plan.Weight_Man = null;
plan.Stock_Man = null;
plan.Plan_Batch = null;
plan.Plan_State = (int)PlanState.UnStart;
plan.Plan_StateText = CommHelper.StateToShow((int)PlanState.UnStart);
plan.Plan_Date = String.Format("{0:yyyy-MM-dd}", DateTime.Now);
plan.IF_FLAG = 1;
if (string.IsNullOrEmpty(item.Weighbridge))
{
throw new Exception("地磅不可为空!");
}
plan.Weighbridge = item.Weighbridge;
#endregion
if (recipe == null)
{
throw new Exception("配方数据不可为空!");
}
if (weighList == null)
{
throw new Exception("配方物料数据不可为空!");
}
if (plan == null)
{
throw new Exception("计划数据不可为空!");
}
if (repiceList == null)
{
throw new Exception("反应釜计划数据不可为空!");
}
bool r = SaveRecipe(recipe, weighList, plan, repiceList.ToList());
if (r == false)
{
string msg = $"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!";
MessageBox.Show(msg, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
//Console.WriteLine($"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!");
}
//Console.WriteLine($"{item.PId}——{item.ProductName}——{item.Recipe};状态:{r}");
}
catch (Exception ex)
{
string msg = $"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}";
MessageBox.Show(msg, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
//Console.WriteLine($"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}");
}
}
}
#region 根据二维码获取物料
public static Pmt_material GetMaterial(string barCode)
{
IFreeSql _freeSql = FreeHelper.Instance;
string sql = $"select * from Pmt_material where Material_code='{barCode}'";
var obj = _freeSql.Select<Pmt_material>().WithSql(sql).ToOne();
return obj;
}
#endregion
#region 根据二维码获取泵信息
public static Hw_Pump GetPumpInfo(string barCode)
{
IFreeSql _freeSql = FreeHelper.Instance;
string sql = $"select * from Hw_Pump where BarCode='{barCode}'";
var obj = _freeSql.Select<Hw_Pump>().WithSql(sql).ToOne();
return obj;
}
#endregion
public static int GetNextSerialNum()
{
string sql = "SELECT * FROM RT_plan";
IFreeSql _freeSql = FreeHelper.Instance;
var result = _freeSql.Select<RT_plan>(sql).Max(d => d.Plan_Serial);
if (result == 0)
{
return 1;
}
else
{
return Convert.ToInt32(result) + 1;
}
}
#region GetNextSerialNumFromLRPlan 获取下一个可用的序号SerialNum 规则:同机台、同日期、同班次 计划号最后2位的最大值加1(xl_plan)
/// <summary>
/// 获取下一个可用的序号
/// </summary>
/// <param name="planDate">计划生产日期</param>
/// <returns>返回可用的序号</returns>
public static int GetNextSerialNumFromLRPlan(DateTime planDate)
{
string strSql = $"SELECT * FROM Hw_plan where LEFT(Plan_Id,8) = '{String.Format("{0:yyyyMMdd}", planDate)}' and RIGHT(Plan_Id,2)=(select MAX(RIGHT(Plan_Id,2)) as Id from LR_plan)";
IFreeSql _freeSql = FreeHelper.Instance;
var result = _freeSql.Select<Hw_plan>().WithSql(strSql).ToOne();
if (result == null)
{
return 1;
}
else
{
string serial = result.Plan_Id.Substring(result.Plan_Id.Length - 2, 2);
return Convert.ToInt32(serial) + 1;
}
}
#endregion
#region 保存配方、计划物料信息
/// <summary>
/// 保存配方、计划物料信息
/// </summary>
/// <param name="recipeList">配方、物料</param>
/// <param name="xl_weighList"></param>
/// <param name="plan"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static bool SaveRecipe(Pmt_recipe recipeList, List<Pmt_weigh> pmt_weighList, RT_plan plan, List<Hw_WareHouse_Sub> sub)
{
IFreeSql _freeSql = FreeHelper.Instance;
_freeSql.Transaction(() =>
{
var affrows = _freeSql.Insert(recipeList).ExecuteAffrows();//保存配方
if (affrows < 1)
throw new Exception("保存溶剂配方失败!");
affrows = _freeSql.Insert(pmt_weighList).ExecuteAffrows();//保存配方物料
if (affrows < 1)
throw new Exception("保存配方物料失败!");
affrows = _freeSql.Insert(plan).ExecuteAffrows();//保存计划
if (affrows < 1)
throw new Exception("保存计划失败!");
affrows = _freeSql.Insert(sub).ExecuteAffrows();//保存反应釜计划
if (affrows < 1)
throw new Exception("保存反应釜计划失败!");
});
return true;
}
#endregion
}
}