/*
|
* Copyright 2019-2020 Zheng Jie
|
*
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* you may not use this file except in compliance with the License.
|
* You may obtain a copy of the License at
|
*
|
* http://www.apache.org/licenses/LICENSE-2.0
|
*
|
* Unless required by applicable law or agreed to in writing, software
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* See the License for the specific language governing permissions and
|
* limitations under the License.
|
*/
|
package me.zhengjie.modules.contract.service.impl;
|
|
import me.zhengjie.modules.constant.Constant;
|
import me.zhengjie.modules.contract.domain.Contract;
|
import me.zhengjie.modules.contract.domain.ContractInvoice;
|
import me.zhengjie.modules.contract.domain.ContractInvoicePayment;
|
import me.zhengjie.modules.contract.domain.ContractPayment;
|
import me.zhengjie.modules.contract.repository.ContractInvoicePaymentRepository;
|
import me.zhengjie.modules.contract.repository.ContractInvoiceRepository;
|
import me.zhengjie.modules.contract.repository.ContractPaymentRepository;
|
import me.zhengjie.modules.contract.service.dto.ContractFirstDto;
|
import me.zhengjie.modules.contract.service.dto.ContractSecondDto;
|
import me.zhengjie.modules.system.repository.DictDetailRepository;
|
import me.zhengjie.utils.ValidationUtil;
|
import me.zhengjie.utils.FileUtil;
|
import lombok.RequiredArgsConstructor;
|
import me.zhengjie.modules.contract.repository.ContractRepository;
|
import me.zhengjie.modules.contract.service.ContractService;
|
import me.zhengjie.modules.contract.service.dto.ContractDto;
|
import me.zhengjie.modules.contract.service.dto.ContractQueryCriteria;
|
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 me.zhengjie.utils.PageUtil;
|
import me.zhengjie.utils.QueryHelp;
|
|
import java.math.BigDecimal;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.time.ZoneId;
|
import java.time.temporal.ChronoUnit;
|
import java.util.*;
|
import java.io.IOException;
|
import java.util.function.Function;
|
import javax.servlet.http.HttpServletResponse;
|
|
/**
|
* @website https://eladmin.vip
|
* @description 服务实现
|
* @author 赵士淞
|
* @date 2023-02-02
|
**/
|
@Service
|
@RequiredArgsConstructor
|
public class ContractServiceImpl implements ContractService {
|
|
private final ContractRepository contractRepository;
|
private final ContractMapper contractMapper;
|
private final ContractPaymentRepository contractPaymentRepository;
|
private final ContractInvoicePaymentRepository contractInvoicePaymentRepository;
|
private final ContractInvoiceRepository contractInvoiceRepository;
|
private final DictDetailRepository dictDetailRepository;
|
private final ContractFirstServiceImpl contractFirstServiceImpl;
|
private final ContractSecondServiceImpl contractSecondServiceImpl;
|
|
@Override
|
public Map<String,Object> queryAll(ContractQueryCriteria criteria, Pageable pageable){
|
Page<Contract> page = contractRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
// 将实体类转换为DTO(重写)
|
Function<Contract, ContractDto> func = new Function<Contract, ContractDto>(){
|
@Override
|
public ContractDto apply(Contract t) {
|
// 将实体类转换成DTO
|
ContractDto contractDto = contractMapper.toDto(t);
|
|
// 合同款延期情况
|
contractDto.setContractPostponement(contractPostponement(t));
|
// 服务款延期情况
|
contractDto.setServicePostponement(servicePostponement(t));
|
// 预收款情况
|
contractDto.setAdvancePayment(advancePayment(t));
|
// 应收款情况
|
contractDto.setReceivePayment(receivePayment(t));
|
// 开票总金额
|
contractDto.setBillingTotalAmount(contractInvoiceRepository.countTotalAmountByContractId(t.getContractId()));
|
// 付款总金额
|
contractDto.setPaymentTotalAmount(contractPaymentRepository.countTotalAmountByContractId(t.getContractId()));
|
// 合同款付款金额
|
contractDto.setContractPaymentAmount(contractPaymentAmount(t));
|
// 服务款付款金额
|
contractDto.setServicePaymentAmount(servicePaymentAmount(t));
|
// 甲方姓名
|
contractDto.setFirstName(contractFirstName(t));
|
// 乙方姓名
|
contractDto.setSecondName(contractSecondName(t));
|
// 返回处理好的DTO
|
return contractDto;
|
}
|
};
|
return PageUtil.toPage(page.map(func));
|
}
|
|
@Override
|
public List<ContractDto> queryAll(ContractQueryCriteria criteria){
|
// 合同DTO列表
|
List<ContractDto> contractDtoList = new ArrayList<>();
|
// 查询合同列表
|
List<Contract> contractList = contractRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
|
// 循环合同列表
|
for(int i = 0; i < contractList.size(); i ++) {
|
// 实体类转DTO
|
ContractDto contractDto = contractMapper.toDto(contractList.get(i));
|
// 合同款延期情况
|
contractDto.setContractPostponement(contractPostponement(contractList.get(i)));
|
// 服务款延期情况
|
contractDto.setServicePostponement(servicePostponement(contractList.get(i)));
|
// 预收款情况
|
contractDto.setAdvancePayment(advancePayment(contractList.get(i)));
|
// 应收款情况
|
contractDto.setReceivePayment(receivePayment(contractList.get(i)));
|
// 开票总金额
|
contractDto.setBillingTotalAmount(contractInvoiceRepository.countTotalAmountByContractId(contractList.get(i).getContractId()));
|
// 付款总金额
|
contractDto.setPaymentTotalAmount(contractPaymentRepository.countTotalAmountByContractId(contractList.get(i).getContractId()));
|
// 甲方姓名
|
contractDto.setFirstName(contractFirstName(contractList.get(i)));
|
// 乙方姓名
|
contractDto.setSecondName(contractSecondName(contractList.get(i)));
|
// DTO新增
|
contractDtoList.add(contractDto);
|
}
|
// 返回
|
return contractDtoList;
|
}
|
|
@Override
|
@Transactional
|
public ContractDto findById(Long contractId) {
|
Contract contract = contractRepository.findById(contractId).orElseGet(Contract::new);
|
ValidationUtil.isNull(contract.getContractId(),"Contract","contractId",contractId);
|
return contractMapper.toDto(contract);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public ContractDto create(Contract resources) {
|
// 当前日历类
|
Calendar calendar = Calendar.getInstance();
|
// 当前年份
|
String year = String.valueOf(calendar.get(Calendar.YEAR));
|
// 当前月份
|
String month = calendar.get(Calendar.MONTH) + 1 >= 10 ? "" + (calendar.get(Calendar.MONTH) + 1) : "0" + (calendar.get(Calendar.MONTH) + 1);
|
// 当前日份
|
String date = calendar.get(Calendar.DATE) >= 10 ? "" + calendar.get(Calendar.DATE) : "0" + calendar.get(Calendar.DATE);
|
// 查询当天合同数据
|
List<Contract> contracts = contractRepository.findLikeContractNumber(year + "-" + month + date);
|
// 当天合同最大编号
|
int maxNumber = 0;
|
// 循环当天合同数据
|
for(int i = 0; i < contracts.size(); i ++) {
|
// 当前合同编号
|
int nowNumber = Integer.valueOf(contracts.get(i).getContractNumber().substring(10, 13));
|
// 最大合同编号
|
maxNumber = nowNumber > maxNumber ? nowNumber : maxNumber;
|
}
|
// 计算下一编号
|
String nextNumber = maxNumber + 1 >= 100 ? "" + (maxNumber + 1) : maxNumber + 1 >= 10 ? "0" + (maxNumber + 1) : "00" + (maxNumber + 1);
|
// 合同编号
|
resources.setContractNumber(year + "-" + month + date + "-" + nextNumber);
|
// 更新金额相关数据
|
Contract contract = updateAmountData(resources);
|
return contractMapper.toDto(contractRepository.save(contract));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void update(Contract resources) {
|
Contract contract = contractRepository.findById(resources.getContractId()).orElseGet(Contract::new);
|
ValidationUtil.isNull( contract.getContractId(),"Contract","id",resources.getContractId());
|
contract.copy(resources);
|
// 更新合同付款状态
|
Contract contractOne = updatePaymentStatus(contract);
|
// 更新金额相关数据
|
Contract contractTwo = updateAmountData(contractOne);
|
contractRepository.save(contractTwo);
|
}
|
|
@Override
|
public void deleteAll(Long[] ids) {
|
for (Long contractId : ids) {
|
contractRepository.deleteById(contractId);
|
}
|
}
|
|
@Override
|
public void download(List<ContractDto> all, HttpServletResponse response) throws IOException {
|
List<Map<String, Object>> list = new ArrayList<>();
|
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_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());
|
map.put("甲方姓名", firstName);
|
map.put("乙方姓名", secondName);
|
map.put("签订日期", contract.getSignDate());
|
map.put("起始日期", contract.getStartDate());
|
map.put("截止日期", contract.getEndDate());
|
map.put("合同所属地区", contract.getContractArea());
|
map.put("合同类型", contractType);
|
map.put("合同内容", contract.getContractContent());
|
map.put("开票总金额", contract.getBillingTotalAmount());
|
map.put("付款总金额", contract.getPaymentTotalAmount());
|
map.put("预收款金额", contract.getAdvancePayment());
|
map.put("应收款金额", contract.getReceivePayment());
|
map.put("合同费金额", contract.getContractAmount());
|
map.put("合同费预计付款日期", contract.getContractPaymentDate());
|
map.put("合同费付款状态", contractPaymentState);
|
map.put("合同费延期情况", contract.getContractPostponement());
|
map.put("服务开始日期", contract.getServiceStartsDate());
|
map.put("服务费金额", contract.getServiceAmount());
|
map.put("服务费预计付款日期", servicePaymentDate);
|
map.put("服务费付款状态", servicePaymentState);
|
map.put("服务费延期情况", contract.getServicePostponement());
|
map.put("合同初版文件", contract.getRelatedDocumentsFirst());
|
map.put("合同终版文件", contract.getRelatedDocumentsFinal());
|
map.put("合同状态", contractStatus);
|
map.put("备注信息", contract.getRemarks());
|
map.put("创建者", contract.getCreateBy());
|
map.put("创建日期", contract.getCreateTime());
|
list.add(map);
|
}
|
FileUtil.downloadExcel(list, response);
|
}
|
|
/**
|
* 合同甲方姓名
|
* @param resources
|
* @return
|
*/
|
public String contractFirstName(Contract resources) {
|
// 查询合同甲方数据
|
ContractFirstDto contractFirstDto = contractFirstServiceImpl.findById(resources.getFirstId());
|
return contractFirstDto.getFirstName();
|
}
|
|
/**
|
* 合同乙方姓名
|
* @param resources
|
* @return
|
*/
|
public String contractSecondName(Contract resources) {
|
// 查询合同乙方数据
|
ContractSecondDto contractSecondDto = contractSecondServiceImpl.findById(resources.getSecondId());
|
return contractSecondDto.getSecondName();
|
}
|
|
/**
|
* 合同款付款金额
|
* @param resources
|
* @return
|
*/
|
public String contractPaymentAmount(Contract resources){
|
// 查询合同各款项付款金额
|
BigDecimal contractPaymentAmount = contractPaymentRepository.findByContractIdAndFundType(resources.getContractId(), Constant.FUND_CONTRACT);
|
return String.valueOf(contractPaymentAmount);
|
}
|
|
/**
|
* 服务款付款金额
|
* @param resources
|
* @return
|
*/
|
public String servicePaymentAmount(Contract resources){
|
// 时间格式化
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// 服务开始年份
|
String serviceStartsYear = null;
|
// 判断服务开始日期是否为空
|
if(resources.getServiceStartsDate() != null) {
|
// 服务开始年份
|
serviceStartsYear = resources.getServiceStartsDate().toString().substring(0, 4);
|
}
|
// 当前系统时间
|
Date systemTime = new Date();
|
// 当前系统年份
|
String systemYear = sdf.format(systemTime).substring(0, 4);
|
|
// 判断服务开始年份是否为空
|
if(serviceStartsYear != null) {
|
// 返回字符串
|
String returnString = "";
|
// 服务开始年份与当前年份差
|
int years = Integer.valueOf(systemYear) - Integer.valueOf(serviceStartsYear) + 1;
|
// 循环年份差
|
for(int i = 0; i < years; i ++) {
|
// 查询合同服务款项各年度付款金额
|
BigDecimal serviceAmount = contractPaymentRepository.findByContractIdAndFundBelongStart(resources.getContractId(), Integer.valueOf(serviceStartsYear) + i);
|
serviceAmount = serviceAmount == null ? new BigDecimal(0) : serviceAmount;
|
// 返回字符串
|
returnString += serviceAmount + "、";
|
}
|
return returnString;
|
} else {
|
return "";
|
}
|
}
|
|
/**
|
* 预付款情况
|
* @param resources
|
* @return
|
*/
|
private BigDecimal advancePayment(Contract resources) {
|
// 返回金额
|
BigDecimal returnDecimal = new BigDecimal(0);
|
// 查询合同付款数据
|
List<ContractPayment> contractPaymentList = contractPaymentRepository.findByContractId(resources.getContractId());
|
// 循环合同付款数据
|
for(int i = 0; i < contractPaymentList.size(); i ++) {
|
// 当前付款金额
|
BigDecimal paymentAmount = contractPaymentList.get(i).getPaymentAmount();
|
// 临时金额池
|
BigDecimal tempDecimal = new BigDecimal(0);
|
// 查询付款相关发票数据
|
List<ContractInvoicePayment> contractInvoicePaymentList = contractInvoicePaymentRepository.findByPaymentId(contractPaymentList.get(i).getPaymentId());
|
// 循环付款相关发票数据
|
for(int j = 0; j < contractInvoicePaymentList.size(); j ++) {
|
// 获取发票数据
|
ContractInvoice contractInvoice = contractInvoiceRepository.findById(contractInvoicePaymentList.get(j).getInvoiceId()).orElseGet(ContractInvoice::new);
|
// 临时金额池
|
tempDecimal = tempDecimal.add(contractInvoice.getAfterTaxAmount());
|
}
|
// 临时金额
|
BigDecimal temp = paymentAmount.subtract(tempDecimal);
|
temp = temp.compareTo(new BigDecimal(0)) < 1 ? new BigDecimal(0) : temp;
|
// 返回金额
|
returnDecimal = returnDecimal.add(temp);
|
}
|
// 返回
|
return returnDecimal;
|
}
|
|
/**
|
* 应收款情况
|
* @param resources
|
* @return
|
*/
|
private BigDecimal receivePayment(Contract resources) {
|
// 返回金额
|
BigDecimal returnDecimal = new BigDecimal(0);
|
// 循环合同发票数据
|
List<ContractInvoice> contractInvoiceList = contractInvoiceRepository.findByContractId(resources.getContractId());
|
// 循环合同发票数据
|
for(int i = 0; i < contractInvoiceList.size(); i ++) {
|
// 当前发票金额
|
BigDecimal paymentAmount = contractInvoiceList.get(i).getAfterTaxAmount();
|
// 临时金额池
|
BigDecimal tempDecimal = new BigDecimal(0);
|
// 查询发票相关付款数据
|
List<ContractInvoicePayment> contractInvoicePaymentList = contractInvoicePaymentRepository.findByInvoiceId(contractInvoiceList.get(i).getInvoiceId());
|
// 循环发票相关付款数据
|
for(int j = 0; j < contractInvoicePaymentList.size(); j ++) {
|
// 获取付款数据
|
ContractPayment contractPayment = contractPaymentRepository.findById(contractInvoicePaymentList.get(j).getPaymentId()).orElseGet(ContractPayment::new);
|
// 临时金额池
|
tempDecimal = tempDecimal.add(contractPayment.getPaymentAmount());
|
}
|
// 临时金额
|
BigDecimal temp = paymentAmount.subtract(tempDecimal);
|
temp = temp.compareTo(new BigDecimal(0)) < 1 ? new BigDecimal(0) : temp;
|
// 返回金额
|
returnDecimal = returnDecimal.add(temp);
|
}
|
// 返回
|
return returnDecimal;
|
}
|
|
/**
|
* 合同款延期情况
|
* @param resources
|
* @return
|
*/
|
public String contractPostponement(Contract resources) {
|
// 时间格式化
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
// 合同预计付款日期
|
String contractPaymentDate = null;
|
// 合同预计付款时间
|
Date contractPaymentTime = null;
|
// 判断合同预计付款日期是否为空
|
if(resources.getContractPaymentDate() != null) {
|
// 合同预计付款日期
|
contractPaymentDate = resources.getContractPaymentDate().toString();
|
// 合同预计付款时间
|
try {
|
contractPaymentTime = sdf.parse(contractPaymentDate.substring(0, 10) + " 00:00:00");
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// 当前系统时间
|
Date systemTime = new Date();
|
|
// 合同款总金额
|
BigDecimal contractTotalAmount = resources.getContractAmount() == null ? new BigDecimal(0) : resources.getContractAmount();
|
// 查询合同款项付款金额
|
BigDecimal contractAmount = contractPaymentRepository.findByContractIdAndFundType(resources.getContractId(), Constant.FUND_CONTRACT);
|
contractAmount = contractAmount == null ? new BigDecimal(0) : contractAmount;
|
// 判断合同款总金额与付款金额
|
if(contractTotalAmount.compareTo(contractAmount) == 1 && contractPaymentDate != null && systemTime.getTime() > contractPaymentTime.getTime()) {
|
// 两时间差
|
ZoneId zoneId = ZoneId.systemDefault();
|
LocalDateTime fromDateTime = LocalDateTime.ofInstant(contractPaymentTime.toInstant(), zoneId);
|
LocalDateTime toDateTime = LocalDateTime.ofInstant(systemTime.toInstant(), zoneId);
|
LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);
|
long days = tempDateTime.until(toDateTime, ChronoUnit.DAYS);
|
|
return "延期" + days + "天";
|
} else {
|
return "";
|
}
|
}
|
|
/**
|
* 服务款延期情况
|
* @param resources
|
* @return
|
*/
|
public String servicePostponement(Contract resources) {
|
// 时间格式化
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// 服务开始年份
|
String serviceStartsYear = null;
|
// 判断服务开始日期是否为空
|
if(resources.getServiceStartsDate() != null) {
|
// 服务开始年份
|
serviceStartsYear = resources.getServiceStartsDate().toString().substring(0, 4);
|
}
|
// 服务预计付款月日
|
String servicePaymentMonth = null;
|
// 判断服务预计付款日期是否为空
|
if(resources.getServicePaymentDate() != null) {
|
// 服务预计付款月日
|
servicePaymentMonth = resources.getServicePaymentDate().toString().substring(4, 10);
|
|
}
|
// 当前系统时间
|
Date systemTime = new Date();
|
// 当前系统年份
|
String systemYear = sdf.format(systemTime).substring(0, 4);
|
// 当前年付款时间
|
Date systemPaymentTime = null;
|
// 判断服务预计付款日期是否为空
|
if(resources.getServicePaymentDate() != null) {
|
// 当前年付款时间
|
try {
|
systemPaymentTime = sdf.parse(systemYear + servicePaymentMonth + " 00:00:00");
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// 服务款总金额
|
BigDecimal serviceTotalAmount = resources.getServiceAmount() == null ? new BigDecimal(0) : resources.getServiceAmount();
|
// 判断服务开始年份是否为空
|
if(serviceStartsYear != null) {
|
// 返回字符串
|
String returnString = "";
|
// 服务开始年份与当前年份差
|
int years = Integer.valueOf(systemYear) - Integer.valueOf(serviceStartsYear) + 1;
|
// 循环年份差
|
for(int i = 0; i < years; i ++) {
|
// 查询合同服务款项各年度付款金额
|
BigDecimal serviceAmount = contractPaymentRepository.findByContractIdAndFundBelongStart(resources.getContractId(), Integer.valueOf(serviceStartsYear) + i);
|
serviceAmount = serviceAmount == null ? new BigDecimal(0) : serviceAmount;
|
// 判断服务款总金额与付款金额
|
if(serviceTotalAmount.compareTo(serviceAmount) == 1) {
|
// 判断是否是当年数据
|
if(Integer.valueOf(serviceStartsYear) + i == Integer.valueOf(systemYear)) {
|
// 判断是否超出付款时间
|
if(systemPaymentTime != null && systemTime.getTime() > systemPaymentTime.getTime()) {
|
// 两时间差
|
ZoneId zoneId = ZoneId.systemDefault();
|
LocalDateTime fromDateTime = LocalDateTime.ofInstant(systemPaymentTime.toInstant(), zoneId);
|
LocalDateTime toDateTime = LocalDateTime.ofInstant(systemTime.toInstant(), zoneId);
|
LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);
|
long days = tempDateTime.until(toDateTime, ChronoUnit.DAYS);
|
// 返回字符串
|
returnString += "当前年度延期" + days + "天";
|
}
|
} else {
|
// 返回字符串
|
returnString += (Integer.valueOf(serviceStartsYear) + i) + "至" + (Integer.valueOf(serviceStartsYear) + (i + 1)) + "年度未结清、";
|
}
|
}
|
}
|
return returnString;
|
} else {
|
return "";
|
}
|
}
|
|
/**
|
* 更新金额相关数据
|
* @param resources
|
* @return
|
*/
|
public Contract updateAmountData(Contract resources) {
|
// 判断合同类型
|
if (Constant.CONTRACT_DEVELOP_SERVICE.equals(resources.getContractType())) {
|
// 不处理
|
} else if (Constant.CONTRACT_SERVICE.equals(resources.getContractType())) {
|
// 合同费金额
|
resources.setContractAmount(null);
|
// 预计付款日期
|
resources.setContractPaymentDate(null);
|
// 付款状态
|
resources.setContractPaymentState(null);
|
// 服务开始日期
|
resources.setServiceStartsDate(resources.getStartDate());
|
} else {
|
// 服务开始日期
|
resources.setServiceStartsDate(null);
|
// 服务费金额
|
resources.setServiceAmount(null);
|
// 预计付款日期
|
resources.setServicePaymentDate(null);
|
// 付款状态
|
resources.setServicePaymentState(null);
|
}
|
return resources;
|
}
|
|
/**
|
* 更新合同付款状态
|
* @param resources
|
*/
|
public Contract updatePaymentStatus(Contract resources) {
|
// 查询数据库旧数据
|
Contract contract = contractRepository.findById(resources.getContractId()).orElseGet(Contract::new);
|
ValidationUtil.isNull( contract.getContractId(),"Contract","id",resources.getContractId());
|
// 复制
|
contract.copy(resources);
|
|
// 时间格式化
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// 截止日期
|
String endDate = contract.getEndDate().toString();
|
// 截止时间
|
Date endTime = null;
|
try {
|
endTime = sdf.parse(endDate.substring(0, 10) + " 23:59:59");
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
// 服务开始日期
|
String serviceStartsDate = null;
|
// 服务开始时间
|
Date serviceStartsTime = null;
|
// 判断服务开始日期是否为空
|
if(contract.getServiceStartsDate() != null) {
|
// 服务开始日期
|
serviceStartsDate = contract.getServiceStartsDate().toString();
|
// 服务开始时间
|
try {
|
serviceStartsTime = sdf.parse(serviceStartsDate.substring(0, 10) + " 00:00:00");
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// 判断合同款项付款状态
|
if(!Constant.PAYMENT_ALREADY.equals(contract.getContractPaymentState())) {
|
// 合同款总金额
|
BigDecimal contractTotalAmount = contract.getContractAmount() == null ? new BigDecimal(0) : contract.getContractAmount();
|
// 查询合同款项付款金额
|
BigDecimal contractAmount = contractPaymentRepository.findByContractIdAndFundType(contract.getContractId(), Constant.FUND_CONTRACT);
|
contractAmount = contractAmount == null ? new BigDecimal(0) : contractAmount;
|
// 判断合同款项付款金额
|
if(Constant.CONTRACT_SERVICE.equals(contract.getContractType())) {
|
// 付款状态
|
contract.setContractPaymentState(null);
|
} else if(contractAmount.compareTo(new BigDecimal(0)) == 0) {
|
// 付款状态
|
contract.setContractPaymentState(Constant.PAYMENT_NOT);
|
} else if(contractAmount.compareTo(contractTotalAmount) > -1) {
|
// 付款状态
|
contract.setContractPaymentState(Constant.PAYMENT_ALREADY);
|
} else {
|
// 付款状态
|
contract.setContractPaymentState(Constant.PAYMENT_PART);
|
}
|
}
|
|
// 判断服务款项付款状态
|
if(!Constant.PAYMENT_ALREADY.equals(contract.getServicePaymentState())) {
|
// 判断服务开始日期是否为空
|
if(serviceStartsTime == null) {
|
// 付款状态
|
contract.setServicePaymentState(null);
|
} else {
|
// 两时间差
|
ZoneId zoneId = ZoneId.systemDefault();
|
LocalDateTime fromDateTime = LocalDateTime.ofInstant(serviceStartsTime.toInstant(), zoneId);
|
LocalDateTime toDateTime = LocalDateTime.ofInstant(endTime.toInstant(), zoneId);
|
LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);
|
long years = tempDateTime.until(toDateTime, ChronoUnit.YEARS);
|
years = years == 0 ? 1 : years;
|
// 服务款总金额
|
BigDecimal serviceTotalAmount = contract.getServiceAmount() == null ? new BigDecimal(0) : contract.getServiceAmount();
|
// 查询服务款项付款金额
|
BigDecimal serviceAmount = contractPaymentRepository.findByContractIdAndFundType(contract.getContractId(), Constant.FUND_SERVICE);
|
serviceAmount = serviceAmount == null ? new BigDecimal(0) : serviceAmount;
|
// 判断服务款项付款金额
|
if(Constant.CONTRACT_DEVELOP.equals(contract.getContractType())) {
|
// 付款状态
|
contract.setServicePaymentState(null);
|
} else if(serviceAmount.compareTo(new BigDecimal(0)) == 0) {
|
// 付款状态
|
contract.setServicePaymentState(Constant.PAYMENT_NOT);
|
} else if(serviceAmount.compareTo(serviceTotalAmount.multiply(new BigDecimal(years))) > -1) {
|
// 付款状态
|
contract.setServicePaymentState(Constant.PAYMENT_ALREADY);
|
} else {
|
// 付款状态
|
contract.setServicePaymentState(Constant.PAYMENT_PART);
|
}
|
}
|
}
|
|
// 返回
|
return contract;
|
}
|
|
/**
|
* 查询合同款总金额
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public BigDecimal contractTotalMoney(ContractQueryCriteria criteria){
|
// 搜索数据处理
|
String signStartDate = criteria.getSignDate() != null ? criteria.getSignDate().get(0).toString() : null;
|
String signEndDate = criteria.getSignDate() != null ? criteria.getSignDate().get(1).toString() : null;
|
// 查询合同款总金额
|
BigDecimal contractTotalMoney = contractRepository.contractTotalMoney(
|
criteria.getContractNumber(), criteria.getContractName(), criteria.getFirstId(), criteria.getSecondId(), signStartDate, signEndDate, criteria.getContractType(),
|
criteria.getContractPaymentState(), criteria.getServicePaymentState(), criteria.getContractStatus(), criteria.getContractAreaCode()
|
);
|
return contractTotalMoney;
|
}
|
|
/**
|
* 查询合同款延期数
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public Integer contractDelayTotalAmount(ContractQueryCriteria criteria){
|
// 合同款延期数
|
Integer contractDelayTotalAmount = 0;
|
// 搜索数据处理
|
String signStartDate = criteria.getSignDate() != null ? criteria.getSignDate().get(0).toString() : null;
|
String signEndDate = criteria.getSignDate() != null ? criteria.getSignDate().get(1).toString() : null;
|
// 查询有合同款的合同数据
|
List<Contract> contractList = contractRepository.findContractAmountNotNull(
|
criteria.getContractNumber(), criteria.getContractName(), criteria.getFirstId(), criteria.getSecondId(), signStartDate, signEndDate, criteria.getContractType(),
|
criteria.getContractPaymentState(), criteria.getServicePaymentState(), criteria.getContractStatus(), criteria.getContractAreaCode()
|
);
|
// 循环有合同款的合同数据
|
for(int i = 0; i < contractList.size(); i ++) {
|
// 查询合同各款项付款金额
|
BigDecimal paymentAmount = contractPaymentRepository.findByContractIdAndFundType(contractList.get(i).getContractId(), Constant.FUND_CONTRACT);
|
// 判断是否超过付款时间
|
if(new Date().getTime() > contractList.get(i).getContractPaymentDate().getTime()) {
|
// 判断合同各款项付款金额不为空且是否存在延期金额
|
if(paymentAmount == null) {
|
// 合同款延期数
|
contractDelayTotalAmount ++;
|
} else if(contractList.get(i).getContractAmount().compareTo(paymentAmount) == 1) {
|
// 合同款延期数
|
contractDelayTotalAmount ++;
|
}
|
}
|
}
|
return contractDelayTotalAmount;
|
}
|
|
/**
|
* 查询合同款延期金额
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public BigDecimal contractDelayTotalMoney(ContractQueryCriteria criteria){
|
// 合同款延期金额
|
BigDecimal contractDelayTotalMoney = new BigDecimal("0.00");
|
// 搜索数据处理
|
String signStartDate = criteria.getSignDate() != null ? criteria.getSignDate().get(0).toString() : null;
|
String signEndDate = criteria.getSignDate() != null ? criteria.getSignDate().get(1).toString() : null;
|
// 查询有合同款的合同数据
|
List<Contract> contractList = contractRepository.findContractAmountNotNull(
|
criteria.getContractNumber(), criteria.getContractName(), criteria.getFirstId(), criteria.getSecondId(), signStartDate, signEndDate, criteria.getContractType(),
|
criteria.getContractPaymentState(), criteria.getServicePaymentState(), criteria.getContractStatus(), criteria.getContractAreaCode()
|
);
|
// 循环有合同款的合同数据
|
for(int i = 0; i < contractList.size(); i ++) {
|
// 查询合同各款项付款金额
|
BigDecimal paymentAmount = contractPaymentRepository.findByContractIdAndFundType(contractList.get(i).getContractId(), Constant.FUND_CONTRACT);
|
// 判断是否超过付款时间
|
if(new Date().getTime() > contractList.get(i).getContractPaymentDate().getTime()) {
|
// 判断合同各款项付款金额不为空且是否存在延期金额
|
if(paymentAmount == null) {
|
// 合同款延期金额
|
contractDelayTotalMoney = contractDelayTotalMoney.add(contractList.get(i).getContractAmount());
|
} else if(contractList.get(i).getContractAmount().compareTo(paymentAmount) == 1) {
|
// 延期金额
|
BigDecimal tempMoney = contractList.get(i).getContractAmount().subtract(paymentAmount);
|
// 合同款延期金额
|
contractDelayTotalMoney = contractDelayTotalMoney.add(tempMoney);
|
}
|
}
|
}
|
return contractDelayTotalMoney;
|
}
|
|
/**
|
* 查询预收款总金额
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public BigDecimal advancePaymentTotalMoney(ContractQueryCriteria criteria){
|
// 预收款总金额
|
BigDecimal advancePaymentTotalMoney = new BigDecimal("0.00");
|
// 查询所有合同信息
|
List<Contract> contractList = contractRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
|
// 循环所有合同信息
|
for(int i = 0; i < contractList.size(); i ++) {
|
// 预收款总金额
|
advancePaymentTotalMoney = advancePaymentTotalMoney.add(advancePayment(contractList.get(i)));
|
}
|
return advancePaymentTotalMoney;
|
}
|
|
/**
|
* 查询运维服务总金额
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public BigDecimal serviceTotalMoney(ContractQueryCriteria criteria){
|
// 搜索数据处理
|
String signStartDate = criteria.getSignDate() != null ? criteria.getSignDate().get(0).toString() : null;
|
String signEndDate = criteria.getSignDate() != null ? criteria.getSignDate().get(1).toString() : null;
|
// 查询运维服务总金额
|
BigDecimal serviceTotalMoney = contractRepository.serviceTotalMoney(
|
criteria.getContractNumber(), criteria.getContractName(), criteria.getFirstId(), criteria.getSecondId(), signStartDate, signEndDate, criteria.getContractType(),
|
criteria.getContractPaymentState(), criteria.getServicePaymentState(), criteria.getContractStatus(), criteria.getContractAreaCode()
|
);
|
return serviceTotalMoney;
|
}
|
|
/**
|
* 查询运维服务延期数
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public Integer serviceDelayTotalAmount(ContractQueryCriteria criteria){
|
// 运维服务延期数
|
Integer serviceDelayTotalAmount = 0;
|
// 搜索数据处理
|
String signStartDate = criteria.getSignDate() != null ? criteria.getSignDate().get(0).toString() : null;
|
String signEndDate = criteria.getSignDate() != null ? criteria.getSignDate().get(1).toString() : null;
|
// 查询有运维服务的合同数据
|
List<Contract> contractList = contractRepository.findServiceAmountNotNull(
|
criteria.getContractNumber(), criteria.getContractName(), criteria.getFirstId(), criteria.getSecondId(), signStartDate, signEndDate, criteria.getContractType(),
|
criteria.getContractPaymentState(), criteria.getServicePaymentState(), criteria.getContractStatus(), criteria.getContractAreaCode()
|
);
|
// 循环有运维服务的合同数据
|
for(int i = 0; i < contractList.size(); i ++) {
|
// 时间格式化
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// 服务开始年份
|
String serviceStartsYear = null;
|
// 判断服务开始日期是否为空
|
if(contractList.get(i).getServiceStartsDate() != null) {
|
// 服务开始年份
|
serviceStartsYear = contractList.get(i).getServiceStartsDate().toString().substring(0, 4);
|
}
|
// 服务预计付款月日
|
String servicePaymentMonth = null;
|
// 判断服务预计付款日期是否为空
|
if(contractList.get(i).getServicePaymentDate() != null) {
|
// 服务预计付款月日
|
servicePaymentMonth = contractList.get(i).getServicePaymentDate().toString().substring(4, 10);
|
|
}
|
// 当前系统时间
|
Date systemTime = new Date();
|
// 当前系统年份
|
String systemYear = sdf.format(systemTime).substring(0, 4);
|
// 当前年付款时间
|
Date systemPaymentTime = null;
|
// 判断服务预计付款日期是否为空
|
if(contractList.get(i).getServicePaymentDate() != null) {
|
// 当前年付款时间
|
try {
|
systemPaymentTime = sdf.parse(systemYear + servicePaymentMonth + " 00:00:00");
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// 服务款总金额
|
BigDecimal serviceTotalAmount = contractList.get(i).getServiceAmount() == null ? new BigDecimal(0) : contractList.get(i).getServiceAmount();
|
// 判断服务开始年份是否为空
|
if(serviceStartsYear != null) {
|
// 服务开始年份与当前年份差
|
int years = Integer.valueOf(systemYear) - Integer.valueOf(serviceStartsYear) + 1;
|
// 循环年份差
|
for(int j = 0; j < years; j ++) {
|
// 查询合同服务款项各年度付款金额
|
BigDecimal serviceAmount = contractPaymentRepository.findByContractIdAndFundBelongStart(contractList.get(i).getContractId(), Integer.valueOf(serviceStartsYear) + j);
|
serviceAmount = serviceAmount == null ? new BigDecimal(0) : serviceAmount;
|
// 判断服务款总金额与付款金额
|
if(serviceTotalAmount.compareTo(serviceAmount) == 1) {
|
// 判断是否是当年数据
|
if(Integer.valueOf(serviceStartsYear) + j == Integer.valueOf(systemYear)) {
|
// 判断是否超出付款时间
|
if(systemPaymentTime != null && systemTime.getTime() > systemPaymentTime.getTime()) {
|
// 运维服务延期数
|
serviceDelayTotalAmount ++;
|
}
|
} else {
|
// 运维服务延期数
|
serviceDelayTotalAmount ++;
|
}
|
}
|
}
|
}
|
}
|
return serviceDelayTotalAmount;
|
}
|
|
/**
|
* 查询运维服务延期金额
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public BigDecimal serviceDelayTotalMoney(ContractQueryCriteria criteria){
|
// 运维服务延期金额
|
BigDecimal serviceDelayTotalMoney = new BigDecimal("0.00");
|
// 搜索数据处理
|
String signStartDate = criteria.getSignDate() != null ? criteria.getSignDate().get(0).toString() : null;
|
String signEndDate = criteria.getSignDate() != null ? criteria.getSignDate().get(1).toString() : null;
|
// 查询有运维服务的合同数据
|
List<Contract> contractList = contractRepository.findServiceAmountNotNull(
|
criteria.getContractNumber(), criteria.getContractName(), criteria.getFirstId(), criteria.getSecondId(), signStartDate, signEndDate, criteria.getContractType(),
|
criteria.getContractPaymentState(), criteria.getServicePaymentState(), criteria.getContractStatus(), criteria.getContractAreaCode()
|
);
|
// 循环有运维服务的合同数据
|
for(int i = 0; i < contractList.size(); i ++) {
|
// 时间格式化
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// 服务开始年份
|
String serviceStartsYear = null;
|
// 判断服务开始日期是否为空
|
if(contractList.get(i).getServiceStartsDate() != null) {
|
// 服务开始年份
|
serviceStartsYear = contractList.get(i).getServiceStartsDate().toString().substring(0, 4);
|
}
|
// 服务预计付款月日
|
String servicePaymentMonth = null;
|
// 判断服务预计付款日期是否为空
|
if(contractList.get(i).getServicePaymentDate() != null) {
|
// 服务预计付款月日
|
servicePaymentMonth = contractList.get(i).getServicePaymentDate().toString().substring(4, 10);
|
|
}
|
// 当前系统时间
|
Date systemTime = new Date();
|
// 当前系统年份
|
String systemYear = sdf.format(systemTime).substring(0, 4);
|
// 当前年付款时间
|
Date systemPaymentTime = null;
|
// 判断服务预计付款日期是否为空
|
if(contractList.get(i).getServicePaymentDate() != null) {
|
// 当前年付款时间
|
try {
|
systemPaymentTime = sdf.parse(systemYear + servicePaymentMonth + " 00:00:00");
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// 服务款总金额
|
BigDecimal serviceTotalAmount = contractList.get(i).getServiceAmount() == null ? new BigDecimal(0) : contractList.get(i).getServiceAmount();
|
// 判断服务开始年份是否为空
|
if(serviceStartsYear != null) {
|
// 服务开始年份与当前年份差
|
int years = Integer.valueOf(systemYear) - Integer.valueOf(serviceStartsYear) + 1;
|
// 循环年份差
|
for(int j = 0; j < years; j ++) {
|
// 查询合同服务款项各年度付款金额
|
BigDecimal serviceAmount = contractPaymentRepository.findByContractIdAndFundBelongStart(contractList.get(i).getContractId(), Integer.valueOf(serviceStartsYear) + j);
|
serviceAmount = serviceAmount == null ? new BigDecimal(0) : serviceAmount;
|
// 判断服务款总金额与付款金额
|
if(serviceTotalAmount.compareTo(serviceAmount) == 1) {
|
// 判断是否是当年数据
|
if(Integer.valueOf(serviceStartsYear) + j == Integer.valueOf(systemYear)) {
|
// 判断是否超出付款时间
|
if(systemPaymentTime != null && systemTime.getTime() > systemPaymentTime.getTime()) {
|
// 延期金额
|
BigDecimal tempMoney = serviceTotalAmount.subtract(serviceAmount);
|
// 运维服务延期金额
|
serviceDelayTotalMoney = serviceDelayTotalMoney.add(tempMoney);
|
}
|
} else {
|
// 延期金额
|
BigDecimal tempMoney = serviceTotalAmount.subtract(serviceAmount);
|
// 运维服务延期金额
|
serviceDelayTotalMoney = serviceDelayTotalMoney.add(tempMoney);
|
}
|
}
|
}
|
}
|
}
|
return serviceDelayTotalMoney;
|
}
|
|
/**
|
* 查询应收款总金额
|
* @param criteria
|
* @return
|
*/
|
@Override
|
public BigDecimal receivePaymentTotalMoney(ContractQueryCriteria criteria){
|
// 应收款总金额
|
BigDecimal receivePaymentTotalMoney = new BigDecimal("0.00");
|
// 查询所有合同信息
|
List<Contract> contractList = contractRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
|
// 循环所有合同信息
|
for(int i = 0; i < contractList.size(); i ++) {
|
// 应收款总金额
|
receivePaymentTotalMoney = receivePaymentTotalMoney.add(receivePayment(contractList.get(i)));
|
}
|
return receivePaymentTotalMoney;
|
}
|
}
|