package com.trade.service.trade.salesorder.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.trade.service.filestorage.dto.FileAbstractDto;
import com.trade.service.filestorage.service.TFileStorageService;
import com.trade.service.thirdparty.xiaotu.service.XiaotuRequestService;
import com.trade.service.trade.filingdoc.vo.CustomsDeclaration4FilingDocSearch;
import com.trade.service.trade.filingdoc.vo.CustomsDeclaration4FilingDocVO;
import com.trade.service.trade.filingdoc.vo.SalesOrder4CustomsFileQueryVO;
import com.trade.service.trade.salesorder.domain.CustomsDeclaration;
import com.trade.service.trade.salesorder.mapper.CustomsDeclarationMapper;
import com.trade.service.trade.salesorder.service.ICustomsDeclarationGoodsService;
import com.trade.service.trade.salesorder.service.ICustomsDeclarationService;
import com.trade.service.trade.salesorder.vo.*;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

/**
 * <p>
 * 报关单信息 服务实现类
 * </p>
 *
 * @author zhangchenm
 * @since 2024-09-19
 */
@Service
public class CustomsDeclarationServiceImpl extends ServiceImpl<CustomsDeclarationMapper, CustomsDeclaration> implements ICustomsDeclarationService {

    @Resource
    private ICustomsDeclarationGoodsService customsDeclarationGoodsService;
    @Resource
    private TFileStorageService tFileStorageService;
    @Resource
    private XiaotuRequestService xiaotuRequestService;

    @Override
    public void addCustomsDeclaration(CustomsDeclaration customsDeclaration) {
        this.save(customsDeclaration);
    }

    @Override
    public CustomsDeclaration getByBgdh(String bgdh) {
        return this.lambdaQuery().eq(CustomsDeclaration::getBgdhgbh, bgdh).one();
    }

    @Override
    public void updateCustomsDeclaration(CustomsDeclaration customsDeclaration) {
        this.updateById(customsDeclaration);
    }

    @Override
    public List<SalesOrder4CustomsDeclarationVO> queryOrderNo4UpdateCustomsDeclaration() {
        return this.baseMapper.queryOrderNo4UpdateCustomsDeclaration();
    }

    @Override
    public PageInfo<CustomsDeclarationVO> queryListBySearch(CustomsDeclarationSearch search) {
        PageHelper.startPage(search.getPage(), search.getRows());
        List<CustomsDeclarationVO> list = this.baseMapper.queryListBySearch(search);
        return new PageInfo<>(list);
    }

    @Override
    public CustomsDeclarationDtlVO getDetailById(Long id) {
        CustomsDeclarationDtlVO customsDeclarationDtlVO = this.baseMapper.getDetailById(id);
        customsDeclarationDtlVO
                .setHggq(customsDeclarationDtlVO.getSbhggqkajc() + "(" + customsDeclarationDtlVO.getSbhggqkaDm() + ")");
        customsDeclarationDtlVO.setCustomsDeclarationGoodsDtlVOList(customsDeclarationGoodsService
                .listWithCustomsDeclarationDtlByOrderId(customsDeclarationDtlVO.getOrderId()));
        Optional.ofNullable(customsDeclarationDtlVO.getEntrustFileId()).ifPresent(fileId -> customsDeclarationDtlVO
                .setEntrustFile(new FileAbstractDto(tFileStorageService.getFileStorageById(fileId))));
        Optional.ofNullable(customsDeclarationDtlVO.getCustomsFileId()).ifPresent(fileId -> customsDeclarationDtlVO
                .setCustomsFile(new FileAbstractDto(tFileStorageService.getFileStorageById(fileId))));
        return customsDeclarationDtlVO;
    }

    @Override
    public List<SalesOrder4CustomsFileQueryVO> queryData4DownloadCustomsDeclarationFile() {
        return this.baseMapper.queryData4DownloadCustomsDeclarationFile();
    }

    @Override
    public PageInfo<CustomsDeclaration4FilingDocVO> queryList4FilingDocBySearch(
            CustomsDeclaration4FilingDocSearch search) {
        PageHelper.startPage(search.getPage(), search.getRows());
        List<CustomsDeclaration4FilingDocVO> list = this.baseMapper.queryList4FilingDocBySearch(search);
        list.forEach(item -> {
            Optional.ofNullable(item.getEntrustFileId()).ifPresent(
                    fileId -> item.setEntrustFile(new FileAbstractDto(tFileStorageService.getFileStorageById(fileId))));
            Optional.ofNullable(item.getCustomsFileId()).ifPresent(
                    fileId -> item.setCustomsFile(new FileAbstractDto(tFileStorageService.getFileStorageById(fileId))));
        });
        return new PageInfo<>(list);
    }

    @Override
    public List<String> queryAllDeclareCompany4Dict() {
        return this.baseMapper.queryAllDeclareCompany4Dict();
    }

    @Override
    public List<CustomsDeclaration> queryByOrderNo(String orderNo) {
        return this.lambdaQuery().eq(CustomsDeclaration::getSaleOrderNo, orderNo).list();
    }

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