using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QMTask.Core
{
///
/// 计划信息管理
///
public class PlanInfoManage
{
#region 获取计划信息列表
///
/// 获取计划列表从XML配置文件中
///
///
public List GetPlanList()
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
List listrs = xml.GetData("Plan/PlanDetail");
return listrs;
}
catch
{
throw;
}
}
#endregion
#region 获取计划信息
///
/// 获取计划Model
///
///
///
public PlanInfo GetPlanModel(string PlanID)
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
List listrs = xml.GetData("Plan/PlanDetail");
PlanInfo tempModel = listrs.Find(p => p.PlanID == PlanID);
return tempModel;
}
catch
{
throw;
}
}
#endregion
#region 添加计划信息
///
/// 添加一条计划信息
///
///
public bool AddPlan(PlanInfo plan)
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
Dictionary Attribs = new Dictionary();
Type type = plan.GetType();
foreach (var item in type.GetProperties())
{
object value = item.GetValue(plan, null);
Attribs.Add(item.Name, value==null?"":value.ToString());
}
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
xml.InsertElement("Plan", "PlanDetail", Attribs, null);
xml.Save();
return true;
}
catch
{
throw;
}
}
#endregion
#region 修改计划信息
///
/// 修改计划信息
///
///
///
public bool UpdatePlan(PlanInfo plan)
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
Dictionary dict = new Dictionary();
Type type = plan.GetType();
foreach (var item in type.GetProperties())
{
if (item.Name != "PlanID")
{
if (item.GetValue(plan, null) != null)
{
dict.Add(item.Name, item.GetValue(plan, null).ToString());
}
}
}
xml.UpdateAttributes("Plan/PlanDetail[@PlanID= '" + plan.PlanID + "']", dict);
xml.Save();
return true;
}
catch
{
throw;
}
}
#endregion
#region 删除计划信息
///
/// 删除计划信息,返回true只说明代码运行成功,并不保证一定有数据被删除。
///
///
///
public bool DeletePlan(string PlanID)
{
try
{
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
xml.DeleteChild("Plan/PlanDetail[@PlanID='" + PlanID + "']");
xml.Save();
return true;
}
catch(Exception ex)
{
return false;
}
}
#endregion
#region 检查xml文件重复
///
/// 检查Planxml文件PlanID是否重复
///
///
public bool CheckPlanXML()
{
try
{
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
List listrs = xml.GetData("Plan/PlanDetail");
bool rs = true;
foreach (var a in listrs)
{
List temp = listrs.FindAll(p => p.PlanID == a.PlanID);
if (temp.Count > 1)
{
rs = false;
}
}
return rs;
}
catch
{
throw ;
}
}
#endregion
}
}