YH-zlh
2024-12-13 734bb7b9cd7bd73aae6eefb0d0fab904d623b895
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/contract/service/impl/ContractServiceImpl.java
@@ -15,16 +15,23 @@
*/
package me.zhengjie.modules.contract.service.impl;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.config.FileProperties;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.constant.ChinaArea;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.contract.domain.*;
import me.zhengjie.modules.contract.repository.*;
import me.zhengjie.modules.contract.service.dto.*;
import me.zhengjie.modules.contract.service.ContractService;
import me.zhengjie.modules.contract.service.dto.ContractDto;
import me.zhengjie.modules.contract.service.dto.ContractFirstDto;
import me.zhengjie.modules.contract.service.dto.ContractQueryCriteria;
import me.zhengjie.modules.contract.service.dto.ContractSecondDto;
import me.zhengjie.modules.contract.service.mapstruct.ContractMapper;
import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.domain.SysCompany;
@@ -32,15 +39,14 @@
import me.zhengjie.modules.system.repository.DictRepository;
import me.zhengjie.modules.system.repository.SysCompanyRepository;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.contract.service.ContractService;
import me.zhengjie.modules.contract.service.mapstruct.ContractMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
@@ -49,10 +55,10 @@
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.io.IOException;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @website https://eladmin.vip
@@ -1474,4 +1480,75 @@
            return false;
        }
    }
    /**
     * 预警
     *
     * @param ids
     * @return
     */
    @Override
    public void addWarningStatus(Long[] ids) {
        for (Long id : ids) {
            Contract contract = contractRepository.findById(id).orElseGet(Contract::new);
            if (contract.getWarningStatus() != null) {
                if (contract.getWarningStatus() == 0) {
                    contract.setWarningStatus(1);
                } else {
                    contract.setWarningStatus(0);
                }
            } else {
                contract.setWarningStatus(1);
            }
            contractRepository.save(contract);
        }
    }
    /**
     * 下载合同附件
     *
     * @param id
     * @return Map<String, Object>
     */
    @Override
    public void downloadContractAttachments(Long id, HttpServletResponse response) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ZipOutputStream zipOutputStream = null;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        // 合同
        Contract contract = contractRepository.findById(id).orElseGet(Contract::new);
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
            String relatedDocumentsAttachment = contract.getRelatedDocumentsAttachment();
            String[] attachments = relatedDocumentsAttachment.split(",");
            for (String attachment : attachments) {
                if (StringUtils.isNotBlank(attachment)) {
                    String fileName = attachment.substring(attachment.lastIndexOf("/") + 1);
                    String suffix = FileUtil.getExtensionName(fileName);
                    String type = FileUtil.getFileType(suffix);
                    File file = new File(properties.getPath().getPath() + type + File.separator + fileName);
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    ZipEntry zipEntry = new ZipEntry(fileName);
                    zipOutputStream.putNextEntry(zipEntry);
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = bis.read(buffer)) > 0) {
                        zipOutputStream.write(buffer, 0, length);
                    }
                    zipOutputStream.flush();
                    zipOutputStream.closeEntry();
                }
            }
            IoUtil.close(zipOutputStream);
            FileUtil.downloadZip(byteArrayOutputStream, response);
        } catch (Exception e) {
            throw new BadRequestException(e.getMessage());
        } finally {
            IoUtil.close(zipOutputStream);
            IoUtil.close(byteArrayOutputStream);
        }
    }
}