renkang
2024-12-04 2a0d7762b0113bba17bd50203360ec669aa20b18
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwContractServiceImpl.java
@@ -33,6 +33,7 @@
import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 运维合同信息表Service实现
@@ -86,11 +87,11 @@
        dealMultifileBiz(model);//处理附件信息
        dealRoomsForContract(model);//处理房源关联表
        dealLogBiz(model,Constants.YwLogType.CONTRACT_CREATE,model.getLoginUserInfo().getRealname(),"【"+model.getRemark().replace("合同摘要:","")+"】");//记录新建日志
        return model.getId();
    }
    private void dealRoomsForContract(YwContract model) {
        this.dealRoomsValid(model);
        List<YwContractRoom> list = new ArrayList<>();
        for(YwRoom room :model.getRoomList()){
            YwContractRoom t = new YwContractRoom();
@@ -106,6 +107,46 @@
        }
        ywContractRoomMapper.insert(list);
    }
    private void dealRoomsValid(YwContract model){
        List<Integer> roomIds = model.getRoomList().stream().map(i->i.getId()).collect(Collectors.toList());
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(roomIds)){
            if(ywContractMapper.selectJoinCount(new MPJLambdaWrapper<YwContract>()
                    .leftJoin(YwContractRoom.class,YwContractRoom::getContractId,YwContract::getId)
                    .eq(YwContractRoom::getType,Constants.ZERO)
                    .in(YwContractRoom::getRoomId,roomIds)
                    .in(YwContract::getStatus,Constants.ZERO,Constants.ONE,Constants.TWO)
                    .apply(" (" +
                            " ( t.START_DATE < '"+DateUtil.getFomartDate(model.getEndDate(),"yyyy-MM-dd HH:mm:ss")+"'  and t.END_DATE > '"+DateUtil.getFomartDate(model.getStartDate(),"yyyy-MM-dd HH:mm:ss")+"' ) " +
                            "or " +
                            " ( t.START_DATE < '"+DateUtil.getFomartDate(model.getEndDate(),"yyyy-MM-dd HH:mm:ss")+"'  and t.END_DATE > '"+DateUtil.getFomartDate(model.getStartDate(),"yyyy-MM-dd HH:mm:ss")+"' ) " +
                            " ) ")
            )>Constants.ZERO){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"房源已被占用请刷新重试");
            };
            if(ywContractMapper.selectJoinCount(new MPJLambdaWrapper<YwContract>()
                    .leftJoin(YwContractRoom.class,YwContractRoom::getContractId,YwContract::getId)
                    .eq(YwContractRoom::getType,Constants.ZERO)
                    .in(YwContractRoom::getRoomId,roomIds)
                    .in(YwContract::getStatus,Constants.THREE)
                    .apply(" ( t.START_DATE < '"+DateUtil.getFomartDate(model.getEndDate(),"yyyy-MM-dd HH:mm:ss")+"' " +
                            " and t.BT_DATE > '"+DateUtil.getFomartDate(model.getStartDate(),"yyyy-MM-dd HH:mm:ss")+"' ) " )
            )>Constants.ZERO){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"房源已被占用请刷新重试");
            };
        }
    }
    @Override
    public   List<YwContractBill> getBillList(YwContract model){
@@ -148,7 +189,7 @@
            if(!(model.getZlFreeEndDate()==null &&  model.getZlFreeStartDate()==null) && (
                    (model.getZlFreeEndDate()!=null &&  model.getZlFreeStartDate()==null)
                            ||(model.getZlFreeEndDate()==null &&  model.getZlFreeStartDate()!=null)
                            ||model.getZlFreeEndDate().getTime()<= model.getZlFreeStartDate().getTime())
                            ||model.getZlFreeEndDate().getTime()< model.getZlFreeStartDate().getTime())
            ){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请选择正确的租赁条款免租期信息!");
            }
