package com.talentcard.server.talentcard; import com.talentcard.server.common.BusinessException; import com.talentcard.server.common.ErrorCode; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class TalentCardService { private final TalentCardRepository talentCardRepository; public TalentCardService(TalentCardRepository talentCardRepository) { this.talentCardRepository = talentCardRepository; } @Transactional public TalentCardResponse create(TalentCardRequest request) { if (talentCardRepository.existsByCardNo(request.cardNo())) { throw new BusinessException(ErrorCode.CONFLICT, "人才卡号已存在"); } TalentCard talentCard = new TalentCard( request.talentId(), request.cardNo(), defaultCardType(request.cardType()), request.cardLevel() ); return TalentCardResponse.from(talentCardRepository.save(talentCard)); } @Transactional(readOnly = true) public TalentCardResponse getById(Long id) { return talentCardRepository.findById(id) .map(TalentCardResponse::from) .orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND, "人才卡不存在")); } @Transactional(readOnly = true) public Page page(Pageable pageable) { return talentCardRepository.findAll(pageable).map(TalentCardResponse::from); } private String defaultCardType(String cardType) { return cardType == null || cardType.isBlank() ? "PERSONAL" : cardType; } }