package com.ga.api.service.tax;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.common.model.PageResponse;
import com.ga.core.mapper.tax.VatReportMapper;
import com.ga.core.vo.tax.VatReportResp;
import com.ga.core.vo.tax.VatReportSaveReq;
import com.ga.core.vo.tax.VatReportSearchParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

    private final VatReportMapper mapper;

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

    public VatReportResp detail(Long vatId) {
        VatReportResp resp = mapper.selectDetailById(vatId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    @Transactional
    public int regenerate(VatReportSaveReq req) {
        return mapper.upsertAggregate(req.getSettleQuarter());
    }

    @Transactional
    public void complete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BizException(ErrorCode.INVALID_PARAMETER);
        }
        mapper.updateFileStatusToFiled(ids);
    }
}
