|  |  | 
 |  |  | package com.doumee.service.business.impl; | 
 |  |  |  | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.model.LoginUserInfo; | 
 |  |  | 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.YwFloorMapper; | 
 |  |  | import com.doumee.dao.business.YwProjectMapper; | 
 |  |  | import com.doumee.dao.business.YwRoomMapper; | 
 |  |  | import com.doumee.dao.business.dto.DataDTO; | 
 |  |  | import com.doumee.dao.business.model.*; | 
 |  |  | import com.doumee.dao.business.vo.CompanyTree; | 
 |  |  | import com.doumee.dao.business.vo.ProjectDataVO; | 
 |  |  | import com.doumee.dao.business.vo.ProjectTree; | 
 |  |  | import com.doumee.service.business.AreasService; | 
 |  |  | import com.doumee.service.business.YwProjectService; | 
 |  |  | 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.apache.commons.lang3.StringUtils; | 
 |  |  | import org.checkerframework.checker.units.qual.A; | 
 |  |  | import org.springframework.beans.BeanUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | 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; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 运维项目信息表Service实现 | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private YwProjectMapper ywProjectMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private AreasService areasService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private YwFloorMapper ywFloorMapper; | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Integer create(YwProject  model) { | 
 |  |  |         if(Objects.isNull(model) | 
 |  |  |         || StringUtils.isBlank(model.getName())){ | 
 |  |  |             throw new BusinessException(ResponseStatus.BAD_REQUEST); | 
 |  |  |         } | 
 |  |  |         if(ywProjectMapper.selectCount(new QueryWrapper<YwProject>().lambda() | 
 |  |  |                 .eq(YwProject::getName,model.getName()) | 
 |  |  |                 .eq(YwProject::getIsdeleted,Constants.ZERO))>Constants.ZERO){ | 
 |  |  |             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+model.getName()+"】已存在!"); | 
 |  |  |         } | 
 |  |  |         model.setCreator(model.getLoginUserInfo().getId()); | 
 |  |  |         model.setIsdeleted(Constants.ZERO); | 
 |  |  |         model.setCreateDate(new Date()); | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void deleteById(Integer id, LoginUserInfo user) { | 
 |  |  |         //查询项目下的数据 | 
 |  |  |         if(ywBuildingMapper.selectCount(new QueryWrapper<YwBuilding>().lambda().eq(YwBuilding::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .eq(YwBuilding::getProjectId,id))>Constants.ZERO){ | 
 |  |  |             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"项目下存在楼宇数据!"); | 
 |  |  |         }; | 
 |  |  |         YwProject model = new YwProject(); | 
 |  |  |         model.setId(id); | 
 |  |  |         model.setEditDate(new Date()); | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void updateById(YwProject model) { | 
 |  |  |         if(Objects.isNull(model) | 
 |  |  |                 || Objects.isNull(model.getId()) | 
 |  |  |                 || StringUtils.isBlank(model.getName())){ | 
 |  |  |             throw new BusinessException(ResponseStatus.BAD_REQUEST); | 
 |  |  |         } | 
 |  |  |         if(ywProjectMapper.selectCount(new QueryWrapper<YwProject>().lambda() | 
 |  |  |                 .eq(YwProject::getName,model.getName()) | 
 |  |  |                 .ne(YwProject::getId,model.getId()) | 
 |  |  |                 .eq(YwProject::getIsdeleted,Constants.ZERO))>Constants.ZERO){ | 
 |  |  |             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+model.getName()+"】已存在!"); | 
 |  |  |         } | 
 |  |  |         model.setEditDate(new Date()); | 
 |  |  |         model.setEditor(model.getLoginUserInfo().getId()); | 
 |  |  |         ywProjectMapper.updateById(model); | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public YwProject findById(Integer id) { | 
 |  |  |         return ywProjectMapper.selectById(id); | 
 |  |  |         YwProject ywProject = ywProjectMapper.selectById(id); | 
 |  |  |         if(Objects.isNull(ywProject)){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
 |  |  |         } | 
 |  |  |         if(Objects.nonNull(ywProject.getAreaId())){ | 
 |  |  |             Areas a = areasService.findById(ywProject.getAreaId(),Constants.TWO); | 
 |  |  |             if(a != null){ | 
 |  |  |                 ywProject.setAreaName(a.getName()); | 
 |  |  |                 ywProject.setCityId(a.getCityId()); | 
 |  |  |                 ywProject.setCityName(a.getCityName()); | 
 |  |  |                 ywProject.setProvinceId(a.getProvinceId()); | 
 |  |  |                 ywProject.setProvinceName(a.getProvinceName()); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return ywProject; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |         Utils.MP.blankToNull(pageWrap.getModel()); | 
 |  |  |         pageWrap.getModel().setIsdeleted(Constants.ZERO); | 
 |  |  |         queryWrapper.select("*,(select count(1) from yw_room a where a.isdeleted=0 and a.PROJECT_ID=yw_project.id) as roomNum"+ | 
 |  |  |                 ",(select count(1) from yw_room a where a.isdeleted=0 and a.PROJECT_ID=yw_project.id and a.IS_INVESTMENT=1) as roomRentNum"); | 
 |  |  |                 ",(select count(1) from yw_room a where a.isdeleted=0 and a.PROJECT_ID=yw_project.id and a.IS_INVESTMENT=1) as roomRentNum," + | 
 |  |  |                 "( select ifnull(sum(a.RENT_AREA),0) from  yw_room a where a.isdeleted=0 and a.PROJECT_ID=yw_project.id ) as area"); | 
 |  |  |         if (pageWrap.getModel().getId() != null) { | 
 |  |  |             queryWrapper.lambda().eq(YwProject::getId, pageWrap.getModel().getId()); | 
 |  |  |         } | 
 |  |  | 
 |  |  |             queryWrapper.lambda().eq(YwProject::getAddr, pageWrap.getModel().getAddr()); | 
 |  |  |         } | 
 |  |  |         queryWrapper.lambda().orderByDesc(YwProject::getCreateDate); | 
 |  |  |         return PageData.from(ywProjectMapper.selectPage(page, queryWrapper)); | 
 |  |  |         PageData<YwProject> data = PageData.from(ywProjectMapper.selectPage(page, queryWrapper)); | 
 |  |  |         if(data!=null && data.getRecords()!=null){ | 
 |  |  |             for(YwProject model :data.getRecords()){ | 
 |  |  |                     if(model.getAreaId()!=null){ | 
 |  |  |                         Areas a = areasService.findById(model.getAreaId(),Constants.TWO); | 
 |  |  |                         if(a != null){ | 
 |  |  |                             model.setAreaName(a.getName()); | 
 |  |  |                             model.setCityId(a.getCityId()); | 
 |  |  |                             model.setCityName(a.getCityName()); | 
 |  |  |                             model.setProvinceId(a.getProvinceId()); | 
 |  |  |                             model.setProvinceName(a.getProvinceName()); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return data; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<ProjectDataVO> projectTree(){ | 
 |  |  |     public List<ProjectDataVO> projectTree(DataDTO dataDTO){ | 
 |  |  |         List<ProjectDataVO> projectDataVOList = new ArrayList<>(); | 
 |  |  |         List<YwProject>  ywProjectList = ywProjectMapper.selectList(new QueryWrapper<YwProject>().lambda() | 
 |  |  |                 .eq(YwProject::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .eq(YwProject::getStatus,Constants.ZERO) | 
 |  |  |                 .eq(Objects.nonNull(dataDTO.getProjectId()),YwProject::getId,dataDTO.getProjectId()) | 
 |  |  |                 .orderByAsc(YwProject::getSortnum) | 
 |  |  |         ); | 
 |  |  |         for (YwProject ywProject:ywProjectList) { | 
 |  |  | 
 |  |  |         List<YwBuilding>  ywBuildingList = ywBuildingMapper.selectList(new QueryWrapper<YwBuilding>().lambda() | 
 |  |  |                 .eq(YwBuilding::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .eq(YwBuilding::getStatus,Constants.ZERO) | 
 |  |  |                 .eq(Objects.nonNull(dataDTO.getProjectId()),YwBuilding::getProjectId,dataDTO.getProjectId()) | 
 |  |  |                 .orderByAsc(YwBuilding::getSortnum) | 
 |  |  |         ); | 
 |  |  |  | 
 |  |  | 
 |  |  |         List<YwFloor>  ywFloorList = ywFloorMapper.selectList(new QueryWrapper<YwFloor>().lambda() | 
 |  |  |                 .eq(YwFloor::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .eq(YwFloor::getStatus,Constants.ZERO) | 
 |  |  |                 .eq(Objects.nonNull(dataDTO.getProjectId()),YwFloor::getProjectId,dataDTO.getProjectId()) | 
 |  |  |                 .orderByAsc(YwFloor::getSortnum) | 
 |  |  |         ); | 
 |  |  |  | 
 |  |  | 
 |  |  |             projectDataVOList.add(projectDataVO); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         List<YwRoom>  ywRoomList = ywRoomMapper.selectList(new QueryWrapper<YwRoom>().lambda() | 
 |  |  |         List<YwRoom>  ywRoomList = ywRoomMapper.selectJoinList(YwRoom.class,new MPJLambdaWrapper<YwRoom>() | 
 |  |  |                 .selectAll(YwRoom.class) | 
 |  |  | //                        .select("  ( SELECT count(1) FROM  yw_contract y1 " + | 
 |  |  | //                                " left join yw_contract_room y2 on y1.id = y2.contract_id " + | 
 |  |  | //                                " where 1 = 1 and y2.type = 0  and y1.`STATUS` in( 0,1,2) " + | 
 |  |  | //                                " and y1.START_DATE <  now() and y1.END_DATE >  now() and y2.ROOM_ID = yw_room.id ) as investmentStatus ") | 
 |  |  |                 .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) | 
 |  |  |                 .eq(YwRoom::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .eq(YwRoom::getStatus,Constants.ZERO) | 
 |  |  |                 .orderByAsc(YwRoom::getSortnum) | 
 |  |  |                 .eq(YwRoom::getIsInvestment,Constants.ONE) | 
 |  |  |                 .eq(Objects.nonNull(dataDTO.getProjectId()),YwRoom::getProjectId,dataDTO.getProjectId()) | 
 |  |  |                 .apply(Objects.nonNull(dataDTO)&&Objects.nonNull(dataDTO.getStartDate())&&Objects.nonNull(dataDTO.getEndDate())," id not in  (" + | 
 |  |  |                         " SELECT y2.room_id FROM  yw_contract y1 left join yw_contract_room y2 on y1.id = y2.contract_id where 1 = 1 and y1.`STATUS` in( 0,1,2) " + | 
 |  |  |                         " and y1.START_DATE < '"+dataDTO.getEndDate()+" 00:00:00' and y1.END_DATE > '"+ dataDTO.getStartDate() +"  00:00:00' " + | 
 |  |  |                         "  ) ") | 
 |  |  |                 .apply(Objects.nonNull(dataDTO)&&Objects.nonNull(dataDTO.getStartDate())&&Objects.nonNull(dataDTO.getEndDate())," id not in  (" + | 
 |  |  |                         " SELECT y2.room_id FROM  yw_contract y1 left join yw_contract_room y2 on y1.id = y2.contract_id where 1 = 1 and y1.`STATUS` = 3 " + | 
 |  |  |                         " and y1.START_DATE < '"+dataDTO.getEndDate()+" 00:00:00' and y1.BT_DATE > '"+ dataDTO.getStartDate() +"  00:00:00' " + | 
 |  |  |                         "  ) ") | 
 |  |  |                 .orderByAsc(YwRoom::getRoomNum) | 
 |  |  |         ); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         for (YwRoom data:ywRoomList) { | 
 |  |  |             ProjectDataVO projectDataVO = new ProjectDataVO(); | 
 |  |  |             BeanUtils.copyProperties(data,projectDataVO); | 
 |  |  |             projectDataVO.setName(data.getCode()); | 
 |  |  |             projectDataVO.setName(data.getRoomNum()); | 
 |  |  |             projectDataVO.setArea(data.getRentArea().setScale(2, BigDecimal.ROUND_HALF_UP)); | 
 |  |  |             projectDataVO.setPId(data.getFloor()); | 
 |  |  |             projectDataVO.setLv(Constants.THREE); | 
 |  |  |             projectDataVO.setLeaseStatus(data.getLeaseStatus()); | 
 |  |  |             projectDataVOList.add(projectDataVO); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         ProjectTree treeBuild = new ProjectTree(projectDataVOList); | 
 |  |  |         projectDataVOList = treeBuild.buildTree(); | 
 |  |  |         return  projectDataVOList; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  |