@@ -193,7 +234,7 @@
            }
            for(YwContractDetail d :model.getWyDetailList()){
                if(  d.getStartDate() == null
                        ||d.getEditDate() == null
                        ||d.getEndDate() == null
                        ||d.getPrice() == null
                        ||d.getCircleType() == null
                        || d.getCircleType()>6
@@ -201,7 +242,7 @@
                ){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请按页面要求填写物业条款信息!");
                }
                if(d.getEndDate().getTime()<= d.getStartDate().getTime()){
                if(d.getEndDate().getTime()< d.getStartDate().getTime()){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,物业条款有效期结束时间不得早于开始时间!");
                }
                if(!Constants.equalsInteger(model.getWyPayType(),Constants.ZERO) &&
@@ -236,9 +277,9 @@
        String str = "";
        //起租日2024/06/01,租赁数为500㎡。首期租赁三月一付,租金单价35元/㎡·月。首期物业三月一付,物业单价4.3元/㎡·月
        String str0 = "合同摘要:起租日{param1},租赁数为{param2}㎡。首期租赁{param3},租金单价{param4}元{param5}。首期物业{param6},物业单价{param7}{param8}";
        String str1 = "合同摘要:起租日{param1},租赁数为{param2}㎡。首期租赁{param3},租金单价{param4}元{param5}。";
        String str2 = "合同摘要:起租日{param1},首期物业{param6},物业单价{param7}元{param8}";
        String str0 = "合同摘要:起租日{param1},租赁数为{param2}㎡。首期租赁{param3},租金单价{param4}{param5}。首期物业{param6},物业单价{param7}{param8}";
        String str1 = "合同摘要:起租日{param1},租赁数为{param2}㎡。首期租赁{param3},租金单价{param4}{param5}。";
        String str2 = "合同摘要:起租日{param1},首期物业{param6},物业单价{param7}{param8}";
        if(Constants.equalsInteger(model.getType(),Constants.ZERO)){
            str= str0;
        }
@@ -252,19 +293,20 @@
                .replace("{param2}",model.getTotalArea().intValue()+"")
                .replace("{param3}",Constants.getPayTypeByNum(model.getZlPayType()))
                .replace("{param4}",Constants.formatBigdecimal(model.getZlFirstPrice()).intValue()+"")
                .replace("{param5}",Constants.getUnitTypeByNum(model.getZlFirstCircle())
                .replace("{param5}",Constants.getUnitTypeByNum(model.getZlFirstCircle()))
                .replace("{param6}",Constants.getPayTypeByNum(model.getWyPayType()))
                .replace("{param7}",Constants.formatBigdecimal(model.getWyFirstPrice()).intValue()+"")
                .replace("{param8}",Constants.getUnitTypeByNum(model.getWyFirstCircle())));
                .replace("{param8}",Constants.getUnitTypeByNum(model.getWyFirstCircle()));
        return  str;
    }
    private String getbackRentRemarkByParam(YwContract model) {
        BigDecimal fee = Constants.formatBigdecimal(model.getBtFee());
        String str = "退租摘要:剩余未结清账单{param1}份,关闭账单{param2}份【退租日{param3},退租费用合计需{param4}{param5}元。】";
        str = str.replace("{param1}",model.getBtWaitBill()+"")
                .replace("{param2}",DateUtil.getDateLongSlash(model.getBtDate()))
                .replace("{param3}",fee.compareTo(new BigDecimal(0)) >=0?"收":"付")
                .replace("{param4}",(fee.compareTo(new BigDecimal(0)) >=0?
                .replace("{param2}",Integer.toString(model.getBtCLoseBill()))
                .replace("{param3}",DateUtil.getDateLongSlash(model.getBtDate()))
                .replace("{param4}",fee.compareTo(new BigDecimal(0)) >=0?"收":"付")
                .replace("{param5}",(fee.compareTo(new BigDecimal(0)) >=0?
                        Constants.formatBigdecimal(model.getBtFee()).intValue()
                        :(Constants.formatBigdecimal(model.getBtFee()).intValue() * -1))+"" );
        return  str;
@@ -348,7 +390,7 @@
        List<YwContractBill> canBills = new ArrayList<>();//可退租修改数据
        List<YwContractBill> allBills = ywContractBillMapper.selectList(new QueryWrapper<YwContractBill>().
                select("*," +
                        "( select ifnull(sum(yw.ACT_RECEIVABLE_FEE),0) from  yw_contract_revenue yw where yw.bill_id = yw_contract_bill.id and yw.status = 0 and yw.isdeleted = 0 ) as  actReceivableFee " )
                        "( select ifnull(sum(case when yw.REVENUE_TYPE = 0 then yw.ACT_RECEIVABLE_FEE  else  -yw.ACT_RECEIVABLE_FEE end),0) from  yw_contract_revenue yw where yw.bill_id = yw_contract_bill.id and yw.status = 0 and yw.isdeleted = 0 ) as  actReceivableFee " )
                .lambda().
                eq(YwContractBill::getIsdeleted,Constants.ZERO)
                .eq(YwContractBill::getContractId,param.getId()));
@@ -365,15 +407,23 @@
                }
                if(Constants.equalsInteger(bill.getPayStatus(),Constants.ZERO)
                        ||Constants.equalsInteger(bill.getPayStatus(),Constants.THREE)){
                    if(bill.getStartDate().getTime()>param.getEditDate().getTime()){
                    if(bill.getStartDate().getTime()<param.getBtDate().getTime()){
                        canBills.add(bill);
                    }else{
                        //如果还没开始,账单直接关闭
                        closeBills.add(bill);
                        noBills.add(bill);
                    }else{
                        canBills.add(bill);
                    }
//                    if(bill.getStartDate().getTime()>param.getEditDate().getTime()){
//                        //如果还没开始,账单直接关闭
//                        closeBills.add(bill);
//                        noBills.add(bill);
//                    }else{
//                        canBills.add(bill);
//                    }
                }else  if(Constants.equalsInteger(bill.getPayStatus(),Constants.ONE) ){
                    if(bill.getEndDate().getTime()<param.getEditDate().getTime()){
                    if(bill.getStartDate().getTime()>param.getBtDate().getTime()){
//                    if(bill.getEndDate().getTime()<param.getEditDate().getTime()){
                        //如果已结清,账单直接关闭
                        noBills.add(bill);
                    }else{
@@ -394,16 +444,7 @@
                ywContractBillMapper.updateById(b);
            }
        }
        int yjNoBills =0;
        if(yjBills.size()>0){
            // 如果是押金或者保证金,不支持退款,保持原来的状态, 0=租赁费;1=物业费;2=租赁押金;3=物业押金;4=水电费;5=杂项费;6=其他;7=保证金
            for(YwContractBill bill : yjBills){
               if(!Constants.equalsInteger(bill.getPayStatus(),Constants.ONE) ){
                   //如果是未结清押金
                   yjNoBills ++;
               }
            }
        }
        BigDecimal totalBackFee = new BigDecimal(0);
        if(canBills.size()>0){
            //直接关闭关闭账单
@@ -416,14 +457,15 @@
                    fee = Constants.formatBigdecimal(b.getActReceivableFee()).subtract(editBill.getReceivableFee());
                    if(fee.compareTo(new BigDecimal(0))== 0){
                        //如果费用正好,则修改账单信息为已结清
                        b.setStatus(Constants.ONE);
                        b.setPayStatus(Constants.ONE);
                    }else if(fee.compareTo(new BigDecimal(0))> 0){
                        //如果需要进行退款,更新账单信息为待退款
                        b.setStatus(Constants.FOUR);
                        b.setPayStatus(Constants.FOUR);
                    }else if(fee.compareTo(new BigDecimal(0)) < 0){
                        //如果账单还有款待收,则保持状态不变
                    }
                }
                b.setReceivableFee(editBill.getReceivableFee());
                b.setEditDate(param.getEditDate());
                b.setEditor(param.getEditor());
                b.setBtActDate(param.getBtActDate());
@@ -438,12 +480,21 @@
                totalBackFee = totalBackFee.add(fee);//累计退款金额
            }
        }
        List<YwContractBill> ywContractBillList = ywContractBillMapper.selectList(new QueryWrapper<YwContractBill>()
                .lambda().eq(YwContractBill::getContractId,param.getId()).orderByDesc(YwContractBill::getId));
        Integer sortNum = ywContractBillList.size();
        if(param.getAddBillList()!=null && param.getAddBillList().size()>0){
            for(YwContractBill addBill : param.getAddBillList()){
                sortNum = sortNum + 1 ;
                addBill.setIsdeleted(Constants.ZERO);
                addBill.setContractId(param.getId());
                addBill.setType(Constants.ONE);
                addBill.setStatus(Constants.ZERO);
                addBill.setTotleFee(addBill.getReceivableFee());
                if(Constants.equalsInteger(addBill.getFeeType(),Constants.ONE)){
                    addBill.setStartDate(addBill.getPlanPayDate());
                    addBill.setEndDate(addBill.getPlanPayDate());
                }
                if(Constants.equalsInteger(addBill.getBillType(),Constants.ZERO)){
                    //如果是收款
                    totalBackFee = totalBackFee.add(Constants.formatBigdecimal(addBill.getActReceivableFee()));
@@ -455,6 +506,8 @@
                    addBill.setBtFee(Constants.formatBigdecimal(addBill.getActReceivableFee()).multiply(new BigDecimal(-1)));
                }
                totalBackFee = totalBackFee.add(Constants.formatBigdecimal(addBill.getBtFee()));
                addBill.setCreator(param.getEditor());
                addBill.setCreateDate(param.getEditDate());
                addBill.setEditDate(param.getEditDate());
                addBill.setEditor(param.getEditor());
                addBill.setBtActDate(param.getBtActDate());
@@ -463,9 +516,38 @@
                addBill.setBtUserId(param.getBtUserId());
                addBill.setBtSignDate(param.getBtSignDate());
                addBill.setBtType(param.getBtType());
                addBill.setSortnum(sortNum );
                newBills.add(addBill);
            }
            ywContractBillMapper.insert(param.getAddBillList());//批量插入数据
        }
        int yjNoBills =0;
        if(yjBills.size()>0){
            // 如果是押金或者保证金,不支持退款,保持原来的状态, 0=租赁费;1=物业费;2=租赁押金;3=物业押金;4=水电费;5=杂项费;6=其他;7=保证金
            for(YwContractBill bill : yjBills){
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(param.getCanBackRentBills())){
                    List<YwContractBill> optional = param.getCanBackRentBills().stream().filter(i->Constants.equalsInteger(bill.getId(),i.getId())).collect(Collectors.toList());
                    if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(optional)){
                        YwContractBill canBill = optional.get(Constants.ZERO);
                        YwContractBill yjBill = new YwContractBill();
                        yjBill.setId(bill.getId());
                        yjBill.setEditDate(param.getEditDate());
                        yjBill.setEditor(param.getEditor());
                        yjBill.setReceivableFee(BigDecimal.ZERO);
                        if(bill.getActReceivableFee().compareTo(BigDecimal.ZERO)==Constants.ZERO){
                            yjBill.setPayStatus(Constants.FIVE);
                            yjBill.setStatus(Constants.ONE);
                        }else{
                            yjBill.setPayStatus(Constants.FOUR);
                            yjNoBills ++;
                        }
                        yjBill.setPlanPayDate(canBill.getPlanPayDate());
                        ywContractBillMapper.updateById(yjBill);
                    }
                }
            }
        }
        param.setBtWaitBill(canBills.size() + newBills.size()+yjNoBills);//未清算的账单数量
        param.setBtCLoseBill(closeBills.size());
@@ -623,7 +705,7 @@
                billList1.get(i).setSortnum(num++);
            }
        }
          num =1;
        num =1;
        for(int i=0;i<billList2.size();i++){
            if(Constants.equalsInteger( billList2.get(i).getCostType(),Constants.ONE)){
                billList2.get(i).setSortnum(num++);
@@ -649,7 +731,10 @@
        bill.setType(Constants.ZERO);
        bill.setCostType(type);
        bill.setPlanPayDate(bill.getStartDate());
        bill.setTotleFee(type==Constants.THREE?model.getZlDeposit():model.getWyDeposit());//押金费用
        bill.setTotleFee(type==Constants.TWO?
                Objects.isNull(model.getZlDeposit())?BigDecimal.ZERO:model.getZlDeposit()
                :
                Objects.isNull(model.getWyDeposit())?BigDecimal.ZERO:model.getWyDeposit());//押金费用
        bill.setReceivableFee(bill.getTotleFee());
        bill.setBillType(Constants.ZERO);
        bill.setPayStatus(Constants.ZERO);
@@ -762,17 +847,13 @@
            bill.setTotleFee(totalFee);
            bill.setReceivableFee(totalFee);
            bill.setBillType(Constants.ZERO);
            bill.setCompanyId(model.getCompanyId());
            list.add(bill);
        }
    }
    private BigDecimal getTotalFeeByStartEnd(YwContract model, YwContractDetail d, YwContractBill bill,Date freeStart,Date freeEnd) {
        BigDecimal totalFee = new BigDecimal(0);
        DateCompare dateCompare =   DateCompare.dayCompare(bill.getStartDate(),DateUtil.addDaysToDate(bill.getEndDate(),1),freeStart,DateUtil.addDaysToDate(freeEnd,1));
        DateCompare dateCompare =   DateCompare.dayCompare(bill.getStartDate(),bill.getEndDate(),freeStart, freeEnd);
        if(Constants.equalsInteger(d.getCircleType(),Constants.ZERO)){
            //0=元每平米天
            int days = dateCompare.getDay() ;
@@ -845,6 +926,7 @@
            bill.setTotleFee(getTotalFeeByStartEnd(model,d,bill,freeStart,freeEnd));
            bill.setReceivableFee(bill.getTotleFee());
            bill.setBillType(Constants.ZERO);
            bill.setCompanyId(model.getCompanyId());
            list.add(bill);
        }
        return list;
@@ -964,7 +1046,7 @@
            if(!(model.getZlFreeEndDate()==null &&  model.getZlFreeStartDate()==null) && (
                        (model.getZlFreeEndDate()!=null &&  model.getZlFreeStartDate()==null)
                        ||(model.getZlFreeEndDate()==null &&  model.getZlFreeStartDate()!=null)
                        ||model.getZlFreeEndDate().getTime()<= model.getZlFreeStartDate().getTime())
                        ||model.getZlFreeEndDate().getTime()< model.getZlFreeStartDate().getTime())
                  ){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请选择正确的租赁条款免租期信息!");
            }
