天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QMTask.Core
{
/// <summary>
/// 计划信息管理
/// </summary>
public class PlanInfoManage
{
#region 获取计划信息列表
/// <summary>
/// 获取计划列表从XML配置文件中
/// </summary>
/// <returns></returns>
public List<PlanInfo> GetPlanList()
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
List<PlanInfo> listrs = xml.GetData<PlanInfo>("Plan/PlanDetail");
return listrs;
}
catch
{
throw;
}
}
#endregion
#region 获取计划信息
/// <summary>
/// 获取计划Model
/// </summary>
/// <param name="PlanID"></param>
/// <returns></returns>
public PlanInfo GetPlanModel(string PlanID)
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
List<PlanInfo> listrs = xml.GetData<PlanInfo>("Plan/PlanDetail");
PlanInfo tempModel = listrs.Find(p => p.PlanID == PlanID);
return tempModel;
}
catch
{
throw;
}
}
#endregion
#region 添加计划信息
/// <summary>
/// 添加一条计划信息
/// </summary>
/// <param name="plan"></param>
public bool AddPlan(PlanInfo plan)
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
Dictionary<string, string> Attribs = new Dictionary<string, string>();
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 修改计划信息
/// <summary>
/// 修改计划信息
/// </summary>
/// <param name="plan"></param>
/// <returns></returns>
public bool UpdatePlan(PlanInfo plan)
{
try
{
if (!new PlanInfoManage().CheckPlanXML())
{
throw new Exception("PlanConfig.xml中PlanID有重复项!请修改后启动程序.");
}
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
Dictionary<string, string> dict = new Dictionary<string, string>();
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 删除计划信息
/// <summary>
/// 删除计划信息,返回true只说明代码运行成功,并不保证一定有数据被删除。
/// </summary>
/// <param name="PlanID"></param>
/// <returns></returns>
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文件重复
/// <summary>
/// 检查Planxml文件PlanID是否重复
/// </summary>
/// <returns></returns>
public bool CheckPlanXML()
{
try
{
XmlHelper xml = new XmlHelper(Configuration.PlanConfigPath());
List<PlanInfo> listrs = xml.GetData<PlanInfo>("Plan/PlanDetail");
bool rs = true;
foreach (var a in listrs)
{
List<PlanInfo> temp = listrs.FindAll(p => p.PlanID == a.PlanID);
if (temp.Count > 1)
{
rs = false;
}
}
return rs;
}
catch
{
throw ;
}
}
#endregion
}
}