jiangping
2025-06-06 a2299a6d4a6f99e9c11132138f5d3e9ec68f03ea
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwRoomServiceImpl.java
@@ -6,25 +6,27 @@
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.YwBuildingMapper;
import com.doumee.dao.business.YwRoomMapper;
import com.doumee.dao.business.model.YwBuilding;
import com.doumee.dao.business.model.YwFloor;
import com.doumee.dao.business.model.YwProject;
import com.doumee.dao.business.model.YwRoom;
import com.doumee.dao.business.*;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.vo.YwFloorStatusDataVO;
import com.doumee.dao.business.vo.YwRoomContractDataVO;
import com.doumee.dao.business.vo.YwRoomStatusDataVO;
import com.doumee.service.business.YwRoomService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 运维房源信息表Service实现
@@ -38,6 +40,16 @@
    private YwRoomMapper ywRoomMapper;
    @Autowired
    private YwBuildingMapper ywBuildingMapper;
    @Autowired
    private YwContractRoomMapper ywContractRoomMapper;
    @Autowired
    private YwContractMapper ywContractMapper;
    @Autowired
    private YwWorkorderMapper ywWorkorderMapper;
    @Autowired
    private YwFloorMapper ywFloorMapper;
    @Override
    public Integer create(YwRoom model) {
@@ -55,12 +67,23 @@
        model.setStatus(Constants.ZERO);
        model.setEditDate(model.getCreateDate());
        model.setEditor(model.getCreator());
        model.setLeaseNowStatus(Constants.ZERO);
        ywRoomMapper.insert(model);
        return model.getId();
    }
    @Override
    public void deleteById(Integer id, LoginUserInfo user) {
        if(ywContractRoomMapper.selectCount(new QueryWrapper<YwContractRoom>()
                .lambda().eq(YwContractRoom::getIsdeleted,Constants.ZERO)
                .eq(YwContractRoom::getRoomId,id))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"房间信息已使用,无法进行删除!");
        };
        if(ywWorkorderMapper.selectCount(new QueryWrapper<YwWorkorder>()
                .lambda().eq(YwWorkorder::getIsdeleted,Constants.ZERO)
                .eq(YwWorkorder::getRoomId,id))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"房间信息已使用,无法进行删除!");
        };
        YwRoom model = new YwRoom();
        model.setId(id);
        model.setEditDate(new Date());
@@ -112,7 +135,23 @@
    @Override
    public YwRoom findById(Integer id) {
        return ywRoomMapper.selectById(id);
        MPJLambdaWrapper<YwRoom> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(YwRoom.class )
                .selectAs(YwProject::getName,YwRoom::getProjectName)
                .selectAs(YwFloor::getName,YwRoom::getFloorName)
//                .select(" ifnull( ( select case when y1.status = 3 then now() BETWEEN y1.START_DATE and y1.BT_DATE else now() BETWEEN y1.START_DATE and y1.END_DATE END  " +
//                        "from yw_contract y1 left join yw_contract_room y2 on y1.id = y2.CONTRACT_ID and y2.TYPE = 0  " +
//                        "where y1.`STATUS` <> 4  and y2.room_id = t.id order by y1.create_date desc  limit 1  ) ,0) ",YwRoom::getLeaseStatus)
                .select(" ifnull( (  case when t.IS_INVESTMENT = 0 then 2 else t.LEASE_NOW_STATUS end  ) ,0) ",YwRoom::getLeaseStatus)
                .selectAs(YwBuilding::getName,YwRoom::getBuildingName)
                .leftJoin(YwProject.class,YwProject::getId,YwRoom::getProjectId)
                .leftJoin(YwBuilding.class,YwBuilding::getId,YwRoom::getBuildingId)
                .leftJoin(YwFloor.class,YwFloor::getId,YwRoom::getFloor)
                .eq(YwRoom::getId,id)
                .last(" limit 1 ")
        ;
        YwRoom ywRoom = ywRoomMapper.selectJoinOne(YwRoom.class,queryWrapper);
        return ywRoom;
    }
    @Override
