using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Dtos.PayRoll; using F9WebAiTest.Domain.Common; using F9WebAiTest.Domain.Entities.PayRoll; using SysDept = F9WebAiTest.Domain.Entities.SYS.Departments; using SysEmp = F9WebAiTest.Domain.Entities.SYS.Employees; using SysWt = F9WebAiTest.Domain.Entities.SYS.WorkTypes; namespace F9WebAiTest.Application.Services.PayRoll; /// 薪资配置-账期(CONFZQ):考勤统计的「前缫出勤天数」来源 public interface IConfZqService { Task> GetListAsync(int? year); Task GetByYearMonthAsync(int year, int month); Task SaveAsync(ConfZqDto dto); Task DeleteAsync(long id); } /// 薪资配置-职工(CONFEMPLOYEE):法定假小时数 / 是否学工 public interface IConfEmployeeService { Task> GetListAsync(long? employeeId, long? worktypeId); Task SaveAsync(ConfEmployeeDto dto); Task DeleteAsync(long id); } /// 岗位胜任考核:考核结果决定加班费单价档位 public interface IGwsrkhService { Task> GetListAsync(int year, int month, long depId); Task SaveAsync(GwsrkhDto dto); Task DeleteAsync(long id); } public class ConfZqService : IConfZqService { private readonly IRepository _repo; public ConfZqService(IRepository repo) => _repo = repo; public async Task> GetListAsync(int? year) { var all = await _repo.GetAllAsync(); var q = all.AsQueryable(); if (year.HasValue) q = q.Where(x => x.Year == year.Value); return q.ToList().OrderByDescending(x => x.Year).ThenByDescending(x => x.Month) .Select(x => new ConfZqDto { Id = x.Id, Year = x.Year, Month = x.Month, QsQuanqinDays = x.QsQuanqinDays, Memo = x.Memo ?? "" }).ToList(); } public async Task GetByYearMonthAsync(int year, int month) { var hit = await _repo.FirstOrDefaultAsync(x => x.Year == year && x.Month == month); return hit == null ? new ConfZqDto { Year = year, Month = month } : new ConfZqDto { Id = hit.Id, Year = hit.Year, Month = hit.Month, QsQuanqinDays = hit.QsQuanqinDays, Memo = hit.Memo ?? "" }; } public async Task SaveAsync(ConfZqDto dto) { if (dto.Year <= 0 || dto.Month <= 0) throw new ServiceException("请填写年度与月份"); var exist = await _repo.FirstOrDefaultAsync(x => x.Year == dto.Year && x.Month == dto.Month && x.Id != dto.Id); if (exist != null) throw new ServiceException($"{dto.Year} 年 {dto.Month} 月的账期薪资配置已存在"); ConfZq entity; if (dto.Id > 0) { entity = await _repo.GetByIdAsync(dto.Id); if (entity == null) throw new ServiceException("配置不存在", 404); } else entity = new ConfZq(); entity.Year = dto.Year; entity.Month = dto.Month; entity.QsQuanqinDays = dto.QsQuanqinDays; entity.Memo = dto.Memo ?? ""; if (dto.Id > 0) await _repo.UpdateAsync(entity); else await _repo.AddAsync(entity); return entity.Id; } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("配置不存在", 404); await _repo.DeleteAsync(e); } } public class ConfEmployeeService : IConfEmployeeService { private readonly IRepository _repo; private readonly IRepository _empRepo; private readonly IRepository _wtRepo; public ConfEmployeeService(IRepository repo, IRepository empRepo, IRepository wtRepo) { _repo = repo; _empRepo = empRepo; _wtRepo = wtRepo; } public async Task> GetListAsync(long? employeeId, long? worktypeId) { var all = await _repo.GetAllAsync(); var q = all.AsQueryable(); if (employeeId.HasValue && employeeId.Value > 0) q = q.Where(x => x.EmployeeId == employeeId.Value); if (worktypeId.HasValue && worktypeId.Value > 0) q = q.Where(x => x.WorktypeId == worktypeId.Value); var emps = (await _empRepo.GetAllAsync()).ToDictionary(e => e.Id); var wts = (await _wtRepo.GetAllAsync()).ToDictionary(w => w.Id, w => w.WorkName); return q.ToList().OrderBy(x => x.EmployeeId).Select(x => new ConfEmployeeDto { Id = x.Id, EmployeeId = x.EmployeeId, EmployeeCode = emps.TryGetValue(x.EmployeeId, out var e) ? (e.Code ?? "") : "", EmployeeName = emps.TryGetValue(x.EmployeeId, out var e2) ? (e2.Name ?? "") : "", WorktypeId = x.WorktypeId, WorktypeName = wts.TryGetValue(x.WorktypeId, out var wn) ? wn : "", HoursFadingBz = x.HoursFadingBz, HoursFadingGz = x.HoursFadingGz, IsXuegong = x.IsXuegong, Memo = x.Memo ?? "" }).ToList(); } public async Task SaveAsync(ConfEmployeeDto dto) { if (dto.EmployeeId <= 0) throw new ServiceException("请选择职工"); var exist = await _repo.FirstOrDefaultAsync(x => x.EmployeeId == dto.EmployeeId && x.WorktypeId == dto.WorktypeId && x.Id != dto.Id); if (exist != null) throw new ServiceException("该职工该工种的薪资配置已存在"); ConfEmployee entity; if (dto.Id > 0) { entity = await _repo.GetByIdAsync(dto.Id); if (entity == null) throw new ServiceException("配置不存在", 404); } else entity = new ConfEmployee(); entity.EmployeeId = dto.EmployeeId; entity.WorktypeId = dto.WorktypeId; entity.HoursFadingBz = dto.HoursFadingBz; entity.HoursFadingGz = dto.HoursFadingGz; entity.IsXuegong = dto.IsXuegong; entity.Memo = dto.Memo ?? ""; if (dto.Id > 0) await _repo.UpdateAsync(entity); else await _repo.AddAsync(entity); return entity.Id; } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("配置不存在", 404); await _repo.DeleteAsync(e); } } public class GwsrkhService : IGwsrkhService { private readonly IRepository _repo; private readonly IRepository _depRepo; private readonly IRepository _empRepo; public GwsrkhService(IRepository repo, IRepository depRepo, IRepository empRepo) { _repo = repo; _depRepo = depRepo; _empRepo = empRepo; } public async Task> GetListAsync(int year, int month, long depId) { var all = await _repo.GetAllAsync(); var q = all.AsQueryable().Where(x => x.Year == year && x.Month == month); if (depId > 0) q = q.Where(x => x.DepId == depId); var depts = (await _depRepo.GetAllAsync()).ToDictionary(d => d.Id, d => d.Name); var emps = (await _empRepo.GetAllAsync()).ToDictionary(e => e.Id); return q.ToList().OrderBy(x => x.EmployeeId).Select(x => new GwsrkhDto { Id = x.Id, Year = x.Year, Month = x.Month, DepId = x.DepId, DepName = depts.TryGetValue(x.DepId, out var dn) ? dn : "", EmployeeId = x.EmployeeId, EmployeeCode = emps.TryGetValue(x.EmployeeId, out var e) ? (e.Code ?? "") : "", EmployeeName = emps.TryGetValue(x.EmployeeId, out var e2) ? (e2.Name ?? "") : "", KhJieguo = x.KhJieguo ?? "", Memo = x.Memo ?? "" }).ToList(); } public async Task SaveAsync(GwsrkhDto dto) { if (dto.EmployeeId <= 0) throw new ServiceException("请选择职工"); if (string.IsNullOrWhiteSpace(dto.KhJieguo)) throw new ServiceException("请选择考核结果"); var exist = await _repo.FirstOrDefaultAsync(x => x.Year == dto.Year && x.Month == dto.Month && x.EmployeeId == dto.EmployeeId && x.Id != dto.Id); if (exist != null) throw new ServiceException("该职工本月考核结果已存在"); Gwsrkh entity; if (dto.Id > 0) { entity = await _repo.GetByIdAsync(dto.Id); if (entity == null) throw new ServiceException("记录不存在", 404); } else entity = new Gwsrkh(); entity.Year = dto.Year; entity.Month = dto.Month; entity.DepId = dto.DepId; entity.EmployeeId = dto.EmployeeId; entity.KhJieguo = dto.KhJieguo.Trim(); entity.Memo = dto.Memo ?? ""; if (dto.Id > 0) await _repo.UpdateAsync(entity); else await _repo.AddAsync(entity); return entity.Id; } public async Task DeleteAsync(long id) { var e = await _repo.GetByIdAsync(id); if (e == null) throw new ServiceException("记录不存在", 404); await _repo.DeleteAsync(e); } }