134 lines
6.0 KiB
C#
134 lines
6.0 KiB
C#
using DataServer.api;
|
|
using Microsoft.Ajax.Utilities;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Web.Http;
|
|
|
|
namespace JinanCementFactoryAPI.Controllers.api
|
|
{
|
|
/// <summary>
|
|
/// 设备异常统计
|
|
/// </summary>
|
|
public class GetUnitExceptionController : ApiController
|
|
{
|
|
DataServer.BLL.electrical_installation bll = new DataServer.BLL.electrical_installation();
|
|
DataServer.BLL.gw_data bll_gw = new DataServer.BLL.gw_data();
|
|
// GET api/<controller>
|
|
public HttpResponseMessage Get()
|
|
{
|
|
var res = new get_unit_exception();
|
|
try
|
|
{
|
|
var list = bll.GetModelList("");
|
|
var alist=new List<unit_exceptionData>();
|
|
var now=DateTime.Now;
|
|
for (var i = 0; i < 6; i++)
|
|
{
|
|
var sdate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00");
|
|
var edate = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00");
|
|
var jdate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00");
|
|
var zdate = now.AddMonths(-(i + 1)).ToString("yyyy-MM-01 00:00:00");
|
|
if(Convert.ToDateTime(sdate)>=Convert.ToDateTime("2024-03-01 00:00:00"))
|
|
{
|
|
var amodel = new unit_exceptionData();
|
|
amodel.UnitTime = now.AddMonths(-i).ToString("yyyy-MM-dd");
|
|
var alist1 = new List<unit_exception>();
|
|
var bmodel = new unit_exception();
|
|
string stime;
|
|
if (Convert.ToDateTime(sdate).Month < 10)
|
|
{
|
|
stime = Convert.ToDateTime(sdate).ToString("yyyy_M");
|
|
}
|
|
else
|
|
{
|
|
stime = Convert.ToDateTime(sdate).ToString("yyyy_MM");
|
|
}
|
|
string etime;
|
|
if (Convert.ToDateTime(edate).Month < 10)
|
|
{
|
|
etime = Convert.ToDateTime(edate).ToString("yyyy-M");
|
|
}
|
|
else
|
|
{
|
|
etime = Convert.ToDateTime(edate).ToString("yyyy-MM");
|
|
}
|
|
string jtime;
|
|
if (Convert.ToDateTime(jdate).Month < 10)
|
|
{
|
|
jtime = Convert.ToDateTime(jdate).ToString("yyyy_M");
|
|
}
|
|
else
|
|
{
|
|
jtime = Convert.ToDateTime(jdate).ToString("yyyy_MM");
|
|
}
|
|
int count1 = 0;
|
|
int count2 = 0;
|
|
int count3 = 0;
|
|
|
|
var EHs = string.Join(",", list.Select(x => x.EH));
|
|
var Ps = string.Join(",", list.Select(x => x.P));
|
|
var blist1 = new List<DataServer.Model.gw_data>();
|
|
if (i == 0)
|
|
{
|
|
blist1 = bll_gw.GetModelListsDate(EHs, jtime).GroupBy(x => x.XTagName).Select(g => g.OrderByDescending(x => x.XTimeStamp).FirstOrDefault()).ToList();
|
|
// blist1 = bll_gw.GetModelListDate(" XTagName like '%" + item.EH + "'", jtime).OrderByDescending(x => x.XTimeStamp).FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
blist1 = bll_gw.GetModelListsDate(EHs, jtime).Where(x => x.XTimeStamp == Convert.ToDateTime(jdate)).GroupBy(x => x.XTagName).Select(g => g.OrderByDescending(x => x.XTimeStamp).FirstOrDefault()).ToList();
|
|
}
|
|
var blist2 = bll_gw.GetModelListsDate(Ps, stime).Where(x => x.XTimeStamp >= Convert.ToDateTime(sdate) && x.XTimeStamp < Convert.ToDateTime(edate)).DistinctBy(x => new { x.XTagName, x.XTimeStamp }).ToList();
|
|
if (blist1 != null && blist2 != null)
|
|
{
|
|
count1++;
|
|
}
|
|
if (blist1 == null && blist2 != null || blist1 != null && blist2 == null)
|
|
{
|
|
count2++;
|
|
}
|
|
if (blist1 == null && blist2 == null)
|
|
{
|
|
count3++;
|
|
}
|
|
bmodel.Normal = count1;
|
|
bmodel.Early = count2;
|
|
bmodel.Malfunction = count3;
|
|
alist1.Add(bmodel);
|
|
amodel.data = alist1;
|
|
alist.Add(amodel);
|
|
}
|
|
else
|
|
{
|
|
var amodel = new unit_exceptionData();
|
|
amodel.UnitTime = now.AddMonths(-i).ToString("yyyy-MM-dd");
|
|
var alist1 = new List<unit_exception>();
|
|
var bmodel = new unit_exception();
|
|
bmodel.Normal = 0;
|
|
bmodel.Early = 0;
|
|
bmodel.Malfunction = 0;
|
|
alist1.Add(bmodel);
|
|
amodel.data = alist1;
|
|
alist.Add(amodel);
|
|
}
|
|
|
|
}
|
|
res.code = 200;
|
|
res.msg = "成功";
|
|
res.data = alist;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
res.code = 500;
|
|
res.msg = ex.Message;
|
|
}
|
|
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(res), Encoding.GetEncoding("UTF-8"), "application/json") };
|
|
return result;
|
|
}
|
|
}
|
|
} |