package com.ruoyi.ccdi.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ccdi.project.domain.CcdiProject;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedPurchaseDetailQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedPurchaseQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedRecruitmentDetailQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedRecruitmentQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedTransferDetailQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedTransferQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectFamilyAssetLiabilityDetailQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectFamilyAssetLiabilityListQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectIncreaseLendingQueryDTO;
import com.ruoyi.ccdi.project.domain.excel.CcdiProjectIncreaseLendingExcel;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedPurchaseDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedPurchaseListItemVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedPurchaseListVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedRecruitmentDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedRecruitmentListItemVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedRecruitmentListVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedTransferDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedTransferListItemVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedTransferListVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetLiabilityDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetLiabilityListItemVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetLiabilityListVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyDebtDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyIncomeDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectIncreaseLendingListItemVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectIncreaseLendingListVO;
import com.ruoyi.ccdi.project.mapper.CcdiProjectMapper;
import com.ruoyi.ccdi.project.mapper.CcdiProjectSpecialCheckMapper;
import com.ruoyi.ccdi.project.service.ICcdiProjectSpecialCheckService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * 项目专项核查服务实现
 */
@Service
public class CcdiProjectSpecialCheckServiceImpl implements ICcdiProjectSpecialCheckService {

    @Resource
    private CcdiProjectSpecialCheckMapper specialCheckMapper;

    @Resource
    private CcdiProjectMapper projectMapper;

    @Override
    public CcdiProjectFamilyAssetLiabilityListVO getFamilyAssetLiabilityList(
        CcdiProjectFamilyAssetLiabilityListQueryDTO queryDTO
    ) {
        ensureProjectExists(queryDTO.getProjectId());

        CcdiProjectFamilyAssetLiabilityListVO result = new CcdiProjectFamilyAssetLiabilityListVO();
        result.setRows(defaultList(specialCheckMapper.selectFamilyAssetLiabilityList(queryDTO.getProjectId())));
        return result;
    }

    @Override
    public CcdiProjectFamilyAssetLiabilityDetailVO getFamilyAssetLiabilityDetail(
        CcdiProjectFamilyAssetLiabilityDetailQueryDTO queryDTO
    ) {
        ensureProjectExists(queryDTO.getProjectId());

        CcdiProjectFamilyAssetLiabilityDetailVO detail = specialCheckMapper.selectFamilyAssetLiabilityDetail(
            queryDTO.getProjectId(),
            queryDTO.getStaffIdCard()
        );
        if (detail == null) {
            throw new ServiceException("当前员工不属于该项目专项核查范围");
        }
        normalizeDetail(detail);
        return detail;
    }

    @Override
    public CcdiProjectExtendedPurchaseListVO getExtendedPurchaseList(CcdiProjectExtendedPurchaseQueryDTO queryDTO) {
        ensureProjectExists(queryDTO.getProjectId());

        Page<CcdiProjectExtendedPurchaseListItemVO> page = new Page<>(
            defaultPageNum(queryDTO.getPageNum()),
            defaultPageSize(queryDTO.getPageSize())
        );
        Page<CcdiProjectExtendedPurchaseListItemVO> resultPage = specialCheckMapper.selectExtendedPurchasePage(page, queryDTO);

        CcdiProjectExtendedPurchaseListVO result = new CcdiProjectExtendedPurchaseListVO();
        result.setRows(resultPage == null ? List.of() : defaultList(resultPage.getRecords()));
        result.setTotal(resultPage == null ? 0L : resultPage.getTotal());
        return result;
    }

    @Override
    public CcdiProjectExtendedPurchaseDetailVO getExtendedPurchaseDetail(
        CcdiProjectExtendedPurchaseDetailQueryDTO queryDTO
    ) {
        ensureProjectExists(queryDTO.getProjectId());

        CcdiProjectExtendedPurchaseDetailVO detail = specialCheckMapper.selectExtendedPurchaseDetail(
            queryDTO.getProjectId(),
            queryDTO.getPurchaseId()
        );
        if (detail == null) {
            throw new ServiceException("当前记录不属于该项目专项核查范围");
        }
        detail.setSupplierList(defaultList(
            specialCheckMapper.selectExtendedPurchaseSuppliers(queryDTO.getProjectId(), queryDTO.getPurchaseId())
        ));
        return detail;
    }

