package com.ga.api.service.product;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.core.mapper.product.InsuranceCompanyMapper;
import com.ga.core.vo.product.InsuranceCompanyVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class InsuranceCompanyService {

    private final InsuranceCompanyMapper mapper;

    @Transactional(readOnly = true)
    public List<InsuranceCompanyVO> list(boolean activeOnly) {
        return activeOnly ? mapper.selectActive() : mapper.selectAll();
    }

    @Transactional(readOnly = true)
    public InsuranceCompanyVO detail(Long companyId) {
        InsuranceCompanyVO vo = mapper.selectById(companyId);
        if (vo == null) throw new BizException(ErrorCode.NOT_FOUND);
        return vo;
    }

    @Transactional
    public Long create(InsuranceCompanyVO vo) {
        mapper.insert(vo);
        return vo.getCompanyId();
    }

    @Transactional
    public void update(Long companyId, InsuranceCompanyVO vo) {
        if (mapper.selectById(companyId) == null) throw new BizException(ErrorCode.NOT_FOUND);
        vo.setCompanyId(companyId);
        mapper.update(vo);
    }
}
