package com.ga.api.service.income;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.common.model.PageResponse;
import com.ga.core.mapper.income.AgentAnnualIncomeMapper;
import com.ga.core.vo.income.AgentAnnualIncomeResp;
import com.ga.core.vo.income.AgentAnnualIncomeSearchParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AgentAnnualIncomeService {

    private final AgentAnnualIncomeMapper mapper;

    public PageResponse<AgentAnnualIncomeResp> list(AgentAnnualIncomeSearchParam param) {
        param.startPage();
        return PageResponse.of(mapper.selectList(param));
    }

    public AgentAnnualIncomeResp detail(Long agentId, int settleYear) {
        AgentAnnualIncomeResp resp = mapper.selectDetailById(agentId, settleYear);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    @Transactional
    public int regenerate(int year) {
        return mapper.upsertAggregate(year);
    }
}
