| | |
| | | */ |
| | | package me.zhengjie.modules.contract.service.impl; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.poi.excel.ExcelReader; |
| | | import cn.hutool.poi.excel.ExcelUtil; |
| | | 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.mapstruct.ContractProjectMapper; |
| | | import me.zhengjie.modules.project.service.dto.ProjectDto; |
| | | import me.zhengjie.modules.project.service.impl.ProjectServiceImpl; |
| | | import me.zhengjie.modules.system.domain.Dict; |
| | | import me.zhengjie.modules.system.domain.DictDetail; |
| | | import me.zhengjie.modules.system.domain.SysCompany; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | 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 org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | /** |
| | |
| | | private final DictRepository dictRepository; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | private final ContractFirstServiceImpl contractFirstServiceImpl; |
| | | private final ContractFirstRepository contractFirstRepository; |
| | | private final ContractSecondServiceImpl contractSecondServiceImpl; |
| | | private final ContractSecondRepository contractSecondRepository; |
| | | private final ContractProjectRepository contractProjectRepository; |
| | | private final SysCompanyRepository sysCompanyRepository; |
| | | private final FileProperties properties; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(ContractQueryCriteria criteria, Pageable pageable){ |
| | |
| | | } |
| | | return receivePaymentTotalMoney; |
| | | } |
| | | |
| | | /** |
| | | * 批量导入合同详情 |
| | | * @param file |
| | | * @return Map<String, Object> |
| | | */ |
| | | @Override |
| | | public Map<String, Object> batchUpload(MultipartFile file) { |
| | | FileUtil.checkSize(properties.getMaxSize(), file.getSize()); |
| | | if (ObjectUtil.isNull(file)) { |
| | | throw new BadRequestException("上传失败"); |
| | | } |
| | | |
| | | ExcelReader reader = ExcelUtil.getReader(FileUtil.toFile(file)); |
| | | List<Map<String,Object>> readAll = reader.readAll(); |
| | | if (ObjectUtil.isNull(readAll)) { |
| | | throw new BadRequestException("Excel解析失败"); |
| | | } |
| | | Map<String, Object> finalMap = new HashMap<>(); |
| | | finalMap.put("errCode", 0); |
| | | finalMap.put("errMsg", null); |
| | | |
| | | try { |
| | | List<Contract> contractList = new ArrayList<>(); |
| | | Map<String, List<Map<String, Object>>> supplyMap = new HashMap<>(); |
| | | Map<String, List<Integer>> errorMap = new HashMap<>(); |
| | | Map<String, Map<String, String>> chinaAreaMap = ChinaArea.getAreaData(); |
| | | Map<String, Long> firstMap = contractFirstRepository.findAll().stream().collect(Collectors.toMap(ContractFirst::getFirstName, ContractFirst::getFirstId)); |
| | | Map<String, Long> secondMap = contractSecondRepository.findAll().stream().collect(Collectors.toMap(ContractSecond::getSecondName, ContractSecond::getSecondId)); |
| | | Map<String, Long> companyMap = sysCompanyRepository.findAll().stream().collect(Collectors.toMap(SysCompany::getLabel, SysCompany::getId)); |
| | | Map<String, String> provinceMap = chinaAreaMap.get("86").entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); |
| | | int rowNum = 1; |
| | | |
| | | for (Map<String,Object> row: readAll) { |
| | | Contract contract = new Contract(); |
| | | ContractInvoice invoice = new ContractInvoice(); |
| | | ContractPayment payment = new ContractPayment(); |
| | | |
| | | if (checkRequiredColumn(row, "合同编号", rowNum, errorMap)) { |
| | | contract.setContractNumber(row.get("合同编号").toString()); |
| | | } |
| | | checkRequiredColumn(row, "合同编号", rowNum, errorMap); |
| | | if (checkRequiredColumn(row, "合同名称", rowNum, errorMap)) { |
| | | contract.setContractName(row.get("合同名称").toString()); |
| | | } |
| | | if (checkRequiredColumn(row, "甲方姓名", rowNum, errorMap) && checkCompanyColumn(row, "甲方姓名", rowNum, errorMap, firstMap)) { |
| | | contract.setFirstId(firstMap.get(row.get("甲方姓名").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "乙方姓名", rowNum, errorMap) && checkCompanyColumn(row, "乙方姓名", rowNum, errorMap, secondMap)) { |
| | | contract.setSecondId(secondMap.get(row.get("乙方姓名").toString())); |
| | | // 合同付款:所属公司 |
| | | if (companyMap.containsKey(row.get("乙方姓名").toString())) { |
| | | payment.setCompanyId(companyMap.get(row.get("乙方姓名").toString())); |
| | | } |
| | | } |
| | | if (checkRequiredColumn(row, "签订日期", rowNum, errorMap)) { |
| | | contract.setSignDate(Timestamp.valueOf(row.get("签订日期").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "起始日期", rowNum, errorMap)) { |
| | | contract.setStartDate(Timestamp.valueOf(row.get("起始日期").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "截止日期", rowNum, errorMap)) { |
| | | contract.setEndDate(Timestamp.valueOf(row.get("截止日期").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "合同所属地区", rowNum, errorMap)) { |
| | | String contractArea = row.get("合同所属地区").toString(); |
| | | contract.setContractArea(contractArea); |
| | | // 合同所属地区编码 |
| | | int finalRowNum = rowNum; |
| | | String contractAreaCode = Arrays.stream(contractArea.split("/")).filter(item -> StringUtils.isNotBlank(item)).reduce("", (concatStr, itemStr) -> { |
| | | if (concatStr.length() == 0) { |
| | | return provinceMap.get(itemStr); |
| | | } else { |
| | | if (itemStr.equals("本级")) { |
| | | return concatStr; |
| | | } else { |
| | | List<String> contractAreaCodeList = Arrays.stream(concatStr.split("/")).collect(Collectors.toList()); |
| | | String superAreaCode = contractAreaCodeList.get(contractAreaCodeList.size() - 1); |
| | | Map<String, String> superAreaMap = chinaAreaMap.get(superAreaCode).entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); |
| | | if (superAreaMap.containsKey(itemStr)) { |
| | | return concatStr.concat("/").concat(superAreaMap.get(itemStr)); |
| | | } else { |
| | | String newKey = "合同所属地区编码校验未通过"; |
| | | List<Integer> errorRows = errorMap.containsKey(newKey) ? errorMap.get(newKey) : new ArrayList<>(); |
| | | errorRows.add(finalRowNum); |
| | | errorMap.put(newKey, errorRows); |
| | | return concatStr; |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | contract.setContractAreaCode(contractAreaCode); |
| | | } |
| | | if (checkRequiredColumn(row, "合同类型", rowNum, errorMap) && checkTypeColumn(row, "合同类型", rowNum, errorMap)) { |
| | | contract.setContractType(ElAdminConstant.Enum.ContractTypeDict.findByImportValue(row.get("合同类型").toString()).getDictValue()); |
| | | } |
| | | if (checkRequiredColumn(row, "合同类型(二级)", rowNum, errorMap)) { |
| | | contract.setContractSecondType(row.get("合同类型(二级)").toString()); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(row.get("合同内容"))) { |
| | | contract.setContractContent(row.get("合同内容").toString()); |
| | | } |
| | | if (checkRequiredColumn(row, "合同状态", rowNum, errorMap)) { |
| | | if (row.get("合同状态").toString().equals("未签订")) { |
| | | contract.setContractStatus(Constant.CONTRACT_STATUS_UNSIGNED); |
| | | } else if (row.get("合同状态").toString().equals("签订中")) { |
| | | contract.setContractStatus(Constant.CONTRACT_STATUS_SIGNING); |
| | | } else if (row.get("合同状态").toString().equals("已签订")) { |
| | | contract.setContractStatus(Constant.CONTRACT_STATUS_SIGNED); |
| | | } else if (row.get("合同状态").toString().equals("不签订")) { |
| | | contract.setContractStatus(Constant.CONTRACT_STATUS_NOT_SIGN); |
| | | } |
| | | } |
| | | if (ObjectUtil.isNotEmpty(row.get("开票总金额"))) { |
| | | // billingTotalAmount(计算) |
| | | invoice.setBeforeTaxAmount(new BigDecimal(row.get("开票总金额").toString())); |
| | | invoice.setAfterTaxAmount(new BigDecimal(row.get("开票总金额").toString())); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(row.get("开票日期"))) { |
| | | // billingTime |
| | | invoice.setBillingTime(Timestamp.valueOf(row.get("开票日期").toString())); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(row.get("付款总金额"))) { |
| | | // paymentTotalAmount(计算) |
| | | payment.setPaymentAmount(new BigDecimal(row.get("付款总金额").toString())); |
| | | } |
| | | // if (checkRequiredColumn(row, "预收款金额", rowNum, errorMap)) { |
| | | // // advancePayment(计算) |
| | | // } |
| | | // if (checkRequiredColumn(row, "应收款金额", rowNum, errorMap)) { |
| | | // // receivePayment(计算) |
| | | // } |
| | | if (ObjectUtil.isNotEmpty(row.get("合同类型(二级)")) && !row.get("合同类型(二级)").toString().equals(Constant.CONTRACT_SERVICE)) { |
| | | if (checkRequiredColumn(row, "合同费金额", rowNum, errorMap)) { |
| | | contract.setContractAmount(new BigDecimal(row.get("合同费金额").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "合同费预计付款日期", rowNum, errorMap)) { |
| | | contract.setContractPaymentDate(Timestamp.valueOf(row.get("合同费预计付款日期").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "合同费付款状态", rowNum, errorMap) && checkTypeColumn(row, "合同费付款状态", rowNum, errorMap)) { |
| | | if (row.get("合同费付款状态").toString().equals("未付款")) { |
| | | contract.setContractPaymentState(Constant.PAYMENT_NOT); |
| | | } else if (row.get("合同费付款状态").toString().equals("部分付款")) { |
| | | contract.setContractPaymentState(Constant.PAYMENT_PART); |
| | | } else if (row.get("合同费付款状态").toString().equals("已付款")) { |
| | | contract.setContractPaymentState(Constant.PAYMENT_ALREADY); |
| | | } |
| | | } |
| | | } |
| | | if (ObjectUtil.isNotEmpty(row.get("付款时间"))) { |
| | | // paymentTime(合同付款信息) |
| | | payment.setPaymentTime(Timestamp.valueOf(row.get("付款时间").toString())); |
| | | } |
| | | // if (checkRequiredColumn(row, "合同费延期情况", rowNum, errorMap)) { |
| | | // // contractPostponement |
| | | // } |
| | | if (ObjectUtil.isNotEmpty(row.get("合同类型(二级)")) && (row.get("合同类型(二级)").toString().equals(Constant.CONTRACT_SERVICE) || row.get("合同类型(二级)").toString().equals(Constant.CONTRACT_DEVELOP_SERVICE))) { |
| | | if (row.get("合同类型(二级)").toString().equals(Constant.CONTRACT_DEVELOP_SERVICE) && checkRequiredColumn(row, "服务开始日期", rowNum, errorMap)) { |
| | | contract.setServiceStartsDate(Timestamp.valueOf(row.get("服务开始日期").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "服务费金额", rowNum, errorMap)) { |
| | | contract.setServiceAmount(new BigDecimal(row.get("服务费金额").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "服务费预计付款日期", rowNum, errorMap)) { |
| | | contract.setServicePaymentDate(Timestamp.valueOf(row.get("服务费预计付款日期").toString())); |
| | | } |
| | | if (checkRequiredColumn(row, "服务费付款状态", rowNum, errorMap) && checkTypeColumn(row, "服务费付款状态", rowNum, errorMap)) { |
| | | if (row.get("服务费付款状态").toString().equals("未付款")) { |
| | | contract.setServicePaymentState(Constant.PAYMENT_NOT); |
| | | } else if (row.get("服务费付款状态").toString().equals("部分付款")) { |
| | | contract.setServicePaymentState(Constant.PAYMENT_PART); |
| | | } else if (row.get("服务费付款状态").toString().equals("已付款")) { |
| | | contract.setServicePaymentState(Constant.PAYMENT_ALREADY); |
| | | } |
| | | } |
| | | } |
| | | // if (checkRequiredColumn(row, "服务费延期情况", rowNum, errorMap)) { |
| | | // // servicePostponement |
| | | // } |
| | | // if (checkRequiredColumn(row, "合同初版文件", rowNum, errorMap)) { |
| | | // contract.setContractName(row.get("合同初版文件").toString()); |
| | | // } |
| | | // if (checkRequiredColumn(row, "合同终版文件", rowNum, errorMap)) { |
| | | // contract.setContractName(row.get("合同终版文件").toString()); |
| | | // } |
| | | if (checkRequiredColumn(row, "验收状态", rowNum, errorMap)) { |
| | | if (row.get("验收状态").toString().equals("待制作")) { |
| | | contract.setAcceptStatus(Constant.ACCEPT_STATUS_TO_BE); |
| | | } else if (row.get("验收状态").toString().equals("验收中")) { |
| | | contract.setAcceptStatus(Constant.ACCEPT_STATUS_UNDER); |
| | | } else if (row.get("验收状态").toString().equals("部分验收")) { |
| | | contract.setAcceptStatus(Constant.ACCEPT_STATUS_PARTIAL); |
| | | } else if (row.get("验收状态").toString().equals("已验收")) { |
| | | contract.setAcceptStatus(Constant.ACCEPT_STATUS_ACCEPTED); |
| | | } else if (row.get("验收状态").toString().equals("不验收")) { |
| | | contract.setAcceptStatus(Constant.ACCEPT_STATUS_NOT); |
| | | } |
| | | } |
| | | // if (checkRequiredColumn(row, "合同附件", rowNum, errorMap)) { |
| | | // contract.setContractName(row.get("合同附件").toString()); |
| | | // } |
| | | if (ObjectUtil.isNotEmpty(row.get("备注信息"))) { |
| | | contract.setRemarks(row.get("备注信息").toString()); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(row.get("创建者"))) { |
| | | contract.setCreateBy(row.get("创建者").toString()); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(row.get("创建日期"))) { |
| | | contract.setCreateTime(Timestamp.valueOf(row.get("创建日期").toString())); |
| | | } |
| | | |
| | | // 暂存数据 |
| | | if (errorMap.size() <= 0) { |
| | | if (!supplyMap.containsKey(contract.getContractNumber())) { |
| | | contractList.add(contract); |
| | | } |
| | | |
| | | Map<String, Object> supplyMapData = new HashMap<>(); |
| | | if (ObjectUtil.isNotEmpty(invoice.getBeforeTaxAmount())) { |
| | | supplyMapData.put("invoice", invoice); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(payment.getPaymentAmount())) { |
| | | supplyMapData.put("payment", payment); |
| | | } |
| | | if (supplyMapData.size() > 0) { |
| | | List<Map<String, Object>> supplyList = supplyMap.containsKey(contract.getContractNumber()) ? supplyMap.get(contract.getContractNumber()) : new ArrayList<>(); |
| | | supplyList.add(supplyMapData); |
| | | supplyMap.put(contract.getContractNumber(), supplyList); |
| | | } |
| | | } |
| | | |
| | | rowNum += 1; |
| | | } |
| | | |
| | | // 保存数据 |
| | | if (errorMap.size() <= 0) { |
| | | for (Contract contract : contractList) { |
| | | Contract contractUpdate = new Contract(); |
| | | contractUpdate.copy(contract); |
| | | // 编号用于唯一标识但不保存(自动生成) |
| | | contractUpdate.setContractNumber(null); |
| | | |
| | | // 更新金额相关数据 |
| | | contractUpdate = contractRepository.save(updateAmountData(contractUpdate)); |
| | | // 保存后再更新合同编号(获取健全信息,防止顺序编号冲突) |
| | | if (ObjectUtil.isNotEmpty(contract.getCreateTime())) { |
| | | // 因可能导入旧数据,则合同编号签订日期取旧日期 |
| | | contractUpdate.setCreateTime(contract.getCreateTime()); |
| | | } |
| | | Contract savedContract = updateContractNumber(contractUpdate); |
| | | if (supplyMap.containsKey(contract.getContractNumber())) { |
| | | for (Map<String, Object> supplyMapData : supplyMap.get(contract.getContractNumber())) { |
| | | ContractInvoice savedInvoice = null; |
| | | ContractPayment savedPayment = null; |
| | | if (supplyMapData.containsKey("invoice")) { |
| | | savedInvoice = (ContractInvoice) supplyMapData.get("invoice"); |
| | | savedInvoice.setContractId(savedContract.getContractId()); |
| | | savedInvoice = contractInvoiceRepository.save(savedInvoice); |
| | | } |
| | | if (supplyMapData.containsKey("payment")) { |
| | | savedPayment = (ContractPayment) supplyMapData.get("payment"); |
| | | savedPayment.setContractId(savedContract.getContractId()); |
| | | savedPayment = contractPaymentRepository.save(savedPayment); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(savedInvoice) && ObjectUtil.isNotEmpty(savedPayment)) { |
| | | ContractInvoicePayment invoicePayment = new ContractInvoicePayment(); |
| | | invoicePayment.setInvoiceId(savedInvoice.getInvoiceId()); |
| | | invoicePayment.setPaymentId(savedPayment.getPaymentId()); |
| | | contractInvoicePaymentRepository.save(invoicePayment); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | finalMap.put("errCode", 999); |
| | | finalMap.put("errMsg", errorMap.entrySet().stream().map(item -> item.getKey().concat(":").concat(item.getValue().stream().map(col -> col.toString()).collect(Collectors.joining("、")))).collect(Collectors.joining(" \n "))); |
| | | } |
| | | } catch (Exception e){ |
| | | e.printStackTrace(); |
| | | throw new BadRequestException("数据保存失败"); |
| | | } |
| | | |
| | | return finalMap; |
| | | } |
| | | |
| | | private Boolean checkRequiredColumn(Map<String,Object> data, String key, Integer rowNum, Map<String, List<Integer>> errorMap) { |
| | | if (ObjectUtil.isNotEmpty(data.get(key))) { |
| | | return true; |
| | | } else { |
| | | List<Integer> errorRows = errorMap.containsKey(key) ? errorMap.get(key) : new ArrayList<>(); |
| | | errorRows.add(rowNum); |
| | | errorMap.put(key, errorRows); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private Boolean checkCompanyColumn(Map<String,Object> data, String key, Integer rowNum, Map<String, List<Integer>> errorMap, Map<String, Long> companyMap) { |
| | | if (companyMap.containsKey(data.get(key).toString())) { |
| | | return true; |
| | | } else { |
| | | String newKey = key.concat("输入错误"); |
| | | List<Integer> errorRows = errorMap.containsKey(newKey) ? errorMap.get(newKey) : new ArrayList<>(); |
| | | errorRows.add(rowNum); |
| | | errorMap.put(newKey, errorRows); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private Boolean checkTypeColumn(Map<String,Object> data, String key, Integer rowNum, Map<String, List<Integer>> errorMap) { |
| | | if (key.equals("合同类型")) { |
| | | if (ElAdminConstant.Enum.ContractTypeDict.findByImportValue(data.get(key).toString()) != null) { |
| | | return true; |
| | | } else { |
| | | String newKey = key.concat("输入错误"); |
| | | List<Integer> errorRows = errorMap.containsKey(newKey) ? errorMap.get(newKey) : new ArrayList<>(); |
| | | errorRows.add(rowNum); |
| | | errorMap.put(newKey, errorRows); |
| | | return false; |
| | | } |
| | | } else if (key.equals("合同费付款状态") || key.equals("服务费付款状态")) { |
| | | if (data.get(key).toString().equals("未付款") || data.get(key).toString().equals("部分付款") || data.get(key).toString().equals("已付款")) { |
| | | return true; |
| | | } else { |
| | | String newKey = key.concat("输入错误"); |
| | | List<Integer> errorRows = errorMap.containsKey(newKey) ? errorMap.get(newKey) : new ArrayList<>(); |
| | | errorRows.add(rowNum); |
| | | errorMap.put(newKey, errorRows); |
| | | return false; |
| | | } |
| | | } else { |
| | | String newKey = "校验类型不存在"; |
| | | List<Integer> errorRows = errorMap.containsKey(newKey) ? errorMap.get(newKey) : new ArrayList<>(); |
| | | errorRows.add(rowNum); |
| | | errorMap.put(newKey, errorRows); |
| | | return false; |
| | | } |
| | | } |
| | | } |