ComplianceServer/oldcode/CRMServices/AutoRpt/ScheduleExecute.cs

123 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WX.CRM.BLL.Autorpt;
using WX.CRM.Common;
using WX.CRM.Model.Entity;
namespace WX.CRM.CRMServices.AutoRpt
{
public class ScheduleExecute
{
AUTORPT_SCHEDULINGTASKS_BL _taskBl = new AUTORPT_SCHEDULINGTASKS_BL();
Autorpt_executelog_BL _logBl = new Autorpt_executelog_BL();
public void Execute()
{
string saveRptpath = FileUnit.GetBaseDirectory();
saveRptpath = saveRptpath.Substring(0, saveRptpath.IndexOf("\\", System.StringComparison.Ordinal)) + "/AutoPrt/" + string.Format("{0:yyMMdd}", DateTime.Now) + "/";
var tasks = _taskBl.GetAutorptSchedulingtaskss(DateTime.Now);
foreach (var task in tasks)
{
try
{
string _saveRptPath = saveRptpath;
if (!string.IsNullOrEmpty(task.DIRECTORY))
{
_saveRptPath = saveRptpath + task.DIRECTORY + "/";
}
var fileName = task.REPORTNAME + string.Format("{0:yyMMddhh}", DateTime.Now) + ".xls";
string sheetName = task.REPORTNAME;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
var dataset = _taskBl.EnableScheduling(task.PROCEDURENAME.Trim());
sw.Stop();
decimal executiontime = Convert.ToDecimal(sw.ElapsedMilliseconds / 1000.0);
decimal count = dataset.Tables[0].Rows.Count;
if (count > 0)
{
ExcelHandler.SaveDataSetToExcel(dataset, _saveRptPath, fileName, sheetName);
// task.DEPTCODE = _saveRptPath + fileName;
}
string logdirctorypath = _saveRptPath + fileName;
UpdateScheduling(task, true);
CreateLog(task, true, "", count, executiontime, logdirctorypath);
}
catch (Exception ex)
{
if (task.ERRORNUM < task.ENABLENUM)
{
CreateScheduling(task);
}
UpdateScheduling(task, false);
CreateLog(task, false, ex.Message);
LogHelper.Error(task.PROCEDURENAME + ex);
}
}
}
private void CreateLog(AUTORPT_SCHEDULINGTASKS task, bool succ, string msg, decimal count = 0, decimal executiontime = 0, string dirctorypath = "")
{
AUTORPT_EXECUTELOG executelog = new AUTORPT_EXECUTELOG
{
DEPTCODE = task.DEPTCODE,
REPORTNAME = task.REPORTNAME,
//DIRECTORY = task.DIRECTORY,
PROCEDURENAME = task.PROCEDURENAME,
CTIME = DateTime.Now,
RECORDCOUNT = count,
EXECUTIONTIME = executiontime
};
if (succ)
{
executelog.STATUS = 200;
if (!string.IsNullOrEmpty(dirctorypath))
{
executelog.DIRECTORY = dirctorypath;
}
}
else
{
executelog.STATUS = 60;
executelog.ERRORMSG = msg;
executelog.MEMO = msg;
}
_logBl.Create(executelog);
}
private void CreateScheduling(AUTORPT_SCHEDULINGTASKS task)
{
AUTORPT_SCHEDULINGTASKS createschedulingtasks = new AUTORPT_SCHEDULINGTASKS
{
DEPTCODE = task.DEPTCODE,
REPORTNAME = task.REPORTNAME,
DIRECTORY = task.DIRECTORY,
PROCEDURENAME = task.PROCEDURENAME,
SUCCESS = 0,
ERRORNUM = task.ERRORNUM + 1,
ENABLETIME = task.ENABLETIME.AddHours(2),
ENABLENUM = task.ENABLENUM,
SORTID = task.SORTID
};
_taskBl.Create(createschedulingtasks);
}
private void UpdateScheduling(AUTORPT_SCHEDULINGTASKS task2, bool success)
{
AUTORPT_SCHEDULINGTASKS updateschedulingtasks = task2;
if (success)
{
updateschedulingtasks.SUCCESS = 1;
}
else
{
updateschedulingtasks.SUCCESS = -1;
updateschedulingtasks.ERRORNUM = updateschedulingtasks.ERRORNUM + 1;
}
updateschedulingtasks.ENDTIME = DateTime.Now;
_taskBl.Update(updateschedulingtasks);
}
}
}