300 lines
9.2 KiB
C#
300 lines
9.2 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Formatters;
|
|
using Newtonsoft.Json;
|
|
using ServiceSourceClientApp.Models;
|
|
using ServiceSourceExecutorApp;
|
|
using ServiceStationContracts.BindingModels;
|
|
using ServiceStationContracts.HelperModels;
|
|
using ServiceStationContracts.ViewModels;
|
|
using System.Diagnostics;
|
|
|
|
namespace ServiceSourceClientApp.Controllers {
|
|
public class HomeController : Controller {
|
|
private readonly ILogger<HomeController> _logger;
|
|
|
|
public HomeController(ILogger<HomeController> logger) {
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Index() {
|
|
if (APIClient.executor == null) {
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
|
|
var view = APIClient.GetRequest<List<WorkViewModel>>($"api/Main/GetWorks?_executorId={APIClient.executor.Id}");
|
|
return View(view);
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Enter() {
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Enter(string email, string password) {
|
|
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) {
|
|
throw new Exception("Ââåäèòå ïî÷òó è ïàðîëü");
|
|
}
|
|
APIClient.executor = APIClient.GetRequest<ExecutorViewModel>($"api/main/Login?email={email}&password={password}");
|
|
if (APIClient.executor == null) {
|
|
throw new Exception("Íåâåðíûé àäðåñ ïî÷òû èëè ïàðîëü");
|
|
}
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Register() {
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Register(string email, string password, string fio) {
|
|
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) {
|
|
throw new Exception("Ââåäèòå ïî÷òó, ïàðîëü è ÔÈÎ");
|
|
}
|
|
APIClient.PostRequest("api/Main/Register", new ExecutorBindingModel {
|
|
Email = email,
|
|
Password = password,
|
|
FIO = fio
|
|
});
|
|
Response.Redirect("Enter");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Privacy() {
|
|
if (APIClient.executor == null) {
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIClient.executor);
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Privacy(string email, string password, string fio) {
|
|
if (APIClient.executor == null) {
|
|
Response.Redirect("~/Home/Enter");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) {
|
|
throw new Exception("Ââåäèòå ïî÷òó, ïàðîëü è ÔÈÎ");
|
|
}
|
|
APIClient.PostRequest("api/Main/UpdateData", new ExecutorBindingModel {
|
|
Id = APIClient.executor.Id,
|
|
Email = email,
|
|
Password = password,
|
|
FIO = fio
|
|
});
|
|
|
|
APIClient.executor.FIO = fio;
|
|
APIClient.executor.Email = email;
|
|
APIClient.executor.Password = password;
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Error() {
|
|
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult CreateWork() {
|
|
ViewBag.Tasks = APIClient.GetRequest<List<TaskViewModel>>($"api/Main/GetTasks?");
|
|
return View(APIClient.GetRequest<List<ClientViewModel>>($"api/Main/GetClients?"));
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateWork(int[] ids, int task) {
|
|
if (APIClient.executor == null) {
|
|
Response.Redirect("~/Home/Enter");
|
|
return;
|
|
}
|
|
APIClient.PostRequest("api/Main/CreateWork", new WorkFromWebBindingModel {
|
|
Price = Calc(task, ids.Length),
|
|
ExecutorId = APIClient.executor.Id,
|
|
TaskId = task,
|
|
client_ids = ids
|
|
});
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult EditWork(int Id) {
|
|
if (Id == 0) {
|
|
return RedirectToAction("Index");
|
|
}
|
|
var work_info = APIClient.GetRequest<List<List<string>>>($"api/Main/GetWork?_workId={Id}")
|
|
?? throw new Exception("Îøèáêà ïîëó÷åíèÿ äàííûõ");
|
|
|
|
ViewBag.Tasks = APIClient.GetRequest<List<TaskViewModel>>($"api/Main/GetTasks?");
|
|
|
|
int task_load_id = JsonConvert.DeserializeObject<int>(work_info[0][0]);
|
|
var task_load = APIClient.GetRequest<TaskViewModel>($"api/Main/GetTask?_taskId={task_load_id}")
|
|
?? throw new Exception("Îøèáêà ïîëó÷åíèÿ äàííûõ");
|
|
if (ViewBag.Tasks[0].Name != task_load.Name) {
|
|
int index = 0;
|
|
for (int i = 0; i < ViewBag.Tasks.Count; i++) {
|
|
if (ViewBag.Tasks[i].Name == task_load.Name) {
|
|
index = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
var tmp = ViewBag.Tasks[0];
|
|
ViewBag.Tasks[0] = task_load;
|
|
ViewBag.Tasks[index] = tmp;
|
|
}
|
|
|
|
List<ClientViewModel> clients_list = new();
|
|
|
|
|
|
foreach (var rec in work_info[1]) {
|
|
var client = JsonConvert.DeserializeObject<ClientViewModel>(rec) ?? throw new Exception("Îøèáêà äåñåðèàëèçàöèè");
|
|
clients_list.Add(client);
|
|
}
|
|
|
|
var clients = APIClient.GetRequest<List<ClientViewModel>>($"api/Main/GetClients?")
|
|
?? throw new Exception("Îøèáêà ïîëó÷åíèÿ äàííûõ");
|
|
IEnumerable<ClientViewModel> result = clients.Except(clients_list);
|
|
|
|
List<ClientViewModel> new_client = new();
|
|
foreach (var client in result) {
|
|
new_client.Add(client);
|
|
}
|
|
|
|
(int, List<ClientViewModel>, List<ClientViewModel>) tuple = (Id, clients_list, new_client);
|
|
return View(tuple);
|
|
}
|
|
|
|
[HttpPost]
|
|
public void EditWork(int[] ids, int task, int id) {
|
|
if (APIClient.executor == null) {
|
|
Response.Redirect("~/Home/Enter");
|
|
return;
|
|
}
|
|
|
|
APIClient.PostRequest("api/Main/UpdateWork", new WorkFromWebBindingModel {
|
|
Id = id,
|
|
Price = Calc(task, ids.Length),
|
|
ExecutorId = APIClient.executor.Id,
|
|
TaskId = task,
|
|
client_ids = ids
|
|
});
|
|
}
|
|
|
|
[HttpPost]
|
|
public double Calc(int task, int count) {
|
|
var task_info = APIClient.GetRequest<TaskViewModel>($"api/Main/GetTask?_taskId={task}") ?? throw new Exception("Îøèáêà ïîëó÷åèÿ äàííûõ");
|
|
return task_info.Price + 700 * count;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult DeleteWork(int id) {
|
|
APIClient.PostRequest("api/Main/DeleteWork", new WorkBindingModel {
|
|
Id = id,
|
|
});
|
|
return RedirectToAction("Index");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Scoring () {
|
|
if (APIClient.executor == null) {
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Scoring(int client, int work, int points) {
|
|
APIClient.PostRequest("api/Main/AddClientPoints", new WorkClientModel {
|
|
Client_Id = client,
|
|
Work_Id = work,
|
|
Point_count = points
|
|
});
|
|
Response.Redirect("Scoring");
|
|
}
|
|
|
|
public JsonResult Client() {
|
|
var clients = APIClient.GetRequest<List<ClientViewModel>>($"api/Main/GetClients?") ?? throw new Exception("Îøèáêà ïîëó÷åíèÿ äàííûõ");
|
|
return new JsonResult(clients);
|
|
}
|
|
|
|
public JsonResult Work(int id) {
|
|
var work_clients_list = APIClient.GetRequest<List<WorkClientModel>>($"api/Main/GetWorkClients?_clientId={id}")
|
|
?? throw new Exception("Îøèáêà ïîëóåíèÿ äàííûõ");
|
|
List<WorkViewModel> works = new();
|
|
foreach (var record in work_clients_list) {
|
|
int work_id = record.Work_Id;
|
|
var work = APIClient.GetRequest<WorkViewModel>($"api/Main/GetWorkView?_workId={work_id}")
|
|
?? throw new Exception("Îøèáêà ïîëó÷åíèÿ äàííûõ");
|
|
works.Add(work);
|
|
}
|
|
return new JsonResult(works);
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Report() {
|
|
if (APIClient.executor == null) {
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
var view = APIClient.GetRequest<List<WorkViewModel>>($"api/Main/GetWorks?_executorId={APIClient.executor.Id}");
|
|
return View(view);
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Report(DateTime DateFrom, DateTime DateTo) {
|
|
if (DateTo == DateTime.MinValue || DateFrom > DateTo) {
|
|
throw new Exception("Íåêîðåêòíî óêàçàí âðåìåííîé èíòåðâàë");
|
|
}
|
|
Response.Redirect($"ReportSearchDate?_datefrom={DateFrom}&_dateto={DateTo + DateTime.Now.TimeOfDay}");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult ReportSearchDate(string _datefrom, string _dateto) {
|
|
var reports = APIClient.GetRequest<List<WorkViewModel>>($"api/Main/GerWorksDate?_start={_datefrom}&_end={_dateto}");
|
|
(DateTime, DateTime, List<WorkViewModel>?) tuple = (DateTime.Parse(_datefrom), DateTime.Parse(_dateto), reports);
|
|
return View(tuple);
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult CreatePdfReport(string DateFrom, string DateTo) {
|
|
APIClient.PostRequest("api/Main/SendReportPdfMail", new ReportBindeingModel {
|
|
EmailAddress = APIClient.executor?.Email ?? throw new Exception("Îøèáêà ïîëó÷åíèÿ àäðåñà"),
|
|
DateFrom = DateTime.Parse(DateFrom),
|
|
DateTo = DateTime.Parse(DateTo)
|
|
});
|
|
return Redirect("Report");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult CreateWordReport(string ids) {
|
|
|
|
if (string.IsNullOrEmpty(ids)) {
|
|
throw new Exception("Íåò âûáðàíûõ çàïèñåé");
|
|
}
|
|
|
|
|
|
var fileMemStream = APIClient.GetRequest<byte[]>($"api/Main/CreateWordReport?_ids={ids}");
|
|
|
|
if (fileMemStream == null) {
|
|
throw new Exception("Îøèáêà ñîçäàíèÿ îò÷åòà");
|
|
}
|
|
|
|
return File(fileMemStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Report.docx");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult CreateExcelReport(string ids) {
|
|
|
|
if (string.IsNullOrEmpty(ids)) {
|
|
throw new Exception("Íåò âûáðàíûõ çàïèñåé");
|
|
}
|
|
|
|
var fileMemStream = APIClient.GetRequest<byte[]>($"api/Main/CreateXlsxReport?_ids={ids}");
|
|
|
|
if (fileMemStream == null) {
|
|
throw new Exception("Îøèáêà ñîçäàíèÿ îò÷åòà");
|
|
}
|
|
|
|
return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx");
|
|
}
|
|
}
|
|
} |