jiangping
2025-06-06 a2299a6d4a6f99e9c11132138f5d3e9ec68f03ea
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwContractRevenueServiceImpl.java
@@ -24,16 +24,14 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -87,7 +85,12 @@
        ){
             throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        YwContractBill ywContractBill = ywContractBillMapper.selectById(ywContractRevenue.getBillId());
        YwContractBill ywContractBill = ywContractBillMapper.selectJoinOne(YwContractBill.class,
                new MPJLambdaWrapper<YwContractBill>()
                        .selectAll(YwContractBill.class)
                        .selectAs(YwContract::getStatus, YwContractBill::getContractStatus)
                        .leftJoin(YwContract.class,YwContract::getId,YwContractBill::getContractId)
                        .eq(YwContractBill::getId,ywContractRevenue.getBillId()));
        if(Objects.isNull(ywContractBill)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到账单信息");
        }
@@ -128,7 +131,7 @@
        BigDecimal waitPayTotal = BigDecimal.ZERO;
        //无付款记录 则为初次支付 根据账单类型 判断是支出 / 收入
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(ywContractRevenueList)){
            ywContractRevenue.setRevenueType(ywContractBill.getType());
            ywContractRevenue.setRevenueType(ywContractBill.getBillType());
            if(ywContractRevenue.getActReceivableFee().compareTo(ywContractBill.getReceivableFee())>Constants.ZERO){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请输入正确的金额!");
            }else  if(ywContractRevenue.getActReceivableFee().compareTo(ywContractBill.getReceivableFee())==Constants.ZERO){
@@ -138,19 +141,39 @@
            }
        }else{
            //获取已支付的总金额 (可能有收入 有支出)
            payTotal = ywContractRevenueList.stream().map(i->i.getActReceivableFee().multiply(
                    BigDecimal.valueOf(Constants.equalsInteger(ywContractRevenue.getRevenueType(),Constants.ZERO)?Constants.ONE:-Constants.ONE))
            ).reduce(BigDecimal.ZERO,BigDecimal::add);
            //获取待支付款金额 如果账单类型为支出 或 (账单类型为收入 且 待支付金额 小于 0) 则为支付
            waitPayTotal = ywContractBill.getReceivableFee().subtract(payTotal);
            //如果待支付金额 大于 0  则是 收入 否则是支出 其他状态 为异常!
            if(waitPayTotal.compareTo(BigDecimal.ZERO)>Constants.ZERO){
                ywContractRevenue.setRevenueType(Constants.ZERO);
            }else if(waitPayTotal.compareTo(BigDecimal.ZERO)<Constants.ZERO){
                ywContractRevenue.setRevenueType(Constants.ONE);
            if(Constants.equalsInteger(ywContractBill.getBillType(),Constants.ZERO)){
                payTotal = ywContractRevenueList.stream().map(i->
                        i.getActReceivableFee().multiply(
                                BigDecimal.valueOf(Constants.equalsInteger(i.getRevenueType(),Constants.ZERO)?Constants.ONE:-Constants.ONE))
                ).reduce(BigDecimal.ZERO,BigDecimal::add);
                //获取待支付款金额 如果账单类型为支出 或 (账单类型为收入 且 待支付金额 小于 0) 则为支付
                waitPayTotal = ywContractBill.getReceivableFee().subtract(payTotal);
                //如果待支付金额 大于 0  则是 收入 否则是支出 其他状态 为异常!
                if(waitPayTotal.compareTo(BigDecimal.ZERO)>Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ZERO);
                }else if(waitPayTotal.compareTo(BigDecimal.ZERO)<Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ONE);
                }else{
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"收支金额异常!请联系管理员");
                }
            }else{
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"收支金额异常!请联系管理员");
                payTotal = ywContractRevenueList.stream().map(i->
                        i.getActReceivableFee().multiply(
                                BigDecimal.valueOf(Constants.equalsInteger(i.getRevenueType(),Constants.ZERO)?-Constants.ONE:Constants.ONE))
                ).reduce(BigDecimal.ZERO,BigDecimal::add);
                //获取待支付款金额 如果账单类型为支出 或 (账单类型为收入 且 待支付金额 小于 0) 则为支付
                waitPayTotal = ywContractBill.getReceivableFee().subtract(payTotal);
                //如果待支付金额 大于 0  则是 收入 否则是支出 其他状态 为异常!
                if(waitPayTotal.compareTo(BigDecimal.ZERO)>Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ONE);
                }else if(waitPayTotal.compareTo(BigDecimal.ZERO)<Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ZERO);
                }else{
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"收支金额异常!请联系管理员");
                }
            }
            //待支付的流水 如果为收入 则比对 金额值  如果是支出 则获取绝对值 进行对比
            if(Constants.equalsInteger(ywContractRevenue.getRevenueType(),Constants.ZERO)){
                //如果支付金额 大于 待支付金额 则提示异常 如果支付金额小于 待支付金额 则状态不变化 其他状态 异常
@@ -170,6 +193,22 @@
        }
        ywContractRevenueMapper.insert(ywContractRevenue);
        ywContractBillMapper.updateById(ywContractBill);
        //如果账单完结,则查询合同下开启中的账单是否存在退款中 如果不存在则标记合同已退款
        if(Constants.equalsInteger(ywContractBill.getContractStatus(),Constants.THREE)
                && Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.ONE)){
            if( ywContractBillMapper
                    .selectCount(new QueryWrapper<YwContractBill>().lambda().eq(YwContractBill::getContractId,ywContractBill.getContractId())
                            .ne(YwContractBill::getId,ywContractBill.getId())
                    .eq(YwContractBill::getStatus,Constants.ZERO)
                                    .in(YwContractBill::getPayStatus,Constants.ZERO,Constants.FOUR, Constants.TWO,Constants.THREE)
                    ) == Constants.ZERO){
                ywContractMapper.update(new UpdateWrapper<YwContract>().lambda()
                        .set(YwContract::getStatus,Constants.FOUR)
                        .set(YwContract::getEditDate,DateUtil.getCurrDateTime())
                        .eq(YwContract::getId,ywContractBill.getContractId())
                );
            }
        }
        //存储附件信息
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRevenue.getMultifileList())){
            for (Multifile multifile:ywContractRevenue.getMultifileList()) {
@@ -235,6 +274,7 @@
                new MPJLambdaWrapper<YwContractRevenue>().selectAll(YwContractRevenue.class)
                        .selectAs(YwContractBill::getCode,YwContractRevenue::getBillCode)
                        .selectAs(YwContract::getCode,YwContractRevenue::getContractCode)
                        .selectAs(YwContract::getStatus,YwContractRevenue::getContractStatus)
                        .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName)
                        .selectAs(SystemUser::getRealname,YwContractRevenue::getRealname)
                        .selectAs(YwAccount::getTitle,YwContractRevenue::getAccountTitle)
@@ -392,7 +432,9 @@
        IPage<YwContractRevenue> iPage = ywContractRevenueMapper.selectJoinPage(page,YwContractRevenue.class,
                queryWrapper.selectAll(YwContractRevenue.class)
                        .selectAs(YwContractBill::getCode,YwContractRevenue::getBillCode)
                        .selectAs(YwContractBill::getType,YwContractRevenue::getBillType)
                         .selectAs(YwContract::getCode,YwContractRevenue::getContractCode)
                        .selectAs(YwContract::getStatus,YwContractRevenue::getContractStatus)
                        .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName)
                        .selectAs(SystemUser::getRealname,YwContractRevenue::getRealname)
                        .leftJoin(YwContractBill.class,YwContractBill::getId,YwContractRevenue::getBillId)
@@ -407,7 +449,8 @@
                        .eq(Objects.nonNull(model)&&Objects.nonNull(model.getRevenueType()),
                                YwContractRevenue::getRevenueType,model.getRevenueType())
                        .ge(Objects.nonNull(model)&&Objects.nonNull(model.getPayDateStart()),YwContractRevenue::getActPayDate, Utils.Date.getStart(model.getPayDateStart()))
                        .le(Objects.nonNull(model)&&Objects.nonNull(model.getPayDateEnd()),YwContractRevenue::getActPayDate, Utils.Date.getEnd(model.getPayDateEnd())) )
                        .le(Objects.nonNull(model)&&Objects.nonNull(model.getPayDateEnd()),YwContractRevenue::getActPayDate, Utils.Date.getEnd(model.getPayDateEnd()))
                        .orderByDesc(YwContractRevenue::getId))
                ;
        this.dealRoomDetail(iPage.getRecords());
