using DeviceRepair.DataAccess.Data; using DeviceRepair.Models; using DeviceRepair.Models.Enum; using DeviceRepair.Models.Plan.View; using DeviceRepair.Utils; using NLog; using SqlSugar; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; namespace DeviceRepair.DataAccess.PLAN { public class PlanDa : BaseDa { private static readonly Logger log = LogManager.GetCurrentClassLogger(); public PlanDa() : base() { } public PlanDa(IDictionary apiParams) : base(apiParams) { } /// /// 获取PM的保养计划 /// /// public DataSet Get_PM_PLAN_Datas() { DataSet dsDatas = new DataSet("Datas"); try { string EquipmentID = ApiParameters["EquipmentID"]; int Year = DateTime.Today.Year; int.TryParse(ApiParameters["Year"], out Year); int CurrentMonth = DateTime.Now.Month; var exp = Expressionable.Create() .And(x => x.MaintenanceYear == Year). AndIF(!EquipmentID.IsNull(), x => x.DisplayEquipmentID == SqlFunc.Trim(EquipmentID)).ToExpression();//拼接表达式 List Datas = devMain.Queryable().With(SqlWith.NoLock).Where(exp).ToList(); int[] planIds = Datas.Select(x => x.AutoID).ToArray(); Dictionary recordDict = devMain.Queryable().With(SqlWith.NoLock) .Where(x => SqlFunc.ContainsArray(planIds, x.PlanPrimaryID)).ToList().ToDictionary(x => x.PlanPrimaryID, x => x); List lst = new List(); Type type = typeof(AnnualMaintenancePlan); foreach (View_PM_PLAN data in Datas) { AnnualMaintenancePlan item = null; if (!lst.Any(x => x.EquipmentID == data.EquipmentID && x.MaintenanceYear == data.MaintenanceYear)) { DateTime PMMonth; item = new AnnualMaintenancePlan { EquipmentID = data.EquipmentID, DisplayEquipmentID = data.DisplayEquipmentID, EquipmentName = data.EquipmentName, VersionCode = data.VersionCode, MaintenanceYear = data.MaintenanceYear, CreatUser = data.CreatUser, CreatDate = data.CreatDate, ChangeUser = data.ChangeUser, ChangeDate = data.ChangeDate, ChangeUserName = "", Remarks = data.Remarks }; if (DateTime.TryParse(data.PMStartMonth, out PMMonth)) { item.PMStartMonth = PMMonth.ToString("yyyy-MM"); } lst.Add(item); } item = lst.FirstOrDefault(x => x.EquipmentID == data.EquipmentID && x.MaintenanceYear == data.MaintenanceYear); if (item == null) continue; EnumMonth month = (EnumMonth)(data.MaintenanceMonth); PropertyInfo prop = type.GetProperty(month + ""); prop.SetValue(item, data.MaintenanceType); EnumPlanCompleteStatus status = EnumPlanCompleteStatus.None; if (string.IsNullOrWhiteSpace(data.MaintenanceType)) status = EnumPlanCompleteStatus.None; else if (recordDict.ContainsKey(data.AutoID)) status = EnumPlanCompleteStatus.Complete; else { if (data.MaintenanceMonth == CurrentMonth) { status = EnumPlanCompleteStatus.Current; } else if (data.MaintenanceMonth > CurrentMonth) { status = EnumPlanCompleteStatus.Future; } else if (data.MaintenanceMonth < CurrentMonth) { status = EnumPlanCompleteStatus.TimeOut; } } prop = type.GetProperty(month + "Status"); prop.SetValue(item, status); } DataTable table = lst.OrderByDescending(x => x.ChangeDate).OrderByDescending(x => x.ChangeDate).ToList().ToDataTable(); dsDatas.Tables.Add(table); return dsDatas; } catch (SqlException sqlEx) { throw sqlEx; } catch (Exception ex) { throw ex; } } /// /// 获取当月保养 /// /// public DataSet Get_PM_PLAN_CurrentMonth() { DataSet dsDatas = new DataSet("Datas"); try { List Datas = devMain.Queryable().ToList(); DataTable table = Datas.ToDataTable(); dsDatas.Tables.Add(table); return dsDatas; } catch (SqlException sqlEx) { throw sqlEx; } catch (Exception ex) { throw ex; } } /// /// 获取单条PM计划任务进度 /// /// /// public APIResponseData PM_PLAN_Single(out DataSet dsDatas) { dsDatas = new DataSet("Datas"); try { if (!ApiParameters.ContainsKey("EquipmentID")) { throw new ArgumentException("传入的设备编号不能为空!"); } if (!ApiParameters.ContainsKey("Year")) { throw new ArgumentException("传入的计划年不能为空!"); } int EquipmentAutoID = 0; if (!int.TryParse(ApiParameters["EquipmentID"], out EquipmentAutoID)) { throw new ArgumentException("传入的设备编号格式不正确!"); } int Year = DateTime.Today.Year; if (!int.TryParse(ApiParameters["Year"], out Year)) { throw new ArgumentException("传入的计划年格式不正确!"); } List Datas = devMain.Queryable().Where(x => x.MaintenanceYear == Year && x.EquipmentID == EquipmentAutoID) .Select(x => new AnnualMaintenancePlan { EquipmentID = x.EquipmentID, DisplayEquipmentID = x.DisplayEquipmentID, EquipmentName = x.EquipmentName, VersionCode = x.VersionCode, MaintenanceYear = x.MaintenanceYear, Jan = x.Jan, Feb = x.Feb, Mar = x.Mar, Apr = x.Apr, May = x.May, Jun = x.Jun, Jul = x.Jul, Aug = x.Aug, Sep = x.Sep, Oct = x.Oct, Nov = x.Nov, Dec = x.Dec, PMStartMonth = x.PMStartMonth, CreatUser = x.CreatUser, CreatUserName = x.CreatUserName, CreatDate = x.CreatDate, ChangeUser = x.ChangeUser, ChangeUserName = x.ChangeUserName, ChangeDate = x.ChangeDate, Remarks = x.Remarks }).ToList(); if (Datas == null) { throw new ArgumentException("传入的数据不正确,获取计划信息失败!"); } DataTable table = Datas.ToDataTable(); dsDatas.Tables.Add(table); return new APIResponseData { Code = 1, Message = "操作成功!" }; } catch (SqlException sqlEx) { throw sqlEx; } catch (Exception ex) { throw ex; } } /// /// 获取PM的保养计划导出数据 /// /// /// public APIResponseData Get_PM_PLAN_Xlsx(out DataSet dsDatas) { dsDatas = new DataSet("Datas"); try { int Year = DateTime.Today.Year; int.TryParse(ApiParameters["Year"], out Year); List lst = devMain.Queryable() .With(SqlWith.NoLock).Where(x => x.MaintenanceYear == Year)?.ToList(); DataTable table = lst.ToDataTable(); dsDatas.Tables.Add(table); return new APIResponseData { Code = 1, Message = "操作成功!" }; } catch (SqlException sqlEx) { throw sqlEx; } catch (Exception ex) { throw ex; } } /// /// 删除PM保养计划 /// /// /// /// public APIResponseData Delete_PM_PLAN() { try { if (!ApiParameters.ContainsKey("EquipmentID")) { throw new ArgumentException("传入的设备编号不能为空!"); } if (!ApiParameters.ContainsKey("Year")) { throw new ArgumentException("传入的计划年不能为空!"); } string EquipmentID = ApiParameters["EquipmentID"]; int EquipmentAutoID = 0; if (!int.TryParse(ApiParameters["EquipmentID"], out EquipmentAutoID)) { throw new ArgumentException("传入的设备编号格式不正确!"); } int Year = DateTime.Today.Year; if (!int.TryParse(ApiParameters["Year"], out Year)) { throw new ArgumentException("传入的计划年格式不正确!"); } List Datas = devMain.Queryable().Where(x => x.MaintenanceYear == Year && x.EquipmentID == EquipmentAutoID).ToList(); if ((Datas?.Count ?? 0) == 0) { throw new ArgumentException("传入的数据不正确,获取计划信息失败!"); } Datas.AsParallel().ForEach(x => { x.PlanStatus = "G"; }); devMain.BeginTran(); if (devMain.Updateable(Datas).ExecuteCommand() == Datas.Count) { devMain.CommitTran(); return new APIResponseData { Code = 1, Message = "操作成功!" }; } else { devMain.RollbackTran(); return new APIResponseData { Code = -1, Message = "操作失败!" }; } } catch (SqlException sqlEx) { devMain.RollbackTran(); throw sqlEx; } catch (Exception ex) { devMain.RollbackTran(); throw ex; } } /// /// 获取计划任务进度 /// /// /// public APIResponseData Get_PM_PLAN_ProgressInfo(out DataSet dsDatas) { dsDatas = new DataSet("Datas"); try { if (!ApiParameters.ContainsKey("EquipmentID")) { throw new ArgumentException("传入的设备编号不能为空!"); } if (!ApiParameters.ContainsKey("Year")) { throw new ArgumentException("传入的计划年不能为空!"); } string EquipmentID = ApiParameters["EquipmentID"]; int EquipmentAutoID = 0; if (!int.TryParse(ApiParameters["EquipmentID"], out EquipmentAutoID)) { throw new ArgumentException("传入的设备编号格式不正确!"); } int Year = DateTime.Today.Year; if (!int.TryParse(ApiParameters["Year"], out Year)) { throw new ArgumentException("传入的计划年格式不正确!"); } List Datas = devMain.Queryable().Where(x => x.PlanYear == Year && x.EquipmentID == EquipmentAutoID).ToList(); if ((Datas?.Count ?? 0) == 0) { throw new ArgumentException("传入的数据不正确,获取计划信息失败!"); } DataTable table = Datas.ToDataTable(); dsDatas.Tables.Add(table); return new APIResponseData { Code = 1, Message = "操作成功!" }; } catch (SqlException sqlEx) { throw sqlEx; } catch (Exception ex) { throw ex; } } /// /// 新增或编辑 /// /// /// public APIResponseData Insert_OR_Edit_PM_PLAN(List lst) { APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"新增失败或当前数据无更改,请重试!" }; try { if (!ApiParameters.ContainsKey("OPERATORAUTOID")) { throw new ArgumentException("非法的操作,当前操作人参数不正确!"); } int OperationID = Convert.ToInt32(ApiParameters["OPERATORAUTOID"] + ""); DateTime CurrentTime = DateTime.Now; if (lst == null || lst.Count == 0) throw new Exception("插入的数据集合,不能为空!"); // 操作对象 Dictionary> OperationList = new Dictionary>(); OperationList.Add(EnumOperationType.Add, new List()); OperationList.Add(EnumOperationType.Change, new List()); devMain.BeginTran(); int Year = lst[0].MaintenanceYear; int[] equipmentIds = lst.Select(x => x.EquipmentID).Distinct().ToArray(); // 设备编号校验 Dictionary devs = DeviceAccess.Instance.CurrentDb.AsQueryable().With(SqlWith.NoLock).Where(x => SqlFunc.ContainsArray(equipmentIds, x.AutoID)).ToList().ToDictionary(x => x.AutoID, x => x); System.Text.StringBuilder builder = new StringBuilder(); foreach (int id in equipmentIds) { if (!devs.ContainsKey(id)) { builder.AppendLine($"设备编号:{id} ,不存在于系统中!"); } } if (builder.Length > 0) throw new Exception(builder.ToString()); // 计划校验 List Plans = devMain.Queryable() .With(SqlWith.NoLock).Where(x => SqlFunc.ContainsArray(equipmentIds, x.EquipmentID) && x.MaintenanceYear == Year).ToList(); int[] planIds = Plans.Select(x => x.AutoID).Distinct().ToArray(); // 已保养数据不允许修改 List Records = devMain.Queryable().Where(x => SqlFunc.ContainsArray(planIds, x.PlanPrimaryID)).ToList(); foreach (MaintenanceRecordInfo item in Records) { DriveMaintencePlanInfo t = Plans.FirstOrDefault(x => x.AutoID == item.PlanPrimaryID); if (t != null) { DriveMaintencePlanInfo tt = lst.FirstOrDefault(x => x.MaintenanceMonth == t.MaintenanceMonth && x.EquipmentID == t.EquipmentID); if (tt != null) { // 计划已保养,但本次操作未修改此条保养的数据,正常执行 if (tt.MaintenanceType == t.MaintenanceType) { lst.RemoveAll(x => x.MaintenanceType == t.MaintenanceType && x.MaintenanceMonth == t.MaintenanceMonth && x.EquipmentID == t.EquipmentID); } else { // 计划已保养且本次欲修改此数据,退出保养 builder.AppendLine($"设备编号:{devs[t.EquipmentID].EquipmentID} 在{t.MaintenanceMonth}月的计划,存在已保养的数据,无法更改!"); } } } } if (builder.Length > 0) throw new Exception(builder.ToString()); // 修改的计划 List DelPlanIds = new List(); foreach (DriveMaintencePlanInfo item in Plans) { // 取数据库计划信息,对应当前传入的计划 DriveMaintencePlanInfo t = lst.FirstOrDefault(x => x.MaintenanceMonth == item.MaintenanceMonth && x.EquipmentID == item.EquipmentID); if (t != null && ((t.MaintenanceType != item.MaintenanceType) || t.PMStartMonth != item.PMStartMonth || t.Remarks != item.Remarks)) { // 修改的数据 item.ChangeDate = CurrentTime; item.ChangeUser = OperationID; item.MaintenanceType = t.MaintenanceType; item.PMStartMonth = t.PMStartMonth; item.Remarks = t.Remarks; OperationList[EnumOperationType.Change].Add(item); } } // 新增的计划 int[] EditEquipmentIds = OperationList[EnumOperationType.Change].Select(x => x.EquipmentID).Distinct().ToArray(); List InPlans = lst.Where(x => x.MaintenanceYear == Year && !EditEquipmentIds.Contains(x.EquipmentID)).ToList(); foreach (DriveMaintencePlanInfo item in InPlans) { item.PlanStatus = "A"; item.CreatDate = CurrentTime; item.CreatUser = OperationID; item.ChangeDate = null; item.ChangeUser = 0; OperationList[EnumOperationType.Add].Add(item); } // 存在新增项 List FulfillmentLst = new List(); FulfillmentLst.AddRange(OperationList[EnumOperationType.Add]); FulfillmentLst.AddRange(OperationList[EnumOperationType.Change]); if (devMain.Saveable(FulfillmentLst).ExecuteCommand() == FulfillmentLst.Count) { devMain.CommitTran(); apiResponseData.Code = 1; apiResponseData.Data = lst.Count; List logs = new List(); foreach (KeyValuePair> item in OperationList) { foreach (DriveMaintencePlanInfo data in item.Value) { DeviceInformationInfo dev = null; if (devs.ContainsKey(data.EquipmentID)) dev = devs[data.EquipmentID]; else throw new Exception($"没有查询到设备表主键编号为【{data.EquipmentID}】的数据!"); logs.Add(new PlanlogInfo { EquipmentAutoID = data.EquipmentID, EquipmentID = dev.EquipmentID, PlanMonth = data.MaintenanceMonth, PlanYear = data.MaintenanceYear, PlanType = data.MaintenanceType, OperationUser = OperationID, OperationUserName = ApiParameters["OPERATORNAME"], OperationComputer = ApiParameters["CLIENTNAME"], OperationIP = ApiParameters["CLIENTIP"], OperationDate = CurrentTime, OperationType = EnumOperationType.Add == item.Key ? "新增" : "修改" }); } } devLog.Insertable(logs).ExecuteCommand(); } else { devMain.RollbackTran(); apiResponseData.Code = -1; } return apiResponseData; } catch (SqlException sqlEx) { devMain.RollbackTran(); throw sqlEx; } catch (Exception ex) { devMain.RollbackTran(); throw ex; } } } }