namespace TaxBaik.Application.Services; using TaxBaik.Application.DTOs; using TaxBaik.Domain.Entities; using TaxBaik.Domain.Interfaces; public class ClientService(IClientRepository repository) { public static readonly string[] ServiceTypes = ["기장", "부동산", "증여·상속", "종합소득세", "법인세", "부가가치세", "기타"]; public static readonly string[] TaxTypes = ["개인사업자", "법인사업자", "면세사업자", "근로소득자", "기타"]; public static readonly string[] Sources = ["홈페이지 문의", "소개", "직접 방문", "카카오 채널", "블로그", "기타"]; public async Task<(IEnumerable Items, int Total)> GetPagedAsync( int page, int pageSize, string? status = null, string? search = null, CancellationToken ct = default) => await repository.GetPagedAsync(Math.Max(1, page), Math.Clamp(pageSize, 1, 100), status, search, ct); public async Task GetByIdAsync(int id, CancellationToken ct = default) => await repository.GetByIdAsync(id, ct); public async Task GetByEmailAsync(string email, CancellationToken ct = default) => await repository.GetByEmailAsync(email, ct); public async Task GetByPhoneAsync(string phone, CancellationToken ct = default) => await repository.GetByPhoneAsync(phone, ct); public async Task CountCreatedAtRangeAsync(DateTime startDateUtc, DateTime endDateUtc, CancellationToken ct = default) => await repository.CountByCreatedAtRangeAsync(startDateUtc, endDateUtc, ct); public async Task CreateAsync(CreateClientDto dto, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(dto.Name)) throw new ValidationException("고객명을 입력하세요."); var client = new Client { Name = dto.Name.Trim(), CompanyName = dto.CompanyName?.Trim(), Phone = dto.Phone?.Trim(), Email = dto.Email?.Trim(), ServiceType = dto.ServiceType, TaxType = dto.TaxType, Status = dto.Status, Source = dto.Source, Memo = dto.Memo?.Trim() }; return await repository.CreateAsync(client, ct); } public async Task UpdateAsync(int id, CreateClientDto dto, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(dto.Name)) throw new ValidationException("고객명을 입력하세요."); var client = await repository.GetByIdAsync(id, ct) ?? throw new KeyNotFoundException($"고객 ID {id}를 찾을 수 없습니다."); client.Name = dto.Name.Trim(); client.CompanyName = dto.CompanyName?.Trim(); client.Phone = dto.Phone?.Trim(); client.Email = dto.Email?.Trim(); client.ServiceType = dto.ServiceType; client.TaxType = dto.TaxType; client.Status = dto.Status; client.Source = dto.Source; client.Memo = dto.Memo?.Trim(); await repository.UpdateAsync(client, ct); } public async Task CreateFromInquiryAsync(string name, string? phone, string? serviceType, CancellationToken ct = default) { var client = new Client { Name = name.Trim(), Phone = phone?.Trim(), ServiceType = serviceType, Status = "active", Source = "홈페이지 문의" }; return await repository.CreateAsync(client, ct); } public async Task DeleteAsync(int id, CancellationToken ct = default) => await repository.DeleteAsync(id, ct); }