1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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<TalentCardResponse> page(Pageable pageable) {
        return talentCardRepository.findAll(pageable).map(TalentCardResponse::from);
    }
 
    private String defaultCardType(String cardType) {
        return cardType == null || cardType.isBlank() ? "PERSONAL" : cardType;
    }
}