renkang
2025-01-09 cdf974d19bab00ac22fe2896eb34b920d58d74a6
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwRoomServiceImpl.java
@@ -6,12 +6,13 @@
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.YwContractRoomMapper;
import com.doumee.dao.business.YwRoomMapper;
import com.doumee.dao.business.YwWorkorderMapper;
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;
@@ -23,8 +24,12 @@
import org.springframework.stereotype.Service;
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.stream.Collectors;
/**
 * 运维房源信息表Service实现
@@ -41,7 +46,13 @@
    @Autowired
    private YwContractRoomMapper ywContractRoomMapper;
    @Autowired
    private YwContractMapper ywContractMapper;
    @Autowired
    private YwWorkorderMapper ywWorkorderMapper;
    @Autowired
    private YwFloorMapper ywFloorMapper;
    @Override
    public Integer create(YwRoom model) {
@@ -247,4 +258,172 @@
        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.getCode());
                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)
                        .ge(YwContract::getStartDate, DateUtil.getCurrDateTime())
                        .eq(YwContractRoom::getType,Constants.ZERO)
                        .eq(YwContractRoom::getRoomId,ywRoom.getId())
                        .ne(YwContract::getBillStatus,Constants.THREE)
                        .orderByDesc(YwContract::getStartDate)
                );
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(roomContractList)){
                    roomStatusDataVO.setRoomStatus(Constants.ZERO);
                    roomStatusDataVO.setFreeDayAmount(Constants.ZERO);
                }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{
                            if(Objects.nonNull(ywContract.getEndDate()) && DateUtil.getBetweenDays(DateUtil.getFomartDate(ywContract.getEndDate(),"yyyy-MM-dd"),DateUtil.getFomartDate(new Date(),"yyyy-MM-dd"))<=90){
                                roomStatusDataVO.setRoomStatus(Constants.TWO);
                                roomStatusDataVO.setOverData(ywContract.getEndDate());
                            }
                        }
                    }
                }
                roomStatusDataList.add(roomStatusDataVO);
            }
        }
        for (YwFloor ywFloor:ywFloorList) {
            YwFloorStatusDataVO ywFloorStatusDataVO = new YwFloorStatusDataVO();
            ywFloorStatusDataVO.setFloor(ywFloor.getFloor());
            ywFloorStatusDataVO.setFloorId(ywFloor.getId());
            ywFloorStatusDataVO.setFloorArea(ywFloor.getArea());
            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);
            }
            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);
        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  ) " , YwContract::getTotalFee)
                .eq(YwContract::getIsdeleted,Constants.ZERO)
                .in(YwContract::getStatus,Constants.ONE,Constants.TWO,Constants.THREE)
                .ne(YwContract::getBillStatus,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()+" ) ")
                .apply(Objects.nonNull(model.getProjectId())," ")
                .orderByDesc(YwContract::getStartDate)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(roomContractList)){
            return ywRoomContractDataVO;
        }
        //处理租赁单价 根据  合同租赁总额 /  (租赁时长 * 租赁面积)
        for (YwContract ywContract:roomContractList) {
            long contractDayAmount = DateUtil.getBetweenDays(DateUtil.getFomartDate(ywContract.getEndDate(),"yyyy-MM-dd"),DateUtil.getFomartDate(ywContract.getStartDate(),"yyyy-MM-dd"));
            if(Objects.nonNull(contractDayAmount) && Objects.nonNull(ywContract.getTotalArea()) && Objects.nonNull(ywContract.getTotalFee())){
                ywRoomContractDataVO.setLeasePrice(
                        ywRoomContractDataVO.getLeasePrice().add(
                            ywContract.getTotalFee().divide((ywContract.getTotalArea().multiply(new BigDecimal(Long.toString(contractDayAmount)))),2,BigDecimal.ROUND_HALF_UP)
                        )
                );
            }
        }
        ywRoomContractDataVO.setLeaseContractAmount(roomContractList.size());
        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)
                .orderByAsc(YwRoom::getSortnum)
        );
        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;
    }
}