From 0b08291d7c1489893089a73f1979b7884a824919 Mon Sep 17 00:00:00 2001 From: zhangshuai Date: Tue, 10 Sep 2024 15:27:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ASW-61=20=20Environment=20=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=8E=A5=E5=8F=A3=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/EnvironmentController.java | 31 ++-- .../dao/EnvironmentSessionDao.java | 4 + .../environment/entity/EnvironmentEntity.java | 12 ++ .../service/IEnvironmentService.java | 8 +- .../service/IEnvironmentSessionService.java | 4 + .../service/impl/EnvironmentServiceImpl.java | 135 +++++++++++++----- .../impl/EnvironmentSessionServiceImpl.java | 6 + .../db/mapper/device/EnvironmentMapper.xml | 21 ++- .../device/EnvironmentSessionMapper.xml | 43 ++++++ 9 files changed, 200 insertions(+), 64 deletions(-) create mode 100644 src/main/resources/db/mapper/device/EnvironmentSessionMapper.xml diff --git a/src/main/java/net/geedge/asw/module/environment/controller/EnvironmentController.java b/src/main/java/net/geedge/asw/module/environment/controller/EnvironmentController.java index f088327..7a5ad4a 100644 --- a/src/main/java/net/geedge/asw/module/environment/controller/EnvironmentController.java +++ b/src/main/java/net/geedge/asw/module/environment/controller/EnvironmentController.java @@ -70,20 +70,29 @@ public class EnvironmentController { return R.ok(page); } - @PostMapping - public R add(@RequestBody EnvironmentEntity entity) { - T.VerifyUtil.is(entity).notNull() - .and(entity.getName()).notEmpty(RCode.NAME_CANNOT_EMPTY) - .and(entity.getWorkspaceId()).notEmpty(RCode.WORKSPACE_ID_CANNOT_EMPTY); - - EnvironmentEntity deviceEntity = environmentService.saveDevice(entity); - return R.ok().putData("id", deviceEntity.getId()); + @GetMapping("/mgt") + public R queryList(@RequestParam Map params) { + Page page = environmentService.findEnvironmentByCurrentUserId(params); + return R.ok().putData(page); } - @DeleteMapping - public R delete(String[] ids) { + + @PostMapping("/mgt") + public R save(@RequestBody EnvironmentEntity entity) { + EnvironmentEntity env = environmentService.saveEnv(entity); + return R.ok().putData("record", env.getId()); + } + + @PutMapping("/mgt") + public R update(@RequestBody EnvironmentEntity entity) { + EnvironmentEntity env = environmentService.updateEnv(entity); + return R.ok().putData("record", env.getId()); + } + + @DeleteMapping("/mgt") + public R delete(String ids) { T.VerifyUtil.is(ids).notEmpty(); - environmentService.removeDevice(T.ListUtil.of(ids)); + environmentService.removeEnv(T.ListUtil.of(ids.split(","))); return R.ok(); } diff --git a/src/main/java/net/geedge/asw/module/environment/dao/EnvironmentSessionDao.java b/src/main/java/net/geedge/asw/module/environment/dao/EnvironmentSessionDao.java index f67b451..9b03be5 100644 --- a/src/main/java/net/geedge/asw/module/environment/dao/EnvironmentSessionDao.java +++ b/src/main/java/net/geedge/asw/module/environment/dao/EnvironmentSessionDao.java @@ -4,7 +4,11 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import net.geedge.asw.module.environment.entity.EnvironmentSessionEntity; import org.apache.ibatis.annotations.Mapper; +import java.util.List; + @Mapper public interface EnvironmentSessionDao extends BaseMapper { + List queryListByUsed(); + } diff --git a/src/main/java/net/geedge/asw/module/environment/entity/EnvironmentEntity.java b/src/main/java/net/geedge/asw/module/environment/entity/EnvironmentEntity.java index b50e1b4..8552576 100644 --- a/src/main/java/net/geedge/asw/module/environment/entity/EnvironmentEntity.java +++ b/src/main/java/net/geedge/asw/module/environment/entity/EnvironmentEntity.java @@ -9,6 +9,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import net.geedge.asw.common.util.T; import net.geedge.asw.module.sys.entity.SysUserEntity; +import net.geedge.asw.module.workspace.entity.WorkspaceEntity; + +import java.util.List; @Data @TableName("environment") @@ -41,6 +44,15 @@ public class EnvironmentEntity { @TableField(exist = false) private JSONObject useUser; + @TableField(exist = false) + private List workspaces; + + @TableField(exist = false) + private EnvironmentSessionEntity session; + + @TableField(exist = false) + private List workspaceIds; + @JsonIgnore public String getParamStr() { return null == this.param ? "{}" : T.JSONUtil.toJsonStr(this.param); diff --git a/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentService.java b/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentService.java index 350c8cd..fb98195 100644 --- a/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentService.java +++ b/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentService.java @@ -13,9 +13,13 @@ public interface IEnvironmentService extends IService{ Page queryList(Map params); - EnvironmentEntity saveDevice(EnvironmentEntity entity); + Page findEnvironmentByCurrentUserId(Map params); - void removeDevice(List ids); + void removeEnv(List ids); Page mySession(Map params); + + EnvironmentEntity saveEnv(EnvironmentEntity entity); + + EnvironmentEntity updateEnv(EnvironmentEntity entity); } diff --git a/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentSessionService.java b/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentSessionService.java index 6a07f5c..40cd04f 100644 --- a/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentSessionService.java +++ b/src/main/java/net/geedge/asw/module/environment/service/IEnvironmentSessionService.java @@ -3,7 +3,11 @@ package net.geedge.asw.module.environment.service; import com.baomidou.mybatisplus.extension.service.IService; import net.geedge.asw.module.environment.entity.EnvironmentSessionEntity; +import java.util.List; + public interface IEnvironmentSessionService extends IService{ EnvironmentSessionEntity saveSession(String envId, String workspaceId); + + List queryListByUsed(); } diff --git a/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentServiceImpl.java b/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentServiceImpl.java index 000ba65..928e954 100644 --- a/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentServiceImpl.java +++ b/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentServiceImpl.java @@ -1,12 +1,12 @@ package net.geedge.asw.module.environment.service.impl; import cn.dev33.satoken.stp.StpUtil; -import cn.hutool.json.JSONObject; import cn.hutool.log.Log; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import net.geedge.asw.common.config.Query; +import net.geedge.asw.common.util.ASWException; import net.geedge.asw.common.util.RCode; import net.geedge.asw.common.util.T; import net.geedge.asw.module.environment.dao.EnvironmentDao; @@ -18,12 +18,16 @@ import net.geedge.asw.module.environment.service.IEnvironmentSessionService; import net.geedge.asw.module.environment.service.IEnvironmentWorkspaceService; import net.geedge.asw.module.sys.entity.SysUserEntity; import net.geedge.asw.module.sys.service.ISysUserService; +import net.geedge.asw.module.workspace.entity.WorkspaceEntity; +import net.geedge.asw.module.workspace.service.IWorkspaceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; @Service public class EnvironmentServiceImpl extends ServiceImpl implements IEnvironmentService { @@ -34,10 +38,13 @@ public class EnvironmentServiceImpl extends ServiceImpl() + // workspaces + List environmentWorkspaceList = environmentWorkspaceService.list(new LambdaQueryWrapper().eq(EnvironmentWorkspaceEntity::getEnvId, id)); + List workspaceIds = environmentWorkspaceList.stream().map(x -> x.getWorkspaceId()).toList(); + List workspaceList = workspaceService.list(new LambdaQueryWrapper().in(WorkspaceEntity::getId, workspaceIds)); + environment.setWorkspaces(workspaceList); + + // session + EnvironmentSessionEntity deviceSession = environmentSessionService.getOne(new LambdaQueryWrapper() .eq(EnvironmentSessionEntity::getEnvId, environment.getId()) - .orderByDesc(EnvironmentSessionEntity::getStartTimestamp).last("limit 1")); + .eq(EnvironmentSessionEntity::getStatus, 1)); if (null != deviceSession) { SysUserEntity useUser = sysUserService.getById(deviceSession.getUserId()); - - JSONObject jsonObject = new JSONObject(); - jsonObject.set("id", useUser.getId()); - jsonObject.set("name", useUser.getName()); - jsonObject.set("startTimestamp", deviceSession.getStartTimestamp()); - jsonObject.set("endTimestamp", deviceSession.getEndTimestamp()); - environment.setUseUser(jsonObject); + useUser.setPwd(null); + WorkspaceEntity workspace = workspaceService.getById(deviceSession.getWorkspaceId()); + deviceSession.setUser(useUser); + deviceSession.setWorkspace(workspace); + environment.setSession(deviceSession); + environment.setStatus(environment.getStatus() == 1 ? 2 : environment.getStatus()); } return environment; } @@ -73,52 +89,38 @@ public class EnvironmentServiceImpl extends ServiceImpl packageList = this.getBaseMapper().queryList(page, params); - List sessionEntityList = deviceSessionService.list(new LambdaQueryWrapper().eq(EnvironmentSessionEntity::getStatus, 1)); + List sessionEntityList = environmentSessionService.queryListByUsed(); List envIdList = sessionEntityList.stream().map(x -> x.getEnvId()).toList(); + Map sessionByEnvId = sessionEntityList.stream().collect(Collectors.toMap(EnvironmentSessionEntity::getEnvId, Function.identity())); + for (EnvironmentEntity entity : packageList) { entity.setParam(entity.getParamJSONObject()); entity.setStatus(envIdList.contains(entity.getId()) ? 2 : entity.getStatus()); + entity.setSession(sessionByEnvId.get(entity.getId())); } page.setRecords(packageList); return page; } @Override - public EnvironmentEntity saveDevice(EnvironmentEntity entity) { - // param - entity.setParam(entity.getParamStr()); - - // default android - entity.setPlatform(T.StrUtil.emptyToDefault(entity.getPlatform(), "android")); - - entity.setCreateTimestamp(System.currentTimeMillis()); - entity.setUpdateTimestamp(System.currentTimeMillis()); - entity.setCreateUserId(StpUtil.getLoginIdAsString()); - entity.setUpdateUserId(StpUtil.getLoginIdAsString()); - // save - this.save(entity); - - EnvironmentWorkspaceEntity environmentWorkspace = new EnvironmentWorkspaceEntity(); - environmentWorkspace.setEnvId(entity.getId()); - environmentWorkspace.setWorkspaceId(entity.getWorkspaceId()); - environmentWorkspace.setCreateUserId(StpUtil.getLoginIdAsString()); - environmentWorkspace.setCreateTimestamp(System.currentTimeMillis()); - deviceWorkspaceService.save(environmentWorkspace); - return entity; + public Page findEnvironmentByCurrentUserId(Map params) { + params.put("currentUserId", StpUtil.getLoginIdAsString()); + Page page = this.queryList(params); + return page; } @Override @Transactional(rollbackFor = Exception.class) - public void removeDevice(List ids) { + public void removeEnv(List ids) { // remove - this.removeBatchByIds(ids); + this.remove(new LambdaQueryWrapper().in(EnvironmentEntity::getId, ids).eq(EnvironmentEntity::getCreateUserId, StpUtil.getLoginIdAsString())); // session - deviceSessionService.remove(new LambdaQueryWrapper().in(EnvironmentSessionEntity::getEnvId, ids)); + environmentSessionService.remove(new LambdaQueryWrapper().in(EnvironmentSessionEntity::getEnvId, ids)); //device workspace - deviceWorkspaceService.remove(new LambdaQueryWrapper().in(EnvironmentWorkspaceEntity::getEnvId, ids)); + environmentWorkspaceService.remove(new LambdaQueryWrapper().in(EnvironmentWorkspaceEntity::getEnvId, ids)); } @Override @@ -127,7 +129,7 @@ public class EnvironmentServiceImpl extends ServiceImpl sessionEntityList = deviceSessionService.list(new LambdaQueryWrapper().eq(EnvironmentSessionEntity::getStatus, 1)); + List sessionEntityList = environmentSessionService.list(new LambdaQueryWrapper().eq(EnvironmentSessionEntity::getStatus, 1)); List envIdList = sessionEntityList.stream().map(x -> x.getEnvId()).toList(); List packageList = this.getBaseMapper().mySession(page, params); @@ -139,4 +141,59 @@ public class EnvironmentServiceImpl extends ServiceImpl list = T.ListUtil.list(false); + for (String workspaceId : entity.getWorkspaceIds()) { + EnvironmentWorkspaceEntity environmentWorkspace = new EnvironmentWorkspaceEntity(); + environmentWorkspace.setEnvId(entity.getId()); + environmentWorkspace.setWorkspaceId(workspaceId); + environmentWorkspace.setCreateTimestamp(System.currentTimeMillis()); + environmentWorkspace.setCreateUserId(StpUtil.getLoginIdAsString()); + list.add(environmentWorkspace); + } + environmentWorkspaceService.saveBatch(list); + } + return entity; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public EnvironmentEntity updateEnv(EnvironmentEntity entity) { + + EnvironmentEntity environment = this.getOne(new LambdaQueryWrapper().eq(EnvironmentEntity::getId, entity.getId()).eq(EnvironmentEntity::getCreateUserId, StpUtil.getLoginIdAsString())); + if (T.ObjectUtil.isNull(environment)) { + throw new ASWException(RCode.ENVIRONMENT_NOT_EXIST); + } + entity.setUpdateUserId(StpUtil.getLoginIdAsString()); + entity.setUpdateTimestamp(System.currentTimeMillis()); + entity.setParam(entity.getParamStr()); + + environmentWorkspaceService.remove(new LambdaQueryWrapper().eq(EnvironmentWorkspaceEntity::getEnvId, entity.getId())); + // save env workspace + if (T.CollUtil.isNotEmpty(entity.getWorkspaceIds())){ + List list = T.ListUtil.list(false); + for (String workspaceId : entity.getWorkspaceIds()) { + EnvironmentWorkspaceEntity environmentWorkspace = new EnvironmentWorkspaceEntity(); + environmentWorkspace.setEnvId(entity.getId()); + environmentWorkspace.setWorkspaceId(workspaceId); + environmentWorkspace.setCreateTimestamp(System.currentTimeMillis()); + environmentWorkspace.setCreateUserId(StpUtil.getLoginIdAsString()); + list.add(environmentWorkspace); + } + environmentWorkspaceService.saveBatch(list); + } + return entity; + } + } \ No newline at end of file diff --git a/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentSessionServiceImpl.java b/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentSessionServiceImpl.java index 07a4f5f..bb5a61f 100644 --- a/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentSessionServiceImpl.java +++ b/src/main/java/net/geedge/asw/module/environment/service/impl/EnvironmentSessionServiceImpl.java @@ -41,4 +41,10 @@ public class EnvironmentSessionServiceImpl extends ServiceImpl queryListByUsed() { + List sessionEntityList = this.getBaseMapper().queryListByUsed(); + return sessionEntityList; + } } diff --git a/src/main/resources/db/mapper/device/EnvironmentMapper.xml b/src/main/resources/db/mapper/device/EnvironmentMapper.xml index fe35323..92acaba 100644 --- a/src/main/resources/db/mapper/device/EnvironmentMapper.xml +++ b/src/main/resources/db/mapper/device/EnvironmentMapper.xml @@ -31,8 +31,7 @@ - - + @@ -46,19 +45,12 @@ uu.id AS uu_id, uu.name AS uu_name, - uu.user_name AS uu_user_name, - - es.user_id AS u_id, - u.name AS u_name, - es.start_timestamp AS u_start_timestamp, - es.end_timestamp AS u_end_timestamp + uu.user_name AS uu_user_name FROM environment e LEFT JOIN sys_user cu ON e.create_user_id = cu.id LEFT JOIN sys_user uu ON e.update_user_id = uu.id LEFT JOIN environment_session es ON e.id = es.env_id LEFT JOIN environment_workspace ew ON e.id = ew.env_id - LEFT JOIN sys_user u ON es.user_id = u.id - e.id in @@ -74,6 +66,10 @@ AND ew.workspace_id = #{params.workspaceId} + + + AND e.create_user_id = #{params.currentUserId} + @@ -87,14 +83,15 @@ cu.id AS cu_id, cu.name AS cu_name, + cu.user_name AS cu_user_name, uu.id AS uu_id, uu.name AS uu_name, + uu.user_name AS uu_user_name, es.user_id AS u_id, u.name AS u_name, - es.start_timestamp AS u_start_timestamp, - es.end_timestamp AS u_end_timestamp + u.user_name AS u_user_name FROM environment e LEFT JOIN sys_user cu ON e.create_user_id = cu.id LEFT JOIN sys_user uu ON e.update_user_id = uu.id diff --git a/src/main/resources/db/mapper/device/EnvironmentSessionMapper.xml b/src/main/resources/db/mapper/device/EnvironmentSessionMapper.xml new file mode 100644 index 0000000..5c19a0e --- /dev/null +++ b/src/main/resources/db/mapper/device/EnvironmentSessionMapper.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file