using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExcelDataReader;
using GxPress.Common.Exceptions;
using GxPress.Common.Page;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.HumanAffairs;
using GxPress.Request.User;
using GxPress.Result.HumanAffairs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace GxPress.Api.AdminControllers
{
///
/// 人事数据
///
[Route("api/admin/human-affairs")]
[ApiController]
[Authorize]
public class AdminHumanAffairsController : ControllerBase
{
private readonly ILogger _logger;
private readonly IHumanAffairsRepository _repository;
public AdminHumanAffairsController(ILogger logger, IHumanAffairsRepository repository)
{
_logger = logger;
_repository = repository;
}
///
/// 导入人事数据Excel数据
///
///
///
[HttpPost("upload")]
public async Task FinanceExcelUpload([FromBody] UserExcelUploadRequest request)
{
var relativePath = request.ExcelUrl;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
//获取excel分析
var stream = System.IO.File.Open("wwwroot/" + relativePath, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream);
var result = reader.AsDataSet();
var sheet = result.Tables["Sheet1"];
var error = string.Empty;
var listHumanAffairs = new List();
for (int i = 1; i < sheet.Rows.Count; i++) //行
{
var humanAffairs = new HumanAffairs();
if (i == 1)
continue;
var numberPeople = sheet.Rows[i][0].ToString().Trim(); //人数
if (string.IsNullOrEmpty(numberPeople))
error = "入职/离职人数";
humanAffairs.NumberPeople = int.Parse(numberPeople);
var salary = sheet.Rows[i][1].ToString().Trim(); //工资
if (string.IsNullOrEmpty(salary))
error = "入职/离职工资元";
humanAffairs.Salary = int.Parse(salary);
var humanAffairsType = sheet.Rows[i][2].ToString().Trim(); //工资
if (string.IsNullOrEmpty(humanAffairsType))
error = "类型:1:入职 2:离职";
humanAffairs.HumanAffairsType = int.Parse(humanAffairsType);
var enteringDateTime = sheet.Rows[i][3].ToString().Trim(); //录入时间
if (string.IsNullOrEmpty(enteringDateTime))
error = "录入时间必填";
humanAffairs.EnteringDateTime = DateTime.Parse(enteringDateTime);
if (!string.IsNullOrEmpty(error))
break;
listHumanAffairs.Add(humanAffairs);
}
if (!string.IsNullOrEmpty(error)) throw new BusinessException(error);
//计算
var success = await _repository.InsertAsync(listHumanAffairs);
if (success == false)
throw new BusinessException("服务器异常");
return true;
}
///
/// 人事数据列表
///
///
///
[HttpPost("list")]
public async Task> GetPagedList(PageParameter request)
{
return await _repository.GetPageListAsync(request);
}
///
/// 人事图像
///
///
///
[HttpPost("chart")]
public async Task GetHumanAffairsChart(HumanAffairsRequest request)
{
return await _repository.GetHumanAffairsChartAsync(request);
}
}
}