|
@@ -0,0 +1,186 @@
|
|
|
+package com.hfln.portal.infrastructure.gateway.impl;
|
|
|
+
|
|
|
+import cn.hfln.framework.extension.BizException;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.hfln.portal.common.dto.data.tenant.TenantDto;
|
|
|
+import com.hfln.portal.common.dto.data.tenant.TenantInfoDto;
|
|
|
+import com.hfln.portal.common.request.tenant.BuildingAddReq;
|
|
|
+import com.hfln.portal.common.request.tenant.FloorAddReq;
|
|
|
+import com.hfln.portal.common.request.tenant.TenantAddReq;
|
|
|
+import com.hfln.portal.common.request.tenant.TenantQueryReq;
|
|
|
+import com.hfln.portal.common.vo.PageRecord;
|
|
|
+import com.hfln.portal.domain.customer.OprLogType;
|
|
|
+import com.hfln.portal.domain.customer.TinfoType;
|
|
|
+import com.hfln.portal.domain.customer.util.CopyUtils;
|
|
|
+import com.hfln.portal.domain.exception.ErrorEnum;
|
|
|
+import com.hfln.portal.domain.gateway.WebTenantGateway;
|
|
|
+import com.hfln.portal.infrastructure.po.TblTenant;
|
|
|
+import com.hfln.portal.infrastructure.po.TblTenantInfo;
|
|
|
+import com.hfln.portal.infrastructure.service.TblOprLogService;
|
|
|
+import com.hfln.portal.infrastructure.service.TblTenantInfoService;
|
|
|
+import com.hfln.portal.infrastructure.service.TblTenantService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class WebTenantGatewayImpl implements WebTenantGateway {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TblTenantService tblTenantService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TblTenantInfoService tblTenantInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TblOprLogService tblOprLogService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void addTenant(TenantAddReq req) {
|
|
|
+
|
|
|
+ if (req.getTenantId() == null) {
|
|
|
+
|
|
|
+ // 新增租户
|
|
|
+ List<TblTenant> tenantList = tblTenantService.queryByCodeOrName(req.getTenantCode(), req.getTenantName());
|
|
|
+ if (!CollectionUtils.isEmpty(tenantList)) {
|
|
|
+ throw new BizException(ErrorEnum.TENANT_IS_ALREADY_EXIST.getErrorCode(), ErrorEnum.TENANT_IS_ALREADY_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ TblTenant saveTenant = CopyUtils.copy(req, TblTenant.class);
|
|
|
+ tblTenantService.save(saveTenant);
|
|
|
+
|
|
|
+ tblOprLogService.saveLog(OprLogType.ADD_TENANT.getCode(), saveTenant);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ List<TblTenant> tenantList = tblTenantService.queryByCodeOrName(req.getTenantCode(), req.getTenantName());
|
|
|
+ if (CollectionUtils.isEmpty(tenantList) || tenantList.size() > 2 || !tenantList.get(0).getTenantId().equals(req.getTenantId())) {
|
|
|
+ throw new BizException(ErrorEnum.TENANT_IS_ALREADY_EXIST.getErrorCode(), ErrorEnum.TENANT_IS_ALREADY_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+ // 更新租户
|
|
|
+ TblTenant tenant = tblTenantService.getById(req.getTenantId());
|
|
|
+ BeanUtils.copyProperties(req, tenant);
|
|
|
+ tblTenantService.updateById(tenant);
|
|
|
+
|
|
|
+ tblOprLogService.saveLog(OprLogType.MOD_TENANT.getCode(), tenant);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageRecord<TenantDto> queryTenant(TenantQueryReq req) {
|
|
|
+
|
|
|
+ // 执行分页查询
|
|
|
+ Page<TblTenant> tenantList = tblTenantService.queryTenantList(req);
|
|
|
+ // 换为目标VO
|
|
|
+ List<TenantDto> targets = CopyUtils.copyList(tenantList.getRecords(), TenantDto.class);
|
|
|
+ if (!req.isEnabled() || CollectionUtils.isEmpty(targets)) {
|
|
|
+ return CopyUtils.copyPage(tenantList, targets);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果需要级联查询 查询三级信息 租户信息 --》 楼栋信息 --》 楼层信息
|
|
|
+ List<TblTenantInfo> list = tblTenantInfoService.queryByTenantIds(targets.stream().map(TenantDto::getTenantId).collect(Collectors.toList()));
|
|
|
+ Map<Long, Map<String, List<TblTenantInfo>>> map = list.stream().collect(Collectors.groupingBy(
|
|
|
+ info -> Optional.ofNullable(info.getTenantId()).orElse(-1L),
|
|
|
+ Collectors.groupingBy(
|
|
|
+ info -> Optional.ofNullable(info.getTinfoType()).orElse("DEFAULT"),
|
|
|
+ Collectors.toList()
|
|
|
+ )
|
|
|
+ ));
|
|
|
+
|
|
|
+ for (TenantDto target : targets) {
|
|
|
+ Map<String, List<TblTenantInfo>> tenantInfoMap = map.get(target.getTenantId());
|
|
|
+ if (!CollectionUtils.isEmpty(tenantInfoMap)) {
|
|
|
+
|
|
|
+ List<TblTenantInfo> buildings = tenantInfoMap.get(TinfoType.COMMUNITY_BUILDING.getCode());
|
|
|
+ List<TblTenantInfo> floors = tenantInfoMap.get(TinfoType.UNIT_FLOOR.getCode());
|
|
|
+ Map<Long, List<TblTenantInfo>> floorMap = new HashMap<>();
|
|
|
+ if (!CollectionUtils.isEmpty(floors)) {
|
|
|
+ floorMap = floors.stream().collect(Collectors.groupingBy(TblTenantInfo::getParentId));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(buildings)) {
|
|
|
+ List<TenantInfoDto> buildingList = new ArrayList<>();
|
|
|
+ for (TblTenantInfo building : buildings) {
|
|
|
+ TenantInfoDto copy = CopyUtils.copy(building, TenantInfoDto.class);
|
|
|
+ copy.setChildren(CopyUtils.copyList(floorMap.get(building.getTinfoId()), TenantInfoDto.class));
|
|
|
+ buildingList.add(copy);
|
|
|
+ }
|
|
|
+ target.setChildren(buildingList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return CopyUtils.copyPage(tenantList, targets);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void addBuilding(BuildingAddReq req) {
|
|
|
+
|
|
|
+ List<TblTenantInfo> saveList = new ArrayList<>();
|
|
|
+ req.getBuildingInfos().forEach(buildingInfo -> {
|
|
|
+ TblTenantInfo tblBuilding = new TblTenantInfo();
|
|
|
+ BeanUtils.copyProperties(buildingInfo, tblBuilding);
|
|
|
+
|
|
|
+ tblBuilding.setTenantId(req.getTenantId());
|
|
|
+ tblBuilding.setTinfoType(TinfoType.COMMUNITY_BUILDING.getCode());
|
|
|
+
|
|
|
+ saveList.add(tblBuilding);
|
|
|
+ });
|
|
|
+ tblTenantInfoService.saveBatch(saveList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void addFloor(FloorAddReq req) {
|
|
|
+
|
|
|
+ List<TblTenantInfo> saveList = new ArrayList<>();
|
|
|
+ req.getFloorInfos().forEach(buildingInfo -> {
|
|
|
+ TblTenantInfo floorInfo = new TblTenantInfo();
|
|
|
+ BeanUtils.copyProperties(buildingInfo, floorInfo);
|
|
|
+
|
|
|
+ floorInfo.setTenantId(req.getTenantId());
|
|
|
+ floorInfo.setTinfoType(TinfoType.UNIT_FLOOR.getCode());
|
|
|
+ floorInfo.setParentId(req.getParentId());
|
|
|
+ saveList.add(floorInfo);
|
|
|
+ });
|
|
|
+ tblTenantInfoService.saveBatch(saveList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void delInfo(Long tinfoId) {
|
|
|
+
|
|
|
+ TblTenantInfo byId = tblTenantInfoService.getById(tinfoId);
|
|
|
+ if (byId == null) {
|
|
|
+ throw new BizException(ErrorEnum.DATA_NOT_EXISTS.getErrorCode(), ErrorEnum.DATA_NOT_EXISTS.getErrorMessage());
|
|
|
+ }
|
|
|
+ tblOprLogService.saveLog(OprLogType.DEL_TENANT_INFO.getCode(), byId);
|
|
|
+
|
|
|
+ tblTenantInfoService.delById(tinfoId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TenantInfoDto queryInfo(Long tinfoId) {
|
|
|
+
|
|
|
+ TblTenantInfo byId = tblTenantInfoService.getById(tinfoId);
|
|
|
+ if (byId == null) {
|
|
|
+ throw new BizException(ErrorEnum.DATA_NOT_EXISTS.getErrorCode(), ErrorEnum.DATA_NOT_EXISTS.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TinfoType.UNIT_FLOOR.getCode().equals(byId.getTinfoType())) {
|
|
|
+ return CopyUtils.copy(byId, TenantInfoDto.class);
|
|
|
+ }
|
|
|
+ TenantInfoDto tenantInfoDto = CopyUtils.copy(byId, TenantInfoDto.class);
|
|
|
+ List<TblTenantInfo> children = tblTenantInfoService.queryByParentId(tinfoId);
|
|
|
+ tenantInfoDto.setChildren(CopyUtils.copyList(children, TenantInfoDto.class));
|
|
|
+ return tenantInfoDto;
|
|
|
+ }
|
|
|
+}
|