ClearLogAction.cs 5.74 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using Quartz;
using WebRepository;

namespace WebMvc
{
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class ClearLogAction
    {
        private string ConnString { set; get; }
        private IJobExecutionContext Context { set; get; }

        public ClearLogAction(string _ConnString, IJobExecutionContext _Context)
        {
            ConnString = _ConnString;
            Context = _Context;
        }

        public void Execute()
        {
            UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
            string sql = "";
            string LogName = Context.JobDetail.JobDataMap.GetString("LogName");
            int Days = Context.JobDetail.JobDataMap.GetInt("Days");

            DbHelp dbHelp = new DbHelp(ConnString);
            #region 执行任务语句
            sql = string.Format("DELETE FROM [dbo].[sys_oper_log] WHERE createTime < '{0}';", DateTime.Now.AddDays(-1 * double.Parse(Days.ToString())));
            dbHelp.DataOperator(sql);

            sql = string.Format("DELETE FROM [dbo].[sys_job_log] WHERE createTime < '{0}';", DateTime.Now.AddDays(-1 * double.Parse(Days.ToString())));
            dbHelp.DataOperator(sql);

            sql = string.Format("DELETE FROM [dbo].[sys_interface_log] WHERE createTime < '{0}';", DateTime.Now.AddDays(-1 * double.Parse(30.ToString())));
            dbHelp.DataOperator(sql);

            sql = string.Format("DELETE FROM [dbo].[task_detail_history] WHERE createTime < '{0}';", DateTime.Now.AddDays(-1 * double.Parse(120.ToString())));
            dbHelp.DataOperator(sql);

            sql = string.Format("DELETE FROM [dbo].[task_history] WHERE createTime < '{0}';", DateTime.Now.AddDays(-1 * double.Parse(120.ToString())));
            dbHelp.DataOperator(sql);

            sql = string.Format("DELETE FROM [dbo].[inventory_transaction] WHERE createTime < '{0}';", DateTime.Now.AddDays(-1 * double.Parse(90.ToString())));
            dbHelp.DataOperator(sql);

            sql = string.Format("DELETE FROM [dbo].[inventory_out] WHERE createTime < '{0}';", DateTime.Now.AddDays(-1 * double.Parse(90.ToString())));
            dbHelp.DataOperator(sql);

            List<ReceiptHeader> receiptHeaders = _unitWork
                .Find<ReceiptHeader>(n => n.CreateTime < DateTime.Now.AddDays(-1 * double.Parse(60.ToString())))
                .ToList();
            if (receiptHeaders.Any())
            {
                foreach (var receiptHeader in receiptHeaders)
                {
                    ReceiptHeaderHistory receiptHeaderHistory = new ReceiptHeaderHistory();
                    receiptHeader.CopyTo(receiptHeaderHistory);
                    receiptHeaderHistory.Id = null;
                    _unitWork.Add(receiptHeaderHistory);

                    List<ReceiptDetail> receiptDetail1s = _unitWork.Find<ReceiptDetail>(u => u.ReceiptCode == receiptHeader.Code).ToList();
                    foreach (ReceiptDetail rd in receiptDetail1s)
                    {
                        ReceiptDetailHistory receiptDetailHistory = new ReceiptDetailHistory();
                        rd.CopyTo(receiptDetailHistory);
                        receiptDetailHistory.Id = null;
                        receiptDetailHistory.ReceiptId = receiptHeaderHistory.Id;
                        _unitWork.Add(receiptDetailHistory);

                        _unitWork.Delete(rd);
                    }
                    _unitWork.Delete(receiptHeader);
                }
            }
            List<ShipmentHeader> shipmentHeaders = _unitWork
                .Find<ShipmentHeader>(n => n.CreateTime < DateTime.Now.AddDays(-1 * double.Parse(60.ToString())))
                .ToList();
            if (shipmentHeaders.Any())
            {
                foreach (var shipmentHeader in shipmentHeaders)
                {
                    ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
                    shipmentHeader.CopyTo(shipmentHeaderHistory);
                    shipmentHeaderHistory.Id = null;
                    _unitWork.Add(shipmentHeaderHistory);
                    List<ShipmentDetail> shipdtls = _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode == shipmentHeader.Code).ToList();
                    foreach (ShipmentDetail sd in shipdtls)
                    {
                        ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                        sd.CopyTo(shipmentDetailHistory);
                        shipmentDetailHistory.Id = null;
                        shipmentDetailHistory.ShipmentId = shipmentHeaderHistory.Id;
                        _unitWork.Add(shipmentDetailHistory);

                        _unitWork.Delete(sd);
                    }
                    _unitWork.Delete(shipmentHeader);
                }
            }
            List<InterfaceLog> DelinterfaceLogs = _unitWork.Find<InterfaceLog>(n => n.Status == 1 && (n.Type == "LK_In" || n.Type == "LK_Out")).ToList();
            if (DelinterfaceLogs.Any())
            {
                foreach (var DelinterfaceLog in DelinterfaceLogs)
                {
                    List<InterfaceLog> interfaceLogs = _unitWork.Find<InterfaceLog>(n => n.TaskNo == DelinterfaceLog.TaskNo).ToList();
                    foreach (var interfaceLog in interfaceLogs)
                    {
                        InterfaceLog_Old interfaceLogOld = new InterfaceLog_Old();
                        interfaceLog.CopyTo(interfaceLogOld);
                        interfaceLogOld.Id = null;
                        _unitWork.Add(interfaceLogOld);
                        _unitWork.Delete(interfaceLog);
                    }
                }
            }
            #endregion
        }
    }
}