EchoTex_Payroll/Erecruitment.UI/Controllers/Survey/SurveyController.cs
2024-10-14 10:01:49 +06:00

527 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using AutoMapper.Configuration;
using Ease.Core;
using HRM.BO;
using HRM.DA;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using Payroll.BO;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
namespace Erecruitment.UI.Controllers
{
[ApiController]
[Route("api/survey")]
[Authorize]
public class SurveyController : Controller
{
#region Declerations
private readonly IQuestionCategoryService _questionCategoryService;
private readonly IQuestionService _questionService;
private readonly IConfiguration _config;
private readonly IEmployeeService _employeeService;
private readonly IQuestionAnswerService _questionAnswerService;
private readonly ISurveyCategoryService _surveyCategoryService;
private readonly ISurveyService _surveyService;
private readonly ISurveyQuestionService _surveyQuestionService;
private readonly ISurveyResultService _surveyResultService;
private readonly ISurveyEmployeeService _surveyEmployeeService;
private readonly IErCircularSurveyService _erCircularSurveyService;
#endregion
#region Constructor
public SurveyController(IConfiguration config,
IQuestionCategoryService questionCategoryService,
IQuestionService questionService,
IQuestionAnswerService questionAnswerService,
ISurveyCategoryService surveyCategoryService,
ISurveyService surveyService,
ISurveyQuestionService surveyQuestionService,
ISurveyResultService surveyResultService,
ISurveyEmployeeService surveyEmployeeService,
IErCircularSurveyService erCircularSurveyService
)
{
_config = config;
_questionCategoryService = questionCategoryService;
_questionService = questionService;
_questionAnswerService = questionAnswerService;
_surveyCategoryService = surveyCategoryService;
_surveyService = surveyService;
_surveyQuestionService = surveyQuestionService;
_surveyResultService = surveyResultService;
_surveyEmployeeService = surveyEmployeeService;
_erCircularSurveyService = erCircularSurveyService;
}
#endregion
#region Functions
[HttpPost]
[Route("savequestion")]
public ActionResult SaveQuestion(Question item)
{
int ans;
var isNew = item.ID <= 0;
string connectionString = _config.GetSection("dbSettings").GetSection("SqlCommandConnection").Value;
item.ConnectionString = connectionString;
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
if (isNew)
{
item.CreatedBy = currentUser.UserID;
item.CreatedDate = DateTime.Today;
item.UserID= currentUser.UserID;
}
else
{
item.ModifiedBy = currentUser.UserID;
item.ModifiedDate = DateTime.Today;
}
try
{
ans = _questionService.Save(item);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(ans);
}
[HttpGet]
[Route("getQuestionCategories")]
public ActionResult GetQuestionCategories()
{
List<QuestionCategory> obs = new List<QuestionCategory>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
obs = _questionCategoryService.Get(EnumStatus.Active);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(obs);
}
[HttpGet]
[Route("GetByCategory/{categoryId}")]
public ActionResult GetByCategory(int categoryId)
{
List<Question> items = new List<Question>();
try
{
items = _questionService.GetByCategory(categoryId);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet]
[Route("GetQuestionsByCategory/{categoryId}")]
public ActionResult GetQuestionsByCategory(int categoryId)
{
List<Survey> items = new List<Survey>();
try
{
items = _surveyService.GetQuestionSetByCategoryID(categoryId);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet]
[Route("getQuestionById/{questionId}")]
public ActionResult getQuestionById(int questionId)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
Question question = new Question();
try
{
question = _questionService.Get(questionId);
question.QuestionAnswers = _questionAnswerService.GetByQuestionID(questionId);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(question);
}
[HttpGet]
[Route("getSurveyById/{surveyId}")]
public ActionResult getSurveyById(int surveyId)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
Survey survey = new Survey();
try
{
survey = _surveyService.Get(surveyId);
survey.SurveyQuestions = _surveyQuestionService.GetSurveyQuestion(surveyId);
foreach(var item in survey.SurveyQuestions)
{
item.Question = _questionService.Get(item.QuestionID);
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(survey);
}
[HttpGet]
[Route("getAllFilesById/{questionId}")]
public ActionResult GetAllFilesById(int questionId)
{
List<QuestionAnswerAttachment> files = new List<QuestionAnswerAttachment>();
try
{
files = _questionAnswerService.GetQuestionFileAttachments(questionId);
if (files != null && files.Count > 0)
{
foreach (var item in files)
{
item.PreviousFileTobase64 = item.FileTobase64;
}
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(files);
}
[HttpGet]
[Route("getSurveyCategories")]
public ActionResult GetSurveyCategories()
{
List<SurveyCategory> obs = new List<SurveyCategory>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
obs = _surveyCategoryService.Get(EnumStatus.Active);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(obs);
}
[HttpGet]
[Route("getQuestionSet")]
public ActionResult GetQuestionSet()
{
List<Survey> obs = new List<Survey>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
obs = _surveyService.Get();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(obs);
}
[HttpGet]
[Route("getQuestionsBySurveyId/{jobId}")]
public ActionResult GetQuestionsBySurveyId(int jobId)
{
Survey survey = new Survey();
List<QuestionAnswerAttachment> files = new List<QuestionAnswerAttachment>();
try
{
List<ErCircularSurvey> erCircularSurveys = null;
erCircularSurveys = _erCircularSurveyService.GetByCircularID(jobId);
var erCircularSurvey = erCircularSurveys[0];
survey = _surveyService.Get(erCircularSurvey.SurveyID);
survey.EstimatedTime = 0;
survey.SurveyQuestions = _surveyQuestionService.GetSurveyQuestion(erCircularSurvey.SurveyID);
if (survey.SurveyQuestions != null && survey.SurveyQuestions.Count > 0)
{
foreach (var item in survey.SurveyQuestions)
{
item.Question = _questionService.GetWithChild(item.QuestionID);
if(item.Question != null && item.Question.QuestionAnswers != null && item.Question.QuestionAnswers.Count > 0)
{
foreach (var temp in item.Question.QuestionAnswers)
{
var tempAttachment = _questionService.GetAnswerByAttchmentId(item.Question.ID,temp.ID);
if(tempAttachment != null)
{
temp.QuestionAnswerAttachments = new List<QuestionAnswerAttachment>();
temp.QuestionAnswerAttachments.Add(tempAttachment);
}
}
survey.EstimatedTime += item.Question.EstimatedTime;
}
}
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(survey);
}
[HttpPost]
[Route("saveSurveyResult")]
public ActionResult SaveSurveyResult(SurveyResult item)
{
int ans;
var isNew = item.ID <= 0;
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
item.CandidateID = (int)currentUser.UserID;
item.EmployeeID = null;
var prevResult = _surveyResultService.GetSurveyResultByCandidateID(item.SurveyID, item.CandidateID, item.QuestionID);
if(prevResult != null)
{
_surveyResultService.DeleteByCandidateID(item.SurveyID, item.CandidateID, item.QuestionID);
}
ans = _surveyResultService.Save(item);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(ans);
}
[HttpPost]
[Route("saveSurveyEmployee")]
public ActionResult saveSurveyEmployee(SurveyEmployee item)
{
int ans;
var isNew = item.ID <= 0;
List<SurveyEmployee> surveyEmployees = new List<SurveyEmployee>();
SurveyEmployee prevItem = null;
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
item.CandidateID = currentUser.UserID;
item.EmployeeID = null;
item.CurrentTime = (double)(DateTime.Now - (DateTime)item.StartTime).Minutes;
surveyEmployees = _surveyEmployeeService.GetSurveyEmployeeByCandidateId(item.SurveyID, item.CandidateID,item.ErCircularID);
if (surveyEmployees != null && surveyEmployees.Count > 0)
{
prevItem = new SurveyEmployee();
prevItem = surveyEmployees[0];
item.SurveyMark = prevItem.SurveyMark;
item.SurveyParticipantDate = prevItem.SurveyParticipantDate;
item.SurveyTime = prevItem.SurveyTime;
if(prevItem.IsCompleted == false && (prevItem.SurveyTime > prevItem.CurrentTime) && prevItem.IsDisconnected==true)
{
item.CurrentTime = (double)(DateTime.Now - (DateTime)item.StartTime).Minutes + prevItem.CurrentTime;
}
if (item.IsCompleted == true)
{
item.CompleteTime = DateTime.Now;
}
}
if(prevItem != null)
{
_surveyEmployeeService.DeleteByCandidateID(prevItem.SurveyID, prevItem.CandidateID, prevItem.ErCircularID);
}
ans = _surveyEmployeeService.Save(item);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(ans);
}
[HttpGet]
[Route("getResultByCandidateID/{surveyId}/{jobId}")]
public ActionResult getResultByCandidateID(int surveyId,int jobId)
{
Survey survey = new Survey();
Result result = new Result();
// List<QuestionAnswerAttachment> files = new List<QuestionAnswerAttachment>();
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
survey = _surveyService.Get(surveyId);
List<SurveyResult> surveyResult = new List<SurveyResult>();
List<SurveyEmployee> surveyEmployees = new List<SurveyEmployee>();
surveyResult = _surveyResultService.GetSurveyResults(surveyId, (int)currentUser.UserID);
survey.EstimatedTime = 0;
survey.SurveyQuestions = _surveyQuestionService.GetSurveyQuestion(surveyId);
surveyEmployees = _surveyEmployeeService.GetSurveyEmployeeByCandidateId(surveyId, (int)currentUser.UserID, jobId);
if (survey.SurveyQuestions != null && survey.SurveyQuestions.Count > 0)
{
foreach (var item in survey.SurveyQuestions)
{
item.Question = _questionService.GetWithChild(item.QuestionID);
if (item.Question != null && item.Question.QuestionAnswers != null && item.Question.QuestionAnswers.Count > 0)
{
survey.EstimatedTime += item.Question.EstimatedTime;
}
}
}
result.totalTime = survey.EstimatedTime;
result.questionCount = survey.SurveyQuestions.Count();
if(surveyResult != null && surveyResult.Count > 0)
{
result.answerCount = surveyResult.Count(x => x.AnswerID != 0);
result.totalMark= surveyResult.Where(x=>x.IsCorrect == true).Sum(x=>x.Weight);
}
if (surveyEmployees != null && surveyEmployees.Count > 0)
{
result.takenTime = (surveyEmployees[0].CompleteTime - surveyEmployees[0].StartTime).Value.TotalMinutes.ToString("0.0");
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(result);
}
//[HttpPost]
//[Route("completeSurveyEmployee")]
//public ActionResult CompleteSurveyEmployee(SurveyEmployee item)
//{
// int ans;
// var isNew = item.ID <= 0;
// List<SurveyEmployee> surveyEmployees = new List<SurveyEmployee>();
// SurveyEmployee prevItem = null;
// try
// {
// CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
// item.CandidateID = currentUser.UserID;
// item.EmployeeID = null;
// item.CurrentTime = (double)(DateTime.Now - (DateTime)item.StartTime).Minutes;
// surveyEmployees = _surveyEmployeeService.GetSurveyEmployeeByCandidateId(item.SurveyID, item.CandidateID, item.ErCircularID);
// if (surveyEmployees != null && surveyEmployees.Count > 0)
// {
// prevItem = new SurveyEmployee();
// prevItem = surveyEmployees[0];
// prevItem.CurrentTime = item.CurrentTime;
// prevItem.CurrentQuestionID = item.CurrentQuestionID;
// prevItem.IsCompleted = true;
// prevItem.CompleteTime = DateTime.Now;
// }
// if (prevItem != null)
// {
// _surveyEmployeeService.DeleteByCandidateID(prevItem.SurveyID, prevItem.CandidateID, prevItem.ErCircularID);
// }
// ans = _surveyEmployeeService.Save(item);
// }
// catch (Exception ex)
// {
// return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
// }
// return Ok(ans);
//}
[HttpGet]
[Route("checkDisconncetedSurveyEmployee/{erCircularID}/{surveyId}")]
public ActionResult CheckDisconncetedSurveyEmployee(int erCircularID,int surveyId)
{
int ans;
List<SurveyEmployee> surveyEmployees = new List<SurveyEmployee>();
SurveyEmployee prevItem = null;
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int candidateID = currentUser.UserID;
surveyEmployees = _surveyEmployeeService.GetSurveyEmployeeByCandidateId(surveyId, candidateID, erCircularID);
if (surveyEmployees != null && surveyEmployees.Count > 0)
{
prevItem = new SurveyEmployee();
prevItem = surveyEmployees[0];
}
if(prevItem != null)
{
if (prevItem.IsCompleted == false && prevItem.SurveyTime > prevItem.CurrentTime)
{
prevItem.IsDisconnected = true;
}
else
{
prevItem = null;
}
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(prevItem);
}
#endregion
}
}