@@ -136,6 +175,10 @@
                .selectAs(YwProject::getName,YwRoom::getProjectName)
                .selectAs(YwFloor::getName,YwRoom::getFloorName)
                .selectAs(YwBuilding::getName,YwRoom::getBuildingName)
//                .select(" ifnull( ( select case when y1.status = 3 then now() BETWEEN y1.START_DATE and y1.BT_DATE else now() BETWEEN y1.START_DATE and y1.END_DATE END  " +
//                        "from yw_contract y1 left join yw_contract_room y2 on y1.id = y2.CONTRACT_ID and y2.TYPE = 0  " +
//                        "where y1.`STATUS` <> 4  and y2.room_id = t.id order by y1.create_date desc  limit 1  ) ,0) ",YwRoom::getLeaseStatus)
                .select(" ifnull( (  case when t.IS_INVESTMENT = 0 then 2 else t.LEASE_NOW_STATUS end  ) ,0) ",YwRoom::getLeaseStatus)
                .leftJoin(YwProject.class,YwProject::getId,YwRoom::getProjectId)
                .leftJoin(YwBuilding.class,YwBuilding::getId,YwRoom::getBuildingId)
                .leftJoin(YwFloor.class,YwFloor::getId,YwRoom::getFloor);
@@ -204,7 +247,13 @@
            queryWrapper.eq(YwRoom::getBuildingId, pageWrap.getModel().getBuildingId());
        }
        queryWrapper.orderByAsc(YwRoom::getRoomNum);
        return PageData.from(ywRoomMapper.selectJoinPage(page,YwRoom.class, queryWrapper));
        IPage<YwRoom> iPage = ywRoomMapper.selectJoinPage(page,YwRoom.class, queryWrapper);
//        for (YwRoom ywRoom:iPage.getRecords()) {
//            if(Constants.equalsInteger(ywRoom.getIsInvestment(),Constants.ZERO)){
//                ywRoom.setLeaseStatus(Constants.TWO);
//            }
//        }
        return PageData.from(iPage);
    }
    @Override
