using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Services;
using NPP.SmartSchedue.Api.Contracts.Services.Time;
using NPP.SmartSchedue.Api.Contracts.Services.Time.Input;
using NPP.SmartSchedue.Api.Contracts.Services.Time.Output;
using NPP.SmartSchedue.Api.Repositories.Time;
using NPP.SmartSchedue.Api.Contracts.Domain.Time;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;
using Microsoft.Extensions.Logging;
namespace NPP.SmartSchedue.Api.Services.Time;
///
/// 班次规则映射服务
///
[DynamicApi(Area = "app")]
public class ShiftRuleMappingService : BaseService, IShiftRuleMappingService, IDynamicApi
{
private readonly ShiftRuleMappingRepository _shiftRuleMappingRepository;
private readonly ShiftRepository _shiftRepository;
private readonly ShiftRuleRepository _shiftRuleRepository;
private readonly ILogger _logger;
public ShiftRuleMappingService(
ShiftRuleMappingRepository shiftRuleMappingRepository,
ShiftRepository shiftRepository,
ShiftRuleRepository shiftRuleRepository,
ILogger logger)
{
_shiftRuleMappingRepository = shiftRuleMappingRepository;
_shiftRepository = shiftRepository;
_shiftRuleRepository = shiftRuleRepository;
_logger = logger;
}
///
/// 查询
///
///
///
public async Task GetAsync(long id)
{
var output = await _shiftRuleMappingRepository.Select
.WhereDynamic(id)
.ToOneAsync();
return output;
}
///
/// 查询分页
///
///
///
[HttpPost]
public async Task> GetPageAsync(PageInput input)
{
var list = await _shiftRuleMappingRepository.Select
.WhereIf(input.Filter?.ShiftId.HasValue == true, a => a.ShiftId == input.Filter.ShiftId.Value)
.WhereIf(input.Filter?.RuleId.HasValue == true, a => a.RuleId == input.Filter.RuleId.Value)
.WhereIf(input.Filter?.IsEnabled.HasValue == true, a => a.IsEnabled == input.Filter.IsEnabled.Value)
.Count(out var total)
.OrderByDescending(a => a.Id)
.Page(input.CurrentPage, input.PageSize)
.ToListAsync();
var data = new PageOutput()
{
List = list,
Total = total
};
return data;
}
///
/// 根据班次查询映射的规则
///
/// 班次ID
/// 映射的规则列表
[HttpGet]
public async Task> GetRulesByShiftIdAsync(long shiftId)
{
var list = await _shiftRuleMappingRepository.Select
.Where(a => a.ShiftId == shiftId)
.OrderBy(a => a.ExecutionPriority)
.OrderBy(a => a.Id)
.ToListAsync();
_logger.LogInformation($"查询班次{shiftId}的规则映射,共{list.Count}条");
return list;
}
///
/// 添加
///
///
///
public async Task AddAsync(ShiftRuleMappingAddInput input)
{
try
{
// 验证班次是否存在
var shiftExists = await _shiftRepository.Select
.Where(x => x.Id == input.ShiftId)
.AnyAsync();
if (!shiftExists)
{
throw new Exception($"班次不存在: {input.ShiftId}");
}
// 验证规则是否存在
var ruleExists = await _shiftRuleRepository.Select
.Where(x => x.Id == input.RuleId)
.AnyAsync();
if (!ruleExists)
{
throw new Exception($"班次规则不存在: {input.RuleId}");
}
// 检查是否已存在相同的映射
var existingMapping = await _shiftRuleMappingRepository.Select
.Where(x => x.ShiftId == input.ShiftId && x.RuleId == input.RuleId)
.FirstAsync();
if (existingMapping != null)
{
throw new Exception($"班次已经关联了该规则");
}
var entity = Mapper.Map(input);
var result = await _shiftRuleMappingRepository.InsertAsync(entity);
return result.Id;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return 0;
}
///
/// 班次增加规则
///
/// 班次ID
/// 规则ID
/// 映射配置
/// 映射ID
[HttpPost]
public async Task AddRuleToShiftAsync(long shiftId, long ruleId, ShiftRuleMappingAddInput input)
{
// 验证班次是否存在A
var shiftExists = await _shiftRepository.Select
.Where(x => x.Id == shiftId)
.AnyAsync();
if (!shiftExists)
{
throw new Exception($"班次不存在: {shiftId}");
}
// 验证规则是否存在
var ruleExists = await _shiftRuleRepository.Select
.Where(x => x.Id == ruleId)
.AnyAsync();
if (!ruleExists)
{
throw new Exception($"班次规则不存在: {ruleId}");
}
// 检查是否已存在相同的映射
var existingMapping = await _shiftRuleMappingRepository.Select
.Where(x => x.ShiftId == shiftId && x.RuleId == ruleId)
.FirstAsync();
if (existingMapping != null)
{
throw new Exception($"班次已经关联了该规则");
}
var entity = Mapper.Map(input);
entity.ShiftId = shiftId;
entity.RuleId = ruleId;
var result = await _shiftRuleMappingRepository.InsertAsync(entity);
return result.Id;
}
///
/// 修改
///
///
///
public async Task UpdateAsync(ShiftRuleMappingUpdateInput input)
{
var entity = await _shiftRuleMappingRepository.GetAsync(input.Id);
Mapper.Map(input, entity);
await _shiftRuleMappingRepository.UpdateAsync(entity);
}
///
/// 班次修改规则
///
/// 映射ID
/// 修改信息
/// 操作结果
[HttpPut]
public async Task UpdateShiftRuleAsync(long mappingId, ShiftRuleMappingUpdateInput input)
{
var entity = await _shiftRuleMappingRepository.GetAsync(mappingId);
if (entity == null)
{
throw new Exception($"班次规则映射不存在: {mappingId}");
}
// 如果修改了规则ID,需要验证新规则是否存在
if (input.RuleId != entity.RuleId)
{
var ruleExists = await _shiftRuleRepository.Select
.Where(x => x.Id == input.RuleId)
.AnyAsync();
if (!ruleExists)
{
throw new Exception($"班次规则不存在: {input.RuleId}");
}
// 检查是否与其他映射冲突
var existingMapping = await _shiftRuleMappingRepository.Select
.Where(x => x.ShiftId == entity.ShiftId && x.RuleId == input.RuleId && x.Id != mappingId)
.FirstAsync();
if (existingMapping != null)
{
throw new Exception("班次已经关联了该规则");
}
}
Mapper.Map(input, entity);
await _shiftRuleMappingRepository.UpdateAsync(entity);
_logger.LogInformation($"更新班次规则映射成功: ID {mappingId}");
}
///
/// 删除
///
///
///
public async Task DeleteAsync(long id)
{
await _shiftRuleMappingRepository.DeleteAsync(id);
}
///
/// 软删除
///
///
///
public async Task SoftDeleteAsync(long id)
{
await _shiftRuleMappingRepository.SoftDeleteAsync(id);
}
///
/// 批量软删除
///
///
///
public async Task BatchSoftDeleteAsync(long[] ids)
{
await _shiftRuleMappingRepository.SoftDeleteAsync(ids);
}
///
/// 班次删除规则
///
/// 班次ID
/// 规则ID
/// 操作结果
[HttpDelete]
public async Task RemoveRuleFromShiftAsync(long shiftId, long ruleId)
{
var entity = await _shiftRuleMappingRepository.Select
.Where(x => x.ShiftId == shiftId && x.RuleId == ruleId)
.FirstAsync();
if (entity == null)
{
throw new Exception($"未找到班次{shiftId}和规则{ruleId}的映射关系");
}
await _shiftRuleMappingRepository.SoftDeleteAsync(entity.Id);
_logger.LogInformation($"班次{shiftId}删除规则{ruleId}成功");
}
///
/// 获取映射状态描述
///
/// 是否启用
/// 生效开始时间
/// 生效结束时间
/// 状态描述
private string GetMappingStatusDescription(bool isEnabled, DateTime? effectiveStartTime, DateTime? effectiveEndTime)
{
if (!isEnabled)
{
return "已禁用";
}
var now = DateTime.Now;
if (effectiveStartTime.HasValue && now < effectiveStartTime.Value)
{
return "未生效";
}
if (effectiveEndTime.HasValue && now > effectiveEndTime.Value)
{
return "已过期";
}
return "正常";
}
}