@@ -993,7 +1075,7 @@
                        Constants.equalsInteger(d.getCircleType(),Constants.SIX)){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,租赁条款只有选择一次性付款时,才能选择该按每场收费;");
                }
                if(model.getZlFirstCircle() == null){//首期信息
                if(model.getZlFirstPrice() == null){//首期信息
                    model.setZlFirstPrice(d.getPrice());
                    model.setZlFirstCircle(d.getCircleType());
                }
@@ -1042,7 +1124,7 @@
                        Constants.equalsInteger(d.getCircleType(),Constants.SIX)){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,物业条款只有选择一次性付款时,才能选择该按每场收费;");
                }
                if(model.getWyFirstCircle() == null){
                if(model.getWyFirstPrice() == null){
                    //首期信息
                    model.setWyFirstPrice(d.getPrice());
                    model.setWyFirstCircle(d.getCircleType());
@@ -1206,9 +1288,16 @@
            //查询账单集合
            model.setBillList(ywContractBillMapper.selectJoinList(YwContractBill.class,new MPJLambdaWrapper<YwContractBill>()
                            .selectAll(YwContractBill.class )
                            //.select(" ( select ifnull(sum(case when yw.REVENUE_TYPE = 0 then yw.ACT_RECEIVABLE_FEE  else  -yw.ACT_RECEIVABLE_FEE end),0) from  yw_contract_revenue yw where yw.bill_id = t.id and yw.status = 0 and yw.isdeleted = 0 ) as  actReceivableFee  ")
                            .select(" ( select ifnull( sum( CASE WHEN t.bill_type = 0 and yw.REVENUE_TYPE = 0 THEN yw.ACT_RECEIVABLE_FEE when  t.bill_type = 0 and yw.REVENUE_TYPE = 1 then -yw.ACT_RECEIVABLE_FEE  when t.bill_type = 1 and yw.REVENUE_TYPE = 0 then -yw.ACT_RECEIVABLE_FEE else  yw.ACT_RECEIVABLE_FEE END),0) from  yw_contract_revenue yw where yw.bill_id = t.id and yw.status = 0 and yw.isdeleted = 0 ) as  actReceivableFee  ")
                            .eq(  YwContractBill::getContractId,model.getId())
                            .eq(YwContractBill::getIsdeleted,Constants.ZERO)
                            .orderByAsc(YwContractBill::getSortnum,YwContractBill::getCreateDate)));
            for (YwContractBill ywContractBill:model.getBillList()) {
                ywContractBill.setNeedReceivableFee(ywContractBill.getReceivableFee().subtract(ywContractBill.getActReceivableFee()));
            }
            model.setCanBackRentBills(new ArrayList<>());
            long nowStart = Utils.Date.getStart(new Date()).getTime();
            long nowEnd = Utils.Date.getEnd(new Date()).getTime();
@@ -1226,7 +1315,7 @@
                    }else  if(Constants.equalsInteger(bill.getPayStatus(),Constants.ONE) ){
                        if(bill.getEndDate().getTime()<nowStart){
                            //如果已结清,账单直接关闭
                          continue;
                            continue;
                        }
                        model.getCanBackRentBills().add(bill);
                    }else  if(Constants.equalsInteger(bill.getPayStatus(),Constants.TWO) ){
@@ -1248,10 +1337,18 @@
    @Override
    public List<YwContract> findList(YwContract ywContract) {
        QueryWrapper<YwContract> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YwContract::getIsdeleted,Constants.ZERO);
        wrapper.lambda().in(YwContract::getStatus,Constants.ZERO,Constants.ONE,Constants.TWO);
        return ywContractMapper.selectList(wrapper);
        return ywContractMapper.selectJoinList(YwContract.class,new MPJLambdaWrapper<YwContract>().selectAll(YwContract.class )
                .selectAs(Company::getName,YwContract::getCompanyName )
                .selectAs(SystemUser::getRealname,YwContract::getUserName )
                .selectAs(YwCustomer::getName,YwContract::getRenterName )
                .selectAs(YwProject::getName,YwContract::getProjectName )
                .leftJoin(Company.class,Company::getId,YwContract::getCompanyId)
                .leftJoin(SystemUser.class,SystemUser::getId,YwContract::getUserId)
                .leftJoin(SystemUser.class,SystemUser::getId,YwContract::getCreator)
                .leftJoin(YwProject.class,YwProject::getId,YwContract::getProjectId)
                .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId)
                .eq(YwContract::getIsdeleted,Constants.ZERO)
                .in(YwContract::getStatus,Constants.ZERO,Constants.ONE,Constants.TWO));
    }
  
    @Override