@@ -212,4 +261,218 @@
        QueryWrapper<YwRoom> wrapper = new QueryWrapper<>(ywRoom);
        return ywRoomMapper.selectCount(wrapper);
    }
    @Override
    public List<YwFloorStatusDataVO>  getRoomStatusList(YwRoom model){
        if(Objects.isNull(model)
        || (Objects.isNull(model.getBuildingId()) && Objects.isNull(model.getFloor()))){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        List<YwFloorStatusDataVO> ywFloorStatusDataVOList = new ArrayList<>();
        List<YwFloor> ywFloorList = ywFloorMapper.selectList(new QueryWrapper<YwFloor>().lambda()
                .eq(YwFloor::getIsdeleted,Constants.ZERO)
                .eq(Objects.nonNull(model.getBuildingId()),YwFloor::getBuildingId,model.getBuildingId())
                .eq(Objects.nonNull(model.getFloor()),YwFloor::getId,model.getFloor())
                .orderByAsc(YwFloor::getFloor)
        );
        List<YwRoom> ywRoomList = ywRoomMapper.selectList(new QueryWrapper<YwRoom>().lambda()
                .eq(YwRoom::getIsdeleted,Constants.ZERO)
                .eq(YwRoom::getIsInvestment,Constants.ONE)
                .eq(YwRoom::getStatus,Constants.ZERO)
                .eq(Objects.nonNull(model.getBuildingId()),YwRoom::getBuildingId,model.getBuildingId())
                .eq(Objects.nonNull(model.getFloor()),YwRoom::getFloor,model.getFloor())
                .orderByAsc(YwRoom::getSortnum)
        );
        List<YwRoomStatusDataVO> roomStatusDataList = new ArrayList<>();
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywRoomList)){
            for (YwRoom ywRoom:ywRoomList) {
                YwRoomStatusDataVO roomStatusDataVO = new YwRoomStatusDataVO();
                roomStatusDataVO.setFloorId(ywRoom.getFloor());
                roomStatusDataVO.setRoomId(ywRoom.getId());
                roomStatusDataVO.setRoomCode(ywRoom.getRoomNum());
                roomStatusDataVO.setRoomRentArea(ywRoom.getRentArea());
                List<YwContract> roomContractList =   ywContractMapper.selectJoinList(YwContract.class,new MPJLambdaWrapper<YwContract>()
                        .selectAll(YwContract.class)
                        .selectAs(YwCustomer::getName,YwContract::getRenterName)
                        .leftJoin(YwContractRoom.class,YwContractRoom::getContractId,YwContract::getId)
                        .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId)
                        .eq(YwContract::getIsdeleted,Constants.ZERO)
                        .le(YwContract::getStartDate, DateUtil.getCurrDateTime())
                        .ge(YwContract::getEndDate, DateUtil.getCurrDateTime())
                        .eq(YwContractRoom::getType,Constants.ZERO)
                        .eq(YwContractRoom::getRoomId,ywRoom.getId())
                        .orderByDesc(YwContract::getStartDate)
                        .orderByDesc(YwContract::getId)
                );
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(roomContractList)){
                    roomStatusDataVO.setRoomStatus(Constants.ZERO);
                    //查询是否有已退租合同
                    List<YwContract> roomRentContractList =   ywContractMapper.selectJoinList(YwContract.class,new MPJLambdaWrapper<YwContract>()
                            .selectAll(YwContract.class)
                            .selectAs(YwCustomer::getName,YwContract::getRenterName)
                            .leftJoin(YwContractRoom.class,YwContractRoom::getContractId,YwContract::getId)
                            .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId)
                            .eq(YwContract::getIsdeleted,Constants.ZERO)
                            .eq(YwContract::getStatus,Constants.FOUR)
                            .eq(YwContractRoom::getType,Constants.ZERO)
                            .eq(YwContractRoom::getRoomId,ywRoom.getId())
                            .orderByDesc(YwContract::getStartDate)
                            .orderByDesc(YwContract::getId)
                    );
                    if(CollectionUtils.isEmpty(roomRentContractList)){
                        roomStatusDataVO.setFreeDayAmount(Constants.ZERO);
                    }else{
                        YwContract ywContract = roomRentContractList.get(Constants.ZERO);
                        //查询退租日期和当前日期相差天数
                        Integer btDays = DateUtil.daysBetweenDates(new Date(),ywContract.getBtDate());
                        if(btDays>=Constants.ZERO){
                            roomStatusDataVO.setFreeDayAmount(btDays);
                        }
                    }
                }else{
                    YwContract ywContract = roomContractList.get(Constants.ZERO);
                    //合同状态 0待执行 1执行中 2已到期 3退租中 4已退租
                    //待执行/执行中/退租中 均属于已出租 根据具体到期时间判断是否处于即将到期 90天
                    if(Constants.equalsInteger(ywContract.getStatus(),Constants.ZERO)||Constants.equalsInteger(ywContract.getStatus(),Constants.ONE)||
                            Constants.equalsInteger(ywContract.getStatus(),Constants.THREE)){
                        roomStatusDataVO.setRoomStatus(Constants.ONE);
                        roomStatusDataVO.setCustomerName(ywContract.getRenterName());
                        if(Constants.equalsInteger(ywContract.getStatus(),Constants.THREE)){
                            if(Objects.nonNull(ywContract.getBtDate()) && DateUtil.getBetweenDays(DateUtil.getFomartDate(ywContract.getBtDate(),"yyyy-MM-dd"),DateUtil.getFomartDate(new Date(),"yyyy-MM-dd"))<=90){
                                roomStatusDataVO.setRoomStatus(Constants.TWO);
                                roomStatusDataVO.setOverData(ywContract.getBtDate());
                            }
                        }else{
                            System.out.println(DateUtil.daysBetweenDates(ywContract.getEndDate(),new Date()));
                            if(Objects.nonNull(ywContract.getEndDate())
                                    &&
                                DateUtil.daysBetweenDates(ywContract.getEndDate(),new Date())<=90){
                                roomStatusDataVO.setRoomStatus(Constants.TWO);
                                roomStatusDataVO.setOverData(ywContract.getEndDate());
                            }else{
                                roomStatusDataVO.setOverData(ywContract.getEndDate());
                            }
                        }
                    }else{
                        roomStatusDataVO.setRoomStatus(Constants.ZERO);
                        //已退租 计算退租时间
                        if(Objects.nonNull(ywContract.getBtDate())){
                            //查询退租日期和当前日期相差天数
                            Integer btDays = DateUtil.daysBetweenDates(new Date(),ywContract.getBtDate());
                            if(btDays>=Constants.ZERO){
                                roomStatusDataVO.setFreeDayAmount(btDays);
                            }
                        }
                    }
                }
                roomStatusDataList.add(roomStatusDataVO);
            }
        }
        for (YwFloor ywFloor:ywFloorList) {
            YwFloorStatusDataVO ywFloorStatusDataVO = new YwFloorStatusDataVO();
            ywFloorStatusDataVO.setFloorName(ywFloor.getName());
            ywFloorStatusDataVO.setFloorId(ywFloor.getId());
            ywFloorStatusDataVO.setFloorArea(BigDecimal.ZERO);
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(roomStatusDataList)){
                List<YwRoomStatusDataVO> floorRoomList = roomStatusDataList.stream().filter(i->Objects.nonNull(i.getFloorId())&&Constants.equalsInteger(i.getFloorId(),ywFloor.getId())).collect(Collectors.toList());
                ywFloorStatusDataVO.setYwRoomStatusDataVOList(floorRoomList);
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(floorRoomList)){
                    ywFloorStatusDataVO.setFloorArea(floorRoomList.stream().filter(i->Objects.nonNull(i.getRoomRentArea())).map(i->i.getRoomRentArea()).reduce(BigDecimal.ZERO,BigDecimal::add));
                }
            }
            ywFloorStatusDataVOList.add(ywFloorStatusDataVO);
        }
        return ywFloorStatusDataVOList;
    }
    @Override
    public YwRoomContractDataVO getRoomContract(YwRoom model){
        if(Objects.isNull(model)
                || (Objects.isNull(model.getBuildingId()) && Objects.isNull(model.getProjectId()))){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        YwRoomContractDataVO ywRoomContractDataVO = new YwRoomContractDataVO();
        ywRoomContractDataVO.setInvestmentRoomAmount(Constants.ZERO);
        ywRoomContractDataVO.setInvestmentArea(BigDecimal.ZERO);
        ywRoomContractDataVO.setLeaseContractAmount(Constants.ZERO);
        ywRoomContractDataVO.setLeasePrice(BigDecimal.ZERO);
        ywRoomContractDataVO.setLeaseArea(BigDecimal.ZERO);
        ywRoomContractDataVO.setLeaseRoomAmount(Constants.ZERO);
        ywRoomContractDataVO.setLeaseDayPrice(BigDecimal.ZERO);
        List<YwRoom> ywRoomList = ywRoomMapper.selectList(new QueryWrapper<YwRoom>().lambda()
                .eq(YwRoom::getIsdeleted,Constants.ZERO)
                .eq(YwRoom::getStatus,Constants.ZERO)
                .eq(YwRoom::getIsInvestment,Constants.ONE)
                .eq(Objects.nonNull(model.getBuildingId()),YwRoom::getBuildingId,model.getBuildingId())
                .eq(Objects.nonNull(model.getProjectId()),YwRoom::getProjectId,model.getProjectId())
                .orderByAsc(YwRoom::getSortnum)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(ywRoomList)){
            return ywRoomContractDataVO;
        }
        ywRoomContractDataVO.setInvestmentRoomAmount(ywRoomList.size());
        ywRoomContractDataVO.setInvestmentArea(ywRoomList.stream().filter(i->Objects.nonNull(i.getRentArea())).map(i->i.getRentArea()).reduce(BigDecimal.ZERO,BigDecimal::add));
        List<YwContract> roomContractList =   ywContractMapper.selectJoinList(YwContract.class,new MPJLambdaWrapper<YwContract>()
                .selectAll(YwContract.class)
                .select(" ( select  ifnull(sum(yr.RENT_AREA),0)  from yw_contract_room y left join yw_room yr on y.room_id = yr.id  where y.type = 0 and y.CONTRACT_ID = t.id ) " , YwContract::getTotalArea)
                .select(" ( select  ifnull(sum(y.TOTLE_FEE),0)  from yw_contract_bill y   where y.CONTRACT_ID = t.id and y.type = 0 and y.COST_TYPE=0 and y.BILL_TYPE = 0  ) " , YwContract::getTotalFee)
                .eq(YwContract::getIsdeleted,Constants.ZERO)
                .in(YwContract::getStatus,Constants.ZERO,Constants.ONE,Constants.TWO,Constants.THREE)
                .apply(Objects.nonNull(model.getBuildingId()),
                        " t.id in ( select y.CONTRACT_ID from yw_contract_room y left join yw_room yr on y.room_id = yr.id  where y.type = 0 and  yr.BUILDING_ID = "+model.getBuildingId()+" ) ")
                .apply(Objects.nonNull(model.getProjectId()),
                        " t.id in ( select y.CONTRACT_ID from yw_contract_room y left join yw_room yr on y.room_id = yr.id  where y.type = 0 and  yr.PROJECT_ID = "+model.getProjectId()+" ) ")
                .orderByDesc(YwContract::getStartDate)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(roomContractList)){
            return ywRoomContractDataVO;
        }
        //处理租赁单价 根据  合同租赁总额 /  (租赁时长 * 租赁面积)
        for (YwContract ywContract:roomContractList) {
            long contractDayAmount = DateUtil.daysBetweenDates(ywContract.getEndDate()
                    ,ywContract.getStartDate());
            if(Objects.nonNull(contractDayAmount) && Objects.nonNull(ywContract.getTotalArea()) && Objects.nonNull(ywContract.getTotalFee())){
                ywRoomContractDataVO.setLeaseDayPrice(
                        ywRoomContractDataVO.getLeaseDayPrice().add(
                            ywContract.getTotalFee().divide((ywContract.getTotalArea().multiply(new BigDecimal(Long.toString(contractDayAmount)))),2,BigDecimal.ROUND_HALF_UP)
                        )
                );
            }
        }
        ywRoomContractDataVO.setLeaseContractAmount(roomContractList.size());
        if(Objects.nonNull(ywRoomContractDataVO.getLeaseDayPrice())&&ywRoomContractDataVO.getLeaseDayPrice().compareTo(BigDecimal.ZERO)>Constants.ZERO){
            ywRoomContractDataVO.setLeasePrice(
                    ywRoomContractDataVO.getLeaseDayPrice().divide(new BigDecimal(Integer.toString(ywRoomContractDataVO.getLeaseContractAmount())),2,BigDecimal.ROUND_HALF_UP));
        }
        List<Integer> contractId = roomContractList.stream().map(i->i.getId()).collect(Collectors.toList());
        List<YwRoom> ywLeaseRoomList = ywRoomMapper.selectJoinList(YwRoom.class,new MPJLambdaWrapper<YwRoom>()
                .selectAll(YwRoom.class)
                .leftJoin(YwContractRoom.class,YwContractRoom::getRoomId,YwRoom::getId)
                .eq(YwRoom::getIsdeleted,Constants.ZERO)
                .eq(YwRoom::getIsInvestment,Constants.ONE)
                .eq(YwRoom::getStatus,Constants.ZERO)
                .eq(Objects.nonNull(model.getBuildingId()),YwRoom::getBuildingId,model.getBuildingId())
                .eq(Objects.nonNull(model.getProjectId()),YwRoom::getProjectId,model.getProjectId())
                .in(YwContractRoom::getContractId,contractId)
                .last(" group by t.id  " +
                        "ORDER BY t.sortnum ASC  ")
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywLeaseRoomList)){
            ywRoomContractDataVO.setLeaseArea(
                    ywLeaseRoomList.stream().filter(i->Objects.nonNull(i.getRentArea())).map(i->i.getRentArea()).reduce(BigDecimal.ZERO,BigDecimal::add)
            );
            ywRoomContractDataVO.setLeaseRoomAmount(ywLeaseRoomList.size());
        }
        return ywRoomContractDataVO;
    }
}