ASCU_ALL/ApiServer/Controllers/DateController.cs
2024-09-22 04:27:05 +05:00

325 lines
8.8 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NLog;
using DataClient.Struct;
using System.IO;
using ApiServer.ApiStruct;
namespace ApiServer.Controllers
{
[ApiController, Route("[controller]")]
public class DateController : ControllerBase
{
private Logger log = LogManager.GetCurrentClassLogger();
[HttpPost, Route("getanalog")]
public DateGetAnalogClient GetAnalog([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<DateGetAnalogApi>(
value.ToString());
log.Info("Ask analog: vdp-" +
getResult.Vdp.ToString() + " index-" +
getResult.Index.ToString() + " " +
getResult.Start.ToString("yyyy-MM-dd HH:mm:ss") + " " +
getResult.End.ToString("yyyy-MM-dd HH:mm:ss"));
var list_analogs = new Dictionary<int, List<Structures.Analog>>();
var flagDate = new DateTime(
getResult.Start.Year,
getResult.Start.Month,
getResult.Start.Day,
0, 0, 0
);
var sub_res = new List<Structures.Analog>();
while (flagDate <= getResult.End)
{
var analogs = WorkWithFiles.GetAnalogs(flagDate, getResult.Vdp);
foreach (var analog_arr in analogs)
{
if (analog_arr.Index != getResult.Index)
continue;
Structures.Analog first_analog = null;
for (var i = 0; i < analog_arr.Analogs.Length; i++)
{
if (analog_arr.Analogs[i].start < getResult.Start)
{
first_analog = analog_arr.Analogs[i];
first_analog.start = getResult.Start;
continue;
}
if (analog_arr.Analogs[i].start > getResult.End)
continue;
if (sub_res.Count == 0 && first_analog != null)
sub_res.Add(first_analog);
sub_res.Add(analog_arr.Analogs[i]);
}
}
flagDate = flagDate.AddDays(1);
}
return new DateGetAnalogClient()
{
Analog = sub_res.ToArray()
};
}
catch (Exception e)
{
log.Warn(e);
throw;
//return new DateGetAnalogClient();
}
}
[HttpPost, Route("getanalogs")]
public DateGetAnalogsClient GetAnalogs([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<DateGetAnalogsApi>(
value.ToString());
log.Info("Ask analog: vdp-" +
getResult.Vdp.ToString() + " index-" +
getResult.Index.ToString() + " " +
getResult.Start.ToString("yyyy-MM-dd HH:mm:ss") + " " +
getResult.End.ToString("yyyy-MM-dd HH:mm:ss"));
var list_analogs = new Dictionary<int, List<Structures.Analog>>();
var flagDate = new DateTime(
getResult.Start.Year,
getResult.Start.Month,
getResult.Start.Day,
0, 0, 0
);
var sub_res = new Dictionary<int, List<Structures.Analog>>();
while (flagDate <= getResult.End)
{
var analogs = WorkWithFiles.GetAnalogs(flagDate, getResult.Vdp);
foreach (var analog_arr in analogs)
{
if (!getResult.Index.Contains(analog_arr.Index))
continue;
if (!sub_res.ContainsKey(analog_arr.Index))
sub_res.Add(analog_arr.Index, new List<Structures.Analog>());
Structures.Analog first_analog = null;
for (var i = 0; i < analog_arr.Analogs.Length; i++)
{
if (analog_arr.Analogs[i].start < getResult.Start)
{
first_analog = analog_arr.Analogs[i];
first_analog.start = getResult.Start;
continue;
}
if (analog_arr.Analogs[i].start > getResult.End)
continue;
if (sub_res[analog_arr.Index].Count == 0 && first_analog != null)
sub_res[analog_arr.Index].Add(first_analog);
sub_res[analog_arr.Index].Add(analog_arr.Analogs[i]);
}
}
flagDate = flagDate.AddDays(1);
}
var result = new List<Structures.AnalogArr>();
foreach (var d in sub_res)
result.Add(new Structures.AnalogArr() {
Index = d.Key,
Analogs = d.Value.ToArray()
});
return new DateGetAnalogsClient()
{
Analogs = result.ToArray()
};
}
catch (Exception e)
{
log.Warn(e);
return new DateGetAnalogsClient();
}
}
[HttpPost, Route("gettechcycle")]
public DateGetTechCycleClient GetTechCycle([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<DateGetTechCycleApi>(
value.ToString());
log.Info("Ask tech cycle: " +
getResult.Vdp.ToString() + " " +
getResult.Start.ToString("yyyy-MM-dd HH:mm:ss") + " " +
getResult.End.ToString("yyyy-MM-dd HH:mm:ss"));
var list_tech_cycles = new List<Structures.TechCycle>();
var flagDate = new DateTime(
getResult.Start.Year,
getResult.Start.Month,
getResult.Start.Day,
0, 0, 0
);
while (flagDate <= getResult.End)
{
var lines = WorkWithFiles.GetFile(
flagDate.Year.ToString("D4") +
flagDate.Month.ToString("D2") +
flagDate.Day.ToString("D2") +
"." +
getResult.Vdp.ToString("D2") +
"3"
);
Structures.TechCycle first_tech_cycle = null;
for (var i = 0; i < lines.Length; i++)
{
var tc = new Structures.TechCycle(flagDate, lines[i]);
if (tc.start < getResult.Start)
{
first_tech_cycle = tc;
continue;
}
if (tc.start > getResult.End)
continue;
if (list_tech_cycles.Count == 0 && first_tech_cycle != null)
{
first_tech_cycle.start = getResult.Start;
list_tech_cycles.Add(first_tech_cycle);
}
list_tech_cycles.Add(tc);
}
flagDate = flagDate.AddDays(1);
}
return new DateGetTechCycleClient()
{
TechCycle = list_tech_cycles.ToArray(),
};
}
catch (Exception e)
{
log.Warn(e);
return new DateGetTechCycleClient();
}
}
[HttpPost, Route("getprotect")]
public DateGetProtectClient GetProtect([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<DateGetProtectApi>(
value.ToString());
log.Info("Ask protects: " +
getResult.Vdp.ToString() + " " +
getResult.Start.ToString("yyyy-MM-dd HH:mm:ss") + " " +
getResult.End.ToString("yyyy-MM-dd HH:mm:ss"));
var list_protects = new List<Structures.Protect>();
var flagDate = new DateTime(
getResult.Start.Year,
getResult.Start.Month,
getResult.Start.Day,
0, 0, 0
);
while (flagDate <= getResult.End)
{
var protects = WorkWithFiles.GetProtects(flagDate, getResult.Vdp);
for (var i = 0; i < protects.Length; i++)
{
if (protects[i].start < getResult.Start)
continue;
if (protects[i].start > getResult.End)
continue;
list_protects.Add(protects[i]);
}
flagDate = flagDate.AddDays(1);
}
return new DateGetProtectClient()
{
Protect = list_protects.ToArray()
};
}
catch (Exception e)
{
log.Warn(e);
return new DateGetProtectClient();
}
}
[HttpPost, Route("getdiscret")]
public DateGetDiscretClient GetDiscret([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<DateGetDiscretApi>(
value.ToString());
log.Info("Ask discrets: " +
getResult.Vdp.ToString() + " " +
getResult.Start.ToString("yyyy-MM-dd HH:mm:ss") + " " +
getResult.End.ToString("yyyy-MM-dd HH:mm:ss"));
var list_discrets = new List<Structures.Discret>();
var flagDate = new DateTime(
getResult.Start.Year,
getResult.Start.Month,
getResult.Start.Day,
0, 0, 0
);
while (flagDate <= getResult.End)
{
var discrets = WorkWithFiles.GetDiscrets(flagDate, getResult.Vdp);
var first_discrets = new Dictionary<int, Structures.Discret>();
for (var i = 0; i < discrets.Length; i++)
{
if (discrets[i].start < getResult.Start)
{
if (first_discrets.ContainsKey(discrets[i].Index))
first_discrets[discrets[i].Index] = discrets[i];
else
first_discrets.Add(discrets[i].Index, discrets[i]);
first_discrets[discrets[i].Index].start = getResult.Start;
continue;
}
if (discrets[i].start > getResult.End)
continue;
var last_id = list_discrets.FindLastIndex(x => x.Index == discrets[i].Index);
if (first_discrets.ContainsKey(discrets[i].Index) &&
last_id == -1 &&
discrets[i].start != getResult.Start)
{
list_discrets.Add(first_discrets[discrets[i].Index]);
first_discrets.Remove(discrets[i].Index);
}
if (last_id != -1 &&
list_discrets[last_id].State == discrets[i].State)
continue;
list_discrets.Add(discrets[i]);
}
flagDate = flagDate.AddDays(1);
}
return new DateGetDiscretClient()
{
Discret = list_discrets.ToArray()
};
}
catch (Exception e)
{
log.Warn(e);
return new DateGetDiscretClient();
}
}
}
}