347 lines
13 KiB
C#
347 lines
13 KiB
C#
using Dlhis.Entity;
|
||
using Dlhis.Entity.Basic.Entity;
|
||
using Dlhis.Entity.Canteen.Entities;
|
||
using Dlhis.Entity.Canteen.Enums;
|
||
using Dlhis.ScheduledTask.TaskAssignment.Request;
|
||
using Dlhis.ScheduledTask.TaskAssignment.Response;
|
||
using Dlhis.ScheduledTask.TaskAssignment.Services;
|
||
using Dlhis.Sys.Api;
|
||
using Dlhis.Sys.Api.Request;
|
||
using Kurisu.AspNetCore.CustomClass;
|
||
using Kurisu.AspNetCore.DataAccess.Dto;
|
||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop;
|
||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||
using Kurisu.AspNetCore.UnifyResultAndValidation.Aop;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using SqlSugar;
|
||
|
||
namespace Dlhis.ScheduledTask.TaskAssignment.Controllers;
|
||
|
||
/// <summary>
|
||
/// 任务分配
|
||
/// </summary>
|
||
[Authorize]
|
||
[Route("api/[controller]")]
|
||
[ApiController]
|
||
public class AssignmentsController : ControllerBase
|
||
{
|
||
private readonly IAssignmentService _assignmentService;
|
||
private readonly IDbContext _dbContext;
|
||
private readonly IUserApi _userApi;
|
||
|
||
public AssignmentsController(IAssignmentService assignmentService, IDbContext dbContext, IUserApi userApi)
|
||
{
|
||
_assignmentService = assignmentService;
|
||
_dbContext = dbContext;
|
||
_userApi = userApi;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取设备任务(按设备)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[DisableApiLog]
|
||
[IgnoreTenant]
|
||
[HttpGet("deviceTasks")]
|
||
public async Task<List<DeviceTaskToDeviceByDeviceResponse>> DeviceGetTasksGetAsync([FromQuery] DeviceTaskGetRequest request)
|
||
{
|
||
return await _assignmentService.DeviceGetTasksAsync(request);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取设备任务(按设备)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[DisableApiLog]
|
||
[HttpPost("deviceTasks")]
|
||
[IgnoreTenant]
|
||
public async Task<List<DeviceTaskToDeviceByDeviceResponse>> DeviceGetTasksPostAsync(DeviceTaskGetRequest request)
|
||
{
|
||
return await _assignmentService.DeviceGetTasksAsync(request);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新任务状态
|
||
/// </summary>
|
||
/// <param name="requests"></param>
|
||
/// <returns></returns>
|
||
[DisableApiLog]
|
||
[HttpPost("taskStatus")]
|
||
[IgnoreTenant]
|
||
public async Task UpdateDeviceTaskStatusAsync(List<UpdateDeviceTaskStatusRequest> requests)
|
||
{
|
||
await _assignmentService.UpdateDeviceTaskStatusAsync(requests);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost("balance/calc/{month:int}")]
|
||
[IgnoreTenant]
|
||
public async Task Calc([FromRoute] int month)
|
||
{
|
||
if (month is < 1 or > 12)
|
||
{
|
||
throw new UserFriendlyException("月份错误");
|
||
}
|
||
|
||
var s = new DateTime(2025, month, 1);
|
||
var e = new DateTime(2025, month + 1, 1);
|
||
|
||
var userCodes = await _dbContext.Queryable<UserBalanceEntity>()
|
||
.Where(x => SqlFunc.Subqueryable<UserBalanceTmpEntity>().Where(p => p.UserCode == x.UserCode).NotAny())
|
||
.Select(x => x.UserCode)
|
||
.Distinct()
|
||
.ToListAsync();
|
||
|
||
foreach (var userCode in userCodes)
|
||
{
|
||
if (!await _dbContext.Queryable<UserBalanceTmpEntity>().Where(x => x.UserCode == userCode).AnyAsync())
|
||
{
|
||
await _dbContext.InsertAsync(new UserBalanceTmpEntity
|
||
{
|
||
UserCode = userCode,
|
||
Balance = 0,
|
||
Version = 0
|
||
});
|
||
}
|
||
|
||
var balance = await _dbContext.Queryable<BalanceRecordEntity>()
|
||
.Where(x => x.UserCode == userCode)
|
||
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||
.Where(x => x.CostDate >= s && x.CostDate < e)
|
||
.Where(x => !BalanceRecordSourceConstants.IgnoreSources.Contains(x.Source))
|
||
.SumAsync(x => x.Amount);
|
||
|
||
await _dbContext.Updateable<UserBalanceTmpEntity>()
|
||
.SetColumns(x => x.Balance == balance)
|
||
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
||
.Where(x => x.UserCode == userCode)
|
||
.ExecuteCommandAsync();
|
||
}
|
||
|
||
//
|
||
// var userCodes = await _dbContext.Queryable<GainfoLeaveEntity>().Where(x => x.ResignationDate.HasValue && x.ResignationDate >= s && x.ResignationDate <= e)
|
||
// .Where(x => x.Type != GainfoUserTypeEnumType.External && x.Type != GainfoUserTypeEnumType.Outsource)
|
||
// .Select(x => x.Code)
|
||
// .Distinct()
|
||
// .ToListAsync();
|
||
//
|
||
// foreach (var userCode in userCodes)
|
||
// {
|
||
// if (!await _dbContext.Queryable<UserBalanceTmpEntity>().Where(x => x.UserCode == userCode).AnyAsync())
|
||
// {
|
||
// await _dbContext.InsertAsync(new UserBalanceTmpEntity
|
||
// {
|
||
// UserCode = userCode,
|
||
// Balance = 0,
|
||
// Version = 0
|
||
// });
|
||
// }
|
||
//
|
||
// var balance = await _dbContext.Queryable<BalanceRecordEntity>()
|
||
// .Where(x => x.UserCode == userCode)
|
||
// .Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
||
// .Where(x => !BalanceRecordSourceConstants.IgnoreSources.Contains(x.Source))
|
||
// .Where(x => x.CostDate >= s && x.CostDate <= e)
|
||
// .Where(x => x.Type == BalanceRecordTypeEnumType.In)
|
||
// .SumAsync(x => x.Amount);
|
||
//
|
||
// await _dbContext.Updateable<UserBalanceTmpEntity>()
|
||
// .SetColumns(x => x.Balance == balance)
|
||
// .SetColumns(x => x.ModifiedTime == DateTime.Now)
|
||
// .Where(x => x.UserCode == userCode)
|
||
// .ExecuteCommandAsync();
|
||
// }
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[IgnoreTenant]
|
||
[HttpPost("settlement/supplier/calc/{year:int}/{month:int}")]
|
||
public async Task CalcSettlementSupplier([FromRoute] int year, [FromRoute] int month)
|
||
{
|
||
if (month is < 1 or > 12)
|
||
{
|
||
throw new UserFriendlyException("月份错误");
|
||
}
|
||
|
||
//周期:上个月19号到本月18号
|
||
var s = new DateTime(year, month, 19).AddMonths(-1);
|
||
var e = new DateTime(year, month, 19);
|
||
|
||
var previousSettlementSupplierMonth = $"{s.Year}{s.Month:00}";
|
||
var currentSettlementSupplierMonth = $"{e.Year}{e.Month:00}";
|
||
|
||
var businessSettlements = await _dbContext.Queryable<BalanceRecordEntity>()
|
||
.Where(x => x.CostDate >= s && x.CostDate < e)
|
||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out && x.Status == BalanceRecordStatusEnumType.Valid)
|
||
.Where(x => x.TenantId == "2401" && x.BusinessPlaceCode.HasValue)
|
||
.GroupBy(x => new { x.BusinessPlaceCode })
|
||
.Select(x => new
|
||
{
|
||
x.BusinessPlaceCode,
|
||
Amount = SqlFunc.IsNull(SqlFunc.AggregateSum(x.Amount), 0)
|
||
})
|
||
.ToListAsync();
|
||
|
||
var businessPlaceCodes = businessSettlements.Select(x => x.BusinessPlaceCode).ToList();
|
||
var businessPlaces = await _dbContext.Queryable<BusinessPlaceEntity>()
|
||
.Where(x => businessPlaceCodes.Contains(x.Code))
|
||
.Where(x => x.TenantId == "2401")
|
||
.ToListAsync();
|
||
|
||
var previousData = await _dbContext.Queryable<SettlementSupplierMonthly>()
|
||
.Where(x => x.SettleMonth == previousSettlementSupplierMonth)
|
||
.Where(x => businessPlaceCodes.Contains(x.BusinessPlaceCode))
|
||
.Where(x => x.TenantId == "2401")
|
||
.ToListAsync();
|
||
|
||
var settlementSupplierMonthlies = businessSettlements.Select(x =>
|
||
{
|
||
var businessPlace = businessPlaces.First(p => p.Code == x.BusinessPlaceCode);
|
||
var before = previousData.FirstOrDefault(p => p.BusinessPlaceCode == x.BusinessPlaceCode);
|
||
|
||
return new SettlementSupplierMonthly
|
||
{
|
||
BusinessPlaceCode = x.BusinessPlaceCode!.Value,
|
||
BusinessPlaceName = businessPlace.Name,
|
||
SupplierName = businessPlace.Supplier,
|
||
|
||
SettleMonth = currentSettlementSupplierMonth,
|
||
OpeningBalance = before?.ClosingAmount ?? 0,
|
||
|
||
CostAmount = x.Amount,
|
||
PaidAmount = 0,
|
||
RefundAmount = 0,
|
||
ClosingAmount = (before?.ClosingAmount ?? 0) + x.Amount,
|
||
Valid = true,
|
||
TenantId = "2401"
|
||
};
|
||
}).ToList();
|
||
|
||
await _dbContext.InsertAsync(settlementSupplierMonthlies);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[Transactional]
|
||
[IgnoreTenant]
|
||
[HttpPost("settlement/supplier/pay")]
|
||
public async Task ManuallySettlementSupplier1(ManuallySettlementSupplierRequest1 request)
|
||
{
|
||
var data = await _dbContext.Queryable<SettlementSupplierMonthly>()
|
||
.Where(x => x.SettleMonth == request.SettleMonth)
|
||
.Where(x => x.Valid)
|
||
.FirstAsync();
|
||
|
||
//复制data然后修改
|
||
var newData = new SettlementSupplierMonthly
|
||
{
|
||
BusinessPlaceCode = data.BusinessPlaceCode,
|
||
BusinessPlaceName = data.BusinessPlaceName,
|
||
SupplierName = data.SupplierName,
|
||
SettleMonth = data.SettleMonth,
|
||
OpeningBalance = data.OpeningBalance,
|
||
CostAmount = data.CostAmount,
|
||
PaidAmount = request.Pay,
|
||
RefundAmount = 0,
|
||
ClosingAmount = data.ClosingAmount - request.Pay,
|
||
Valid = true,
|
||
TenantId = data.TenantId
|
||
};
|
||
|
||
if (newData.ClosingAmount < 0)
|
||
{
|
||
throw new UserFriendlyException("结算金额过大,导致期末金额小于0");
|
||
}
|
||
|
||
data.Valid = false;
|
||
await _dbContext.UpdateAsync(data);
|
||
await _dbContext.InsertAsync(newData);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[IgnoreTenant]
|
||
[HttpPost("settlement/supplier/refund")]
|
||
public async Task ManuallySettlementSupplier1(ManuallySettlementSupplierRequest2 request)
|
||
{
|
||
var data = await _dbContext.Queryable<SettlementSupplierMonthly>()
|
||
.Where(x => x.SettleMonth == request.SettleMonth)
|
||
.Where(x => x.Valid)
|
||
.FirstAsync();
|
||
|
||
//复制data然后修改
|
||
var newData = new SettlementSupplierMonthly
|
||
{
|
||
BusinessPlaceCode = data.BusinessPlaceCode,
|
||
BusinessPlaceName = data.BusinessPlaceName,
|
||
SupplierName = data.SupplierName,
|
||
SettleMonth = data.SettleMonth,
|
||
OpeningBalance = data.OpeningBalance,
|
||
CostAmount = data.CostAmount,
|
||
PaidAmount = 0,
|
||
RefundAmount = request.Refund,
|
||
ClosingAmount = data.ClosingAmount - request.Refund,
|
||
Valid = true,
|
||
TenantId = data.TenantId
|
||
};
|
||
|
||
if (newData.ClosingAmount < 0)
|
||
{
|
||
throw new UserFriendlyException("退款金额过大,导致期末金额小于0");
|
||
}
|
||
|
||
data.Valid = false;
|
||
await _dbContext.UpdateAsync(data);
|
||
await _dbContext.InsertAsync(newData);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[IgnoreTenant]
|
||
[HttpGet("settlement/supplier/page")]
|
||
public async Task<Pagination<SettlementSupplierPageResponse>> GetSettlementSupplierPageAsync([FromQuery] SettlementSupplierPageRequest request)
|
||
{
|
||
var page = await _dbContext.Queryable<SettlementSupplierMonthly>()
|
||
.Where(x => x.SettleMonth == request.SettleMonth && x.TenantId == "2401")
|
||
.Where(x => x.Valid)
|
||
.OrderByDescending(x => x.CreateTime)
|
||
.Select<SettlementSupplierPageResponse>()
|
||
.ToPageListAsync(request);
|
||
|
||
try
|
||
{
|
||
var userIds = page.Data.Select(x => x.CreatedBy).Distinct().ToList();
|
||
var userInfos = await _userApi.GetUserByIdsAsync(new IdsRequest { Ids = userIds });
|
||
page.Data.ForEach(item => item.Creator = userInfos.FirstOrDefault(x => x.Id == item.CreatedBy)?.OperatorName);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
|
||
return page;
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[IgnoreTenant]
|
||
[HttpGet("settlement/supplier/originPage")]
|
||
public async Task<Pagination<SettlementSupplierPageResponse>> GetSettlementSupplierOriginPageAsync([FromQuery] SettlementSupplierPageRequest request)
|
||
{
|
||
var page = await _dbContext.Queryable<SettlementSupplierMonthly>()
|
||
.Where(x => x.SettleMonth == request.SettleMonth && x.TenantId == "2401")
|
||
.OrderByDescending(x => x.CreateTime)
|
||
.Select<SettlementSupplierPageResponse>()
|
||
.ToPageListAsync(request);
|
||
|
||
try
|
||
{
|
||
var userIds = page.Data.Select(x => x.CreatedBy).Distinct().ToList();
|
||
var userInfos = await _userApi.GetUserByIdsAsync(new IdsRequest { Ids = userIds });
|
||
page.Data.ForEach(item => item.Creator = userInfos.FirstOrDefault(x => x.Id == item.CreatedBy)?.OperatorName);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
|
||
return page;
|
||
}
|
||
} |