baolhyhzhg
2024-12-16 6009943ffdde7acbf0f06dfb1d2542cf3532d910
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/contract/service/impl/ContractServiceImpl.java
@@ -15,16 +15,20 @@
*/
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.ContractService;
import me.zhengjie.modules.contract.service.dto.*;
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 +36,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 +52,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
@@ -233,19 +236,19 @@
    @Override
    public void download(List<ContractDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, String> contractTypeMap = dictDetailRepository.findByDictName(Constant.CONTRACT_DICT_TYPE).parallelStream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
        Map<String, String> paymentStateMap = dictDetailRepository.findByDictName(Constant.PAYMENT_STATE).parallelStream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
        Map<String, String> contractStatusMap = dictDetailRepository.findByDictName(Constant.CONTRACT_STATUS).parallelStream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
        for (ContractDto contract : all) {
            String firstName = contractFirstName(contractMapper.toEntity(contract));
            String secondName = contractSecondName(contractMapper.toEntity(contract));
            String contractType = contract.getContractType() != null ? dictDetailRepository.findByDictNameAndValue(Constant.CONTRACT_DICT_TYPE, contract.getContractType()).getLabel() : "";
            String contractPaymentState = contract.getContractPaymentState() != null ? dictDetailRepository.findByDictNameAndValue(Constant.PAYMENT_STATE, contract.getContractPaymentState()).getLabel() : "";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String servicePaymentDate = null;
            if(contract.getServicePaymentDate() != null) {
                servicePaymentDate = sdf.format(contract.getServicePaymentDate());
                servicePaymentDate = servicePaymentDate.substring(5, 10);
            }
            String servicePaymentState = contract.getServicePaymentState() != null ? dictDetailRepository.findByDictNameAndValue(Constant.PAYMENT_STATE, contract.getServicePaymentState()).getLabel() : "";
            String contractStatus = contract.getContractStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.CONTRACT_STATUS, contract.getContractStatus()).getLabel() : "";
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("合同编号", contract.getContractNumber());
            map.put("合同名称", contract.getContractName());
@@ -255,7 +258,7 @@
            map.put("起始日期", contract.getStartDate());
            map.put("截止日期", contract.getEndDate());
            map.put("合同所属地区", contract.getContractArea());
            map.put("合同类型(一级)", contractType);
            map.put("合同类型(一级)", contractTypeMap.containsKey(contract.getContractType()) ? contractTypeMap.get(contract.getContractType()) : "");
            map.put("合同类型(二级)", contract.getContractSecondType());
            map.put("合同内容", contract.getContractContent());
            map.put("开票总金额", contract.getBillingTotalAmount());
@@ -264,21 +267,113 @@
            map.put("应收款金额", contract.getReceivePayment());
            map.put("合同费金额", contract.getContractAmount());
            map.put("合同费预计付款日期", contract.getContractPaymentDate());
            map.put("合同费付款状态", contractPaymentState);
            map.put("合同费付款状态", paymentStateMap.containsKey(contract.getContractPaymentState()) ? paymentStateMap.get(contract.getContractPaymentState()) : "");
            map.put("合同费延期情况", contract.getContractPostponement());
            map.put("服务开始日期", contract.getServiceStartsDate());
            map.put("服务费金额", contract.getServiceAmount());
            map.put("服务费预计付款日期", servicePaymentDate);
            map.put("服务费付款状态", servicePaymentState);
            map.put("服务费付款状态", paymentStateMap.containsKey(contract.getServicePaymentState()) ? paymentStateMap.get(contract.getServicePaymentState()) : "");
            map.put("服务费延期情况", contract.getServicePostponement());
            map.put("合同初版文件", contract.getRelatedDocumentsFirst());
            map.put("合同终版文件", contract.getRelatedDocumentsFinal());
            map.put("合同状态", contractStatus);
            map.put("合同状态", contractStatusMap.containsKey(contract.getContractStatus()) ? contractStatusMap.get(contract.getContractStatus()) : "");
            map.put("备注信息", contract.getRemarks());
            map.put("创建者", contract.getCreateBy());
            map.put("创建日期", contract.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    @Override
    public void download(ContractQueryCriteria criteria, HttpServletResponse response) throws IOException {
        // 查询合同列表
        List<Contract> contractList = contractRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, String> contractTypeMap = dictDetailRepository.findByDictName(Constant.CONTRACT_DICT_TYPE).parallelStream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
        Map<String, String> paymentStateMap = dictDetailRepository.findByDictName(Constant.PAYMENT_STATE).parallelStream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
        Map<String, String> contractStatusMap = dictDetailRepository.findByDictName(Constant.CONTRACT_STATUS).parallelStream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
        Map<String, String> acceptStatusMap = dictDetailRepository.findByDictName(Constant.ACCEPT_STATUS).parallelStream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
        for (Contract contract : contractList) {
            String firstName = contractFirstName(contract);
            String secondName = contractSecondName(contract);
            String relatedDocumentsAttachment = "";
            if (StringUtils.isNotBlank(contract.getRelatedDocumentsAttachment())) {
                relatedDocumentsAttachment = Arrays.stream(contract.getRelatedDocumentsAttachment().split(",")).map(item -> Arrays.stream(item.trim().split("/")).reduce("", (f, s) -> s)).reduce("", (first, second) -> first.length() == 0 ? second : first.concat(",").concat(second));
            }
            ContractPaymentQueryCriteria paymentQueryCriteria = new ContractPaymentQueryCriteria();
            paymentQueryCriteria.setContractId(contract.getContractId());
            List<ContractPayment> paymentList = contractPaymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,paymentQueryCriteria,criteriaBuilder));
            if (paymentList.size() <= 0) {
                // 至少一条
                paymentList.add(new ContractPayment());
            }
            for (ContractPayment payment : paymentList) {
                List<ContractInvoice> invoiceList = new ArrayList<>();
                if (ObjectUtil.isNotEmpty(payment.getPaymentId())) {
                    ContractInvoicePaymentQueryCriteria invoicePaymentQueryCriteria = new ContractInvoicePaymentQueryCriteria();
                    invoicePaymentQueryCriteria.setPaymentId(payment.getPaymentId());
                    List<ContractInvoicePayment> invoicePaymentList = contractInvoicePaymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,invoicePaymentQueryCriteria,criteriaBuilder));
                    for (ContractInvoicePayment invoicePayment : invoicePaymentList) {
                        ContractInvoice invoice = contractInvoiceRepository.getById(invoicePayment.getInvoiceId());
                        invoiceList.add(invoice);
                    }
                } else {
                    invoiceList.add(new ContractInvoice());
                }
                BigDecimal paymentAmount = ObjectUtil.isNotEmpty(payment.getPaymentAmount()) ? payment.getPaymentAmount() : new BigDecimal(0);
                BigDecimal advancePayment = ObjectUtil.isNotEmpty(payment.getPaymentAmount()) ? payment.getPaymentAmount() : new BigDecimal(0);
                for (ContractInvoice invoice : invoiceList) {
                    BigDecimal afterTaxAmount = ObjectUtil.isNotEmpty(invoice.getAfterTaxAmount()) ? invoice.getAfterTaxAmount() : new BigDecimal(0);
                    BigDecimal receivePayment = afterTaxAmount.subtract(paymentAmount);
                    advancePayment = advancePayment.subtract(afterTaxAmount);
                    Map<String,Object> map = new LinkedHashMap<>();
                    map.put("合同编号", contract.getContractNumber());
                    map.put("合同名称", contract.getContractName());
                    map.put("甲方姓名", firstName);
                    map.put("乙方姓名", secondName);
                    map.put("签订日期", contract.getSignDate());
                    map.put("起始日期", contract.getStartDate());
                    map.put("截止日期", contract.getEndDate());
                    map.put("合同所属地区", contract.getContractArea());
                    map.put("合同类型", contractTypeMap.get(contract.getContractType()));
                    map.put("合同类型(二级)", contract.getContractSecondType());
                    map.put("合同内容", contract.getContractContent());
                    map.put("合同状态", contractStatusMap.get(contract.getContractStatus()));
                    map.put("开票金额", invoice.getAfterTaxAmount());
                    map.put("开票日期", invoice.getBillingTime());
                    map.put("付款金额", payment.getPaymentAmount());
                    map.put("预收款金额", advancePayment.compareTo(new BigDecimal(0)) < 1 ? new BigDecimal(0) : advancePayment);
                    map.put("应收款金额", receivePayment.compareTo(new BigDecimal(0)) < 1 ? new BigDecimal(0) : receivePayment);
                    map.put("合同费金额", contract.getContractAmount());
                    map.put("合同费预计付款日期", contract.getContractPaymentDate());
                    map.put("合同费付款状态", paymentStateMap.get(contract.getContractPaymentState()));
                    map.put("付款时间", payment.getPaymentTime());
                    map.put("合同费延期情况", contractPostponement(contract));
                    map.put("服务开始日期", contract.getServiceStartsDate());
                    map.put("服务费金额", contract.getServiceAmount());
                    map.put("服务费预计付款日期", contract.getServicePaymentDate());
                    map.put("服务费付款状态", paymentStateMap.get(contract.getServicePaymentState()));
                    map.put("服务费延期情况", servicePostponement(contract));
                    map.put("合同初版文件", contract.getRelatedDocumentsFirst());
                    map.put("合同终版文件", contract.getRelatedDocumentsFinal());
                    map.put("验收状态", acceptStatusMap.get(contract.getAcceptStatus()));
                    map.put("合同附件", relatedDocumentsAttachment);
                    map.put("备注信息", contract.getRemarks());
                    map.put("创建者", contract.getCreateBy());
                    map.put("创建日期", contract.getCreateTime());
                    list.add(map);
                }
            }
        }
        FileUtil.downloadExcel(list, response);
    }
@@ -1474,4 +1569,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);
        }
    }
}