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.

1354 lines
56 KiB
C#

1 year ago
using Admin.Core.IRepository;
using Admin.Core.IService;
using Admin.Core.Model;
using System.Threading.Tasks;
using System.Linq;
using System;
using Admin.Core.Model.ViewModels;
using log4net;
using System.Collections.Generic;
1 year ago
using Microsoft.Data.SqlClient;
1 year ago
using Admin.Core.PlcServer;
using System.Data;
using SqlSugar;
using System.Numerics;
using System.Text;
using NPOI.POIFS.FileSystem;
using System.Linq.Expressions;
using NPOI.SS.Formula.Functions;
using Admin.Core.Repository;
using Microsoft.AspNetCore.SignalR;
using Admin.Core.Common;
1 year ago
namespace Admin.Core.Service
{
public class Hw_WarehouseServices : BaseServices<Hw_Warehouse>, IHw_WarehouseServices
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(Hw_WarehouseServices));
private readonly IBaseRepository<Hw_Warehouse> _dal;
private readonly IHw_WarehouseRepository _wareHouse;
private readonly IPmt_materialRepository _material;
1 year ago
private readonly IHw_WareHouse_SubRepository _wareHouse_Sub;
private readonly IHw_FeedReportRepository _feed;
private readonly IHw_BarrelRepository _barrel;
private readonly Ixl_materialRepository _materialRepository;
private readonly Ixl_recipeRepository _recipeRepository;
1 year ago
private readonly ILR_weighRepository _lrWeighRepository;
private readonly Ixl_planRepository _planRepository;//小料计划
private readonly IHw_PumpRepository _hwPumpRepository;
private readonly IRT_planRepository _rtplanRepository;
private readonly IPmt_weighRepository _pmtWeighRepository;
private readonly IPmt_BinRepository _binRepository;
private readonly IPmt_recipeRepository _pmtRecipeRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly Ixl_weighRepository _xlWeighRepository;
private readonly IXLPlan_weightRepository _XLPlan_weightRepository;
1 year ago
1 year ago
public Hw_WarehouseServices(IBaseRepository<Hw_Warehouse> dal, IHw_WarehouseRepository wareHouse,
IHw_WareHouse_SubRepository wareHouse_Sub, IHw_FeedReportRepository feed,
IHw_BarrelRepository barrel, Ixl_materialRepository materialRepository,
Ixl_recipeRepository recipeRepository, ILR_weighRepository lrWeighRepository, Ixl_planRepository planRepository, IXLPlan_weightRepository IXLPlan_weightRepository, IXLPlan_weightRepository xLPlan_weightRepository, IPmt_materialRepository PmtmaterialRepository, IHw_PumpRepository hwPumpRepository, IRT_planRepository rtplanRepository, IPmt_recipeRepository pmtRecipeRepository, IUnitOfWork unitOfWork, IPmt_weighRepository pmtWeighRepository, IPmt_BinRepository binRepository, Ixl_weighRepository xlWeighRepository)
1 year ago
{
this._dal = dal;
base.BaseDal = dal;
_wareHouse = wareHouse;
_wareHouse_Sub = wareHouse_Sub;
_feed = feed;
_barrel = barrel;
_materialRepository = materialRepository;
_recipeRepository = recipeRepository;
1 year ago
_lrWeighRepository = lrWeighRepository;
_planRepository = planRepository;
_XLPlan_weightRepository = xLPlan_weightRepository;
_material = PmtmaterialRepository;
_hwPumpRepository = hwPumpRepository;
_rtplanRepository = rtplanRepository;
_pmtRecipeRepository = pmtRecipeRepository;
_unitOfWork = unitOfWork;
_pmtWeighRepository = pmtWeighRepository;
_binRepository = binRepository;
_xlWeighRepository = xlWeighRepository;
1 year ago
}
1 year ago
#region 扫描桶二维码,获取绑定的小料配方物料
1 year ago
/// <summary>
/// 扫描桶二维码,获取绑定的小料配方
/// </summary>
10 months ago
/// <param name="kettleBarCode">反应釜</param>
/// <param name="planId">计划Id</param>
/// <param name="code">料桶Id</param>
1 year ago
/// <returns></returns>
1 year ago
public async Task<BarrelView> GetXlInfo(string kettleBarCode, string planId, string code)
1 year ago
{
1 year ago
List<MaterialView> list = new List<MaterialView>();
List<MaterialView> materList = new List<MaterialView>();
1 year ago
try
{
1 year ago
Hw_Barrel hw = await _barrel.FirstAsync(d => d.IsEnable == "是" && d.BarCode == code);
1 year ago
if (hw == null) { return null; }
10 months ago
var recipe = await _recipeRepository.FirstAsync(d => d.Recipe_Verify == 1 && d.ID == hw.MaterialID);
1 year ago
if (recipe == null) { return null; }
var materialList = await _lrWeighRepository.QueryAsync(d => d.Plan_ID == hw.PlanId && d.Recipe_ID == recipe.ID);
if (materialList.Count() == 0) { return null; }
foreach (var material in materialList)
{
MaterialView childView = new MaterialView();
childView.MaterialID = material.Material_ID;
var mat = await _materialRepository.FirstAsync(d => d.ID.Equals(material.Material_ID));
if (materialList == null) { continue; }
childView.MaterialBarCode = mat.Material_code;
childView.MaterialName = material.Material_Name;
childView.Weight = material.Real_Weight;
childView.Error = material.Real_Error;
childView.MatchOrNot = 0;
list.Add(childView);
}
var kettle = await _dal.FirstAsync(d => d.BarCode.Equals(kettleBarCode));
var subList = await _wareHouse_Sub.QueryAsync(d => d.MainId == kettle.ID && d.PId.Equals(planId) &&d.MaterialType.Contains("尾料"));
foreach (var check in list)
{
MaterialView childView = new MaterialView();
childView.MaterialID = check.MaterialID;
var mat = await _materialRepository.FirstAsync(d => d.ID.Equals(check.MaterialID));
childView.MaterialBarCode = mat.Material_code;
childView.MaterialName = check.MaterialName;
var pList = subList.Where(d => d.Material_Code.Equals(check.MaterialBarCode));
if (pList == null)
{
childView.MatchOrNot = 0;
materList.Add(childView);
continue;
}
foreach (var item in pList)
{
decimal? maxValue = item.SetWeight + item.SetError;
decimal? minValue = item.SetWeight - item.SetError;
if (minValue <= check.Weight && check.Weight <= maxValue)
{
childView.Weight = check.Weight;
childView.Error = check.Error;
childView.MatchOrNot = 1;
}
else
{
childView.Weight = check.Weight;
childView.Error = check.Error;
childView.MatchOrNot = 0;
}
}
materList.Add(childView);
}
1 year ago
BarrelView view = new BarrelView()
{
BarrelID = hw.BarrelID,
BarrelName = hw.BarrelName,
BarCode = hw.BarCode,
1 year ago
child = materList
1 year ago
};
return view;
}
catch (Exception ex)
{
log.Error(ex.Message);
return null;
}
}
#endregion
#region GetNextSerialNum 获取下一个可用的序号SerialNum 规则:同机台、同日期、同班次 计划号最后2位的最大值加1
/// <summary>
/// 获取下一个可用的序号
/// </summary>
/// <param name="planDate">计划生产日期</param>
/// <returns>返回可用的序号</returns>
public int GetNextSerialNumAsync()
{
string strSql = "SELECT MAX(Plan_Serial) FROM RT_plan";
Expression<Func<RT_plan, bool>> exp = s1 => true;
Expression<Func<RT_plan, object>> order = (x) => x.Plan_Serial;
var result = _rtplanRepository.Query(exp,order,false).First();
if (result == null)
{
return 1;
}
else
{
return Convert.ToInt32(result.Plan_Serial) + 1;
}
}
public int GetXLNextSerialNumAsync()
{
string strSql = "SELECT MAX(Plan_Serial) FROM xl_plan";
Expression<Func<xl_plan, bool>> exp = s1 => true;
Expression<Func<xl_plan, object>> order = (x) => x.Plan_Serial;
var result = _planRepository.Query(exp, order, false).First();
if (result == null)
{
return 1;
}
else
{
return Convert.ToInt32(result.Plan_Serial) + 1;
}
}
public int GetNextSerialNumFromLRPlan(DateTime planDate)
{
string strSql = "SELECT MAX(RIGHT(Plan_Id,2)) FROM Hw_plan WHERE LEFT(Plan_Id,8) = @PlanDate";
string ss = String.Format("{0:yyyyMMdd}", planDate);
SugarParameter[] parameters = new SugarParameter[]
{
new SugarParameter($"@PlanDate", String.Format("{0:yyyyMMdd}", planDate))
};
var result = _planRepository.ExecSql(strSql, parameters);
if (result == null)
{
return 1;
}
else
{
return Convert.ToInt32(result) + 1;
}
}
public int GetXLNextSerialNumFromLRPlan(DateTime planDate)
{
string strSql = "SELECT MAX(RIGHT(Plan_Id,2)) FROM LR_plan WHERE LEFT(Plan_Id,8) = @PlanDate";
string ss = String.Format("{0:yyyyMMdd}", planDate);
SugarParameter[] parameters = new SugarParameter[]
{
new SugarParameter($"@PlanDate", String.Format("{0:yyyyMMdd}", planDate))
};
var result = _planRepository.ExecSql(strSql, parameters);
if (result == null)
{
return 1;
}
else
{
return Convert.ToInt32(result) + 1;
}
}
#endregion
/// <summary>
/// 获取下一个可用的计划号 (6位日期、2位机台、1位班次、1位网络/本机、2位流水)
/// </summary>
/// <param name="equipCode">机台号</param>
/// <param name="planDate">计划生产日期</param>
/// <param name="shiftID">班次</param>
/// <returns>返回生成的12位计划号</returns>
public static string GenerateNextPlanIDNew(DateTime planDate, int shiftID, int sNum)
{
string code = "01";
return String.Format("{0:yyyyMMdd}{1}{2}", planDate, code, shiftID) + FillZero(sNum.ToString(), 2);
}
#region "按字符串位数补0"
/// <summary>
/// 按字符串位数补0
/// </summary>
/// <param name="CharTxt">字符串</param>
/// <param name="CharLen">字符长度</param>
/// <returns></returns>
public static string FillZero(string CharTxt, int CharLen)
{
if (CharTxt.Length < CharLen)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < CharLen - CharTxt.Length; i++)
{
sb.Append("0");
}
sb.Append(CharTxt);
return sb.ToString();
}
else
{
return CharTxt;
}
}
#endregion
public async Task<string> DownRJPlanAsync(List<MesDownLoadPlanList> MesDownLoadPlanList)
{
try
{
var list = MesDownLoadPlanList;
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 = int.Parse(a.MainId);
sb.PId = a.PId;
sb.ProductName = a.ProductName;
sb.MaterialID = a.Material_Code;
sb.Material_Code = a.Material_Code;
sb.MaterialName = a.MaterialName;
sb.MaterialType = a.MaterialType;
sb.SetWeight = Convert.ToDecimal(a.SetWeight);
sb.SetError = Convert.ToDecimal(a.SetError);
sb.Difference = a.Difference;
sb.Fixed = a.Fixed == "" || a.Fixed == null ? false : true;
sb.FixedRatio = a.FixedRatio == "" || a.FixedRatio == null ? Convert.ToDecimal(0.00) : Convert.ToDecimal(a.FixedRatio);
sb.Weighbridge = a.Weighbridge;
sb.PumpCode = a.PumpCode;
sb.Recipe = a.Recipe;
sb.Remark = a.Remark;
sb.CreateTime = DateTime.Now;
sb.Tare = Convert.ToDecimal(a.Tare);
subList.Add(sb);
});
//查询溶剂数据
var xlList = subList.Where(d => d.MaterialType.Contains("溶剂")).ToList();
var xt = (from d in xlList
select new
{
d.PId,
d.ProductName,
d.MaterialType,
d.Recipe,
d.Weighbridge,
d.PumpCode
}).Distinct().ToList();
if (xt.Count == 0)
{
return "成功";
}
foreach (var item in xt)
{
try
{
#region 配方
string recipeCode = Guid.NewGuid().ToString("N");
string recipeID = Guid.NewGuid().ToString("N");
Pmt_recipe recipe = new Pmt_recipe();
recipe.ID = recipeID;
recipe.Equip_Code = "01";
recipe.Recipe_Code = recipeCode;
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)).ToList();
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 = recipeID;
material.Weight_Id = i;
if (sub.Material_Code == null | sub.Material_Code == "")
{
throw new Exception("未查询到物料!");
}
var obj = await _material.FirstAsync(d => d.Material_code == sub.Material_Code);
if (obj == null)
{
throw new Exception("未查询到当前物料!");
}
material.Material_ID = obj.ID.ToString();
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 = GetNextSerialNumAsync(); //xl_plan序号
int lrPlanSNum = GetNextSerialNumFromLRPlan(DateTime.Now); //Hw_Plan序号
if (rtPlanSNum >= lrPlanSNum)
{
serialNum = rtPlanSNum;
}
else
{
serialNum = lrPlanSNum;
}
string planID = GenerateNextPlanIDNew(DateTime.Now, 1, serialNum); //计划号
plan.Plan_Id = planID;
plan.Plan_Serial = serialNum;
plan.Recipe_ID = recipeID;
plan.Recipe_Code = recipeCode;
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 = 5;
plan.Plan_StateText = "未启动";
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("投料泵为空!");
}
var pumpList = _hwPumpRepository.Query();
var pump = pumpList.FirstOrDefault(d => d.BarCode == item.PumpCode);
plan.PumpName = pump.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}],导入数据失败!";
return msg;
Console.WriteLine($"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!");
}
return "成功";
//Console.WriteLine($"{item.PId}——{item.ProductName}——{item.Recipe};状态:{r}");
}
catch (Exception ex)
{
string msg = $"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}";
return msg;
}
}
}
catch (Exception ex)
{
return ex.Message;
}
return "执行失败";
}
public async Task<string> DownZBPlanAsync(List<MesDownLoadPlanList> MesDownLoadPlanList)
{
try
{
var list = MesDownLoadPlanList;
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 = int.Parse(a.MainId);
sb.PId = a.PId;
sb.ProductName = a.ProductName;
sb.MaterialID = a.Material_Code;
sb.Material_Code = a.Material_Code;
sb.MaterialName = a.MaterialName;
sb.MaterialType = a.MaterialType;
sb.SetWeight = Convert.ToDecimal(a.SetWeight);
sb.SetError = Convert.ToDecimal(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;
sb.Tare = Convert.ToDecimal(a.Tare);
subList.Add(sb);
});
//查询数据
var xlList = subList.Where(d => d.MaterialType.Contains("整包")).ToList();
var xt = from d in xlList
group d by new { PId = d.PId, ProductName = d.ProductName, Material_Code = d.Material_Code, MaterialType = d.MaterialType, Recipe = d.Recipe, SetWeight = d.Recipe, SetError = d.SetError, Weighbridge = d.Weighbridge } into g
select new
{
g.Key.PId,
g.Key.ProductName,
g.Key.Material_Code,
g.Key.MaterialType,
g.Key.Recipe,
SetWeight = g.Sum(m => m.SetWeight),
SetError = g.Sum(m => m.SetError),
g.Key.Weighbridge
};
if (xt.ToList().Count == 0)
{
return "成功";
}
foreach (var item in xt)
{
try
{
#region 配方
string recipeID = Guid.NewGuid().ToString("N");
string recipeCode = Guid.NewGuid().ToString("N");
Pmt_recipe recipe = new Pmt_recipe();
{
recipe.ID = recipeID;
recipe.Equip_Code = "01";
recipe.Recipe_Code = recipeCode;
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 物料
List<Pmt_weigh> weighList = new List<Pmt_weigh>();
{
Pmt_weigh material = new Pmt_weigh();
material.ID = Guid.NewGuid().ToString("N");
material.Equip_Code = "01";
material.Recipe_ID = recipe.ID;
material.Weight_Id = 1;
if (item.Material_Code == null | item.Material_Code == "")
{
throw new Exception("未查询到物料!");
}
var obj = await _material.FirstAsync(d => d.Material_code == item.Material_Code);
if (obj == null)
{
throw new Exception("未查询到当前物料!");
}
material.Material_ID = obj.ID;
material.Set_Weight = Convert.ToDecimal(item.SetWeight);
material.Set_Error = Convert.ToDecimal(item.SetError);
recipe.Total_Weight = Convert.ToDecimal(item.SetWeight);
recipe.Total_Error = Convert.ToDecimal(item.SetError);
weighList.Add(material);
}
#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 = GetNextSerialNumAsync(); //xl_plan序号
int lrPlanSNum = GetNextSerialNumFromLRPlan(DateTime.Now); //Hw_Plan序号
if (rtPlanSNum >= lrPlanSNum)
{
serialNum = rtPlanSNum;
}
else
{
serialNum = lrPlanSNum;
}
string planID = GenerateNextPlanIDNew(DateTime.Now, 1, serialNum); //计划号
plan.Plan_Id = planID;
plan.Plan_Serial = serialNum;
plan.Recipe_ID = recipe.ID;
plan.Recipe_Code = recipeCode;
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 = 5;
plan.Plan_StateText = "未启动";
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("计划数据不可为空!");
}
var repiceList = xlList.Where(d => item.PId.Equals(d.PId) && item.ProductName.Equals(d.ProductName) && item.Recipe.Equals(d.Recipe)).ToList();
if (repiceList == null)
{
throw new Exception("反应釜计划数据不可为空!");
}
bool r = SaveRecipe(recipe, weighList, plan, repiceList.ToList());
if (r == false)
{
string msg = $"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!";
return msg;
//Console.WriteLine($"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!");
}
//Console.WriteLine($"{item.PId}——{item.ProductName}——{item.Recipe};状态:{r}");
return "成功";
}
catch (Exception ex)
{
string msg = $"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}";
return msg;
//Console.WriteLine($"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}");
}
}
}
catch (Exception ex)
{
return ex.Message;
}
return "执行失败";
}
public async Task<string> DownXLPlanAsync(List<MesDownLoadPlanList> MesDownLoadPlanList)
{
try
{
var list = MesDownLoadPlanList;
List <Hw_WareHouse_Sub> subList = new List<Hw_WareHouse_Sub>();
List<xl_recipe> recipeList = new List<xl_recipe>();
list.ForEach((a) =>
{
Hw_WareHouse_Sub sb = new Hw_WareHouse_Sub();
sb.MainId = int.Parse(a.MainId);
sb.PId = a.PId;
sb.ProductName = a.ProductName;
sb.BinId = a.BinId == "" || a.BinId == null ? 0 : int.Parse(a.BinId);
sb.MaterialID = a.Material_Code;
sb.Material_Code = a.Material_Code;
sb.MaterialName = a.MaterialName;
sb.MaterialType = a.MaterialType;
sb.SetWeight = Convert.ToDecimal(a.SetWeight);
sb.SetError = Convert.ToDecimal(a.SetError);
sb.Difference = a.Difference;
sb.Fixed = a.Fixed == "" || a.Fixed == null ? false : true;
sb.FixedRatio = a.FixedRatio == a.FixedRatio ? Convert.ToDecimal(0.00) : Convert.ToDecimal(a.FixedRatio);
sb.Recipe = a.Recipe;
sb.Remark = a.Remark;
subList.Add(sb);
});
//查询小料数据
var xlList = subList.Where(d => d.MaterialType.Contains("尾料")).ToList();
var xt = (from d in xlList
select new
{
d.PId,
d.ProductName,
d.MaterialType,
d.Recipe,
}).Distinct();
foreach (var item in xt)
{
try
{
string recipeID = Guid.NewGuid().ToString("N");
string recipeCode = Guid.NewGuid().ToString("N");
#region 配方
xl_recipe recipe = new xl_recipe();
recipe.ID = recipeID;
recipe.Equip_Code = "01";
recipe.Recipe_Code = recipeCode;
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)).ToList();
int i = 1;
List<xl_weigh> weighList = new List<xl_weigh>();
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 = await _materialRepository.FirstAsync(d => d.Material_code == sub.Material_Code);
if (obj == null)
{
throw new Exception("未查询到当前物料!");
}
material.Material_ID = obj.ID;
if (sub.BinId == 0)
{
throw new Exception("未查询到对应料仓!");
}
var binInfo = await _binRepository.FirstAsync(d => d.Bin_Serial == sub.BinId && d.Material_ID == 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 = GetXLNextSerialNumAsync(); //xl_plan序号
int lrPlanSNum = GetXLNextSerialNumFromLRPlan(DateTime.Now); //LR_Plan序号
if (rtPlanSNum >= lrPlanSNum)
{
serialNum = rtPlanSNum;
}
else
{
serialNum = lrPlanSNum;
}
string planID = GenerateNextPlanIDNew(DateTime.Now, 1, serialNum); //计划号
plan.Plan_Id = planID;
plan.Plan_Serial = serialNum;
plan.Recipe_ID = recipe.ID;
plan.Recipe_Code = recipeID;
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 = 5;
plan.Plan_StateText = "未启动";
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 = SaveXLRecipe(recipe, weighList, plan, repiceList.ToList());
if (r == false)
{
string msg = $"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!";
return msg;
//Console.WriteLine($"批次[{plan.Batch}];配方[{item.Recipe}],导入数据失败!");
}
else
{
return "成功";
}
//Console.WriteLine($"{item.PId}——{item.ProductName}——{item.Recipe};状态:{r}");
}
catch (Exception ex)
{
string msg = $"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}";
return msg;
//Console.WriteLine($"配方[{item.Recipe}] 导入失败!异常信息:{ex.Message}");
}
}
}
catch (Exception ex)
{
return ex.Message;
}
return "执行失败";
}
#region 保存配方、计划物料信息
/// <summary>
/// 保存配方、计划物料信息
/// </summary>
/// <param name="recipeList">配方、物料</param>
/// <param name="xl_weighList"></param>
/// <param name="plan"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public bool SaveRecipe(Pmt_recipe recipeList, List<Pmt_weigh> pmt_weighList, RT_plan plan, List<Hw_WareHouse_Sub> sub)
{
try
{
var iflag3 = _rtplanRepository.Add(plan);
if (iflag3.Result < 1)
throw new Exception("保存计划失败!");
var iflag4 = _wareHouse_Sub.Add(sub);
if (iflag4 < 1)
throw new Exception("保存反应釜计划失败!");
var iflag1 = _pmtRecipeRepository.Add(recipeList);
if (iflag1.Result < 1)
throw new Exception("保存溶剂配方失败!");
var iflag2 = _pmtWeighRepository.Add(pmt_weighList);
if (iflag2 < 1)
throw new Exception("保存配方物料失败!");
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool SaveXLRecipe(xl_recipe recipeList, List<xl_weigh> pmt_weighList, xl_plan plan, List<Hw_WareHouse_Sub> sub)
{
try
{
var iflag3 = _planRepository.Add(plan);
if (iflag3.Result < 1)
throw new Exception("保存计划失败!");
var iflag4 = _wareHouse_Sub.Add(sub);
if (iflag4 < 1)
throw new Exception("保存反应釜计划失败!");
var iflag1 = _recipeRepository.Add(recipeList);
if (iflag1.Result < 1)
throw new Exception("保存溶剂配方失败!");
var iflag2 = _xlWeighRepository.Add(pmt_weighList);
if (iflag2 < 1)
throw new Exception("保存配方物料失败!");
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
#region 根据二维码获取物料
public async Task<Pmt_material> GetMaterialAsync(string barCode)
{
Pmt_material sub = await _material.FirstAsync(d => d.Material_code == barCode);
return sub;
}
#endregion
1 year ago
1 year ago
#region 根据传入的二维码、计划Id 查询反应釜下所有的物料
1 year ago
/// <summary>
1 year ago
/// 根据传入的二维码、计划Id 查询反应釜下所有的物料
1 year ago
/// </summary>
1 year ago
/// <param name="code">反应釜二维码</param>
/// <param name="planId">计划Id</param>
1 year ago
/// <returns></returns>
1 year ago
public async Task<WarehouseView> QueryByCode(string code, string planId)
1 year ago
{
1 year ago
List<HwWareHouseSubView> list = new List<HwWareHouseSubView>();
1 year ago
try
{
1 year ago
Hw_Warehouse wh = await _wareHouse.FirstAsync(d => d.BarCode == code);
1 year ago
if (wh == null) return null;
1 year ago
var sub = await _wareHouse_Sub.QueryAsync(d => d.MainId == wh.ID&& d.PId== planId&&(d.MaterialType.Contains("整包")|| d.MaterialType.Contains("尾料")));
if (sub.Count == 0) return null;
sub.ForEach(sub =>
{
HwWareHouseSubView view = new HwWareHouseSubView();
view.ID = sub.ID;
view.MainId = sub.MainId;
view.PId = sub.PId;
view.ProductName = sub.ProductName;
view.MaterialID = sub.MaterialID;
view.Material_Code = sub.Material_Code;
view.MaterialName = sub.MaterialName;
view.BinId = sub.BinId;
view.MaterialType = sub.MaterialType;
10 months ago
view.SetWeight = sub.SetWeight- sub.Tare;//设置重量减去皮重
1 year ago
view.SetError = sub.SetError;
10 months ago
view.Tare = sub.Tare;
1 year ago
list.Add(view);
});
WarehouseView warehouseView = new WarehouseView()
1 year ago
{
ID = wh.ID,
Name = wh.Name,
BarCode = wh.BarCode,
1 year ago
Children = list
};
1 year ago
return warehouseView;
}
catch (Exception ex)
{
log.Error(ex.Message);
return null;
}
}
#endregion
#region 更新PLC状态
public async Task<string> RealWeightInfo(string WeightNo)
{
string WeightInfo = "";
try
{
string name = string.Empty;
if (string.IsNullOrEmpty(WeightNo))
{
log.Error("点位名称为空!");
return "点位名称为空!";
}
switch (WeightNo)
{
case "1":
name = "DB104.DBW132";
break;
case "2":
name = "DB109.DBW134";
break;
case "3":
name = "DB104.DBW136";
break;
case "4":
name = "DB104.DBW138";
break;
case "5":
name = "DB104.DBW140";
break;
case "6":
name = "DB104.DBW142";
break;
case "7":
name = "DB104.DBW144";
break;
case "8":
name = "DB104.DBW146";
break;
case "9":
name = "DB104.DBW148";
break;
case "10":
name = "DB104.DBW150";
break;
case "11":
name = "DB104.DBW152";
break;
case "12":
name = "DB104.DBW154";
break;
case "13":
name = "DB104.DBW156";
break;
case "14":
name = "DB104.DBW158";
break;
case "15":
name = "DB104.DBW160";
break;
case "16":
name = "DB104.DBW162";
break;
default:
break;
}
var s7 = PlcConnect.Instance;
var result = s7.ReadInt16(name);//反应釜点位
log.Error($"读取重量:{result.IsSuccess},返回信息:{result.Message}");
if (result.IsSuccess)
{
return (result.Content / 1000).ToString();
}
else
{
log.Error(result.Message+",读取失败!");
return WeightNo+"称,读取失败!";
}
//return WeightInfo;
}
catch (Exception ex)
{
log.Error(ex.Message);
return WeightInfo;
}
}
public async Task<List<XLPlanWeightView>> GetPlanWeightInfo()
{
try
{
List<XLPlanWeightView> XLPlanWeightView = await _XLPlan_weightRepository.GetPlanWeightList();
if (XLPlanWeightView != null)
{
return XLPlanWeightView;
}
return XLPlanWeightView;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 投料口开/投料口关
/// </summary>
/// <param name="Kcode">投料釜条码</param>
/// <param name="state">状态 0:关闭 1:开启</param>
/// <returns></returns>
public async Task<bool> UpdatePlcState(string Kcode, int state)
{
List<Hw_WareHouse_Sub> subList = null;
Hw_Warehouse wh = null;
string name = string.Empty;
wh = await _wareHouse.FirstAsync(d => d.BarCode == Kcode);
if (wh == null) return false;
#region 反应釜工位信号
//反应釜工位报警
switch (wh.ID)
{
case 1:
name = "DB109.DBW4.0";
break;
case 2:
name = "DB109.DBW10.0";
break;
case 3:
name = "DB109.DBW16.0";
break;
case 4:
name = "DB109.DBW22.0";
break;
case 5:
name = "DB109.DBW28.0";
break;
case 6:
name = "DB109.DBW34.0";
break;
case 7:
name = "DB109.DBW40.0";
break;
case 8:
name = "DB109.DBW46.0";
break;
case 9:
name = "DB109.DBW52.0";
break;
case 10:
name = "DB109.DBW58.0";
break;
case 11:
name = "DB109.DBW64.0";
break;
case 12:
name = "DB109.DBW70.0";
break;
case 13:
name = "DB109.DBW76.0";
break;
case 14:
name = "DB109.DBW82.0";
break;
case 15:
name = "DB109.DBW88.0";
break;
default:
break;
}
#endregion
if (string.IsNullOrEmpty(name))
{
log.Error("点位名称为空!");
return false;
}
var s7 = PlcConnect.Instance;
var result = s7.Write(name, short.Parse(state.ToString()));//反应釜点位
log.Error($"反应釜开启点位状态:{result.IsSuccess},返回信息:{result.Message}");
if (result.IsSuccess)
{
return result.IsSuccess;
}
else
{
log.Error("反应釜写入点位失败!");
return result.IsSuccess;
}
return true;
}
1 year ago
/// <summary>
/// 更新PLC状态
/// </summary>
/// <param name="view">本次验证状态 1开启0关闭</param>
/// <returns></returns>
public async Task<bool> UpdatePlcState(KettleView view)
{
List<Hw_WareHouse_Sub> subList = null;
1 year ago
List<Hw_FeedReport> report = new List<Hw_FeedReport>();
1 year ago
Hw_Warehouse wh = null;
string message = string.Empty;
string name = string.Empty;
#region 反应釜工位信号
//反应釜工位报警
switch (view.Station)
{
case 1:
name = "DB109.DBW4.0";
break;
case 2:
name = "DB109.DBW10.0";
break;
case 3:
name = "DB109.DBW16.0";
break;
case 4:
name = "DB109.DBW22.0";
break;
case 5:
name = "DB109.DBW28.0";
break;
case 6:
name = "DB109.DBW34.0";
break;
case 7:
name = "DB109.DBW40.0";
break;
case 8:
name = "DB109.DBW46.0";
break;
case 9:
name = "DB109.DBW52.0";
break;
case 10:
name = "DB109.DBW58.0";
break;
case 11:
name = "DB109.DBW64.0";
break;
case 12:
name = "DB109.DBW70.0";
break;
case 13:
name = "DB109.DBW76.0";
break;
case 14:
name = "DB109.DBW82.0";
break;
case 15:
name = "DB109.DBW88.0";
break;
default:
break;
}
#endregion
try
{
10 months ago
log.Info("开始投料");
#region 记录数据
//同时记录该扫码到数据库中
wh = await _wareHouse.FirstAsync(d => d.BarCode == view.KCode);
if (wh == null) return false;
subList = await _wareHouse_Sub.QueryAsync(d => d.MainId == wh.ID);
if (subList.Count == 0) return false;
log.Info("开启模式");
if (view.State == 1)
1 year ago
{
10 months ago
message = "开启";
}
if (view.State == 0)
{
message = "关闭";
}
if (string.IsNullOrEmpty(message))
{
log.Error("传递状态为空!");
return false;
}
foreach (var item in view.Child)
{
Hw_FeedReport hw = new Hw_FeedReport()
1 year ago
{
10 months ago
WID = wh.ID,
PlanId = view.PlanId,
WName = wh.Name,
WCode = wh.BarCode,
MaterialId = item.MatCode,
MaterialName = item.MatName,
MCode = item.MatCode,
MType = item.MatType,
IsTrue = message,
CreateTime = DateTime.Now,
WholePackage = item.WholePackage.Trim()
};
report.Add(hw);
}
await _feed.AddAsync(report);
1 year ago
10 months ago
if (string.IsNullOrEmpty(name))
{
log.Error("点位名称为空!");
return false;
}
var s7 = PlcConnect.Instance;
var result = s7.Write(name,short.Parse(view.State.ToString()));//反应釜点位
log.Error($"反应釜开启点位状态:{result.IsSuccess},返回信息:{result.Message}");
if (result.IsSuccess)
{
if (view.State == 1)
1 year ago
{
10 months ago
foreach (var item in view.Child.Where(d=>d.MatType== "散装"))
1 year ago
{
10 months ago
1 year ago
}
}
10 months ago
return result.IsSuccess;
1 year ago
}
else
{
10 months ago
log.Error("反应釜写入点位失败!");
return result.IsSuccess;
1 year ago
}
10 months ago
#endregion
1 year ago
}
catch (Exception ex)
{
10 months ago
log.Error($"异常:{ex.Message}");
1 year ago
return false;
}
}
#endregion
#region 获取所有计划号
/// <summary>
/// 获取所有计划号
/// </summary>
/// <returns></returns>
1 year ago
public async Task<List<string>> GetWarehousePlan(string code)
1 year ago
{
1 year ago
Hw_Warehouse wh = await _wareHouse.FirstAsync(d=>d.BarCode== code.Trim());
1 year ago
if (wh==null) return null;
var list = await _wareHouse_Sub.QueryAsync(d=>d.MainId== wh.ID);
var planList = (from d in list
select d.PId).Distinct();
return planList.ToList();
1 year ago
}
#endregion
}
}