using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BLL
{
///
/// 集团主数据在建项目数据分析
///
public static class ProjectMasterDataAnalysisService
{
///
/// 根据主键获取分析数据
///
///
///
public static Model.Project_MasterDataAnalysis GetProjectMasterDataAnalysisById(string Id)
{
return Funs.DB.Project_MasterDataAnalysis.FirstOrDefault(e => e.Id == Id);
}
///
/// 根据CollCropCode获取分析数据【取最近一条】
///
///
///
public static Model.Project_MasterDataAnalysis GetProjectMasterDataAnalysisByCollCropCode(string CollCropCode)
{
return Funs.DB.Project_MasterDataAnalysis.OrderByDescending(x => x.DataDate).FirstOrDefault(e => e.CollCropCode == CollCropCode);
}
///
/// 获取分析数据【取最近一条】
///
///
public static Model.Project_MasterDataAnalysis GetLastProjectMasterDataAnalysis()
{
return Funs.DB.Project_MasterDataAnalysis.OrderByDescending(x => x.DataDate).FirstOrDefault();
}
///
/// 添加分析数据
///
///
public static void AddProjectMasterDataAnalysis(Model.Project_MasterDataAnalysis model)
{
Model.SUBQHSEDB db = Funs.DB;
Model.Project_MasterDataAnalysis newModel = new Model.Project_MasterDataAnalysis
{
Id = model.Id,
CollCropCode = model.CollCropCode,
UnitId = model.UnitId,
ReportDate = model.ReportDate,
DataDate = model.DataDate,
Total = model.Total,
MasterTotal = model.MasterTotal,
QHSETotal = model.QHSETotal,
ZhengChang = model.ZhengChang,
DaiJian = model.DaiJian,
BuYong = model.BuYong,
YuLiXiang = model.YuLiXiang,
//ZiJian = model.ZiJian,
CreateUser = model.CreateUser,
};
db.Project_MasterDataAnalysis.InsertOnSubmit(newModel);
db.SubmitChanges();
}
///
/// 修改分析数据
///
///
public static void UpdateProjectMasterDataAnalysis(Model.Project_MasterDataAnalysis model)
{
Model.SUBQHSEDB db = Funs.DB;
Model.Project_MasterDataAnalysis newModel = db.Project_MasterDataAnalysis.FirstOrDefault(e => e.Id == model.Id);
if (newModel != null)
{
//newModel.Id = model.Id;
//newModel.CollCropCode = model.CollCropCode;
//newModel.UnitId = model.UnitId;
//newModel.ReportDate = model.ReportDate;
//newModel.DataDate = model.DataDate;
newModel.Total = model.Total;
newModel.MasterTotal = model.MasterTotal;
newModel.QHSETotal = model.QHSETotal;
newModel.ZhengChang = model.ZhengChang;
newModel.DaiJian = model.DaiJian;
newModel.BuYong = model.BuYong;
newModel.YuLiXiang = model.YuLiXiang;
//newModel.ZiJian = model.ZiJian;
db.SubmitChanges();
}
}
///
/// 根据主键删除分析数据
///
///
public static void DeleteProjectMasterDataAnalysisById(string Id)
{
Model.SUBQHSEDB db = Funs.DB;
Model.Project_MasterDataAnalysis model = db.Project_MasterDataAnalysis.FirstOrDefault(e => e.Id == Id);
if (model != null)
{
db.Project_MasterDataAnalysis.DeleteOnSubmit(model);
db.SubmitChanges();
}
}
///
/// 获取最近的汇总记录【近一小时】
///
///
public static List GetRecentHourAnalysisReportList()
{
var list = (from x in Funs.DB.Project_MasterDataAnalysis
where x.DataDate > DateTime.Now.AddHours(-1)
orderby x.DataDate descending
select x).ToList();
return list;
}
#region 汇总上报
///
/// 集团主数据在建项目使用情况上报
///
///
///
///
public static string ReportProjectMasterDataAnalysis(string userName,ref string message)
{
string code = "0";
using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString))
{
#region 汇总数据
//获取集团在建主数据
List masterProDatas = ProjectService.GetMasterProjectDataInfos();
//获取集团在建主数据
List projects = ProjectService.GetProjectWorkList();
int masterTotal = masterProDatas.Count();
int qhseTotal = projects.Count();
//1、正常
int zhengChang = (from x in masterProDatas
join pro in db.Base_Project on x.Pro_id equals pro.MasterSysId into proGroup
from pro in proGroup.DefaultIfEmpty()
where pro != null && pro.MasterSysId != null
//&& pro.ProjectState == Const.ProjectState_1
select x).Count();
//2、待建
int daiJian = (from x in masterProDatas
join pro in db.Base_Project on x.Pro_id equals pro.MasterSysId into proGroup
from pro in proGroup.DefaultIfEmpty()
join use in db.Project_MasterDataUsage on x.Pro_id equals use.Pro_id into useGroup
from use in useGroup.DefaultIfEmpty()
where pro == null && (use == null || (use != null && (use.Reason == null || use.Reason == "")))
select x).Count();
//3、申请不用
int buYong = (from x in masterProDatas
join pro in db.Base_Project on x.Pro_id equals pro.MasterSysId into proGroup
from pro in proGroup.DefaultIfEmpty()
join use in db.Project_MasterDataUsage on x.Pro_id equals use.Pro_id into useGroup
from use in useGroup.DefaultIfEmpty()
where pro == null && use != null && use.Reason != null && use.Reason != ""
select x).Count();
//4、预立项
int yuLiXiang = (from x in projects where x.MasterSysId == null select x).Count();
var thisUnit = CommonService.GetIsThisUnit();
var now = DateTime.Now;
//保存汇总数据
Model.Project_MasterDataAnalysis analysis = new Project_MasterDataAnalysis
{
Id = SQLHelper.GetNewID(typeof(Model.Project_MasterDataAnalysis)),
CollCropCode = thisUnit.CollCropCode,
UnitId = thisUnit.UnitId,
ReportDate = now.Date,
DataDate = now,
Total = zhengChang + daiJian + yuLiXiang,
MasterTotal = masterTotal,
QHSETotal = qhseTotal,
ZhengChang = zhengChang,
DaiJian = daiJian,
BuYong = buYong,
YuLiXiang = yuLiXiang,
CreateUser = userName
};
#endregion
#region 上报数据
string filePath = Funs.SGGLUrl;
try
{
//var upReport = GetLastProjectMasterDataAnalysis();
var upReport = analysis;
string baseurl = SysConstSetService.CNCECPath + "/api/Common/ProjectMasterDataAnalysis";
string contenttype = "application/json;charset=unicode";
Hashtable newToken = new Hashtable
{
{ "token", ServerService.GetToken().Token }
};
var pushContent = JsonConvert.SerializeObject(upReport);
//ErrLogInfo.WriteLog($"【集团主数据在建项目使用情况上报】:{pushContent}");
var strJosn = APIGetHttpService.Http(baseurl, "Post", contenttype, newToken, pushContent);
if (!string.IsNullOrEmpty(strJosn))
{
//ErrLogInfo.WriteLog($"【集团主数据在建项目使用情况上报】:{strJosn}");
JObject obj = JObject.Parse(strJosn);
code = obj["code"].ToString();
message = obj["message"].ToString();
if (code == "1")
{
AddProjectMasterDataAnalysis(upReport);
}
}
}
catch (Exception ex)
{
ErrLogInfo.WriteLog("【集团主数据在建项目使用情况上报】上传到服务器", ex);
}
#endregion
return code;
}
}
#endregion
}
}