|
@@ -0,0 +1,204 @@
|
|
|
+/**
|
|
|
+ * Copyright (c) 2018 业主系统 All rights reserved.
|
|
|
+ * <p>
|
|
|
+ * https://www.yezhu.io
|
|
|
+ * <p>
|
|
|
+ * 版权所有,侵权必究!
|
|
|
+ */
|
|
|
+
|
|
|
+package com.kioor.room.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.kioor.common.constant.Constant;
|
|
|
+import com.kioor.common.dto.SysRegionDTO;
|
|
|
+import com.kioor.common.page.PageData;
|
|
|
+import com.kioor.common.service.SysRegionService;
|
|
|
+import com.kioor.common.service.impl.BaseServiceImpl;
|
|
|
+import com.kioor.common.utils.ConvertUtils;
|
|
|
+import com.kioor.room.dao.HousingEstateDao;
|
|
|
+import com.kioor.room.dto.HousingEstateDTO;
|
|
|
+import com.kioor.room.dto.HousingEstateInitDTO;
|
|
|
+import com.kioor.room.dto.UnitInfoDTO;
|
|
|
+import com.kioor.room.entity.*;
|
|
|
+import com.kioor.room.service.*;
|
|
|
+import com.kioor.user.service.UserService;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class HousingEstateServiceImpl extends BaseServiceImpl<HousingEstateDao, HousingEstateEntity> implements HousingEstateService {
|
|
|
+
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
+ private final SysRegionService sysRegionService;
|
|
|
+
|
|
|
+ private final BuildingService buildingService;
|
|
|
+
|
|
|
+ private final UnitService unitService;
|
|
|
+
|
|
|
+ private final FloorService floorService;
|
|
|
+
|
|
|
+ private final RoomService roomService;
|
|
|
+
|
|
|
+ private static final int BATCH_SIZE = 100; // 批量处理大小,根据实际情况调整
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public PageData<HousingEstateDTO> page(Map<String, Object> params) {
|
|
|
+ paramsToLike(params, "name");
|
|
|
+
|
|
|
+ //分页
|
|
|
+ IPage<HousingEstateEntity> page = getPage(params, Constant.CREATE_DATE, false);
|
|
|
+
|
|
|
+ //查询
|
|
|
+ List<HousingEstateEntity> list = baseDao.getList(params);
|
|
|
+
|
|
|
+ List<HousingEstateDTO> dtoList = new ArrayList<>();
|
|
|
+ for (HousingEstateEntity entity : list) {
|
|
|
+ HousingEstateDTO dto = ConvertUtils.sourceToTarget(entity, HousingEstateDTO.class);
|
|
|
+ //省市区
|
|
|
+ SysRegionDTO sysRegionDTO = sysRegionService.get(dto.getRegionId());
|
|
|
+ if(sysRegionDTO!=null){
|
|
|
+ //区
|
|
|
+ dto.setRegionStr(sysRegionDTO.getName());
|
|
|
+ //市
|
|
|
+ if(sysRegionDTO.getPid()!=null){
|
|
|
+ dto.setCityId(sysRegionDTO.getPid());
|
|
|
+ dto.setCityStr(sysRegionDTO.getParentName());
|
|
|
+ SysRegionDTO parentSysRegionDTO = sysRegionService.get(sysRegionDTO.getPid());
|
|
|
+ if(parentSysRegionDTO!=null){
|
|
|
+ //省
|
|
|
+ dto.setProvinceId(parentSysRegionDTO.getPid());
|
|
|
+ dto.setProvinceStr(parentSysRegionDTO.getParentName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //创建人名字
|
|
|
+ dto.setCreateUserName(userService.getUserByUserId(dto.getCreateUser()).getUsername());
|
|
|
+ dtoList.add(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ return getPageData(dtoList, page.getTotal(), HousingEstateDTO.class);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public HousingEstateDTO get(Long id) {
|
|
|
+ HousingEstateEntity entity = baseDao.selectById(id);
|
|
|
+
|
|
|
+ HousingEstateDTO dto = ConvertUtils.sourceToTarget(entity, HousingEstateDTO.class);
|
|
|
+ //省市区
|
|
|
+ SysRegionDTO sysRegionDTO = sysRegionService.get(dto.getRegionId());
|
|
|
+ if(sysRegionDTO!=null){
|
|
|
+ //区
|
|
|
+ dto.setRegionStr(sysRegionDTO.getName());
|
|
|
+ //市
|
|
|
+ if(sysRegionDTO.getPid()!=null){
|
|
|
+ dto.setCityId(sysRegionDTO.getPid());
|
|
|
+ dto.setCityStr(sysRegionDTO.getParentName());
|
|
|
+ SysRegionDTO parentSysRegionDTO = sysRegionService.get(sysRegionDTO.getPid());
|
|
|
+ if(parentSysRegionDTO!=null){
|
|
|
+ //省
|
|
|
+ dto.setProvinceId(parentSysRegionDTO.getPid());
|
|
|
+ dto.setProvinceStr(parentSysRegionDTO.getParentName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //创建人名字
|
|
|
+ dto.setCreateUserName(userService.getUserByUserId(dto.getCreateUser()).getUsername());
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void save(HousingEstateDTO dto) {
|
|
|
+ HousingEstateEntity entity = ConvertUtils.sourceToTarget(dto, HousingEstateEntity.class);
|
|
|
+ insert(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void update(HousingEstateDTO dto) {
|
|
|
+ HousingEstateEntity entity = ConvertUtils.sourceToTarget(dto, HousingEstateEntity.class);
|
|
|
+
|
|
|
+ updateById(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化小区信息
|
|
|
+ * @param housingEstateInitDTOList
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void housingEstateInit(List<HousingEstateInitDTO> housingEstateInitDTOList) {
|
|
|
+ Map<Long, BuildingEntity> buildingsMap = new HashMap<>();
|
|
|
+ Map<Long, UnitEntity> unitsMap = new HashMap<>();
|
|
|
+ List<FloorEntity> floorsList = new ArrayList<>();
|
|
|
+ List<RoomEntity> roomsList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (HousingEstateInitDTO estateInit : housingEstateInitDTOList) {
|
|
|
+ Long housingEstateId = estateInit.getHousingEstateId();
|
|
|
+ String buildingNum = estateInit.getBuildingNum();
|
|
|
+ Long buildingId = IdWorker.getId();
|
|
|
+ BuildingEntity buildingEntity = buildingsMap.computeIfAbsent(buildingId,
|
|
|
+ k -> new BuildingEntity(buildingId,housingEstateId, buildingNum));
|
|
|
+
|
|
|
+ for (UnitInfoDTO unitInfo : estateInit.getUnitInfo()) {
|
|
|
+ String unitNum = unitInfo.getUnitNum();
|
|
|
+ int floorCount = unitInfo.getFloorCount();
|
|
|
+ int apartmentPerFloor = unitInfo.getApartmentPerFloor();
|
|
|
+ Long unitId = IdWorker.getId();
|
|
|
+ UnitEntity unitEntity = unitsMap.computeIfAbsent(unitId,
|
|
|
+ k -> new UnitEntity(IdWorker.getId(),housingEstateId, buildingEntity.getId(), unitNum));
|
|
|
+
|
|
|
+ for (int floor = 1; floor <= floorCount; floor++) {
|
|
|
+ FloorEntity floorEntity = new FloorEntity(IdWorker.getId(),housingEstateId, unitEntity.getId(), floor);
|
|
|
+ floorsList.add(floorEntity);
|
|
|
+
|
|
|
+ for (int apartment = 1; apartment <= apartmentPerFloor; apartment++) {
|
|
|
+ RoomEntity roomEntity = new RoomEntity(IdWorker.getId(),housingEstateId, floorEntity.getId(), apartment, buildingNum + "-" + unitNum + "-" + floor + String.format("%02d", apartment));
|
|
|
+ roomsList.add(roomEntity);
|
|
|
+
|
|
|
+ // 检查是否需要批量保存房间
|
|
|
+ if (roomsList.size() >= BATCH_SIZE) {
|
|
|
+ roomService.insertBatch(roomsList);
|
|
|
+ roomsList.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量保存剩余的房间
|
|
|
+ if (!roomsList.isEmpty()) {
|
|
|
+ roomService.insertBatch(roomsList);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量保存楼栋、单元和楼层(如果有必要的话,应先检查是否有新增数据)
|
|
|
+ for (BuildingEntity building : buildingsMap.values()) {
|
|
|
+ buildingService.insertBatch(Collections.singletonList(building));
|
|
|
+ }
|
|
|
+ for (UnitEntity unit : unitsMap.values()) {
|
|
|
+ unitService.insertBatch(Collections.singletonList(unit));
|
|
|
+ }
|
|
|
+ if (!floorsList.isEmpty()) {
|
|
|
+ floorService.insertBatch(floorsList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清空小区房号
|
|
|
+ *
|
|
|
+ * @param housingEstateId
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void cleanAll(Long housingEstateId) {
|
|
|
+ roomService.delete(new LambdaQueryWrapper<RoomEntity>().eq(RoomEntity::getHousingEstateId, housingEstateId));
|
|
|
+ floorService.delete(new LambdaQueryWrapper<FloorEntity>().eq(FloorEntity::getHousingEstateId, housingEstateId));
|
|
|
+ unitService.delete(new LambdaQueryWrapper<UnitEntity>().eq(UnitEntity::getHousingEstateId, housingEstateId));
|
|
|
+ buildingService.delete(new LambdaQueryWrapper<BuildingEntity>().eq(BuildingEntity::getHousingEstateId, housingEstateId));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|