EchoTex_Payroll/HRM.UI/Controllers/CommonInterface/DataUploadController.cs

205 lines
7.9 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HRM.BO;
using HRM.DA;
using Microsoft.AspNetCore.Authorization;
using System.IO;
using Newtonsoft.Json;
using Microsoft.AspNetCore.StaticFiles;
namespace HRM.UI.Controllers.Payroll
{
[Route("api/dataUpload")]
[ApiController]
[Authorize]
public class DataUploadController : ControllerBase
{
private static string _filePath = @"ClientApp\Documents\";
public readonly IDataUploadColumnDefinition _columnDefination;
public DataUploadController(IDataUploadColumnDefinition columnDefination)
{
this._columnDefination = columnDefination;
}
// loan
[HttpGet("prepareUpload/{uploadId}/{itemid}")]
public ActionResult GetPrepareUpload(int uploadId, string itemid)
{
int? newItemid = GlobalFunctions.GetApiDefaultIntData(itemid);
DataUploadColumnDefinition dataColumnDefinition = new DataUploadColumnDefinition();
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
dataColumnDefinition = this._columnDefination.GetColumnDefinition(uploadId, newItemid, (int)currentUser.PayrollTypeID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(dataColumnDefinition);
}
//[HttpGet("uploadData/{uploadId}/{oColumnDefination}/{path}")]
//public ActionResult GetUploadDataMessage(int uploadId, DataUploadColumnDefinitionVM oColumnDefination, string path)
//{
// RegularDataUpload regUpload= new RegularDataUpload();
// List<UploadErrorOrSuccess> errorOrSuccessList = new List<UploadErrorOrSuccess>();
// try
// {
// // regUpload.UploadData(uploadId, oColumnDefination, path);
// }
// catch (Exception ex)
// {
// return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
// }
// return Ok(regUpload);
//}
[HttpPost]
[Route("regularExcelInterface")]
[AllowAnonymous]
public ActionResult regularExcelInterface()
{
DataUploadColumnDefinition oColumnDefination = new DataUploadColumnDefinition();
RegularDataUploadService regUpload = new RegularDataUploadService();
List<UploadErrorOrSuccess> errorOrSuccessList = new List<UploadErrorOrSuccess>();
DateTime selectedDate = DateTime.Today;
try
{
int uploadID = Convert.ToInt32(Request.Form["uploadID"]);
int? itemid = GlobalFunctions.GetApiDefaultIntData( Convert.ToString(Request.Form["itemID"]));
int? itemidTwo = GlobalFunctions.GetApiDefaultIntData(Convert.ToString(Request.Form["itemIDTwo"]));
int? payrolltypeid = GlobalFunctions.GetApiDefaultIntData(Convert.ToString(Request.Form["payrolltypeid"]));
int? userid = GlobalFunctions.GetApiDefaultIntData(Convert.ToString(Request.Form["userid"]));
string date = Request.Form["selectedDate"];
if (!string.IsNullOrEmpty(date))
{
selectedDate = DateTime.Parse(date);
}
var files = Request.Form.Files;
foreach (var formFile in files)
{
oColumnDefination = this._columnDefination.GetColumnDefinition(uploadID, itemid, (int)payrolltypeid);
if (itemid != null)
{
oColumnDefination.uploadItemID =(int) itemid;
oColumnDefination.SelectedItemOneID =(int) itemid;
}
if (itemidTwo != null)
{
oColumnDefination.SelectedItemTwoID =(int) itemidTwo;
}
errorOrSuccessList = regUpload.UploadData(uploadID, oColumnDefination, formFile.OpenReadStream()
, (int)payrolltypeid, (int) userid, itemid, selectedDate);
break;
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(errorOrSuccessList);
}
[HttpPost]
[Route("uploadBonusFile")]
[AllowAnonymous]
public ActionResult uploadBonusFile()
{
DataUploadColumnDefinition oColumnDefination = new DataUploadColumnDefinition();
RegularDataUploadService regUpload = new RegularDataUploadService();
List<BonusProcess.BonusProcessDetail> items = null;
try
{
int? payrolltypeid = GlobalFunctions.GetApiDefaultIntData(Convert.ToString(Request.Form["payrolltypeid"]));
var files = Request.Form.Files;
foreach (var formFile in files)
{
oColumnDefination = this._columnDefination.GetColumnDefinition(
(int)EnumRegularDataUpload.Bonus, null, (int)payrolltypeid);
items = regUpload.GetBonusData((int)EnumRegularDataUpload.Bonus, oColumnDefination, formFile.OpenReadStream()
, (int)payrolltypeid);
break;
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpPost]
[Route("get-download-file")]
[AllowAnonymous]
[IgnoreAntiforgeryToken]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> EmployeeDownload(dynamic data)
{
//var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
//var filePath = Path.Combine(uploads, file);
var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
string tempfileName = (string) item["fileName"].ToObject<string>();
string tempmappingpath = (string) item["mappingPath"].ToObject<string>();
var fileName = _filePath + tempmappingpath;
var contentType = GetFileType(data);
string fullPath = System.IO.Path.Combine(System.Environment.CurrentDirectory, fileName);
if (!System.IO.File.Exists(fullPath))
return NotFound();
var memory = new MemoryStream();
using (var stream = new FileStream(fullPath, FileMode.Open))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
return File(memory, GetContentType(fullPath));
}
[HttpPost]
[Route("get-file-type")]
[AllowAnonymous]
[IgnoreAntiforgeryToken]
[ProducesResponseType(StatusCodes.Status200OK)]
public string GetFileType(dynamic data)
{
var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
string fileName = (string) item["fileName"].ToObject<string>();
string contentType;
new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);
return contentType ?? "application/octet-stream";
}
private string GetContentType(string path)
{
var provider = new FileExtensionContentTypeProvider();
string contentType;
if (!provider.TryGetContentType(path, out contentType))
{
contentType = "application/octet-stream";
}
return contentType;
}
}
}