using Admin.Core.IRepository; using Admin.Core.IService; using Admin.Core.Model; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System; using Aucma.Core.PLc; using Admin.Core.Common; using log4net; using SqlSugar; using System.Linq.Expressions; using StackExchange.Profiling.Internal; using Admin.Core.Model.ViewModels; namespace Admin.Core.Service { public class ExecutePlanInfoServices : BaseServices, IExecutePlanInfoServices { private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(ExecutePlanInfoServices)); private readonly IBaseRepository _dal; private readonly IExecutePlanInfoRepository _executePlanInfoRepository; private readonly IProductOrderInfoServices _productOrderInfoServices; private readonly IProductPlanInfoServices _productPlanInfoServices; public ExecutePlanInfoServices(IBaseRepository dal, IProductOrderInfoServices productOrderInfoServices, IProductPlanInfoServices productPlanInfoServices, IExecutePlanInfoRepository executePlanInfoRepository) { this._dal = dal; base.BaseDal = dal; _productOrderInfoServices = productOrderInfoServices; _productPlanInfoServices = productPlanInfoServices; _executePlanInfoRepository = executePlanInfoRepository; } #region 计划删除 /// /// 计划删除 /// /// /// public async Task ExecPlanDelete(string id) { if (string.IsNullOrWhiteSpace(id)) { return false; } return await _dal.DeleteByIdAsync(id); } #endregion #region 执行计划下移 /// /// 执行计划下移 /// /// /// /// public async Task PlanMoveDown(string id,string station) { List execPlans = new List(); var list = await _dal.QueryAsync(d=>d.ProductLineCode.Equals(station)); List planInfos = (list.OrderBy(d => d.ExecuteOrder)).ToList(); if (planInfos.Count()==0) return false; ExecutePlanInfo planInfo = planInfos.FirstOrDefault(x => x.ObjId == int.Parse(id)); int executeOrder = planInfo.ExecuteOrder; int planIndex = planInfos.IndexOf(planInfo); if (planIndex == 0 && planInfos.Count == 0) return false; if ((planInfos.Count - 1) == planIndex) return false; var lastPlanInfo = planInfos[planIndex + 1]; planInfo.ExecuteOrder = lastPlanInfo.ExecuteOrder; lastPlanInfo.ExecuteOrder = executeOrder; execPlans.Add(planInfo); execPlans.Add(lastPlanInfo); var result = await _dal.UpdateAsync(execPlans); if (result) return true; else return false; } #endregion #region 计划上移 /// /// 计划上移 /// /// /// /// /// public async Task PlanMoveUp(string id, string station) { List execPlans = new List(); var list = await _dal.QueryAsync(d => d.ProductLineCode.Equals(station)); List planInfos = (list.OrderBy(d => d.ExecuteOrder)).ToList(); if (planInfos.Count()==0) return false; ExecutePlanInfo planInfo = planInfos.FirstOrDefault(x => x.ObjId == int.Parse(id)); int executeOrder = planInfo.ExecuteOrder; int planIndex = planInfos.IndexOf(planInfo); if (planIndex == 0) return false; var lastPlanInfo = planInfos[planIndex - 1]; planInfo.ExecuteOrder = lastPlanInfo.ExecuteOrder; lastPlanInfo.ExecuteOrder = executeOrder; execPlans.Add(planInfo); execPlans.Add(lastPlanInfo); var result = await _dal.UpdateAsync(execPlans); if (result) return true; else return false; } #endregion #region 计划下达 /// /// 计划下达 /// /// /// /// /// public async Task PlanNextPass(ExecutePlanInfo sm) { ExecutePlanInfo planInfo = null; try { List planInfos = new List(); if (sm != null) { //下传计划到PLC,同时更改计划状态 var rearPanel = PlcHelper.melsecList.FirstOrDefault(d=>d.EquipName.Equals("后板Plc")); if (rearPanel.plc.IsConnected) { rearPanel.plc.WriteString("M100", sm.ProductPlanCode); rearPanel.plc.WriteInt32("M100", sm.PlanAmount); } var box = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("U壳PLC")); if (box.plc.IsConnected) { box.plc.WriteString("M100", sm.ProductPlanCode); box.plc.WriteInt32("M100", sm.PlanAmount); } //将计划写入拆分计划表中 return planInfo; } } catch (Exception ex) { logHelper.Error(ex); } return planInfo; } #endregion /// /// 通过产线工位获取执行顺序(默认+1) /// /// /// public async Task GetExecuteOrderByProductLineCode(string productLineCode) { int result = 0; try { Expression> exp = s1 => true; exp = exp.And(x => x.ProductLineCode == productLineCode); List planInfos =await _dal.QueryAsync(exp); int order = planInfos.OrderByDescending(x => x.ExecuteOrder).First().ExecuteOrder; result = order + 1; } catch (Exception ex) { logHelper.Error("通过产线工位获取执行顺异常", ex); } return result; } /// /// 通过执行计划编号获取执行计划 /// /// /// public async Task GetExecutePlanInfoByPlanCodeAsync(string executePlanCode) { ExecutePlanInfo planInfo = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.ExecutePlanCode == executePlanCode); planInfo = await _dal.FirstAsync(exp); logHelper.Info($"根据执行计划编号{executePlanCode};获取到的执行计划信息:{planInfo.ToJson()}"); } catch (Exception ex) { logHelper.Error("通过执行计划编号获取执行计划异常", ex); } return planInfo; } /// /// 通过产线工位获取执行计划 /// /// /// public async Task> GetExecutePlanInfosByProductLineCode(string productLineCode) { List planInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.ProductLineCode == productLineCode); planInfos =await _dal.QueryAsync(exp); logHelper.Info($"根据产线工位编号:{productLineCode};获取到的执行计划信息:{planInfos.ToJson}"); } catch (Exception ex) { logHelper.Error("通过产线工位获取执行计划异常", ex); } return planInfos; } /// /// 通过生产计划编号获取执行计划 /// /// /// public async Task> GetExecutePlanInfosByProductPlanCode(string productPlanCode) { List planInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.ProductPlanCode == productPlanCode); planInfos =await _dal.QueryAsync(exp); logHelper.Info($"根据生产计划编号:{productPlanCode};获取到的执行计划信息:{planInfos.ToJson()}"); } catch (Exception ex) { logHelper.Error("通过生产计划编号获取执行计划异常", ex); } return planInfos; } /// /// 新增执行计划 /// /// /// public async Task InsertExecutePlanInfo(ExecutePlanInfo executePlanInfo) { bool result = false; try { int r =await _dal.AddAsync(executePlanInfo); if (r > 0) result = true; } catch (Exception ex) { logHelper.Error("新增执行计划异常", ex); } return result; } /// /// 修改执行计划 /// /// /// public async Task UpdateExecutePlanInfo(ExecutePlanInfo executePlanInfo) { bool result = false; try { result =await _dal.UpdateAsync(executePlanInfo); } catch (Exception ex) { logHelper.Error("修改执行计划异常", ex); } return result; } /// /// 批量修改执行计划 /// /// /// public async Task UpdateRangeExecutePlanInfo(List executePlanInfos) { bool result = false; try { result =await _dal.UpdateAsync(executePlanInfos); } catch (Exception ex) { logHelper.Error("修改执行计划异常", ex); } return result; } /// /// 根据执行计划编号删除执行计划 /// /// /// public async Task DeleteExecutePlanInfoByPlanCode(string executePlanCode) { bool result = false; try { ExecutePlanInfo planInfo =await this.GetExecutePlanInfoByPlanCodeAsync(executePlanCode); result =await _dal.DeleteAsync(planInfo); } catch (Exception ex) { logHelper.Error("根据执行计划编号删除执行计划异常", ex); } return result; } public async Task> GetStationHourAmount(string stationCode) { List result = null; try { result =await _executePlanInfoRepository.GetStationHourAmount(stationCode); } catch (Exception ex) { logHelper.Error("获取小时产量数据异常", ex); } return result; } public async Task> GetStationMaterialStats(string stationCode) { List result = null; try { result = await _executePlanInfoRepository.GetStationMaterialStats(stationCode); } catch (Exception ex) { logHelper.Error("获取型号统计数据异常", ex); } return result; } /// /// 通过执行计划编号获取执行计划 /// /// /// public async Task GetExecutePlanInfoByPlanCode(string executePlanCode) { ExecutePlanInfo planInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.ProductPlanCode == executePlanCode); planInfos = await _executePlanInfoRepository.FirstAsync(exp); logHelper.Info($"根据生产计划编号:{executePlanCode};获取到的执行计划信息:{planInfos.ToJson()}"); } catch (Exception ex) { logHelper.Error("通过生产计划编号获取执行计划异常", ex); } return planInfos; } /// /// 获取钣金生产小时数 /// /// /// public async Task> GetStationSheetMetalHourAmountAsync(string stationCode) { return await _executePlanInfoRepository.GetStationSheetMetalHourAmountAsync(stationCode); } public List GetStationSheetMetalStats(string stationCode) { return _executePlanInfoRepository.GetStationSheetMetalStats(stationCode); } public List QuerySheetMetalData(string stationCode) { return _executePlanInfoRepository.QuerySheetMetalData(stationCode); } public List QuerySheetMetalPlanData(string stationCode) { return _executePlanInfoRepository.QuerySheetMetalPlanData(stationCode); } public List QuerySheetMetalSendPlanData(string stationCode) { return _executePlanInfoRepository.QuerySheetMetalSendPlanData(stationCode); } /// /// 修改执行计划 /// /// /// public async Task UpdateSheetMetalExecutePlanInfo(int execPanId, int status) { bool result = false; try { var executePlanInfo =await _dal.FirstAsync(d=>d.ObjId== execPanId); executePlanInfo.ExecuteStatus = status; result = await _dal.UpdateAsync(executePlanInfo); } catch (Exception ex) { logHelper.Error("修改执行计划异常", ex); } return result; } } }