feat: ASW-61 Environment 管理接口开发

This commit is contained in:
zhangshuai
2024-09-10 15:27:52 +08:00
parent 0c59be48f9
commit 0b08291d7c
9 changed files with 200 additions and 64 deletions

View File

@@ -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<String, Object> 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();
}

View File

@@ -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<EnvironmentSessionEntity> {
List<EnvironmentSessionEntity> queryListByUsed();
}

View File

@@ -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<WorkspaceEntity> workspaces;
@TableField(exist = false)
private EnvironmentSessionEntity session;
@TableField(exist = false)
private List<String> workspaceIds;
@JsonIgnore
public String getParamStr() {
return null == this.param ? "{}" : T.JSONUtil.toJsonStr(this.param);

View File

@@ -13,9 +13,13 @@ public interface IEnvironmentService extends IService<EnvironmentEntity>{
Page queryList(Map<String, Object> params);
EnvironmentEntity saveDevice(EnvironmentEntity entity);
Page findEnvironmentByCurrentUserId(Map<String, Object> params);
void removeDevice(List<String> ids);
void removeEnv(List<String> ids);
Page mySession(Map params);
EnvironmentEntity saveEnv(EnvironmentEntity entity);
EnvironmentEntity updateEnv(EnvironmentEntity entity);
}

View File

@@ -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>{
EnvironmentSessionEntity saveSession(String envId, String workspaceId);
List<EnvironmentSessionEntity> queryListByUsed();
}

View File

@@ -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<EnvironmentDao, EnvironmentEntity> implements IEnvironmentService {
@@ -34,10 +38,13 @@ public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentDao, Environm
private ISysUserService sysUserService;
@Autowired
private IEnvironmentSessionService deviceSessionService;
private IEnvironmentSessionService environmentSessionService;
@Autowired
private IEnvironmentWorkspaceService deviceWorkspaceService;
private IEnvironmentWorkspaceService environmentWorkspaceService;
@Autowired
private IWorkspaceService workspaceService;
@Override
public EnvironmentEntity queryInfo(String id) {
@@ -47,23 +54,32 @@ public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentDao, Environm
// param
environment.setParam(environment.getParamJSONObject());
// user
SysUserEntity createUser = sysUserService.getById(environment.getCreateUserId());
SysUserEntity updateUser = sysUserService.getById(environment.getUpdateUserId());
createUser.setPwd(null);
updateUser.setPwd(null);
environment.setCreateUser(createUser);
environment.setUpdateUser(updateUser);
EnvironmentSessionEntity deviceSession = deviceSessionService.getOne(new LambdaQueryWrapper<EnvironmentSessionEntity>()
// workspaces
List<EnvironmentWorkspaceEntity> environmentWorkspaceList = environmentWorkspaceService.list(new LambdaQueryWrapper<EnvironmentWorkspaceEntity>().eq(EnvironmentWorkspaceEntity::getEnvId, id));
List<String> workspaceIds = environmentWorkspaceList.stream().map(x -> x.getWorkspaceId()).toList();
List<WorkspaceEntity> workspaceList = workspaceService.list(new LambdaQueryWrapper<WorkspaceEntity>().in(WorkspaceEntity::getId, workspaceIds));
environment.setWorkspaces(workspaceList);
// session
EnvironmentSessionEntity deviceSession = environmentSessionService.getOne(new LambdaQueryWrapper<EnvironmentSessionEntity>()
.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<EnvironmentDao, Environm
Page page = T.PageUtil.getPage(params);
List<EnvironmentEntity> packageList = this.getBaseMapper().queryList(page, params);
List<EnvironmentSessionEntity> sessionEntityList = deviceSessionService.list(new LambdaQueryWrapper<EnvironmentSessionEntity>().eq(EnvironmentSessionEntity::getStatus, 1));
List<EnvironmentSessionEntity> sessionEntityList = environmentSessionService.queryListByUsed();
List<String> envIdList = sessionEntityList.stream().map(x -> x.getEnvId()).toList();
Map<String, EnvironmentSessionEntity> 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<String, Object> params) {
params.put("currentUserId", StpUtil.getLoginIdAsString());
Page page = this.queryList(params);
return page;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void removeDevice(List<String> ids) {
public void removeEnv(List<String> ids) {
// remove
this.removeBatchByIds(ids);
this.remove(new LambdaQueryWrapper<EnvironmentEntity>().in(EnvironmentEntity::getId, ids).eq(EnvironmentEntity::getCreateUserId, StpUtil.getLoginIdAsString()));
// session
deviceSessionService.remove(new LambdaQueryWrapper<EnvironmentSessionEntity>().in(EnvironmentSessionEntity::getEnvId, ids));
environmentSessionService.remove(new LambdaQueryWrapper<EnvironmentSessionEntity>().in(EnvironmentSessionEntity::getEnvId, ids));
//device workspace
deviceWorkspaceService.remove(new LambdaQueryWrapper<EnvironmentWorkspaceEntity>().in(EnvironmentWorkspaceEntity::getEnvId, ids));
environmentWorkspaceService.remove(new LambdaQueryWrapper<EnvironmentWorkspaceEntity>().in(EnvironmentWorkspaceEntity::getEnvId, ids));
}
@Override
@@ -127,7 +129,7 @@ public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentDao, Environm
params.put("currentUserId", currentUserId);
Page page = new Query(EnvironmentEntity.class).getPage(params);
List<EnvironmentSessionEntity> sessionEntityList = deviceSessionService.list(new LambdaQueryWrapper<EnvironmentSessionEntity>().eq(EnvironmentSessionEntity::getStatus, 1));
List<EnvironmentSessionEntity> sessionEntityList = environmentSessionService.list(new LambdaQueryWrapper<EnvironmentSessionEntity>().eq(EnvironmentSessionEntity::getStatus, 1));
List<String> envIdList = sessionEntityList.stream().map(x -> x.getEnvId()).toList();
List<EnvironmentEntity> packageList = this.getBaseMapper().mySession(page, params);
@@ -139,4 +141,59 @@ public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentDao, Environm
return page;
}
@Override
@Transactional(rollbackFor = Exception.class)
public EnvironmentEntity saveEnv(EnvironmentEntity entity) {
entity.setCreateUserId(StpUtil.getLoginIdAsString());
entity.setUpdateUserId(StpUtil.getLoginIdAsString());
entity.setCreateTimestamp(System.currentTimeMillis());
entity.setUpdateTimestamp(System.currentTimeMillis());
entity.setParam(entity.getParamStr());
this.save(entity);
// save env workspace
if (T.CollUtil.isNotEmpty(entity.getWorkspaceIds())){
List<EnvironmentWorkspaceEntity> 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<EnvironmentEntity>().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<EnvironmentWorkspaceEntity>().eq(EnvironmentWorkspaceEntity::getEnvId, entity.getId()));
// save env workspace
if (T.CollUtil.isNotEmpty(entity.getWorkspaceIds())){
List<EnvironmentWorkspaceEntity> 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;
}
}

View File

@@ -41,4 +41,10 @@ public class EnvironmentSessionServiceImpl extends ServiceImpl<EnvironmentSessio
this.save(session);
return session;
}
@Override
public List<EnvironmentSessionEntity> queryListByUsed() {
List<EnvironmentSessionEntity> sessionEntityList = this.getBaseMapper().queryListByUsed();
return sessionEntityList;
}
}

View File

@@ -31,8 +31,7 @@
<association property="useUser" columnPrefix="u_" javaType="cn.hutool.json.JSONObject">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="startTimestamp" column="start_timestamp"/>
<result property="endTimestamp" column="end_timestamp"/>
<result property="userName" column="user_name"/>
</association>
</resultMap>
@@ -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
<where>
<if test="params.ids != null and params.ids != ''">
e.id in
@@ -74,6 +66,10 @@
<if test="params.workspaceId != null and params.workspaceId != ''">
AND ew.workspace_id = #{params.workspaceId}
</if>
<if test="params.currentUserId != null and params.currentUserId != ''">
AND e.create_user_id = #{params.currentUserId}
</if>
</where>
<if test="params.orderBy == null or params.orderBy == ''">
@@ -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

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="net.geedge.asw.module.environment.dao.EnvironmentSessionDao">
<resultMap id="session" type="net.geedge.asw.module.environment.entity.EnvironmentSessionEntity">
<result property="id" column="id"/>
<result property="envId" column="env_id"/>
<result property="status" column="status"/>
<result property="userId" column="user_id"/>
<result property="startTimestamp" column="start_timestamp"/>
<result property="endTimestamp" column="end_timestamp"/>
<result property="jobId" column="job_id"/>
<result property="workspaceId" column="workspace_id"/>
<association property="user" columnPrefix="u_" javaType="net.geedge.asw.module.sys.entity.SysUserEntity">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="userName" column="user_name"/>
</association>
<association property="workspace" columnPrefix="w_" javaType="net.geedge.asw.module.workspace.entity.WorkspaceEntity">
<id property="id" column="id"/>
<result property="name" column="name"/>
</association>
</resultMap>
<select id="queryListByUsed" resultMap="session">
SELECT
es.*,
u.id AS u_id,
u.name AS u_name,
u.user_name AS u_user_name,
w.id AS w_id,
w.name AS w_name
FROM environment_session es
LEFT JOIN sys_user u ON es.user_id = u.id
LEFT JOIN workspace w ON es.workspace_id = w.id
<where>
es.status = 1
</where>
</select>
</mapper>