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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trade.service.filestorage.entity.TFileStorage;
import com.trade.service.filestorage.enums.FileModuleEnum;
import com.trade.service.filestorage.service.OSSFileService;
import com.trade.service.filestorage.util.FileUtil;
import com.trade.service.thirdparty.xiaotu.dto.BusinessCommonRequest;
import com.trade.service.thirdparty.xiaotu.dto.customsDeclarationFileResponse;
import com.trade.service.thirdparty.xiaotu.service.XiaotuRequestService;
import com.trade.service.trade.salesorder.domain.CustomsDeclarationFile;
import com.trade.service.trade.salesorder.mapper.CustomsDeclarationFileMapper;
import com.trade.service.trade.salesorder.service.ICustomsDeclarationFileService;
import com.trade.service.trade.filingdoc.vo.SalesOrder4CustomsFileQueryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 报关单相关文件 服务实现类
 * </p>
 *
 * @author zhangchenm
 * @since 2024-10-26
 */
@Service
public class CustomsDeclarationFileServiceImpl extends ServiceImpl<CustomsDeclarationFileMapper, CustomsDeclarationFile> implements ICustomsDeclarationFileService {

    @Resource
    private XiaotuRequestService xiaotuRequestService;
    @Resource
    private OSSFileService ossFileService;

    @Override
    public void downloadCustomsDeclarationFile(SalesOrder4CustomsFileQueryVO fileQueryVO) {
        CustomsDeclarationFile customsDeclarationFile = this.lambdaQuery()
                .eq(CustomsDeclarationFile::getCustomsNo, fileQueryVO.getQueryNo()).one();
        if (null != customsDeclarationFile) {
            if (null != customsDeclarationFile.getCustomsFileId()
                    && null != customsDeclarationFile.getEntrustFileId()) {
                return;
            }
        } else {
            customsDeclarationFile = new CustomsDeclarationFile();
            customsDeclarationFile.setCustomsNo(fileQueryVO.getQueryNo());
        }
        BusinessCommonRequest businessCommonRequest = new BusinessCommonRequest();
        businessCommonRequest.setXtEid(fileQueryVO.getXtEid());
        businessCommonRequest.setXtUid(fileQueryVO.getXtUserId());
        customsDeclarationFileResponse fileResponse = xiaotuRequestService
                .customsDeclarationFileQuery(fileQueryVO.getQueryNo(), fileQueryVO.getType(), businessCommonRequest);
        try {
            if (null != fileResponse.getBgdBlob()) {
                TFileStorage fileStorage = ossFileService.addStorageWithBase64Str(fileResponse.getBgdWjm(),
                        fileResponse.getBgdBlob(), FileModuleEnum.CUSTOMS_DECLARATION);
                customsDeclarationFile.setCustomsFileId(fileStorage.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (null != fileResponse.getWtbgxyBlob()) {
                TFileStorage fileStorage = ossFileService.addStorageWithBase64Str(fileResponse.getWtbgxyWjm(),
                        fileResponse.getWtbgxyBlob(), FileModuleEnum.ENTRUSTED_CUSTOMS_AGREEMENT);
                customsDeclarationFile.setEntrustFileId(fileStorage.getId());
                customsDeclarationFile.setEntrustNo(fileResponse.getXybh());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != customsDeclarationFile.getId()) {
            this.updateById(customsDeclarationFile);
        } else {
            this.save(customsDeclarationFile);
        }
    }

    @Override
    public CustomsDeclarationFile getByCustomsNo(String customsNo) {
        return this.lambdaQuery().eq(CustomsDeclarationFile::getCustomsNo, customsNo).one();
    }
}
