using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using QMAPP.BLL;
using QMAPP.Entity;
using QMAPP.FJC.BLL.Common;
using QMAPP.FJC.DAL.CompleteStatistics;
using QMAPP.FJC.Entity;
using QMAPP.FJC.Entity.Common.Constants;
using QMAPP.FJC.Entity.CompleteStatistics;
using QMFrameWork.Data;
using QMFrameWork.Log;
using System.IO;
namespace QMAPP.FJC.BLL.CompleteStatistics
{
public class CompleteStatisticsBLL : BaseBLL
{
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataResult GetList(CompleteStatisticsEntity condition, DataPage page)
{
DataResult result = new DataResult() {IsSuccess = true};
try {
result.Result = new CompleteStatisticsDAL().GetList(condition, page);
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo {ErrorInfo = ex, Tag = ex.StackTrace, Info = "获取统计信息列表错误!"});
result.IsSuccess = false;
result.Msg = "获取统计信息列表错误!";
}
return result;
}
public int GenericCompleteStatistics(DateTime starTime, DateTime endTime)
{
try {
//插入需要的数据
int count = new CompleteStatisticsDAL().GenericCompleteStatistics(starTime, endTime);
string isAutoExport = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_AUTO_EXPORT);
if (isAutoExport.Equals("1")) {
this.ExportCompleteStatistics(new CompleteStatisticsEntity { SENDTIME = starTime.ToString("yyyy/MM/dd HH:mm:ss"), EENDTIME = endTime.ToString("yyyy/MM/dd HH:mm:ss") });
}
return count;
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "生成统计信息列表错误!" });
throw;
}
}
///
/// 获取列表
///
/// 条件
/// 全部数据
public List GetAllList(CompleteStatisticsEntity condition)
{
try {
return new CompleteStatisticsDAL().GetList(condition);
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo {ErrorInfo = ex, Tag = ex.StackTrace, Info = "获取统计信息列表错误!"});
throw;
}
}
///
/// 获取信息
///
/// 条件
/// 信息
public CompleteStatisticsEntity Get(CompleteStatisticsEntity model)
{
try {
return new CompleteStatisticsDAL().Get(model);
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "取得统计信息异常!" });
throw;
}
}
///
/// 获取信息
///
/// 主键
/// 信息
public CompleteStatisticsEntity GetById(string pid)
{
return this.Get(new CompleteStatisticsEntity { PID = pid });
}
///
/// 获取完工统计周期
///
/// 周期(秒)
public int GetRunPeriod()
{
string value = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_STATIS_PERIOD);
int period;
if (!Int32.TryParse(value, out period)) {
period = 24*60*60;
}
return period;
}
///
/// 获取上次统计时间
///
/// 周期(分)
public DateTime GetLastRunTime()
{
string value = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_STATIS_LSTTIME);
DateTime lastRunTime;
if (!DateTime.TryParse(value, out lastRunTime)) {
lastRunTime = DateTime.Now;
}
return lastRunTime;
}
///
/// 获取信息
///
/// codeType
/// 信息
public string GetSysConfigValue(string codeType)
{
try
{
SystemConfig systemConfig = new CompleteStatisticsDAL().GetSystemConfig(codeType);
return systemConfig.CODEVALUE;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "取得统计信息异常!" });
throw;
}
}
///
/// 插入信息
///
/// 信息
/// 插入行数
public DataResult Insert(CompleteStatisticsEntity model)
{
DataResult result = new DataResult
{
IsSuccess = true,
Msg = Resource.MsgSuccess
};
try {
//基本信息
model.PID = Guid.NewGuid().ToString();
model.CREATEUSER = this.LoginUser.UserID;
model.CREATETIME = DateTime.Now;
model.UPDATEUSER = this.LoginUser.UserID;
model.DELFLAG = "0";
//if (this.ExistsMain(model)) {
// return -1;
//}
result.Result = new CompleteStatisticsDAL().Insert(model);
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo {ErrorInfo = ex, Tag = ex.StackTrace, Info = "插入统计信息异常!"});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
}
return result;
}
///
/// 更新信息
///
///
/// 更新行数
public DataResult Update(CompleteStatisticsEntity model)
{
DataResult result = new DataResult
{
IsSuccess = true,
Msg = Resource.MsgSuccess
};
try {
model.UPDATEUSER = this.LoginUser.UserID;
result.Result = new CompleteStatisticsDAL().Update(model);
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo {ErrorInfo = ex, Tag = ex.StackTrace, Info = "更新统计信息异常!"});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
}
return result;
}
///
/// 判断统计信息是否存在
///
///
/// true:已存在;fasel:不存在。
protected bool ExistsMain(CompleteStatisticsEntity model)
{
try {
return new CompleteStatisticsDAL().ExistCompleteStatistics(model);
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "判断统计信息是否存在异常!" });
throw;
}
}
public DataResult ExportExcelData(CompleteStatisticsEntity model)
{
DataResult result = new DataResult
{
IsSuccess = true,
Msg = Resource.MsgSuccess
};
try
{
result.Result = new CompleteStatisticsDAL().ExportExcelData(model);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "完工数量统计Excel导出异常!" });
result.IsSuccess = false;
result.Msg = Resource.SystemException;
}
return result;
}
///
/// 插入信息
///
/// 信息
/// 插入行数
public DataResult ExportCompleteStatistics(CompleteStatisticsEntity model)
{
IDataSession session = null;
DataResult result = new DataResult
{
IsSuccess = true,
Msg = "导出成功!"
};
try {
session = AppDataFactory.CreateMainSession();
session.OpenTs();
string localfilePath = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_STATIS_FILEPATH);
string remotefilePath = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_PATH);
List completeStatisticsEntities = new CompleteStatisticsDAL().GetExportData(model, session);
//用生产线分组,然后用每条生产线生成一个txt文件
var lineEntitiesGroup = completeStatisticsEntities.GroupBy(x => x.PRODUCTLINE);
if (lineEntitiesGroup.Count() > 0)
{
foreach (var lineName in lineEntitiesGroup)
{
//var lineName = lineEntity.PRODUCTLINE;
var lineEntities = completeStatisticsEntities.Where(x => x.PRODUCTLINE == lineName.Key);
//本地路径
if (!string.IsNullOrEmpty(localfilePath))
{
if (Directory.Exists(localfilePath) == false)
{
Directory.CreateDirectory(localfilePath);
}
if (lineEntities.Count() > 0)
{
var orderName = lineEntities.ToList()[0].ORDERNO.Substring(0, 4);
var lineTxt = lineEntities.Select(t => t.ToString()).ToList();
string txtName = string.Format("{0}bkfl{1}{3}{2}.txt", "", DateTime.Now.ToString("yyyyMMddHHmmss"), model.ENDTIME.ToString("MMdd"), orderName);
new FileHelper(string.Format("{0}{1}", localfilePath, txtName)).InsertLines(lineTxt);
}
}
//远程路径
if (!string.IsNullOrEmpty(remotefilePath))
{
string user = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_USER);
string domain = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_DOMAIN);
string password = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_PWD);
//模拟远程登陆
using (var id = new IdentityScope(user, domain, password))
{
//new FileHelper(string.Format("{0}{1}", remotefilePath, filename)).InsertLines(lines);
if (lineEntities.Count() > 0)
{
var orderName = lineEntities.ToList()[0].ORDERNO.Substring(0, 4);
var lineTxt = lineEntities.Select(t => t.ToString()).ToList();
string txtName = string.Format("{0}bkfl{1}{3}{2}.txt", "", DateTime.Now.ToString("yyyyMMddHHmmss"), model.ENDTIME.ToString("MMdd"), orderName);
new FileHelper(string.Format("{0}{1}", remotefilePath, txtName)).InsertLines(lineTxt);
}
}
}
}
}
session.CommitTs();
}
catch (Exception ex) {
LogManager.LogHelper.Error(new LogInfo {ErrorInfo = ex, Tag = ex.StackTrace, Info = "导出统计信息异常!"});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
if (session != null) {
session.RollbackTs();
}
}
return result;
}
//public DataResult ExportCompleteStatistics(CompleteStatisticsEntity model)
//{
// IDataSession session = null;
// DataResult result = new DataResult
// {
// IsSuccess = true,
// Msg = "导出成功!"
// };
// try
// {
// session = AppDataFactory.CreateMainSession();
// session.OpenTs();
// List completeStatisticsEntities = new CompleteStatisticsDAL().GetExportData(model, session);
// var lines = completeStatisticsEntities.Select(t => t.ToString()).ToList();
// string localfilePath = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_STATIS_FILEPATH);
// string filename = string.Format("{0}bkfl{1}{3}{2}.txt", "", DateTime.Now.ToString("yyyyMMddHHmmss"), model.ENDTIME.ToString("MMdd"), this.GetSysConfigValue(CompleteStatisticsConstants.COMP_STATIS_ORDER));
// if (!string.IsNullOrEmpty(localfilePath)) //本地路径
// {
// new FileHelper(string.Format("{0}{1}", localfilePath, filename)).InsertLines(lines);
// }
// string remotefilePath = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_PATH);
// if (!string.IsNullOrEmpty(remotefilePath)) //远程路径
// {
// string user = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_USER);
// string domain = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_DOMAIN);
// string password = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_REMOTE_PWD);
// //模拟远程登陆
// using (var id = new IdentityScope(user, domain, password))
// {
// new FileHelper(string.Format("{0}{1}", remotefilePath, filename)).InsertLines(lines);
// }
// }
// session.CommitTs();
// }
// catch (Exception ex)
// {
// LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "导出统计信息异常!" });
// result.IsSuccess = false;
// result.Msg = Resource.SystemException;
// if (session != null)
// {
// session.RollbackTs();
// }
// }
// return result;
//}
//执行task服务
///
/// 导出EE SQL
///
/// 信息
/// 插入行数
public DataResult ExportCompleteStatisticsSQL(CompleteStatisticsEntity model)
{
IDataSession session = null;
DataResult result = new DataResult
{
IsSuccess = true,
Msg = "导出成功!"
};
try
{
session = AppDataFactory.CreateMainSession();
session.OpenTs();
//if (string.IsNullOrEmpty(model.PIDList) == false)
//{
// model.PIDList = "'" + model.PIDList.Replace(":", "','") + "'";
//}
//导出数据信息
List completeStatisticsEntities = new CompleteStatisticsDAL().GetExportData(model, session);
var sqls = completeStatisticsEntities.Select(t => t.ToSQL()).ToList();
var lines = new List();
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_EE_CONNSTR);
conn.Open();
foreach (var sql in sqls)
{
try
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
lines.Add(string.Format("--{0}--{1}", sql, "Success"));
}
catch (Exception e)
{
lines.Add(string.Format("--{0}--{1}\r\n/*{2}*/", sql, "Failed", e.Message));
}
}
conn.Close();
string localfilePath = this.GetSysConfigValue(CompleteStatisticsConstants.COMP_STATIS_FILEPATH);
string filename = string.Format("{0}bkfl{1}{3}{2}.sql", "", DateTime.Now.ToString("yyyyMMddHHmmss"), model.ENDTIME.ToString("MMdd"), this.GetSysConfigValue(CompleteStatisticsConstants.COMP_STATIS_ORDER));
if (!string.IsNullOrEmpty(localfilePath)) //本地路径
{
new FileHelper(string.Format("{0}{1}", localfilePath, filename)).InsertLines(lines);
}
session.CommitTs();
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "导出统计信息异常!" });
result.IsSuccess = false;
result.Msg = Resource.SystemException;
if (session != null)
{
session.RollbackTs();
}
}
return result;
}
public void GenericMFG()
{
//int interval = 30;
//DateTime lastRunTime = GetLastRunTime();
int interval = 24;
DateTime lastRunTime = GetLastRunTime();
DateTime judgementTime = lastRunTime.AddHours(interval);
if (judgementTime < DateTime.Now)
{
QMFrameWork.Log.LogManager.LogHelper.Info(
new QMFrameWork.Log.LogInfo { ClientIP = "localhost", UserName = "admin", Info = "完工数量统计开始" });
GenericCompleteStatistics(lastRunTime, judgementTime);
QMFrameWork.Log.LogManager.LogHelper.Info(
new QMFrameWork.Log.LogInfo { ClientIP = "localhost", UserName = "admin", Info = "完工数量统计结束" });
}
}
}
}