@ -7,6 +7,7 @@ using System;
using Admin.Core.Model.ViewModels ;
using log4net ;
using System.Collections.Generic ;
using Microsoft.Data.SqlClient ;
using Admin.Core.PlcServer ;
namespace Admin.Core.Service
@ -21,8 +22,12 @@ namespace Admin.Core.Service
private readonly IHw_BarrelRepository _barrel ;
private readonly Ixl_materialRepository _materialRepository ;
private readonly Ixl_recipeRepository _recipeRepository ;
private readonly ILR_weighRepository _lrWeighRepository ;
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 )
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 )
{
this . _dal = dal ;
base . BaseDal = dal ;
@ -32,31 +37,84 @@ namespace Admin.Core.Service
_barrel = barrel ;
_materialRepository = materialRepository ;
_recipeRepository = recipeRepository ;
_lrWeighRepository = lrWeighRepository ;
}
#region 扫描桶二维码,获取绑定的小料配方
#region 扫描桶二维码,获取绑定的小料配方 物料
/// <summary>
/// 扫描桶二维码,获取绑定的小料配方
/// </summary>
/// <param name="code">桶条码</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task < BarrelView > GetXlInfo ( string code)
public async Task < BarrelView > GetXlInfo ( string kettleBarCode, string planId , string code)
{
List < MaterialView > list = new List < MaterialView > ( ) ;
List < MaterialView > materList = new List < MaterialView > ( ) ;
try
{
Hw_Barrel hw = ( await _barrel . Query Async( d = > d . IsEnable = = "是" ) ) . FirstOrDefault ( d = > d . BarCode = = code ) ;
Hw_Barrel hw = await _barrel . First Async( d = > d . IsEnable = = "是" & & d . BarCode = = code ) ;
if ( hw = = null ) { return null ; }
var recipe = ( await _recipeRepository . QueryAsync ( d = > d . Recipe_Verify = = 1 ) ) . FirstOrDefault ( d = > d . Recipe_Code = = hw . MaterialID ) ;
var recipe = await _recipeRepository . FirstAsync ( d = > d . Recipe_Verify = = 1 & & d . Recipe_Code = = hw . MaterialID ) ;
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 ) ;
}
BarrelView view = new BarrelView ( )
{
BarrelID = hw . BarrelID ,
BarrelName = hw . BarrelName ,
BarCode = hw . BarCode ,
MaterialID = recipe . Recipe_Code ,
MaterialName = recipe . Recipe_Name ,
Weight = hw . Weight
child = materList
} ;
return view ;
}
@ -68,31 +126,46 @@ namespace Admin.Core.Service
}
# endregion
#region 根据传入的 code 查询投料 釜下所有的物料
#region 根据传入的 二维码、计划Id 查询反应 釜下所有的物料
/// <summary>
/// 根据传入的 code 查询投料 釜下所有的物料
/// 根据传入的 二维码、计划Id 查询反应 釜下所有的物料
/// </summary>
/// <param name="code"></param>
/// <param name="code">反应釜二维码</param>
/// <param name="planId">计划Id</param>
/// <returns></returns>
public async Task < WarehouseView > QueryByCode ( string code , string planId )
{
List < HwWareHouseSubView > list = new List < HwWareHouseSubView > ( ) ;
try
{
Hw_Warehouse wh = await _wareHouse . FirstAsync ( d = > d . BarCode = = code ) ;
if ( wh = = null ) return null ;
var sub = await _wareHouse_Sub . QueryAsync ( d = > d . MainId = = wh . ID & & d . PId = = planId ) ;
if ( sub = = null ) return null ;
WarehouseView warehouseView = new WarehouseView ( )
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 ;
view . SetWeight = sub . SetWeight ;
view . SetError = sub . SetError ;
list . Add ( view ) ;
} ) ;
WarehouseView warehouseView = new WarehouseView ( )
{
ID = wh . ID ,
PlanId = wh . PlanId ,
Name = wh . Name ,
BarCode = wh . BarCode ,
CreateTime = wh . CreateTime ,
UpdateTime = wh . UpdateTime ,
Children = sub
} ;
Children = list
} ;
return warehouseView ;
}
catch ( Exception ex )
@ -112,10 +185,8 @@ namespace Admin.Core.Service
public async Task < bool > UpdatePlcState ( KettleView view )
{
List < Hw_WareHouse_Sub > subList = null ;
List < Hw_Barrel > hbList = null ;
Hw_WareHouse_Sub sub = null ;
List < Hw_FeedReport > report = new List < Hw_FeedReport > ( ) ;
Hw_Warehouse wh = null ;
Hw_Barrel hwBarrel = null ;
string message = string . Empty ;
string name = string . Empty ;
@ -175,20 +246,18 @@ namespace Admin.Core.Service
try
{
var solventPlc = PlcHelper . siemensList . SingleOrDefault ( d = > d . EquipName . Equals ( "小料PLC" ) ) ;
//log.Info("开始");
var solventPlc = PlcHelper . siemensList . SingleOrDefault ( d = > d . EquipName . Equals ( "溶剂PLC" ) ) ;
log . Info ( $"PLC状态: {solventPlc.plc.IsConnected}" ) ;
if ( solventPlc . plc . IsConnected )
{
#region 记录数据
//同时记录该扫码到数据库中
List < Hw_Warehouse > list = await _wareHouse . QueryAsync ( ) ;
if ( list ! = null )
{
wh = list . FirstOrDefault ( d = > d . BarCode = = view . KCode ) ;
subList = await _wareHouse_Sub . QueryAsync ( d = > d . MainId = = wh . ID ) ;
if ( subList . Count > 0 )
{
sub = subList . FirstOrDefault ( d = > d . MaterialID = = view . MatCode . Trim ( ) ) ;
}
}
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 )
{
message = "开启" ;
@ -202,49 +271,37 @@ namespace Admin.Core.Service
log . Error ( "传递状态为空!" ) ;
return false ;
}
Hw_FeedReport hw = new Hw_FeedReport ( )
foreach ( var item in view . Child )
{
WID = wh . ID ,
PlanId = view . PlanId ,
WName = wh . Name ,
WCode = wh . BarCode ,
MaterialId = sub . MaterialID ,
MaterialName = sub . MaterialName ,
MCode = sub . Material_Code ,
MType = view . MatType ,
IsTrue = message ,
CreateTime = DateTime . Now ,
WholePackage = view . WholePackage . Trim ( )
} ;
//解绑 桶绑定
//hbList = await _barrel.QueryAsync();
//if (hbList != null)
//{
// hwBarrel = hbList.FirstOrDefault(d => d.BarCode == view.BarrelCode);
// if (hwBarrel != null)
// {
// hwBarrel.MaterialID = null;
// hwBarrel.MaterialName = null;
// await _feed.UpdateAsync(hw);
// }
//}
Hw_FeedReport hw = new Hw_FeedReport ( )
{
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 ) ;
//保存记录
// int i = await _feed.AddAsync(hw);
if ( string . IsNullOrEmpty ( name ) )
{
log . Error ( "点位名称为空!" ) ;
return false ;
}
bool result = solventPlc . plc . WriteInt16 ( name , view . State . ToString ( ) ) ; //反应釜点位
if ( result )
{
if ( view . State = = 1 )
{
await _feed . AddAsync ( hw ) ;
await _feed . AddAsync ( report ) ;
}
return result ;
}
@ -253,11 +310,16 @@ namespace Admin.Core.Service
log . Error ( "写入点位失败!" ) ;
return result ;
}
# endregion
}
else
{
return false ;
}
return false ;
}
catch ( Exception ex )
{
log . Info ( "异常:" + ex . Message ) ;
log . Error ( ex . Message ) ;
return false ;
}
@ -271,7 +333,8 @@ namespace Admin.Core.Service
/// <returns></returns>
public async Task < List < string > > GetWarehousePlan ( string code )
{
Hw_Warehouse wh = await _wareHouse . FirstAsync ( d = > d . BarCode = = code ) ;
Hw_Warehouse wh = await _wareHouse . FirstAsync ( d = > d . BarCode = = code . Trim ( ) ) ;
if ( wh = = null ) return null ;
var list = await _wareHouse_Sub . QueryAsync ( d = > d . MainId = = wh . ID ) ;
var planList = ( from d in list