package com.trade.admin.trade.purchaseorder.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import cn.hutool.core.util.StrUtil;
import com.trade.admin.job.PurchaseInvoiceSchedule;
import com.trade.admin.trade.purchaseorder.service.BPurchaseOrderService;
import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.trade.common.utils.StringUtils;
import com.trade.service.system.domain.InvoiceTypeMapping;
import com.trade.service.system.service.InvoiceTypeMappingService;
import com.trade.service.thirdparty.xiaotu.service.XiaotuRequestService;
import com.trade.service.trade.purchaseorder.dto.*;
import com.trade.service.trade.purchaseorder.service.PurchaseOrderInvoiceService;
import com.trade.service.trade.purchaseorder.service.PurchaseOrderLogisticsService;
import com.trade.service.trade.purchaseorder.vo.*;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.trade.common.core.page.GridPage;

import com.trade.common.utils.LoggerUtil;

import com.trade.service.trade.purchaseorder.service.PurchaseOrderService;

/**
 * @description: 采购订单服务实现类
 * @Author: wanglang
 * @Date: 2024/9/12
 * @Version: 1.0
 */
@Service
public class BPurchaseOrderServiceImpl implements BPurchaseOrderService {

    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);;
    @Resource
    private PurchaseOrderService purchaseOrderService;
    @Resource
    private PurchaseOrderLogisticsService purchaseOrderLogisticsService;
    @Resource
    private PurchaseInvoiceSchedule purchaseInvoiceSchedule;
    @Resource
    private PurchaseOrderInvoiceService purchaseOrderInvoiceService;
    @Resource
    private InvoiceTypeMappingService invoiceTypeMappingService;
    @Resource
    private XiaotuRequestService xiaotuRequestService;

    @Override
    public GridPage<PurchaseOrderVo> queryListByPage(PurchaseSearchDto purchaseSearch) {
        purchaseSearch.setCustomerUserId(null);
        purchaseSearch.setPoint(false);
        Page<PurchaseOrderVo> page = PageHelper.startPage(purchaseSearch.getPage(), purchaseSearch.getRows());
        List<PurchaseOrderVo> voList = purchaseOrderService.selectByCondition(purchaseSearch);
        return new GridPage<>(purchaseSearch.getPage(), purchaseSearch.getRows(), page.getTotal(), voList);
    }

    @Override
    public PurchaseOrderDetailsVo getDetailsById(Long id) {
        return purchaseOrderService.getDetailsById(id);
    }

    @Override
    public GridPage<PurchaseOrderLogisticsPageVo> queryOrderLogisticsByPage(
            PurchaseOrderLogisticsSearchDto orderLogisticsSearch) {
        orderLogisticsSearch.setCustomerUserId(null);
        orderLogisticsSearch.setPoint(false);
        Page<PurchaseOrderVo> page = PageHelper.startPage(orderLogisticsSearch.getPage(),
                orderLogisticsSearch.getRows());
        List<PurchaseOrderLogisticsPageVo> voList = purchaseOrderLogisticsService
                .queryOrderLogisticsByPage(orderLogisticsSearch);
        return new GridPage<>(orderLogisticsSearch.getPage(), orderLogisticsSearch.getRows(), page.getTotal(), voList);
    }

    @Override
    public Boolean pullPurchaseInvoice(List<Long> orderIds) {
        purchaseInvoiceSchedule.pullPurchaseInvoice(orderIds, 1);
        return true;
    }

    @Override
    public GridPage<PurchaseOrderInvoiceVo> queryInvoiceListByPage(
            PurchaseOrderInvoiceSearchDto purchaseOrderInvoiceSearchDto) {
        purchaseOrderInvoiceSearchDto.setSource(1);
        if (StringUtils.isNotEmpty(purchaseOrderInvoiceSearchDto.getInvoiceType())) {
            List<String> types = invoiceTypeMappingService
                    .getInvoiceKjtCode(purchaseOrderInvoiceSearchDto.getInvoiceType()).stream()
                    .map(InvoiceTypeMapping::getCode).collect(Collectors.toList());
            purchaseOrderInvoiceSearchDto.setInvoiceTypeList(types);
        }
        Page<PurchaseOrderVo> page = PageHelper.startPage(purchaseOrderInvoiceSearchDto.getPage(),
                purchaseOrderInvoiceSearchDto.getRows());
        List<PurchaseOrderInvoiceVo> voList = purchaseOrderInvoiceService
                .queryByCondition(purchaseOrderInvoiceSearchDto);
        return new GridPage<>(purchaseOrderInvoiceSearchDto.getPage(), purchaseOrderInvoiceSearchDto.getRows(),
                page.getTotal(), voList);
    }

    @Override
    public PurchaseOrderInvoiceDetailsVo getInvoiceDetails(Long InvoiceId) {
        return purchaseOrderInvoiceService.getDetails(InvoiceId);
    }

    @Override
    public GridPage<PurchaseOrderInvoiceDocumentVo> queryInvoiceDocumentListByPage(
            PurchaseOrderInvoiceDocumentSearchDto documentSearchDto) {
        if (StringUtils.isNotEmpty(documentSearchDto.getInvoiceType())) {
            List<String> types = invoiceTypeMappingService.getInvoiceKjtCode(documentSearchDto.getInvoiceType())
                    .stream().map(InvoiceTypeMapping::getCode).collect(Collectors.toList());
            documentSearchDto.setInvoiceTypeList(types);
        }
        Page<PurchaseOrderVo> page = PageHelper.startPage(documentSearchDto.getPage(), documentSearchDto.getRows());
        List<PurchaseOrderInvoiceDocumentVo> voList = purchaseOrderInvoiceService
                .queryPurchaseOrderInvoiceDocument(documentSearchDto);
        return new GridPage<>(documentSearchDto.getPage(), documentSearchDto.getRows(), page.getTotal(), voList);
    }

    @Override
    public GridPage<PurchaseOrderLogisticsDocumentVo> queryPurchaseOrderLogisticsDocument(
            PurchaseOrderLogisticsDocumentSearchDto documentSearchDto) {
        documentSearchDto.setCustomerUserId(null);
        Page<PurchaseOrderVo> page = PageHelper.startPage(documentSearchDto.getPage(), documentSearchDto.getRows());
        List<PurchaseOrderLogisticsDocumentVo> voList = purchaseOrderLogisticsService
                .queryPurchaseOrderLogisticsDocument(documentSearchDto);
        return new GridPage<>(documentSearchDto.getPage(), documentSearchDto.getRows(), page.getTotal(), voList);
    }

    @Override
    public List<String> queryLogisticsCompanyName(String purchaseContractNo) {
        return purchaseOrderLogisticsService.queryByPurchaseContractNo(purchaseContractNo).stream()
                .map(PurchaseOrderLogisticsVo::getCompanyName).collect(Collectors.toList());
    }

    @Override
    public String getDownloadPurchaseInvoiceUrl(String xtUserId, String xtEid) {
        if (StrUtil.isEmpty(xtUserId) || StrUtil.isEmpty(xtEid)) {
            throw new ServiceException("贸易平台未认证", HttpStatus.BAD_REQUEST);
        }
        return xiaotuRequestService.getExportRebateUrl(xtUserId, xtEid, "/02/0202/020203");
    }
}