    @Override
    public CcdiProjectExtendedRecruitmentListVO getExtendedRecruitmentList(
        CcdiProjectExtendedRecruitmentQueryDTO queryDTO
    ) {
        ensureProjectExists(queryDTO.getProjectId());

        Page<CcdiProjectExtendedRecruitmentListItemVO> page = new Page<>(
            defaultPageNum(queryDTO.getPageNum()),
            defaultPageSize(queryDTO.getPageSize())
        );
        Page<CcdiProjectExtendedRecruitmentListItemVO> resultPage = specialCheckMapper.selectExtendedRecruitmentPage(
            page,
            queryDTO
        );

        CcdiProjectExtendedRecruitmentListVO result = new CcdiProjectExtendedRecruitmentListVO();
        result.setRows(resultPage == null ? List.of() : defaultList(resultPage.getRecords()));
        result.setTotal(resultPage == null ? 0L : resultPage.getTotal());
        return result;
    }

    @Override
    public CcdiProjectExtendedRecruitmentDetailVO getExtendedRecruitmentDetail(
        CcdiProjectExtendedRecruitmentDetailQueryDTO queryDTO
    ) {
        ensureProjectExists(queryDTO.getProjectId());

        CcdiProjectExtendedRecruitmentDetailVO detail = specialCheckMapper.selectExtendedRecruitmentDetail(
            queryDTO.getProjectId(),
            queryDTO.getRecruitId()
        );
        if (detail == null) {
            throw new ServiceException("当前记录不属于该项目专项核查范围");
        }
        return detail;
    }

    @Override
    public CcdiProjectExtendedTransferListVO getExtendedTransferList(CcdiProjectExtendedTransferQueryDTO queryDTO) {
        ensureProjectExists(queryDTO.getProjectId());

        Page<CcdiProjectExtendedTransferListItemVO> page = new Page<>(
            defaultPageNum(queryDTO.getPageNum()),
            defaultPageSize(queryDTO.getPageSize())
        );
        Page<CcdiProjectExtendedTransferListItemVO> resultPage = specialCheckMapper.selectExtendedTransferPage(page, queryDTO);

        CcdiProjectExtendedTransferListVO result = new CcdiProjectExtendedTransferListVO();
        result.setRows(resultPage == null ? List.of() : defaultList(resultPage.getRecords()));
        result.setTotal(resultPage == null ? 0L : resultPage.getTotal());
        return result;
    }

    @Override
    public CcdiProjectExtendedTransferDetailVO getExtendedTransferDetail(
        CcdiProjectExtendedTransferDetailQueryDTO queryDTO
    ) {
        ensureProjectExists(queryDTO.getProjectId());

        CcdiProjectExtendedTransferDetailVO detail = specialCheckMapper.selectExtendedTransferDetail(
            queryDTO.getProjectId(),
            queryDTO.getId()
        );
        if (detail == null) {
            throw new ServiceException("当前记录不属于该项目专项核查范围");
        }
        return detail;
    }

    @Override
    public CcdiProjectIncreaseLendingListVO getIncreaseLendingList(CcdiProjectIncreaseLendingQueryDTO queryDTO) {
        validateIncreaseLendingQuery(queryDTO);

        Page<CcdiProjectIncreaseLendingListItemVO> page = new Page<>(
            defaultPageNum(queryDTO.getPageNum()),
            defaultPageSize(queryDTO.getPageSize())
        );
        Page<CcdiProjectIncreaseLendingListItemVO> resultPage = specialCheckMapper.selectIncreaseLendingPage(page, queryDTO);

        CcdiProjectIncreaseLendingListVO result = new CcdiProjectIncreaseLendingListVO();
        result.setRows(resultPage == null ? List.of() : defaultList(resultPage.getRecords()));
        result.setTotal(resultPage == null ? 0L : resultPage.getTotal());
        return result;
    }

