using Mesnac.Action.ChemicalWeighing.DBHelper; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.Entity.material; 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 XLHelper { public static void DownXlPlan(DataTable dt,int MainId) { var list = DataTableHelper.DataTableToList(dt); List subList = new List(); List recipeList = new List(); list.ForEach((a) => { Hw_WareHouse_Sub sb = new Hw_WareHouse_Sub(); sb.MainId = MainId; sb.PId = a.PId; sb.ProductName = a.ProductName; sb.BinId = a.BinId == "" || a.BinId == null ? 0 : int.Parse(a.BinId); 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 ? 0.00 : Convert.ToDouble(a.FixedRatio); sb.Recipe = a.Recipe; sb.Remark = a.Remark; 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, }).Distinct(); foreach (var item in xt) { try { #region 配方 xl_recipe recipe = new xl_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; recipe.IsCheck = 0; #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 weighList = new List(); foreach (var sub in repiceList) { xl_weigh material = new xl_weigh(); 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; if (sub.BinId == 0) { throw new Exception("未查询到对应料仓!"); } var binInfo = GetBinInfo(sub.BinId, obj.ID); if (binInfo == null) { throw new Exception("未查询到对应料仓!"); } material.Bin_Serial = sub.BinId; 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 计划 xl_plan plan = new xl_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); //LR_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.Recipe_Type = 0; plan.Shift_Id = "1"; plan.Shift_Class = ""; plan.Plan_Num = 1; plan.Real_Num = 0; plan.Duration_Time = 0; plan.Mixer_Line = "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; #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 xl_material GetMaterial(string barCode) { IFreeSql _freeSql = FreeHelper.Instance; string sql = $"select * from xl_material where Material_code='{barCode}'"; var obj = _freeSql.Select().WithSql(sql).ToOne(); return obj; } #endregion #region 根据料桶编号和物料二维码查询料仓信息 public static Pmt_Bin GetBinInfo(int binId, string materialId) { IFreeSql _freeSql = FreeHelper.Instance; string sql = $"select * from Pmt_Bin where Bin_Serial='{binId}' and Material_ID='{materialId}'"; var obj = _freeSql.Select().WithSql(sql).ToOne(); return obj; } #endregion public static int GetNextSerialNum() { string sql = "SELECT * FROM xl_plan"; IFreeSql _freeSql = FreeHelper.Instance; var result = _freeSql.Select(sql).Max(d => d.Plan_Serial); if (result == 0) { return 1; } else { return Convert.ToInt32(result) + 1; } } #region GetNextSerialNumFromLRPlan 获取下一个可用的序号SerialNum 规则:同机台、同日期、同班次 计划号最后2位的最大值加1(xl_plan) /// /// 获取下一个可用的序号 /// /// 计划生产日期 /// 返回可用的序号 public static int GetNextSerialNumFromLRPlan(DateTime planDate) { string strSql = $"SELECT * FROM LR_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().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 保存配方、计划物料信息 /// /// 保存配方、计划物料信息 /// /// 配方、物料 /// /// /// /// public static bool SaveRecipe(xl_recipe recipeList, List xl_weighList, xl_plan plan, List sub) { IFreeSql _freeSql = FreeHelper.Instance; _freeSql.Transaction(() => { var affrows = _freeSql.Insert(recipeList).ExecuteAffrows();//保存配方 if (affrows < 1) throw new Exception("保存小料配方失败!"); affrows = _freeSql.Insert(xl_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 } }