using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Znyc.Admin.AspNetCore.Controllers;
using Znyc.Admin.AspNetCore.Entitys;
using Znyc.Admin.AspNetCore.Mvc;
using Znyc.Admin.Commons.Entitys;
using Znyc.Admin.Commons.Helpers;
using Znyc.Admin.Commons.Pages;
using Znyc.Admin.Security.Dtos;
using Znyc.Admin.Security.Entitys;
using Znyc.Admin.Security.IServices;
namespace Znyc.Admin.WebApi.Controllers
{
///
/// 数据字典接口
///
[ApiController]
[Route("api/Security/[controller]")]
public class StatisticalController : AreaApiController
{
private readonly IStatisticalService _statisticalService;
private readonly IUserService _userService;
private readonly ICompanyService _companyService;
private readonly IVehicleService _vehicleService;
///
/// 构造函数
///
///
///
///
///
public StatisticalController(IStatisticalService statisticalService,
ICompanyService companyService,
IVehicleService vehicleService,
IUserService userService
) : base(statisticalService)
{
_statisticalService = statisticalService;
_userService = userService;
_companyService = companyService;
_vehicleService = vehicleService;
}
///
/// 新增前处理数据
///
///
protected override void OnBeforeInsert(Statistical info)
{
info.Id = 0;
info.CreatedTime = DateTime.Now;
info.CreatedUserId = CurrentUser.UserId;
info.IsDeleted = false;
}
///
/// 在更新数据前对数据的修改操作
///
///
///
protected override void OnBeforeUpdate(Statistical info)
{
info.ModifiedUserId = CurrentUser.UserId;
info.ModifiedTime = DateTime.Now;
}
///
/// 异步新增数据
///
///
[HttpPost("InsertAsync")]
[FunctionAuthorize("Add")]
public async Task InsertAsync()
{
CommonResult result = new CommonResult();
var statistical = await _statisticalService.GetStatisticalAsync();
var userAddTotal = await _userService.GetCountByWhereAsync("dayofmonth(CreatedTime) = dayofmonth(now())");
Statistical info = new Statistical()
{
Id = Yitter.IdGenerator.YitIdHelper.NextId(),
Time = DateTime.Now.ToString("yyyy-MM-dd"),
UserAddTotal = userAddTotal,
UserSumTotal = await _userService.GetCountByWhereAsync(" Status=1"),
CompanyAddTotal = await _companyService.GetCountByWhereAsync("dayofmonth(CreatedTime) = dayofmonth(now())"),
CompanySumTotal = await _companyService.GetCountByWhereAsync("Status = 1"),
VehicleAddTotal = await _vehicleService.GetCountByWhereAsync("dayofmonth(CreatedTime) = dayofmonth(now())"),
VehicleSumTotal = await _vehicleService.GetCountByWhereAsync("Status = 1"),
ExpireAddTotal = await _vehicleService.GetCountByWhereAsync("dayofmonth(ExpireTime) = dayofmonth(now()) and Status=1"),
ExpireSumTotal = await _vehicleService.GetCountByWhereAsync("dayofmonth(ExpireTime) < dayofmonth(now()) and Status=1"),
ModifiedTime = DateTime.Now,
CreatedTime = DateTime.Now
};
if (statistical == null)
{
info.UserAddTotalPercent = 0;
}
else
{
info.UserAddTotalPercent = StringHelper.GetPercent(userAddTotal, statistical.UserSumTotal);
}
OnBeforeInsert(info);
long ln = await _service.InsertAsync(info).ConfigureAwait(false);
if (ln > 0)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43001;
result.ErrCode = "43001";
}
return ToJsonContent(result);
}
///
/// 异步分页查询
///
///
///
[HttpPost("FindWithPagerSearchAsync")]
[FunctionAuthorize("List")]
public async Task FindWithPagerSearchAsync(SearchUserModel search)
{
CommonResult> result = new CommonResult>
{
ResData = await _statisticalService.FindWithPagerSearchAsync(search),
ErrCode = ErrCode.successCode
};
return ToJsonContent(result);
}
}
}