@ -15,6 +15,10 @@ 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 ;
namespace Admin.Core.Service
{
@ -23,6 +27,7 @@ namespace Admin.Core.Service
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 ;
private readonly IHw_WareHouse_SubRepository _wareHouse_Sub ;
private readonly IHw_FeedReportRepository _feed ;
private readonly IHw_BarrelRepository _barrel ;
@ -30,13 +35,21 @@ namespace Admin.Core.Service
private readonly Ixl_recipeRepository _recipeRepository ;
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 ;
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 )
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 )
{
this . _dal = dal ;
base . BaseDal = dal ;
@ -49,6 +62,14 @@ namespace Admin.Core.Service
_lrWeighRepository = lrWeighRepository ;
_planRepository = planRepository ;
_XLPlan_weightRepository = xLPlan_weightRepository ;
_material = PmtmaterialRepository ;
_hwPumpRepository = hwPumpRepository ;
_rtplanRepository = rtplanRepository ;
_pmtRecipeRepository = pmtRecipeRepository ;
_unitOfWork = unitOfWork ;
_pmtWeighRepository = pmtWeighRepository ;
_binRepository = binRepository ;
_xlWeighRepository = xlWeighRepository ;
}
#region 扫描桶二维码,获取绑定的小料配方物料
@ -137,6 +158,8 @@ namespace Admin.Core.Service
}
}
# endregion
#region GetNextSerialNum 获取下一个可用的序号SerialNum 规则:同机台、同日期、同班次 计划号最后2位的最大值加1
/// <summary>
/// 获取下一个可用的序号
@ -144,6 +167,22 @@ namespace Admin.Core.Service
/// <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" ;
@ -160,6 +199,24 @@ namespace Admin.Core.Service
}
}
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 ) ;
@ -177,7 +234,6 @@ namespace Admin.Core.Service
return Convert . ToInt32 ( result ) + 1 ;
}
}
# endregion
/// <summary>
/// 获取下一个可用的计划号 (6位日期、2位机台、1位班次、1位网络/本机、2位流水)
@ -217,66 +273,672 @@ namespace Admin.Core.Service
}
# endregion
public async Task < bool > InsertXLPlanInfo ( DateTime planDate , string recipeGUID , string batch , string productName )
public async Task < string > DownRJPlanAsync ( List < MesDownLoadPlanList > MesDownLoadPlanList )
{
try
{
int shiftID = 1 ;
//根据配方编号查询
var recipe = await _recipeRepository . FirstAsync ( d = > d . Recipe_Verify = = 1 & & d . ID = = recipeGUID ) ;
if ( recipe = = null )
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
{
recipe . ID = recipeGUID ;
recipe . Recipe_Name = recipeGUID ;
#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(planDate); //LR_Plan序号
string planID = GenerateNextPlanIDNew ( planDate , shiftID , rtPlanSNum ) ; //计划号hlcs0929
xl_plan xl_plan = new xl_plan ( )
{
Equip_Code = "01" ,
Dosing_Id = 0 ,
Plan_Id = planID ,
Batch = batch ,
ProductName = productName ,
Plan_Serial = rtPlanSNum ,
Recipe_ID = recipe . ID ,
Recipe_Code = recipe . ID ,
Recipe_Name = recipe . Recipe_Name ,
Version = recipe . Version ,
Mixer_Line = "0" ,
Recipe_Type = 0 ,
Shift_Id = "1" ,
Shift_Class = "0" ,
Plan_Num = 1 ,
Real_Num = 0 ,
Duration_Time = 0 ,
Plan_State = 5 ,
Plan_StateText = "未启动" ,
Plan_Date = DateTime . Now . ToString ( "yyyy-mm-dd" ) ,
IF_FLAG = 1 ,
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
} ;
var result = await _planRepository . Add ( xl_plan ) ;
if ( result = = 1 )
if ( xt. ToList ( ) . Count = = 0 )
{
return true ;
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
{
return false ;
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
#region 根据传入的二维码、计划Id 查询反应釜下所有的物料
/// <summary>