using DeviceRepair.Models; using DeviceRepair.Models.Device; using DeviceRepair.Models.Plan; using DeviceRepair.Models.Plan.View; using DeviceRepair.Models.SFC; using DeviceRepair.Utils; using NLog; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Runtime.CompilerServices; using TsSFCDevice.Client.Biz.Base.Service; using TsSFCDevice.Client.Biz.Base.Utils; namespace TsSFCDevice.Client.Biz.Impl { public class PlanRepository { private readonly Logger log; private static PlanRepository manager; private IDictionary m_ApiParameters; /// /// API输入参数 /// public IDictionary ApiParameters { get { return m_ApiParameters; } set { m_ApiParameters = value; } } public static PlanRepository Instance { get { if (manager == null) manager = new PlanRepository(); return manager; } } public PlanRepository() { log = LogManager.GetCurrentClassLogger(); m_ApiParameters = new Dictionary(); } #region PM /// /// 获取PM保养计划数据 /// /// /// /// public IList Get_PM_PLAN_Datas(string EquipmentID = "", int Year = 0) { IList Datas = new List(); if (Year == 0) Year = DateTime.Today.Year; try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("EquipmentID", EquipmentID); ApiParameters.Add("Year", Year + ""); var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.PM_PLAN, GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList plans = DTOHelper.DataTableToList(dsResults.Tables[0]); if (Utility.SystemRuntimeInfo.CurrentDeviceCaches != null && Utility.SystemRuntimeInfo.CurrentDeviceCaches.Count > 0 && plans != null && plans.Count > 0) { // 此处去除掉没有权限操作的设备 int[] DevIds = Utility.SystemRuntimeInfo.CurrentDeviceCaches.Select(x => x.AutoID).ToArray(); Datas = plans.Where(x => DevIds.Contains(x.EquipmentID)).ToList(); } if (Utility.SystemRuntimeInfo.CurrentUsersCaches != null && Utility.SystemRuntimeInfo.CurrentUsersCaches.Count > 0) { // 赋值各种操作员信息 Datas.AsParallel().ForEach(x => { Dictionary userDict = Utility.SystemRuntimeInfo.CurrentUsersCaches.ToDictionary(s => s.Id, s => s); // 计划创建人 if (userDict.ContainsKey(x.CreatUser)) { x.CreatUserName = userDict[x.CreatUser].UserName; } // 计划修改人 if (x.ChangeUser.HasValue && userDict.ContainsKey(x.ChangeUser.Value)) { x.ChangeUserName = userDict[x.ChangeUser.Value].UserName; } }); } return Datas; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取PM保养计划数据 /// /// /// /// public IList Get_AM_PLAN_Datas(string EquipmentID = "", int Year = 0) { IList Datas = new List(); if (Year == 0) Year = DateTime.Today.Year; try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("EquipmentID", EquipmentID); ApiParameters.Add("Year", Year + ""); var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.AM_PLAN, GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList plans = DTOHelper.DataTableToList(dsResults.Tables[0]); if (Utility.SystemRuntimeInfo.CurrentDeviceCaches != null && Utility.SystemRuntimeInfo.CurrentDeviceCaches.Count > 0 && plans != null && plans.Count > 0) { // 此处去除掉没有权限操作的设备 int[] DevIds = Utility.SystemRuntimeInfo.CurrentDeviceCaches.Select(x => x.AutoID).ToArray(); Datas = plans.Where(x => DevIds.Contains(x.EquipmentID)).ToList(); } if (Utility.SystemRuntimeInfo.CurrentUsersCaches != null && Utility.SystemRuntimeInfo.CurrentUsersCaches.Count > 0) { // 赋值各种操作员信息 Datas.AsParallel().ForEach(x => { Dictionary userDict = Utility.SystemRuntimeInfo.CurrentUsersCaches.ToDictionary(s => s.Id, s => s); // 计划创建人 if (userDict.ContainsKey(x.CreatUser)) { x.CreatUserName = userDict[x.CreatUser].UserName; } // 计划修改人 if (x.ChangeUser.HasValue && userDict.ContainsKey(x.ChangeUser.Value)) { x.ChangeUserName = userDict[x.ChangeUser.Value].UserName; } }); } return Datas; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取当前年月的待保修项 /// /// public IList Get_PM_PLAN_CurrentMonth() { byte[] btResults = null; try { var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.PM_PLAN_CurrentMonth, GetParameters(), out btResults); DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList Datas = DTOHelper.DataTableToList(dsResults.Tables[0]); return Datas; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 取导出数据 /// /// /// public IList Get_PM_PLAN_Xlsx(int Year = 0) { IList Datas = new List(); if (Year == 0) Year = DateTime.Today.Year; try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("Year", Year + ""); var Rtn = Utility.SfcBizService.CurrentSvc.Get_PM_PLAN_Xlsx(GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList xlsxDatas = DTOHelper.DataTableToList(dsResults.Tables[0]); if (xlsxDatas == null) { throw new Exception("实例化当前导出数据时发生异常错误!"); } if (Utility.SystemRuntimeInfo.CurrentDeviceCaches != null && Utility.SystemRuntimeInfo.CurrentDeviceCaches.Count > 0 && xlsxDatas != null && xlsxDatas.Count > 0) { // 此处去除掉没有权限操作的设备 string[] EquipmentIds = Utility.SystemRuntimeInfo.CurrentDeviceCaches .Select(x => x.EquipmentID).ToArray(); Datas = xlsxDatas.Where(x => EquipmentIds.Contains(x.EquipmentID)).ToList(); } if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } return Datas; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取计划任务进度 /// /// /// /// public IList Get_PM_PLAN_ProgressInfo(int EquipmentID, int Year) { try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("EquipmentID", EquipmentID + ""); ApiParameters.Add("Year", Year + ""); var Rtn = Utility.SfcBizService.CurrentSvc.Get_PM_PLAN_ProgressInfo(GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList Datas = DTOHelper.DataTableToList(dsResults.Tables[0]); if (Datas == null) { throw new Exception("实例化当前计划数据时发生异常错误!"); } return Datas; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } #endregion #region AM /// /// 取导出数据 /// /// /// public IList Get_AM_PLAN_Xlsx(int Year = 0) { IList Datas = new List(); if (Year == 0) Year = DateTime.Today.Year; try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("Year", Year + ""); var Rtn = Utility.SfcBizService.CurrentSvc.Get_AM_PLAN_Xlsx(GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList xlsxDatas = DTOHelper.DataTableToList(dsResults.Tables[0]); if (xlsxDatas == null) { throw new Exception("实例化当前导出数据时发生异常错误!"); } if (Utility.SystemRuntimeInfo.CurrentDeviceCaches != null && Utility.SystemRuntimeInfo.CurrentDeviceCaches.Count > 0 && xlsxDatas != null && xlsxDatas.Count > 0) { // 此处去除掉没有权限操作的设备 string[] EquipmentIds = Utility.SystemRuntimeInfo.CurrentDeviceCaches .Select(x => x.EquipmentID).ToArray(); Datas = xlsxDatas.Where(x => EquipmentIds.Contains(x.EquipmentID)).ToList(); } if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } return Datas; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取每日保养的计划完成情况 /// /// /// public IList AM_PLAN_Scheduler(int PlanAutoID) { try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("PlanAutoID", PlanAutoID + ""); var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.AM_PLAN_Scheduler, GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList Datas = DTOHelper.DataTableToList(dsResults.Tables[0]); return Datas; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } #endregion /// /// 新增或修改PM计划 /// /// /// public APIResponseData Insert_OR_Edit_PM_PLAN(List Datas, string Belong) { try { ApiParameters?.Clear(); ApiParameters.Add("OPERATORNAME", Utility.SystemRuntimeInfo.CurrentUser.UserName); ApiParameters.Add("Belong", Belong); var Rtn = Utility.SfcBizService.CurrentSvc.Insert_OR_Edit_PM_PLAN(GetParameters(), Datas.ToDataTable()); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } return new APIResponseData { Code = 1, Message = Rtn.Message }; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 删除PM计划 /// /// /// /// public APIResponseData Del_PM_PLAN(int EquipmentID, int Year, string Belong) { try { ApiParameters?.Clear(); ApiParameters.Add("EquipmentID", EquipmentID + ""); ApiParameters.Add("Year", Year + ""); ApiParameters.Add("Belong", Belong); var Rtn = Utility.SfcBizService.CurrentSvc.Del_PM_PLAN(GetParameters()); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } return new APIResponseData { Code = 1, Message = Rtn.Message }; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取当前计划是否存的保养记录 /// /// /// /// /// public IList Get_PlanRecordProgress(int EquipmentID, int Year, string Belong) { byte[] btResults = null; try { ApiParameters?.Clear(); ApiParameters.Add("EquipmentID", EquipmentID + ""); ApiParameters.Add("Year", Year + ""); ApiParameters.Add("Belong", Belong); var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.PLAN_Pregress, GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); if (dsResults == null || dsResults.Tables.Count == 0) { return null; } IList plans = DTOHelper.DataTableToList(dsResults.Tables[0]); return plans; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取单条PM计划任务进度 /// /// /// /// public AnnualMaintenancePlan PM_PLAN_Single(int EquipmentID, int Year, string Belong) { try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("EquipmentID", EquipmentID + ""); ApiParameters.Add("Year", Year + ""); ApiParameters.Add("Belong", Belong); var Rtn = Utility.SfcBizService.CurrentSvc.PM_PLAN_Single(GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList Datas = DTOHelper.DataTableToList(dsResults.Tables[0]); if (Datas == null) { throw new Exception("实例化当前计划数据时发生异常错误!"); } return Datas.FirstOrDefault(); } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取设备在年度的全部保养信息 /// /// /// /// /// public DeviceAnnPlanView Get_EquiAnnualPlans(int EquipmentID, int Year, string Belong) { try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("EquipmentID", EquipmentID + ""); ApiParameters.Add("Year", Year + ""); ApiParameters.Add("Belong", Belong); var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.Get_EquiAnnualPlans, GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); DeviceAnnPlanView dev = new DeviceAnnPlanView() { Dev = DTOHelper.DataTableToList(dsResults.Tables["DeviceInformationInfo"])?.FirstOrDefault(), Plans = DTOHelper.DataTableToList(dsResults.Tables["DriveMaintencePlanInfo"])?.ToList(), Records = DTOHelper.DataTableToList(dsResults.Tables["MaintenanceRecordInfo"])?.ToList(), }; if (dsResults.Tables["Table"] != null && dsResults.Tables["Table"].Rows.Count > 0) { dev.AM_FormCode = dsResults.Tables["Table"].Rows[0]["AM_FormCode"] + ""; dev.PM_FormCode = dsResults.Tables["Table"].Rows[0]["PM_FormCode"] + ""; } return dev; } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } /// /// 获取单计划数据 /// /// /// public DriveMaintencePlanInfo Get_PLAN_Single(int AutoID) { try { byte[] btResults = null; ApiParameters?.Clear(); ApiParameters.Add("AutoID", AutoID + ""); var Rtn = Utility.SfcBizService.CurrentSvc.Get_PLAN_Single(GetParameters(), out btResults); if (Rtn.Code != 1) { throw new Exception(Rtn.Message); } DataSet dsResults = CompressionHelper.ExactDataSet(btResults); IList Datas = DTOHelper.DataTableToList(dsResults.Tables[0]); if (Datas == null) { throw new Exception("实例化当前计划数据时发生异常错误!"); } return Datas.FirstOrDefault(); } catch (Exception ex) { log.Error(ex.Message, ex); throw ex; } } #region 公共 internal string GetParameters(string cOperator = "", bool bFlag = true) { return ParametersObject.GetInstance(cOperator).GetJsonSerialized(m_ApiParameters); } #endregion } }