package com.trade.service.trade.exportrebate.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.common.constant.HttpStatus;
import com.trade.common.core.page.GridPage;
import com.trade.common.exception.ServiceException;
import com.trade.service.DictConstants;
import com.trade.service.trade.exportrebate.dto.*;
import com.trade.service.trade.exportrebate.dto.search.ExportTaxRebateDetailsForCSearchDto;
import com.trade.service.trade.exportrebate.dto.search.TaxRebateExportAbstractInfoSearchDto;
import com.trade.service.trade.exportrebate.dto.search.TaxRebateSearchDto;
import com.trade.service.trade.exportrebate.dto.search.TaxRebateSummarySearchDto;
import com.trade.service.trade.exportrebate.mapper.TaxRebateExportDetailsMapper;
import com.trade.service.trade.exportrebate.domain.TaxRebateExportDetails;
import com.trade.service.trade.exportrebate.service.TaxRebateExportDetailsBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 出口退税-出口明细表基础服务实现类
 *
 * @author hlb
 * @since 2024-10-22 15:14:59
 */
@Service("taxRebateExportDetailsBaseService")
public class TaxRebateExportDetailsBaseServiceImpl extends ServiceImpl<TaxRebateExportDetailsMapper, TaxRebateExportDetails> implements TaxRebateExportDetailsBaseService {

    private static final Logger logger = LoggerFactory.getLogger(TaxRebateExportDetailsBaseServiceImpl.class);
    @Resource
    private TaxRebateExportDetailsMapper taxRebateExportDetailsMapper;

    @Override
    public void batchUpdatePaymentStatusAndPaymentDate(List<Long> ids, String paymentStatus, Date paymentDate) {
        this.lambdaUpdate().in(TaxRebateExportDetails::getId, ids)
                .set(TaxRebateExportDetails::getPaymentStatus, paymentStatus)
                .set(paymentDate != null, TaxRebateExportDetails::getPaymentDate, paymentDate).update();
    }

    @Override
    public GridPage<ExportTaxRebateStatisticDto> getPagesBySearch(TaxRebateSearchDto search) {
        Page<ExportTaxRebateStatisticDto> page = PageHelper.startPage(search.getPage(), search.getRows());
        List<ExportTaxRebateStatisticDto> rows = taxRebateExportDetailsMapper.getPagesBySearch(search);
        return new GridPage<>(search.getPage(), search.getRows(), page.getTotal(), rows);
    }

    @Override
    public List<TaxRebateExportDetails> getDetailsByCSearch(ExportTaxRebateDetailsForCSearchDto search) {
        return taxRebateExportDetailsMapper.getDetailsByCSearch(search);
    }

    @Override
    public TaxRebateTotalDataDto getTaxRebateTotalDataBySearch(TaxRebateSummarySearchDto search) {
        return taxRebateExportDetailsMapper.getTaxRebateTotalDataBySearch(search);
    }

    @Override
    public GridPage<TaxRebateSummaryDataDto> getTaxRebateSummaryDataPagesBySearch(TaxRebateSummarySearchDto search) {
        Page<TaxRebateSummaryDataDto> page = PageHelper.startPage(search.getPage(), search.getRows());
        List<TaxRebateSummaryDataDto> rows = taxRebateExportDetailsMapper.getTaxRebateSummaryDataPagesBySearch(search);
        return new GridPage<>(search.getPage(), search.getRows(), page.getTotal(), rows);
    }

    @Override
    public GridPage<TaxRebateExportAbstractInfo> getTaxRebateExportAbstractInfoPagesBySearch(
            TaxRebateExportAbstractInfoSearchDto search) {
        if (search.getDeclarationMonthYear() == null) {
            throw new ServiceException("申报年月不能为空", HttpStatus.BAD_REQUEST);
        }
        if (StrUtil.isEmpty(search.getDeclarationBatch())) {
            throw new ServiceException("申报批次不能为空", HttpStatus.BAD_REQUEST);
        }
        if (StrUtil.isEmpty(search.getAgencyCertNo())) {
            throw new ServiceException("代理商证件号不能为空", HttpStatus.BAD_REQUEST);
        }
        if (StrUtil.isEmpty(search.getStatus())) {
            throw new ServiceException("退税状态不能为空", HttpStatus.BAD_REQUEST);
        }
        Page<TaxRebateExportAbstractInfo> page = PageHelper.startPage(search.getPage(), search.getRows());
        List<TaxRebateExportAbstractInfo> rows = taxRebateExportDetailsMapper
                .getTaxRebateExportAbstractInfoPagesBySearch(search);
        return new GridPage<>(search.getPage(), search.getRows(), page.getTotal(), rows);
    }

    @Override
    public List<TaxRebateExportDetails> getByCorrelationNos(List<String> correlationNos) {
        return this.lambdaQuery().in(TaxRebateExportDetails::getCorrelationNo, correlationNos)
                .eq(TaxRebateExportDetails::getDeleted, 0).list();
    }

    @Override
    public List<TaxRebateExportDetails> getByDeclarationSerialNos(List<String> declarationSerialNos) {
        return this.lambdaQuery().in(TaxRebateExportDetails::getDeclarationSerialNo, declarationSerialNos)
                .eq(TaxRebateExportDetails::getDeleted, 0).list();
    }

    @Override
    public GridPage<TaxRebateExportDetails> getPayableTaxRebateExportDetailsForSelfExportUser(int pageNum,
            int pageSize) {
        Page<TaxRebateExportDetails> page = PageHelper.startPage(pageNum, pageSize);
        List<TaxRebateExportDetails> rows = taxRebateExportDetailsMapper
                .getPayableTaxRebateExportDetailsForSelfExportUser();
        return new GridPage<>(pageNum, pageSize, page.getTotal(), rows);
    }
}