    @Override
    public List<CcdiProjectIncreaseLendingExcel> exportIncreaseLendingList(CcdiProjectIncreaseLendingQueryDTO queryDTO) {
        validateIncreaseLendingQuery(queryDTO);
        return defaultList(specialCheckMapper.selectIncreaseLendingExportList(queryDTO));
    }

    private void validateIncreaseLendingQuery(CcdiProjectIncreaseLendingQueryDTO queryDTO) {
        ensureProjectExists(queryDTO.getProjectId());
        if (StringUtils.isBlank(queryDTO.getStaffId())
            && StringUtils.isBlank(queryDTO.getStaffIdCard())
            && StringUtils.isBlank(queryDTO.getApprover())) {
            throw new ServiceException("柜员号、员工身份证和审核人柜员号至少填写一项");
        }
    }

    private void ensureProjectExists(Long projectId) {
        CcdiProject project = projectMapper.selectById(projectId);
        if (project == null) {
            throw new ServiceException("项目不存在");
        }
    }

    private void normalizeDetail(CcdiProjectFamilyAssetLiabilityDetailVO detail) {
        if (detail.getIncomeDetail() == null) {
            CcdiProjectFamilyIncomeDetailVO incomeDetail = new CcdiProjectFamilyIncomeDetailVO();
            incomeDetail.setSelfIncome(BigDecimal.ZERO);
            incomeDetail.setSpouseIncome(BigDecimal.ZERO);
            incomeDetail.setTotalIncome(BigDecimal.ZERO);
            incomeDetail.setExplainableIncome(BigDecimal.ZERO);
            detail.setIncomeDetail(incomeDetail);
        }

        if (detail.getAssetDetail() == null) {
            CcdiProjectFamilyAssetDetailVO assetDetail = new CcdiProjectFamilyAssetDetailVO();
            assetDetail.setMissingSelfAssetInfo(false);
            assetDetail.setSelfTotalAsset(BigDecimal.ZERO);
            assetDetail.setSpouseTotalAsset(BigDecimal.ZERO);
            assetDetail.setTotalAsset(BigDecimal.ZERO);
            assetDetail.setItems(List.of());
            detail.setAssetDetail(assetDetail);
        } else if (detail.getAssetDetail().getItems() == null) {
            detail.getAssetDetail().setItems(List.of());
        }
        if (detail.getAssetDetail().getMissingSelfAssetInfo() == null) {
            detail.getAssetDetail().setMissingSelfAssetInfo(false);
        }

        if (detail.getDebtDetail() == null) {
            CcdiProjectFamilyDebtDetailVO debtDetail = new CcdiProjectFamilyDebtDetailVO();
            debtDetail.setMissingSelfDebtInfo(false);
            debtDetail.setSelfTotalDebt(BigDecimal.ZERO);
            debtDetail.setSpouseTotalDebt(BigDecimal.ZERO);
            debtDetail.setTotalDebt(BigDecimal.ZERO);
            debtDetail.setItems(List.of());
            detail.setDebtDetail(debtDetail);
        } else if (detail.getDebtDetail().getItems() == null) {
            detail.getDebtDetail().setItems(List.of());
        }
        if (detail.getDebtDetail().getMissingSelfDebtInfo() == null) {
            detail.getDebtDetail().setMissingSelfDebtInfo(false);
        }

        if (detail.getSummary() == null) {
            detail.setSummary(new CcdiProjectFamilyAssetLiabilityListItemVO());
        }
    }

    private <T> List<T> defaultList(List<T> list) {
        return list == null ? List.of() : list;
    }

    private long defaultPageNum(Integer pageNum) {
        return pageNum == null || pageNum < 1 ? 1L : pageNum.longValue();
    }

    private long defaultPageSize(Integer pageSize) {
        return pageSize == null || pageSize < 1 ? 10L : pageSize.longValue();
    }
}
