ASCU_ALL/ApiServer/Controllers/PasportController.cs

151 lines
4.4 KiB
C#
Raw Normal View History

2021-07-31 22:27:59 +05:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft;
2021-07-30 14:49:24 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2021-07-31 22:27:59 +05:00
using Newtonsoft.Json;
using NLog;
using DataClient.Struct;
using System.IO;
using ApiServer.ApiStruct;
2021-07-30 14:49:24 +05:00
namespace ApiServer.Controllers
{
2021-07-31 22:27:59 +05:00
[ApiController, Route("[controller]")]
2021-07-30 14:49:24 +05:00
public class PasportController : ControllerBase
{
2021-07-31 22:27:59 +05:00
private Logger log = LogManager.GetCurrentClassLogger();
2021-08-01 19:48:09 +05:00
2021-07-31 22:27:59 +05:00
[HttpPost, Route("check")]
2021-08-01 19:48:09 +05:00
public PasportCheckClient Check([FromBody] object value)
2021-07-30 14:49:24 +05:00
{
2021-07-31 22:27:59 +05:00
try
{
2021-08-01 19:48:09 +05:00
var getResult = JsonConvert.DeserializeObject<PasportCheckApi>(value.ToString());
if (!getResult.Status || string.IsNullOrEmpty(getResult.Name))
{
log.Warn("Wrong answer.");
return new PasportCheckClient();
}
2021-07-31 22:27:59 +05:00
var paspName =
2021-08-01 19:48:09 +05:00
getResult.DateAndTime.Hour.ToString("D2") +
getResult.DateAndTime.Minute.ToString("D2") +
getResult.DateAndTime.Second.ToString("D2") +
"-" + getResult.Name;
2021-07-31 22:27:59 +05:00
var paspDir = Path.Combine(
Directory.GetCurrentDirectory(),
"data", "pasport",
2021-08-01 19:48:09 +05:00
getResult.DateAndTime.Year.ToString("D4"),
getResult.DateAndTime.Month.ToString("D2"),
getResult.DateAndTime.Day.ToString("D2"),
2021-07-31 22:27:59 +05:00
paspName);
2021-08-01 19:48:09 +05:00
log.Info("Search pasport: " + paspDir);
2021-07-31 22:27:59 +05:00
if (!System.IO.File.Exists(paspDir))
2021-08-01 19:48:09 +05:00
{
log.Info("Psport not exist: " + paspDir);
return new PasportCheckClient { Status = true, Exist = false, PaspSum = 0 };
}
var pasport = System.IO.File.ReadAllBytes(paspDir);
ulong paspResult = 0;
foreach (var b in pasport)
paspResult += b;
log.Info("Send pasport size: " + paspResult + " | " + paspDir);
return new PasportCheckClient { Status = true, Exist = true, PaspSum = paspResult };
}
catch(Exception e)
{
log.Warn(e);
return new PasportCheckClient();
2021-07-31 22:27:59 +05:00
}
2021-07-30 14:49:24 +05:00
}
2021-07-31 22:27:59 +05:00
[HttpPost, Route("create")]
2021-08-01 19:48:09 +05:00
public PasportCreateClient Create([FromBody] object value)
2021-07-31 22:27:59 +05:00
{
try
{
2021-08-01 19:48:09 +05:00
var getResult = JsonConvert.DeserializeObject<PasportCreateApi>(value.ToString());
if (!getResult.Status ||
getResult.Pasp == null ||
!getResult.Pasp.HasData)
{
log.Warn("Wrong answer.");
return new PasportCreateClient();
}
2021-07-31 22:27:59 +05:00
var paspName =
2021-08-01 19:48:09 +05:00
getResult.Pasp.dEnd.Value.Hour.ToString("D2") +
getResult.Pasp.dEnd.Value.Minute.ToString("D2") +
getResult.Pasp.dEnd.Value.Second.ToString("D2") +
"-" + (string.IsNullOrEmpty(getResult.Pasp.nplav) ?
getResult.Pasp.numVDP.Value.ToString("D2") :
getResult.Pasp.nplav);
2021-07-31 22:27:59 +05:00
var dir = Path.Combine(
Directory.GetCurrentDirectory(),
"data", "pasport",
2021-08-01 19:48:09 +05:00
getResult.Pasp.dEnd.Value.Year.ToString("D4"),
getResult.Pasp.dEnd.Value.Month.ToString("D2"),
getResult.Pasp.dEnd.Value.Day.ToString("D2"));
2021-07-31 22:27:59 +05:00
var paspDir = Path.Combine(dir, paspName);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
2021-08-01 19:48:09 +05:00
System.IO.File.WriteAllBytes(paspDir, getResult.Pasp.PaspByte);
log.Info("Save pasport: " + paspDir);
return new PasportCreateClient { Status = true };
}
catch (Exception e)
{
log.Warn(e);
return new PasportCreateClient();
2021-07-31 22:27:59 +05:00
}
}
2021-11-12 15:00:41 +05:00
[HttpPost, Route("getlist")]
public PasportGetListClient GetList([FromBody] object value)
{
try
{
var pasportNameList = new List<string>();
var getResult = JsonConvert.DeserializeObject<PasportGetListApi>(value.ToString());
var pasportDir = Path.Combine(
Directory.GetCurrentDirectory(),
"data",
"pasport",
getResult.Date.Year.ToString("D4"),
getResult.Date.ToString("D2"),
getResult.Date.Day.ToString("D2")
);
if (!Directory.Exists(pasportDir))
return new PasportGetListClient();
var allPasportFiles = Directory.GetFiles(pasportDir);
foreach (var fullPasportName in allPasportFiles)
{
var name = Path.GetFileNameWithoutExtension(fullPasportName);
var pasportParts = name.Split('-');
var pasport = "";
for (var i = 1; i < pasportParts.Length; i++)
{
if (pasport.Length != 0)
pasport += "-";
pasport += pasportParts[i];
}
pasportNameList.Add(pasport);
}
log.Info("Send pasport list: " + pasportNameList);
return new PasportGetListClient() { Name = pasportNameList.ToArray() };
}
catch (Exception e)
{
log.Warn(e);
return new PasportGetListClient();
}
}
2021-07-31 22:27:59 +05:00
2021-07-30 14:49:24 +05:00
}
2021-07-31 22:27:59 +05:00
2021-07-30 14:49:24 +05:00
}