package com.ga.common.code;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AccountCodeService {

    private final AccountCodeMapper mapper;

    public List<AccountCodeVO> getActiveCodes(String accountType) {
        return mapper.selectActive(accountType);
    }

    public AccountCodeVO getCode(String accountCode) {
        AccountCodeVO vo = mapper.selectOne(accountCode);
        if (vo == null) throw new BizException(ErrorCode.NOT_FOUND);
        return vo;
    }

    public List<AccountCodeVO> listAll(String accountType) {
        return mapper.selectAll(accountType);
    }

    @Transactional
    public void create(AccountCodeVO vo) {
        if (mapper.existsByCode(vo.getAccountCode()) > 0) {
            throw new BizException(ErrorCode.DUPLICATE_DATA);
        }
        mapper.insert(vo);
    }

    @Transactional
    public void update(AccountCodeVO vo) {
        getCode(vo.getAccountCode());
        mapper.update(vo);
    }

    @Transactional
    public void delete(String accountCode) {
        getCode(accountCode);
        mapper.delete(accountCode);
    }
}
