|
@@ -0,0 +1,241 @@
|
|
|
|
|
+package com.usky.ems.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.usky.common.core.exception.BusinessException;
|
|
|
|
|
+import com.usky.common.security.utils.SecurityUtils;
|
|
|
|
|
+import com.usky.ems.domain.BaseSpace;
|
|
|
|
|
+import com.usky.ems.domain.EmsProject;
|
|
|
|
|
+import com.usky.ems.domain.EmsProjectDeviceSystem;
|
|
|
|
|
+import com.usky.ems.mapper.EmsProjectDeviceSystemMapper;
|
|
|
|
|
+import com.usky.ems.mapper.EmsProjectMapper;
|
|
|
|
|
+import com.usky.ems.service.BaseSpaceService;
|
|
|
|
|
+import com.usky.ems.service.EmsProjectService;
|
|
|
|
|
+import com.usky.ems.service.EmsSystemDictRegionService;
|
|
|
|
|
+import com.usky.ems.service.vo.EmsProjectResponse;
|
|
|
|
|
+import com.usky.ems.service.vo.EmsProjectSaveRequest;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 项目维护实现(逻辑参考原 ProjectServiceImpl:省市区校验、创建项目空间、设备系统关联、删除前校验叶子节点等)
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+public class EmsProjectServiceImpl implements EmsProjectService {
|
|
|
|
|
+
|
|
|
|
|
+ private static final int SPACE_TYPE_PROJECT = 1;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private EmsProjectMapper emsProjectMapper;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private EmsProjectDeviceSystemMapper emsProjectDeviceSystemMapper;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private BaseSpaceService baseSpaceService;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private EmsSystemDictRegionService emsSystemDictRegionService;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public EmsProjectResponse save(EmsProjectSaveRequest request) {
|
|
|
|
|
+ validateSaveRequest(request, false);
|
|
|
|
|
+ Integer currentTenantId = SecurityUtils.getTenantId();
|
|
|
|
|
+ Map<String, String> regionMap = emsSystemDictRegionService.checkAndBuildMap(
|
|
|
|
|
+ request.getProvinceCode(), request.getCityCode(), request.getDistrictCode());
|
|
|
|
|
+ Long spaceId = createProjectSpace(request.getName());
|
|
|
|
|
+ EmsProject po = new EmsProject();
|
|
|
|
|
+ copyRequestToEntity(request, po, regionMap);
|
|
|
|
|
+ po.setSpaceId(spaceId);
|
|
|
|
|
+ po.setTenantId(currentTenantId);
|
|
|
|
|
+ fillAuditOnCreate(po);
|
|
|
|
|
+ emsProjectMapper.insert(po);
|
|
|
|
|
+ replaceDeviceSystems(po.getId(), request.getDeviceSystemList());
|
|
|
|
|
+ return toResponse(po, request.getDeviceSystemList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public EmsProjectResponse update(EmsProjectSaveRequest request) {
|
|
|
|
|
+ if (request.getId() == null) {
|
|
|
|
|
+ throw new BusinessException("修改项目时 id 不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ EmsProject old = emsProjectMapper.selectById(request.getId());
|
|
|
|
|
+ if (old == null) {
|
|
|
|
|
+ throw new BusinessException("错误的项目id值");
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, String> regionMap = emsSystemDictRegionService.checkAndBuildMap(
|
|
|
|
|
+ request.getProvinceCode(), request.getCityCode(), request.getDistrictCode());
|
|
|
|
|
+ EmsProject po = new EmsProject();
|
|
|
|
|
+ copyRequestToEntity(request, po, regionMap);
|
|
|
|
|
+ po.setSpaceId(old.getSpaceId());
|
|
|
|
|
+ po.setTenantId(old.getTenantId());
|
|
|
|
|
+ fillAuditOnUpdate(po);
|
|
|
|
|
+ emsProjectMapper.updateById(po);
|
|
|
|
|
+ baseSpaceService.updateProjectSpaceName(old.getSpaceId(), po.getName());
|
|
|
|
|
+ replaceDeviceSystems(po.getId(), request.getDeviceSystemList());
|
|
|
|
|
+ return toResponse(po, request.getDeviceSystemList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void remove(Long id) {
|
|
|
|
|
+ EmsProject project = emsProjectMapper.selectById(id);
|
|
|
|
|
+ if (project == null) {
|
|
|
|
|
+ throw new BusinessException("错误的项目id值");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!baseSpaceService.isLeafSpace(project.getSpaceId())) {
|
|
|
|
|
+ throw new BusinessException("当前项目下有空间节点不允许删除");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (hasLoggedUserForProject(project.getId())) {
|
|
|
|
|
+ throw new BusinessException("当前项目有关联的用户使用;不允许删除");
|
|
|
|
|
+ }
|
|
|
|
|
+ emsProjectDeviceSystemMapper.delete(
|
|
|
|
|
+ new LambdaQueryWrapper<EmsProjectDeviceSystem>().eq(EmsProjectDeviceSystem::getProjectId, id));
|
|
|
|
|
+ emsProjectMapper.deleteById(id);
|
|
|
|
|
+ baseSpaceService.removeById(project.getSpaceId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 对应原 UserService.hasLoggedUser(projectId)。接入统一用户/权限模块后可在此查询用户与项目绑定关系。
|
|
|
|
|
+ */
|
|
|
|
|
+ private boolean hasLoggedUserForProject(Long projectId) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void validateSaveRequest(EmsProjectSaveRequest request, boolean update) {
|
|
|
|
|
+ if (!StringUtils.hasText(request.getName())) {
|
|
|
|
|
+ throw new BusinessException("项目名称不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.getTypeId() == null || !StringUtils.hasText(request.getTypeName())) {
|
|
|
|
|
+ throw new BusinessException("项目类型不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Long createProjectSpace(String projectName) {
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ String audit = SecurityUtils.getUsername();
|
|
|
|
|
+ BaseSpace space = new BaseSpace();
|
|
|
|
|
+ space.setName(projectName);
|
|
|
|
|
+ space.setDeep(1);
|
|
|
|
|
+ space.setRootId(0L);
|
|
|
|
|
+ space.setParentId(0L);
|
|
|
|
|
+ space.setType(SPACE_TYPE_PROJECT);
|
|
|
|
|
+ space.setPath("");
|
|
|
|
|
+ space.setTenantId(SecurityUtils.getTenantId());
|
|
|
|
|
+ space.setCreatedBy(audit);
|
|
|
|
|
+ space.setUpdatedBy(audit);
|
|
|
|
|
+ space.setCreateTime(now);
|
|
|
|
|
+ space.setUpdateTime(now);
|
|
|
|
|
+ baseSpaceService.save(space);
|
|
|
|
|
+ return space.getId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void copyRequestToEntity(EmsProjectSaveRequest request, EmsProject po, Map<String, String> regionMap) {
|
|
|
|
|
+ po.setId(request.getId());
|
|
|
|
|
+ po.setName(request.getName());
|
|
|
|
|
+ po.setPlatformName(request.getPlatformName());
|
|
|
|
|
+ po.setAbbreviation(request.getAbbreviation());
|
|
|
|
|
+ po.setArea(request.getArea());
|
|
|
|
|
+ po.setCommonArea(request.getCommonArea());
|
|
|
|
|
+ po.setAirConditionedArea(request.getAirConditionedArea());
|
|
|
|
|
+ po.setResidentPopulation(request.getResidentPopulation());
|
|
|
|
|
+ po.setProvinceCode(request.getProvinceCode());
|
|
|
|
|
+ po.setCityCode(request.getCityCode());
|
|
|
|
|
+ po.setDistrictCode(request.getDistrictCode());
|
|
|
|
|
+ po.setProvinceName(regionMap.get(request.getProvinceCode()));
|
|
|
|
|
+ po.setCityName(regionMap.get(request.getCityCode()));
|
|
|
|
|
+ po.setDistrictName(regionMap.get(request.getDistrictCode()));
|
|
|
|
|
+ po.setLocation(request.getLocation());
|
|
|
|
|
+ po.setAddress(request.getAddress());
|
|
|
|
|
+ po.setTypeId(request.getTypeId());
|
|
|
|
|
+ po.setTypeName(request.getTypeName());
|
|
|
|
|
+ po.setImage(request.getImage());
|
|
|
|
|
+ po.setIntroduction(request.getIntroduction());
|
|
|
|
|
+ po.setLogo(request.getLogo());
|
|
|
|
|
+ po.setLogoMin(request.getLogoMin());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void fillAuditOnCreate(EmsProject po) {
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ String uname = SecurityUtils.getUsername();
|
|
|
|
|
+ po.setCreatedBy(uname);
|
|
|
|
|
+ po.setUpdatedBy(uname);
|
|
|
|
|
+ po.setCreateTime(now);
|
|
|
|
|
+ po.setUpdateTime(now);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void fillAuditOnUpdate(EmsProject po) {
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ String uname = SecurityUtils.getUsername();
|
|
|
|
|
+ po.setUpdatedBy(uname);
|
|
|
|
|
+ po.setUpdateTime(now);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void replaceDeviceSystems(Long projectId, List<Integer> deviceSystemList) {
|
|
|
|
|
+ emsProjectDeviceSystemMapper.delete(
|
|
|
|
|
+ new LambdaQueryWrapper<EmsProjectDeviceSystem>().eq(EmsProjectDeviceSystem::getProjectId, projectId));
|
|
|
|
|
+ if (CollectionUtils.isEmpty(deviceSystemList)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (Integer code : deviceSystemList) {
|
|
|
|
|
+ if (code == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ EmsProjectDeviceSystem row = new EmsProjectDeviceSystem();
|
|
|
|
|
+ row.setProjectId(projectId);
|
|
|
|
|
+ row.setDeviceSystem(code);
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ String uname = SecurityUtils.getUsername();
|
|
|
|
|
+ row.setUpdatedBy(uname);
|
|
|
|
|
+ row.setUpdateTime(now);
|
|
|
|
|
+ row.setCreatedBy(uname);
|
|
|
|
|
+ row.setCreateTime(now);
|
|
|
|
|
+ emsProjectDeviceSystemMapper.insert(row);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private EmsProjectResponse toResponse(EmsProject po, List<Integer> deviceSystemList) {
|
|
|
|
|
+ EmsProjectResponse resp = new EmsProjectResponse();
|
|
|
|
|
+ resp.setId(po.getId());
|
|
|
|
|
+ resp.setSpaceId(po.getSpaceId());
|
|
|
|
|
+ resp.setName(po.getName());
|
|
|
|
|
+ resp.setAbbreviation(po.getAbbreviation());
|
|
|
|
|
+ resp.setArea(po.getArea());
|
|
|
|
|
+ resp.setCommonArea(po.getCommonArea());
|
|
|
|
|
+ resp.setAirConditionedArea(po.getAirConditionedArea());
|
|
|
|
|
+ if (po.getResidentPopulation() != null) {
|
|
|
|
|
+ resp.setResidentPopulation(java.math.BigDecimal.valueOf(po.getResidentPopulation()));
|
|
|
|
|
+ }
|
|
|
|
|
+ resp.setDeviceSystemList(deviceSystemList);
|
|
|
|
|
+ resp.setProvinceCode(po.getProvinceCode());
|
|
|
|
|
+ resp.setCityCode(po.getCityCode());
|
|
|
|
|
+ resp.setDistrictCode(po.getDistrictCode());
|
|
|
|
|
+ resp.setProvinceName(po.getProvinceName());
|
|
|
|
|
+ resp.setCityName(po.getCityName());
|
|
|
|
|
+ resp.setDistrictName(po.getDistrictName());
|
|
|
|
|
+ resp.setLocation(po.getLocation());
|
|
|
|
|
+ resp.setAddress(po.getAddress());
|
|
|
|
|
+ resp.setTypeId(po.getTypeId());
|
|
|
|
|
+ resp.setTypeName(po.getTypeName());
|
|
|
|
|
+ resp.setImage(po.getImage());
|
|
|
|
|
+ resp.setIntroduction(po.getIntroduction());
|
|
|
|
|
+ resp.setPlatformName(po.getPlatformName());
|
|
|
|
|
+ resp.setLogo(po.getLogo());
|
|
|
|
|
+ resp.setLogoMin(po.getLogoMin());
|
|
|
|
|
+ resp.setUpdatedBy(po.getUpdatedBy());
|
|
|
|
|
+ resp.setCreatedBy(po.getCreatedBy());
|
|
|
|
|
+ if (po.getUpdateTime() != null) {
|
|
|
|
|
+ resp.setUpdateTime(po.getUpdateTime().toString());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (po.getCreateTime() != null) {
|
|
|
|
|
+ resp.setCreateTime(po.getCreateTime().toString());
|
|
|
|
|
+ }
|
|
|
|
|
+ return resp;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|