using Admin.Core.IRepository ;
using Admin.Core.IService ;
using Admin.Core.IService.IService_New ;
using Admin.Core.Model ;
using Admin.Core.Model.Model_New ;
using Admin.Core.Model.ViewModels ;
using log4net ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace Admin.Core.Service.Service_New
{
public class RecordBoxFoamFixtureComplateServices : BaseServices < RecordBoxFoamFixtureComplate > , IRecordBoxFoamFixtureComplateServices
{
private static readonly log4net . ILog logHelper = LogManager . GetLogger ( typeof ( RecordBoxFoamFixtureComplate ) ) ;
private readonly IBaseRepository < RecordBoxFoamFixtureComplate > _dal ;
private readonly IOldBoxFoamTypeServices _boxFoamTypeServices ;
private readonly IBoxFoamPlanServices _boxFoamPlanServices ;
private readonly IBoxFoamDataServices _oldBoxFoamDataServices ;
private readonly ISysUserInfoServices _sysUserInfoServices ;
private readonly IBaseSpaceInfoServices _baseSpaceInfoServices ;
private readonly IProductPlanInfoServices _productPlanInfoServices ;
private readonly IMaterialCompletionServices _materialCompletionServices ;
public RecordBoxFoamFixtureComplateServices ( IBaseRepository < RecordBoxFoamFixtureComplate > dal , IOldBoxFoamTypeServices boxFoamTypeServices , IBoxFoamPlanServices boxFoamPlanServices , IBoxFoamDataServices oldBoxFoamDataServices , ISysUserInfoServices sysUserInfoServices , IBaseSpaceInfoServices baseSpaceInfoServices , IProductPlanInfoServices productPlanInfoServices , IMaterialCompletionServices materialCompletionServices )
{
this . _dal = dal ;
base . BaseDal = dal ;
_boxFoamTypeServices = boxFoamTypeServices ;
_boxFoamPlanServices = boxFoamPlanServices ;
_oldBoxFoamDataServices = oldBoxFoamDataServices ;
_sysUserInfoServices = sysUserInfoServices ;
_baseSpaceInfoServices = baseSpaceInfoServices ;
_productPlanInfoServices = productPlanInfoServices ;
_materialCompletionServices = materialCompletionServices ;
}
/// <summary>
/// 获取班组产量
/// </summary>
/// <returns></returns>
public List < RecordBoxFoamFixtureComplate > getTeamData ( string productLine , string teamName )
{
List < RecordBoxFoamFixtureComplate > list ;
try
{
var _db = this . BaseDal . Db ;
list = _db . CopyNew ( ) . Ado . SqlQuery < RecordBoxFoamFixtureComplate > ( $"SELECT * FROM DATA_BOXFOAM_FIXTURECOMPLATE WHERE LOGIN_TEAM='{teamName}' and PRODUCTION_LINE='{productLine}'" ) ;
}
catch ( Exception ex )
{
list = null ;
}
return list ;
}
/// <summary>
/// 保存夹具生产数量
/// </summary>
/// <param name="fixture"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveFixtureComplate ( List < RecordBoxFoamFixtureComplate > fixture , string productLineCode , string stationCode )
{
bool result = false ;
try
{
foreach ( var item in fixture )
{
//获取当前工位,前一条数据
var lastFixtureList = _dal . Query ( x = > x . ProductionLine = = productLineCode & & x . StationCode = = stationCode & & x . FixtureCode = = item . FixtureCode ) ;
if ( lastFixtureList ! = null & & lastFixtureList . Count > 0 )
{
RecordBoxFoamFixtureComplate lastFixtureComplate = lastFixtureList . OrderByDescending ( x = > x . RecordTime ) . First ( ) ;
if ( lastFixtureComplate ! = null )
{
if ( item . FixtureOutPut ! = lastFixtureComplate . FixtureOutPut )
{
item . RealOutPut = item . FixtureOutPut - lastFixtureComplate . FixtureOutPut ;
//更新生产计划
UpdatePlanInfo ( item , productLineCode , stationCode ) ;
_dal . Add ( item ) ;
//更新小时数据
UpdateHourInfo ( item , productLineCode , stationCode ) ;
break ;
}
}
}
else
{
_dal . Add ( item ) ;
//更新小时数据
UpdateHourInfo ( item , productLineCode , stationCode ) ;
}
}
}
catch ( Exception ex )
{
logHelper . Error ( "保存夹具生产完成记录异常" , ex ) ;
}
return result ;
}
/// <summary>
/// 更新生产计划
/// </summary>
/// <param name="item"></param>
private void UpdatePlanInfo ( RecordBoxFoamFixtureComplate item , string productLineCode , string stationCode )
{
try
{
//var boxFoamTypeList = _boxFoamTypeServices.Query(x => x.Boxtype == item.FixtureBoxType); //MaterialCode物料型号
var boxFoamTypeList = GetMaterialTypeBySpaceInfo ( item . StationCode , item . FixtureBoxType ) ;
if ( boxFoamTypeList ! = null )
{
if ( boxFoamTypeList . Count > 0 )
{
foreach ( var boxFoamType in boxFoamTypeList )
{
var boxPlanList = _boxFoamPlanServices . Query ( x = > x . ProductLineCode = = productLineCode & & x . StationCode = = stationCode & & x . MaterialCode = = boxFoamType & & x . PlanAmount > x . CompleteAmount & & x . ShiftType = = 1 ) ;
if ( boxPlanList ! = null )
{
if ( boxPlanList . Count > 0 )
{
var boxPlan = boxPlanList . OrderBy ( x = > x . CreateTime ) . First ( ) ;
if ( boxPlan ! = null )
{
boxPlan . CompleteAmount = boxPlan . CompleteAmount + item . RealOutPut ;
bool planResult = _boxFoamPlanServices . UpdateAsync ( boxPlan ) . Result ;
if ( planResult )
{
Console . WriteLine ( $"生产计划更新成功:{boxPlan.CompleteAmount}" ) ;
//更新MES计划
UpdateMesPlanInfo ( boxPlan . PlanCode , item . RealOutPut , stationCode , productLineCode ) ;
}
else
{
Console . WriteLine ( "生产计划更新失败" ) ;
}
break ;
}
}
}
}
}
}
} catch ( Exception ex )
{
logHelper . Error ( "更新计划信息异常" , ex ) ;
}
}
/// <summary>
/// 更新小时产量
/// </summary>
/// <param name="item"></param>
private void UpdateHourInfo ( RecordBoxFoamFixtureComplate item , string producLineCode , string stationCode )
{
BoxFoamData hourData ;
var hourDataList = _oldBoxFoamDataServices . Query ( x = > x . ProductLineCode = = producLineCode & & x . StationCode = = stationCode & & x . MainId = = item . FixtureCode & & x . Fixtureboxtype = = item . FixtureBoxType ) ;
if ( hourDataList ! = null )
{
if ( hourDataList . Count > 0 )
{
hourData = hourDataList . First ( ) ;
if ( hourData ! = null )
{
hourData = SetHourValue ( hourData , item ) ;
hourData . UpdatedTime = DateTime . Now ;
hourData . Fixturestatus = item . FixtureStatus ;
_oldBoxFoamDataServices . UpdateAsync ( hourData ) ;
return ;
}
}
}
//添加小时计划
hourData = new BoxFoamData ( )
{
ProductLineCode = item . ProductionLine ,
StationCode = item . StationCode ,
Fixtureboxtype = item . FixtureBoxType ,
MainId = item . FixtureCode ,
UpdatedTime = DateTime . Now ,
} ;
hourData = SetHourValue ( hourData , item ) ;
_oldBoxFoamDataServices . AddAsync ( hourData ) ;
}
private BoxFoamData SetHourValue ( BoxFoamData hourData , RecordBoxFoamFixtureComplate item )
{
int hour = GetTeamHour ( DateTime . Now ) . Seq ;
switch ( hour )
{
case 1 :
hourData . AnHour = hourData . AnHour + item . RealOutPut ;
break ;
case 2 :
hourData . TwoHour = hourData . TwoHour + item . RealOutPut ; ;
break ;
case 3 :
hourData . ThreeHour = hourData . ThreeHour + item . RealOutPut ; ;
break ;
case 4 :
hourData . FourHour = hourData . FourHour + item . RealOutPut ; ;
break ;
case 5 :
hourData . FiveHour = hourData . FiveHour + item . RealOutPut ; ;
break ;
case 6 :
hourData . SixHour = hourData . SixHour + item . RealOutPut ; ;
break ;
case 7 :
hourData . SevenHour = hourData . SevenHour + item . RealOutPut ; ;
break ;
case 8 :
hourData . EightHour = hourData . EightHour + item . RealOutPut ; ;
break ;
case 9 :
hourData . NineHour = hourData . NineHour + item . RealOutPut ; ;
break ;
case 10 :
hourData . TenHour = hourData . TenHour + item . RealOutPut ; ;
break ;
case 11 :
hourData . ElevenHour = hourData . ElevenHour + item . RealOutPut ; ;
break ;
case 12 :
hourData . TwelveHour = hourData . TwelveHour + item . RealOutPut ; ;
break ;
default :
break ;
}
return hourData ;
}
private CurrentTeamTimeView GetTeamHour ( DateTime now )
{
var obj = _sysUserInfoServices . GetTeamData ( now ) . Result ;
if ( obj ! = null )
{
return obj ;
}
return null ;
}
/// <summary>
/// 通过夹具箱型获取物料型号
/// 老线通过boxFoamTypeServices获取
/// 新线通过baseSpaceInfoServices获取, 夹具箱型添加在物料类型后通过_分割
/// </summary>
/// <param name="stationCode">老线: 1005; 新线: 1105</param>
/// <param name="boxType"></param>
/// <returns></returns>
private List < string > GetMaterialTypeBySpaceInfo ( string stationCode , string boxType )
{
List < string > result = null ;
if ( stationCode = = "1105" )
{
if ( ! string . IsNullOrEmpty ( boxType ) )
{
var boxFoamTypeList = _boxFoamTypeServices . Query ( x = > x . Boxtype = = boxType ) ; //MaterialCode物料型号
if ( boxFoamTypeList ! = null )
{
result = new List < string > ( ) ;
foreach ( var item in boxFoamTypeList )
{
result . Add ( item . MaterialCode ) ;
}
}
}
}
else
{
if ( ! string . IsNullOrEmpty ( boxType ) )
{
List < BaseSpaceInfo > spaceInfos = _baseSpaceInfoServices . GetSpaceInfos ( "PQK-001" ) ;
if ( spaceInfos ! = null )
{
var info = spaceInfos . Where ( x = > x . BoxType = = boxType ) ;
if ( info ! = null )
{
spaceInfos = info . ToList ( ) ;
if ( spaceInfos ! = null )
{
result = new List < string > ( ) ;
foreach ( var item in spaceInfos )
{
result . Add ( item . MaterialType ) ;
}
}
}
}
}
}
return result ;
}
/// <summary>
/// 更新MES计划信息
/// </summary>
private void UpdateMesPlanInfo ( string planCode , int complateAmount , string stationCode , string productLine )
{
if ( string . IsNullOrEmpty ( planCode ) )
{
return ;
}
var planInfo = _productPlanInfoServices . GetProductPlanByPlanCode ( planCode ) . Result ;
if ( planInfo ! = null )
{
if ( planInfo . PlanAmount > planInfo . CompleteAmount )
{
planInfo . CompleteAmount = planInfo . CompleteAmount + complateAmount ;
if ( planInfo . PlanAmount > = planInfo . CompleteAmount )
{
planInfo . EndTime = DateTime . Now ;
}
_productPlanInfoServices . UpdateProductPlanInfo ( planInfo ) ;
}
//保存过点数据
SaveMaterialComplate ( planInfo , complateAmount , stationCode , productLine ) ;
}
}
/// <summary>
/// 保存过点数据
/// </summary>
/// <param name="planInfo"></param>
/// <param name="complateAmount"></param>
/// <param name="stationCode"></param>
private void SaveMaterialComplate ( ProductPlanInfo planInfo , int complateAmount , string stationCode , string productLine )
{
try
{
for ( int i = 0 ; i < complateAmount ; i + + )
{
MaterialCompletion materialCompletion = new MaterialCompletion ( ) ;
materialCompletion . ProductLineCode = productLine ;
materialCompletion . planCode = planInfo . PlanCode ;
materialCompletion . OrderCode = planInfo . OrderCode ;
materialCompletion . MaterialCode = planInfo . MaterialCode ;
materialCompletion . MaterialName = planInfo . MaterialName ;
materialCompletion . StationName = stationCode ;
materialCompletion . CompleteDate = DateTime . Now ;
_materialCompletionServices . AddAsync ( materialCompletion ) ;
}
} catch ( Exception ex )
{
logHelper . Error ( $"保存过点数据异常:{ex.Message}" ) ;
}
}
}
}