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.trade.exportrebate.dto.TaxRebatePurchaseAbstractInfo;
import com.trade.service.trade.exportrebate.dto.search.TaxRebatePurchaseAbstractInfoSearchDto;
import com.trade.service.trade.exportrebate.mapper.TaxRebatePurchaseDetailsMapper;
import com.trade.service.trade.exportrebate.domain.TaxRebatePurchaseDetails;
import com.trade.service.trade.exportrebate.service.TaxRebatePurchaseDetailsBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * 出口退税-进货明细表基础服务实现类
 *
 * @author hlb
 * @since 2024-10-24 20:48:58
 */
@Service("taxRebatePurchaseDetailsBaseService")
public class TaxRebatePurchaseDetailsBaseServiceImpl extends ServiceImpl<TaxRebatePurchaseDetailsMapper, TaxRebatePurchaseDetails> implements TaxRebatePurchaseDetailsBaseService {

    private static final Logger logger = LoggerFactory.getLogger(TaxRebatePurchaseDetailsBaseServiceImpl.class);
    @Resource
    private TaxRebatePurchaseDetailsMapper taxRebatePurchaseDetailsMapper;

    @Override
    public GridPage<TaxRebatePurchaseAbstractInfo> getTaxRebatePurchaseAbstractInfoPagesBySearch(
            TaxRebatePurchaseAbstractInfoSearchDto 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<TaxRebatePurchaseAbstractInfo> page = PageHelper.startPage(search.getPage(), search.getRows());
        List<TaxRebatePurchaseAbstractInfo> rows = taxRebatePurchaseDetailsMapper
                .getTaxRebatePurchaseAbstractInfoPagesBySearch(search);
        return new GridPage<>(search.getPage(), search.getRows(), page.getTotal(), rows);
    }

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

    @Override
    public List<TaxRebatePurchaseAbstractInfo> getByExportDetailIds(List<Long> exportDetailIds) {
        return taxRebatePurchaseDetailsMapper.getByExportDetailIds(exportDetailIds);
    }
}
