Files
T-DAS/Epost.BLL/HistoryXmlBLL.cs
2023-01-13 15:30:20 +08:00

200 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Epost.Common;
using Epost.DAL;
using Epost.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Epost.BLL
{
public class HistoryXmlBLL
{
DB db = new DB();
HistoryDataDAL dal = new HistoryDataDAL();
#region xml()
public bool DataXML(List<string> table, int days)
{
for (int i = 0; i < table.Count() - 1; i++)
{
for (int j = table.Count() - 1; j > i; j--)
{
if (table[j].Equals(table[i]))
{
table.RemoveAt(j);
}
}
}
//保存数据到xml格式中
string datatime = DateTime.Now.ToString();
//主目录
//string sql_xml = System.IO.Directory.GetCurrentDirectory() + "\\" + "delete" + ".xml";
//sql_xml = @"E:\DSL_DPS - 副本\delete.xml";
//【改变】
string sql_xml = System.AppDomain.CurrentDomain.BaseDirectory + "\\Log\\delete.xml";
//string sql_xml = @db.path_dexml;
//public static string sqlcon = ConfigurationManager.ConnectionStrings["path"].ToString();
XmlDocument xmldoc = new XmlDocument();
XmlNode node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmldoc.AppendChild(node);
if (!File.Exists(sql_xml))
{
//根节点
XmlElement root = xmldoc.CreateElement("Delete");
xmldoc.AppendChild(root);
//一级属性
for (int i = 0; i < table.Count; i++)
{
//子节点
XmlElement delete = xmldoc.CreateElement("delete");
root.AppendChild(delete);
XmlAttribute name = xmldoc.CreateAttribute("name");
delete.Attributes.Append(name);
XmlAttribute day = xmldoc.CreateAttribute("days");
delete.Attributes.Append(day);
name.Value = table[i];//表名
day.Value = Convert.ToString(days);//时间名
delete.InnerText = datatime;//当前时间
}
xmldoc.Save(sql_xml);
}
else
{
xmldoc.Load(sql_xml);
XmlNode orderid2 = xmldoc.SelectSingleNode("Delete");
//获取属性
var list = orderid2.ChildNodes;
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < table.Count; j++)
{
if (list[i].Attributes["name"].Value == table[j])
{
list[i].Attributes["days"].Value = Convert.ToString(days);
list[i].InnerText = datatime;
table.RemoveAt(j);
}
else
{
}
}
}
for (int j = 0; j < table.Count; j++)
{
XmlNode root = xmldoc.SelectSingleNode("Delete");
XmlElement delete = xmldoc.CreateElement("delete");
root.AppendChild(delete);
XmlAttribute name = xmldoc.CreateAttribute("name");
delete.Attributes.Append(name);
XmlAttribute day = xmldoc.CreateAttribute("days");
delete.Attributes.Append(day);
name.Value = table[j];//表名
day.Value = Convert.ToString(days);//时间名
delete.InnerText = datatime;//当前时间
}
xmldoc.Save(sql_xml);
}
bool t = true;
return t;
}
#endregion
#region xml文件()
public bool Load()
{
//加载xml数据是否有
string datatime = DateTime.Now.ToString();
//【改变】
string day_xml = System.AppDomain.CurrentDomain.BaseDirectory + "\\Log\\delete.xml";
//string day_xml = @db.path_dexml;
//string s = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
XmlDocument xmldoc = new XmlDocument();
try
{
xmldoc.Load(day_xml);
//查询xml文件中时间和天数和今天的对比如果大于这执行删除语句
XmlNode orderid2 = xmldoc.SelectSingleNode("Delete");
for (int i = 0; i < orderid2.ChildNodes.Count; i++)
{
//循环每一个节点
string name = orderid2.ChildNodes[i].Attributes["name"].Value;//表名
string day = orderid2.ChildNodes[i].Attributes["days"].Value;//时间
string innerday = orderid2.ChildNodes[i].InnerText;//改的时间
DateTime dt1 = Convert.ToDateTime(innerday);
dt1 = dt1.AddDays(Convert.ToInt32(day));
DateTime dt2 = Convert.ToDateTime(datatime);
if (dt1.CompareTo(dt2) >= 0)
{
}
else if (dt1.CompareTo(dt2) < 0)
{
//执行删除任务的操作
DeleteTable(name);//执行删除任务
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogInfo("加载xml文件(数据库历史数据)异常:" + ex.ToString());
}
return true;
}
#endregion
#region
public bool DeleteTable(string table)
{
bool t = dal.DeleteTable(table);
if (t == true)
{
//记录日志
ErrorLogBLL logBLL = new ErrorLogBLL();
ErrorLogModel error_model = new ErrorLogModel();
error_model.Type = "3";
error_model.Remark = "删除表中信息:日志目录: delete from " + table;
logBLL.InsertErrorLog(error_model);
}
return t;
}
#endregion
#region ()
public bool SaveTime(string table, string date, string time)
{
bool t = dal.SaveTime(table, date);
return t;
}
#endregion
#region ()
public IList<HistoryDataModel> HistoryData()
{
DataTable dt = dal.HistoryData();
IList<HistoryDataModel> HistoryData = ModelConvertHelper<HistoryDataModel>.ConvertToModel(dt);
return HistoryData;
}
#endregion
}
}