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, YiShangXian = model.YiShangXian, OnlineRate = model.OnlineRate, ReportDate = model.ReportDate, DataDate = model.DataDate, Total = model.Total, MasterTotal = model.MasterTotal, QHSETotal = model.QHSETotal, ZhengChang = model.ZhengChang, DaiJian = model.DaiJian, BuYong = model.BuYong, BuYongYiWanGong = model.BuYongYiWanGong, BuYongTingHuanJian = model.BuYongTingHuanJian, BuYongTJShouXian = model.BuYongTJShouXian, YuLiXiang = model.YuLiXiang, //ZiJian = model.ZiJian, CreateUser = model.CreateUser, ThisMonActiveUserNum = model.ThisMonActiveUserNum, DailyAvg = model.DailyAvg, TianBao = model.TianBao, }; 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.YiShangXian = model.YiShangXian; newModel.OnlineRate = model.OnlineRate; 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.BuYongYiWanGong = model.BuYongYiWanGong; newModel.BuYongTingHuanJian = model.BuYongTingHuanJian; newModel.BuYongTJShouXian = model.BuYongTJShouXian; newModel.YuLiXiang = model.YuLiXiang; //newModel.ZiJian = model.ZiJian; newModel.ThisMonActiveUserNum = model.ThisMonActiveUserNum; newModel.DailyAvg = model.DailyAvg; newModel.TianBao = model.TianBao; 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、申请不用 var buYongList = (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.Is_use == false && use.Reason != null && use.Reason != "" select new { reason = use.Reason, reasonType = use.ReasonType }).ToList(); //申请不用总数 int buYong = buYongList.Count(); //申请不用——已完工 int buYongYiWanGong = buYongList.Where(x => x.reasonType == "已完工").Count(); //申请不用——停缓建 int buYongTingHuanJian = buYongList.Where(x => x.reasonType == "停缓建").Count(); //申请不用——条件受限(其他) int buYongTJShouXian = buYong - buYongYiWanGong - buYongTingHuanJian; //4、预立项 int yuLiXiang = (from x in projects where x.MasterSysId == null select x).Count(); //应使用项目数=主数据项目+预立项-已完工-停缓建 int beUsedProNum = masterTotal + yuLiXiang - buYongYiWanGong - buYongTingHuanJian; //已上线项目=已关联+预立项; int yiShangXian = zhengChang + yuLiXiang; //上线率=100*已上线/应使用项目数 var onlineRate = (decimal)Math.Round((100.0 * yiShangXian / beUsedProNum), 2); onlineRate = onlineRate > 100 ? 100 : onlineRate; var now = DateTime.Now; #region 统计系统近三个月用户使用情况、日活 ////本月第一天 //var firstDayOfMonth = new DateTime(now.Year, now.Month, 1); //本月第一天 var firstDayOfMonth = now.AddMonths(-3); //本月系统活跃用户 var thisMonActiveUserList = (from x in db.Sys_Log where x.OperationTime > firstDayOfMonth select new { DateStr = ((DateTime)x.OperationTime).ToShortDateString(), UserId = x.UserId }).ToList(); //var ssss = JsonConvert.SerializeObject(thisMonActiveUserList); int thisMonActiveUserNum = thisMonActiveUserList.Distinct().Count(); var dailyAvg = (decimal)Math.Round((1.0 * thisMonActiveUserNum / 30), 2); #endregion var thisUnit = CommonService.GetIsThisUnit(); //保存汇总数据 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, YiShangXian= yiShangXian, OnlineRate = onlineRate, Total = beUsedProNum, MasterTotal = masterTotal, QHSETotal = qhseTotal, ZhengChang = zhengChang, DaiJian = daiJian, BuYong = buYong, BuYongYiWanGong = buYongYiWanGong, BuYongTingHuanJian = buYongTingHuanJian, BuYongTJShouXian = buYongTJShouXian, YuLiXiang = yuLiXiang, CreateUser = userName, ThisMonActiveUserNum = thisMonActiveUserNum, DailyAvg = dailyAvg, }; #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 } }