Files
Backend-Api/ServiceHost/Areas/Camera/Controllers/CameraController.cs

267 lines
8.9 KiB
C#

using ServiceHost.BaseControllers;
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using _0_Framework.Application;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.CameraAccount;
using AccountManagement.Domain.TaskAgg;
using CompanyManagment.App.Contracts.PersonnleCode;
using CompanyManagment.App.Contracts.RollCall;
using CompanyManagment.App.Contracts.RollCallEmployee;
using CompanyManagment.App.Contracts.RollCallService;
using CompanyManagment.App.Contracts.Employee;
using CompanyManagment.App.Contracts.EmployeeFaceEmbedding;
using Microsoft.AspNetCore.Authorization;
namespace ServiceHost.Areas.Camera.Controllers;
public class CameraController : CameraBaseController
{
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IConfiguration _configuration;
private readonly IEmployeeApplication _employeeApplication;
private readonly IRollCallApplication _rollCallApplication;
private readonly IRollCallServiceApplication _rollCallServiceApplication;
private readonly IRollCallEmployeeApplication _rollCallEmployeeApplication;
private readonly IAuthHelper _authHelper;
private readonly IPersonnelCodeApplication _personnelCodeApplication;
private readonly IAccountApplication _accountApplication;
private readonly IPasswordHasher _passwordHasher;
private readonly ICameraAccountApplication _cameraAccountApplication;
private readonly IEmployeeFaceEmbeddingApplication _employeeFaceEmbeddingApplication;
public CameraController(IWebHostEnvironment webHostEnvironment,
IConfiguration configuration,
IEmployeeApplication employeeApplication,
IRollCallApplication rollCallApplication,
IAuthHelper authHelper,
IRollCallServiceApplication rollCallServiceApplication,
IRollCallEmployeeApplication rollCallEmployeeApplication,
IPersonnelCodeApplication personnelCodeApplication,
IAccountApplication accountApplication,
IPasswordHasher passwordHasher,
ICameraAccountApplication cameraAccountApplication,
IEmployeeFaceEmbeddingApplication employeeFaceEmbeddingApplication)
{
_webHostEnvironment = webHostEnvironment;
_configuration = configuration;
_employeeApplication = employeeApplication;
_rollCallApplication = rollCallApplication;
_authHelper = authHelper;
_rollCallServiceApplication = rollCallServiceApplication;
_rollCallEmployeeApplication = rollCallEmployeeApplication;
_personnelCodeApplication = personnelCodeApplication;
_accountApplication = accountApplication;
_passwordHasher = passwordHasher;
_cameraAccountApplication = cameraAccountApplication;
_employeeFaceEmbeddingApplication = employeeFaceEmbeddingApplication;
}
[HttpPost("login")]
[AllowAnonymous]
public IActionResult CameraLogin([FromBody] CameraLoginRequest request)
{
_accountApplication.CameraLogin(request);
return Ok();
}
[HttpPost("embedding")]
[AllowAnonymous]
public async Task<ActionResult<List<EmployeeFaceEmbeddingDto>>> WorkshopEmbedding([FromBody] long[] workshopIds)
{
if (!User.Identity?.IsAuthenticated ?? false)
{
return Unauthorized();
}
var data = await _employeeFaceEmbeddingApplication
.GetByWorkshopIdsAsync(workshopIds.ToList());
return data;
}
[HttpGet("SendPersonelCodeToGetEmployeeId")]
public IActionResult SendPersonelCodeToGetEmployeeId(long personelCode, long workshopId)
{
long employeeId = _personnelCodeApplication.GetEmployeeIdByPersonelCode(personelCode, workshopId);
if (employeeId == 0)
{
return new JsonResult(new
{
isSuccess = false,
message = "کد پرسنلی یافت نشد",
});
}
bool rollcallEmployee = _rollCallEmployeeApplication.IsEmployeeRollCallActive(employeeId, workshopId);
if (!rollcallEmployee)
{
return new JsonResult(new
{
isSuccess = false,
message = "پرسنل مورد نظر غیر فعال است",
});
}
var repeatStatus = _rollCallApplication.CheckRepeat(employeeId, workshopId);
switch (repeatStatus)
{
case "IncomRegistred-InvalidOut":
return new JsonResult(new
{
isSuccess = false,
message = "شما به تازگی ثبت ورود نموده اید ,تا 2 دقیقه نمی توانید ثبت خروج نمایید",
});
case "outRegistred-InvalidIncom":
return new JsonResult(new
{
isSuccess = false,
message = "شما به تازگی ثبت خروج نموده اید تا 2 دقیقه نمی توانید ثبت ورود نمایید",
});
}
return new JsonResult(new
{
isSuccess = true,
message = "",
personId = $"{employeeId}",
});
}
[HttpGet("EmployeeFlag")]
public IActionResult EmployeeFlag(long employeeId, long workshopId)
{
var employee = _rollCallEmployeeApplication.GetByEmployeeIdAndWorkshopId(employeeId, workshopId);
var employeeFullName = employee?.EmployeeFullName ?? string.Empty;
var flagId = _rollCallApplication.Flag(employeeId, workshopId);
return new JsonResult(new
{
employeeName = employeeFullName,
flag = flagId
});
}
[HttpGet("Out")]
public IActionResult Out(long flagId)
{
var res = _rollCallApplication.Edit(flagId);
if (res.IsSuccedded)
{
return new JsonResult(new
{
isSuccess = true,
});
}
else
{
return new JsonResult(new
{
isSuccess = false,
});
}
}
[HttpGet("InCom")]
public IActionResult InCom(long employeeId, long workshopId)
{
var now = DateTime.Now;
var command = new CreateRollCall()
{
WorkshopId = workshopId,
EmployeeId = employeeId,
StartDate = now,
};
var res = _rollCallApplication.Create(command);
if (res.IsSuccedded)
{
return new JsonResult(new
{
isSuccess = true,
});
}
else
{
return new JsonResult(new
{
isSuccess = false,
});
}
}
[HttpGet("ShowPicture")]
public IActionResult ShowPicture(int index, long workshopId, long label)
{
var filePath = Path.Combine(_webHostEnvironment.ContentRootPath, "Faces", workshopId.ToString(),
label.ToString(), $"{index}.jpg");
if (!System.IO.File.Exists(filePath))
{
return NotFound();
}
var contentType = "image/jpeg";
return PhysicalFile(filePath, contentType);
}
[HttpGet("PersonnelWorkshopAjax")]
public IActionResult PersonnelWorkshopAjax()
{
var cameraAccount = _authHelper.CameraAccountInfo();
if (cameraAccount.WorkshopId > 0)
{
var result = _rollCallEmployeeApplication.GetActivePersonnelByWorkshopId(cameraAccount.WorkshopId);
if (cameraAccount.WorkshopId == 170)
result = result.Select(x =>
{
if (x.PersonelCode == 477)
x.EmployeeLName = "نوزاد";
return x;
}).ToList();
return new JsonResult(result);
}
else
{
return new JsonResult(new
{
isSuccess = false,
message = "کارگاه ای یافت نشد!"
});
}
}
[HttpPost("CheckPassword")]
public IActionResult CheckPassword([FromForm] string password)
{
var cameraAccount = _authHelper.CameraAccountInfo();
var cameraPassword = _cameraAccountApplication.GetDetails(cameraAccount.Id).Password;
(bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(cameraPassword, password);
if (result.Verified)
{
_accountApplication.Logout();
return new JsonResult(new
{
isSuccess = true,
});
}
return new JsonResult(new
{
isSuccess = false,
errorMessage = "گذرواژه اشتباه است",
});
}
[HttpGet("Logout")]
public IActionResult Logout()
{
_accountApplication.Logout();
return new JsonResult(new { isSuccess = true });
}
}