@@ -418,6 +461,17 @@
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRoomList)){
                StringBuilder roomPathName = new StringBuilder();
                for (YwContractRoom ywContractRoom:ywContractRoomList) {
                    if(Constants.equalsInteger(ywContractRevenue.getBillType(),Constants.ZERO)){
                        if(!(Constants.equalsInteger(ywContractRevenue.getContractId(),ywContractRoom.getContractId())
                        && Constants.equalsInteger(ywContractRoom.getType(),Constants.ZERO))){
                            continue;
                        }
                    }else{
                        if(!(Constants.equalsInteger(ywContractRevenue.getBillId(),ywContractRoom.getContractId())
                                && Constants.equalsInteger(ywContractRoom.getType(),Constants.ONE))){
                            continue;
                        }
                    }
                    if(StringUtils.isNotBlank(ywContractRoom.getProjectName())){
                        roomPathName.append(ywContractRoom.getProjectName()+"/");
                    }
@@ -447,24 +501,30 @@
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRevenueList)){
            //获取所有数据
            List<Integer> billIdList = ywContractRevenueList.stream().map(i->i.getBillId()).collect(Collectors.toList());
            List<Integer> contractIdList = ywContractRevenueList.stream().map(i->i.getContractId()).collect(Collectors.toList());
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(billIdList)){
                List<YwContractRoom> ywContractRoomList  = ywContractRoomMapper.selectJoinList(YwContractRoom.class,new MPJLambdaWrapper<YwContractRoom>()
                        .selectAll(YwContractRoom.class)
                        .selectAs(YwProject::getName,YwRoom::getProjectName)
                        .selectAs(YwFloor::getName,YwRoom::getFloorName)
                        .selectAs(YwBuilding::getName,YwRoom::getBuildingName)
                        .selectAs(YwRoom::getName,YwContractRoom::getRoomName)
                        .selectAs(YwRoom::getCode,YwContractRoom::getRoomName)
                        .leftJoin(YwRoom.class,YwRoom::getId,YwContractRoom::getRoomId)
                        .leftJoin(YwFloor.class,YwFloor::getId,YwRoom::getFloor)
                        .leftJoin(YwProject.class,YwProject::getId,YwRoom::getProjectId)
                        .leftJoin(YwBuilding.class,YwBuilding::getId,YwRoom::getBuildingId)
                        .in(YwContractRoom::getContractId,billIdList)
                        .eq(YwContractRoom::getType,Constants.ONE)
                        .and(i->i.in(YwContractRoom::getContractId,billIdList).eq(YwContractRoom::getType,Constants.ONE)
                                .or().in(YwContractRoom::getContractId,contractIdList).eq(YwContractRoom::getType,Constants.ZERO)
                        )
//                        .in(YwContractRoom::getContractId,billIdList)
//                        .eq(YwContractRoom::getType,Constants.ONE)
                );
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRoomList)){
                    for (YwContractRevenue ywContractRevenue:ywContractRevenueList) {
                        ywContractRevenue.setYwContractRoomList(
                                ywContractRoomList.stream().filter(i->Constants.equalsInteger(i.getContractId(),ywContractRevenue.getBillId())).collect(Collectors.toList())
                                ywContractRoomList.stream().filter(i->
                                        Constants.equalsInteger(i.getContractId(),ywContractRevenue.getBillId())||Constants.equalsInteger(i.getContractId(),ywContractRevenue.getContractId()))
                                        .collect(Collectors.toList())
                        );
                    }
                }