using System; using System.Linq; using ADM.ProgressMonitor.DataObjects; using System.Collections.Generic; using System.Web.UI; using ADM.ProgressMonitor.WebControl; using System.Web.SessionState; using ADM.ProgressMonitor.BusinessRules; using System.Data.Objects; namespace ADM.ProgressMonitor.DataAccess { /// <summary> /// 基于Session为Cache的Oracle DAO类。 /// 如果Cache中已有相关数据,直接从Cache中读取。 /// </summary> public class SessionCachedOracleDAO { /// <summary> /// 关联的页面 /// </summary> private Page _page; /// <summary> /// 关联的用户控件 /// </summary> private UserControl _userControl; /// <summary> /// 构造函数 /// </summary> /// <param name="page">关联页面</param> public SessionCachedOracleDAO(Page page) { _page = page; } /// <summary> /// 构造函数 /// </summary> /// <param name="userControl">关联控件</param> public SessionCachedOracleDAO(UserControl userControl) { _userControl = userControl; } /// <summary> /// 获取关联的Session /// </summary> HttpSessionState Session { get { if (_page != null) { return _page.Session; } if (_userControl != null) { return _userControl.Session; } throw new Exception("No available session."); } } /// <summary> /// 获取M1分册开工时间 /// </summary> /// <param name="aeroId">型号Id</param> /// <returns>M1分册开工时间。如没找到,返回NULL</returns> public DateTime? GetM1StartDate(string aeroId) { List<SRC_DEF_STAGE> stages = GetStages(aeroId); foreach (SRC_DEF_STAGE stage in stages) { if (Parser.ParseStageCode(stage.TECHNICS_NAME) == "M1") { SRC_DEF_PROCEDURE proce = GetProceduresFromStage(stage.STAGE_ID).FirstOrDefault(item => item.SORT_ID == 1); if (proce != null && proce.WORKING_BEGIN_TIME.HasValue) { return proce.WORKING_BEGIN_TIME.Value.Date; } } } return null; } public DateTime GetAeroEndDate(string aeroId) { List<SRC_DEF_STAGE> stages = GetStages(aeroId); return stages.Where(item => item.WORKING_END_TIME.HasValue && item.WORKING_END_TIME.Value.Year != 9999).Max (item => item.WORKING_END_TIME.Value); } public DateTime GetAeroBeginDate(string aeroId) { List<SRC_DEF_STAGE> stages = GetStages(aeroId); return stages.Where(item => item.WORKING_BEGIN_TIME.HasValue && item.WORKING_BEGIN_TIME.Value.Year != 9999).Min (item => item.WORKING_BEGIN_TIME.Value); } /// <summary> /// 返回DB中所有型号 /// </summary> /// <returns>DB中所有型号</returns> public List<SRC_DEF_AEROCRAFT> GetAllAeros() { string sessionKey = "AERO"; if (Session[sessionKey] == null) { Session[sessionKey] = context.SRC_DEF_AEROCRAFT.ToList(); } return (List<SRC_DEF_AEROCRAFT>)Session[sessionKey]; } /// <summary> /// 返回相关型号的日统计数据。 /// </summary> /// <param name="aeroId">型号ID</param> /// <returns>相关型号的日统计数据</returns> public List<SRC_DEF_AERO_DAILY_STATISTICS> GetSRC_DEF_AERO_DAILY_STATISTICS(string aeroId) { string sessionKey = "AERO_DAILY_STATISTICS_" + aeroId; if (Session[sessionKey] == null) { Session[sessionKey] = context.SRC_DEF_AERO_DAILY_STATISTICS.Where(item => item.AEROCRAFT_ID == aeroId).ToList(); } return (List<SRC_DEF_AERO_DAILY_STATISTICS>)Session[sessionKey]; } /// <summary> /// 返回相关型号们的日统计数据。 /// </summary> /// <param name="aeroIds">型号ID列表</param> /// <returns>相关型号集的日统计数据</returns> public List<SRC_DEF_AERO_DAILY_STATISTICS> GetSRC_DEF_AERO_DAILY_STATISTICS(List<string> aeroIds) { List<SRC_DEF_AERO_DAILY_STATISTICS> ret = new List<SRC_DEF_AERO_DAILY_STATISTICS>(); foreach (string aeroId in aeroIds) { ret.AddRange(GetSRC_DEF_AERO_DAILY_STATISTICS(aeroId)); } return ret; } public void ClearTemplate(int baselineId) { string sessionKey = "TEMPLATE_" + baselineId; Session[sessionKey] = null; } /// <summary> /// 返回相关平台的阶段模版 /// </summary> /// <param name="baselineId">平台ID</param> /// <returns>该平台的阶段模版</returns> public List<SRC_DEF_BASE_PHASE_TEMPLATE> GetTemplate(int baselineId) { string sessionKey = "TEMPLATE_" + baselineId; if (Session[sessionKey] == null) { string PLATFORM_NAME = context.SRC_DEF_PLATFORM.First(item => item.PLATFORM_ID == baselineId).PLATFORM_NAME; Session[sessionKey] = context.SRC_DEF_BASE_PHASE_TEMPLATE.Where(item => item.PLATFORM_NAME == PLATFORM_NAME).OrderBy(item1 => item1.SORT_ID).ToList(); } return (List<SRC_DEF_BASE_PHASE_TEMPLATE>)Session[sessionKey]; } /// <summary> /// 获取相关型号的分册 /// </summary> /// <param name="aeroId">型号ID</param> /// <returns>该型号的分册</returns> public List<SRC_DEF_STAGE> GetStages(string aeroId) { string sessionKey = "STAGE_BY_AERO_" + aeroId; if (Session[sessionKey] == null) { Session[sessionKey] = context.SRC_DEF_STAGE.Where( item => item.AEROCRAFT_ID == aeroId && item.WORKING_BEGIN_TIME.HasValue && item.WORKING_END_TIME.HasValue && item.WORKING_BEGIN_TIME.Value.Year != 9999 && item.WORKING_END_TIME.Value.Year != 9999).OrderBy(item => item.STAGE_ID).ToList(); } return (List<SRC_DEF_STAGE>)Session[sessionKey]; } /// <summary> /// 获取型号列表中所有型号的所属分册 /// </summary> /// <param name="aeroIds">型号ID列表</param> /// <returns>型号列表中所有型号的所属分册</returns> public List<SRC_DEF_STAGE> GetStages(List<string> aeroIds) { List<SRC_DEF_STAGE> ret = new List<SRC_DEF_STAGE>(); foreach (string aeroId in aeroIds) { ret.AddRange(GetStages(aeroId)); } return ret; } /// <summary> /// 获取分册列表中所有分册的所属工序 /// </summary> /// <param name="stageIds">分册ID列表</param> /// <returns>分册列表中所有分册的所属工序</returns> public List<SRC_DEF_PROCEDURE> GetProcedures(List<string> stageIds) { List<SRC_DEF_PROCEDURE> ret = new List<SRC_DEF_PROCEDURE>(); foreach (string stageId in stageIds) { ret.AddRange(GetProceduresFromStage(stageId)); } return ret; } /// <summary> /// 获取型号列表中所有型号的所属工序 /// </summary> /// <param name="aeroIds">型号ID列表</param> /// <returns>型号列表中所有型号的所属工序</returns> public List<SRC_DEF_PROCEDURE> GetProceduresFromAero(List<string> aeroIds) { List<SRC_DEF_PROCEDURE> ret = new List<SRC_DEF_PROCEDURE>(); foreach (string aeroId in aeroIds) { ret.AddRange(GetProceduresFromAero(aeroId)); } return ret; } /// <summary> /// 获取型号所属工序 /// </summary> /// <param name="aeroId"></param> /// <returns></returns> public List<SRC_DEF_PROCEDURE> GetProceduresFromAero(string aeroId) { string sessionKey = "PROCEDURE_BY_AERO_" + aeroId; if (Session[sessionKey] == null) { List<SRC_DEF_STAGE> stages = GetStages(aeroId); List<SRC_DEF_PROCEDURE> ret = new List<SRC_DEF_PROCEDURE>(); foreach (SRC_DEF_STAGE stage in stages) { ret.AddRange(GetProceduresFromStage(stage.STAGE_ID)); } Session[sessionKey] = ret; } return (List<SRC_DEF_PROCEDURE>)Session[sessionKey]; } /// <summary> /// 获取分册下工序 /// </summary> /// <param name="stageId"></param> /// <returns></returns> public List<SRC_DEF_PROCEDURE> GetProceduresFromStage(string stageId) { string sessionKey = "PROCEDURE_BY_STAGE_" + stageId; if (Session[sessionKey] == null) { Session[sessionKey] = context.SRC_DEF_PROCEDURE.Where(item => item.WORKING_BEGIN_TIME.HasValue && item.WORKING_END_TIME.HasValue && item.STAGE_ID == stageId).ToList(); } return (List<SRC_DEF_PROCEDURE>)Session[sessionKey]; } /// <summary> /// 数据库Oracle Context /// </summary> Entities context = new Entities(); /// <summary> /// 删除基线 /// </summary> /// <param name="baselineId">基线Id</param> /// <param name="dataSet">数据集</param> public void DeleteBaseline(int baselineId, AeroDataSet dataSet) { foreach (SRC_DEF_TEMP_WORK_DEVIATION item in context.SRC_DEF_TEMP_WORK_DEVIATION.Where(item => item.BASELINE_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_TEMP_WORK_DEVIATION2 item in context.SRC_DEF_TEMP_WORK_DEVIATION2.Where(item => item.BASELINE_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_QUESTION_LIST_DEV item in context.SRC_DEF_QUESTION_LIST_DEV.Where(item => item.BASELINE_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_PROGRESS_DEVIATION item in context.SRC_DEF_PROGRESS_DEVIATION.Where(item => item.BASELINE_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_PROGRESS_DEVIATION2 item in context.SRC_DEF_PROGRESS_DEVIATION2.Where(item => item.BASELINE_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_PROCEDURE_DEVIATION item in context.SRC_DEF_PROCEDURE_DEVIATION.Where(item => item.BASELINE_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_PROCEDURE_DEVIATION2 item in context.SRC_DEF_PROCEDURE_DEVIATION2.Where(item => item.BASELINE_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_PHASE_PARAMETER item in context.SRC_DEF_PHASE_PARAMETER.Where(item => item.PLATFORM_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_BASELINE_PHASE_STAT item in context.SRC_DEF_BASELINE_PHASE_STAT.Where(item => item.PLATFORM_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_BASELINE_AEROCRAFT item in context.SRC_DEF_BASELINE_AEROCRAFT.Where(item => item.PLATFORM_ID == baselineId)) { context.DeleteObject(item); } foreach (SRC_DEF_BASELINE item in context.SRC_DEF_BASELINE.Where(item => item.PLATFORM_ID == baselineId)) { context.DeleteObject(item); } //foreach ( // SRC_DEF_BASE_PHASE_TEMPLATE item in // context.SRC_DEF_BASE_PHASE_TEMPLATE.Where(item => item.PLATFORM_ID == baselineId)) //{ // context.DeleteObject(item); //} foreach (AeroDataSet.AeroPhaseRow carPhaseRow in dataSet.AeroPhase) { foreach (SRC_DEF_BASE_AEROCRAFT_PHASE srcDefBaseAerocraftPhase in context.SRC_DEF_BASE_AEROCRAFT_PHASE.Where(item => item.AEROCRAFT_ID == carPhaseRow.AeroId)) { context.DeleteObject(srcDefBaseAerocraftPhase); } } context.SaveChanges(); } public void AutoFillAeroStatus() { context.Connection.Open(); using (System.Data.Common.DbTransaction transaction = context.Connection.BeginTransaction()) { foreach (SRC_DEF_AEROCRAFT aerocraft in context.SRC_DEF_AEROCRAFT) { SRC_DEF_AEROCRAFT_STATUS status = context.SRC_DEF_AEROCRAFT_STATUS.FirstOrDefault( item => item.AEROCRAFT_ID == aerocraft.AEROCRAFT_ID); if (status == null) { ObjectParameter objectParameter = new ObjectParameter("isOVER",typeof(string)); context.AERO_STATUS_JUDGE(aerocraft.AEROCRAFT_ID, objectParameter); status = new SRC_DEF_AEROCRAFT_STATUS(); status.AEROCRAFT_ID = aerocraft.AEROCRAFT_ID; status.TYPE = "自动"; //status.STATUS = aerocraft.WORKING_END_TIME.HasValue ? "已完成" : "在研"; status.STATUS = objectParameter.Value == "true" ? "已完成" : "在研"; context.AddToSRC_DEF_AEROCRAFT_STATUS(status); } } context.SaveChanges(); transaction.Commit(); } } /// <summary> /// 填充型号状态表中缺失的数据。 /// </summary> public void FillAeroStatus() { context.Connection.Open(); using (System.Data.Common.DbTransaction transaction = context.Connection.BeginTransaction()) { foreach (SRC_DEF_AEROCRAFT aerocraft in context.SRC_DEF_AEROCRAFT) { SRC_DEF_AEROCRAFT_STATUS status = context.SRC_DEF_AEROCRAFT_STATUS.FirstOrDefault( item => item.AEROCRAFT_ID == aerocraft.AEROCRAFT_ID); if (status == null) { status = new SRC_DEF_AEROCRAFT_STATUS(); status.AEROCRAFT_ID = aerocraft.AEROCRAFT_ID; status.TYPE = "手动"; status.STATUS = aerocraft.WORKING_END_TIME.HasValue ? "已完成" : "在研"; context.AddToSRC_DEF_AEROCRAFT_STATUS(status); } } context.SaveChanges(); transaction.Commit(); } } /// <summary> /// 获取平台下型号ID集合 /// </summary> /// <param name="platformId"></param> /// <returns></returns> public List<string> GetAeroIds(int platformId) { return context.SRC_DEF_PLATFORM_AEROCRAFT.Where(item => item.PLATFORM_ID == platformId).Select(item1 => item1.AEROCRAFT_ID).ToList(); } /// <summary> /// 依据工序名称、分册号、型号号、工序号查找对应工序。 /// </summary> /// <param name="procedureName"></param> /// <param name="stageCode"></param> /// <param name="procedureCode"></param> /// <param name="platformId"></param> /// <param name="aeroId"></param> /// <returns></returns> public AeroStageProcedureSearchResult Search(string procedureName, string stageCode, string procedureCode, int platformId, string aeroId) { if (GetProceduresFromAero(GetAeroIds(platformId)).Count(item => item.TECHNICS_NAME == procedureName) != 0) { SRC_DEF_PROCEDURE startProcedure = GetProceduresFromAero(GetAeroIds(platformId)).First(item => item.TECHNICS_NAME == procedureName); AeroStageProcedureSearchResult ret = new AeroStageProcedureSearchResult(); ret.ProcedureId = startProcedure.PROCEDURE_ID; ret.StageId = startProcedure.STAGE_ID; ret.AeroId = context.SRC_DEF_STAGE.First(item => item.STAGE_ID == ret.StageId).AEROCRAFT_ID; return ret; } else { List<SRC_DEF_STAGE> stages = GetStages(GetAeroIds(platformId)); SRC_DEF_STAGE matchedStage = stages.FirstOrDefault( item => item.TECHNICS_NAME.Contains("(" + stageCode + ")") || item.TECHNICS_NAME.Contains("(" + stageCode + ")")); if (matchedStage != null) { List<SRC_DEF_PROCEDURE> procedures = GetProceduresFromStage(matchedStage.STAGE_ID); SRC_DEF_PROCEDURE matchedProcedure = procedures.FirstOrDefault(item => item.SORT_ID.ToString() == procedureCode); if (matchedProcedure != null) { AeroStageProcedureSearchResult ret = new AeroStageProcedureSearchResult(); ret.ProcedureId = matchedProcedure.PROCEDURE_ID; ret.StageId = matchedProcedure.STAGE_ID; ret.AeroId = matchedStage.AEROCRAFT_ID; return ret; } } } return null; } } }