最新产品
This commit is contained in:
@ -0,0 +1,67 @@
|
||||
package com.yj.earth.common.service;
|
||||
|
||||
import cn.hutool.crypto.digest.BCrypt;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.yj.earth.business.domain.Role;
|
||||
import com.yj.earth.business.domain.Source;
|
||||
import com.yj.earth.business.domain.User;
|
||||
import com.yj.earth.business.service.RoleService;
|
||||
import com.yj.earth.business.service.SourceService;
|
||||
import com.yj.earth.business.service.UserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ServerInitService {
|
||||
@Resource
|
||||
private SourceService sourceService;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private RoleService roleService;
|
||||
|
||||
public void init() {
|
||||
// 查询数据库所有需要加载的资源
|
||||
List<Source> list =sourceService.list(new LambdaQueryWrapper<Source>()
|
||||
.eq(Source::getSourceType, "terrain")
|
||||
.or().eq(Source::getSourceType, "layer")
|
||||
.or().eq(Source::getSourceType, "tileset"));
|
||||
// 依次初始化
|
||||
for (Source source : list) {
|
||||
// 同步资源
|
||||
sourceService.getDetail(source.getSourcePath(), sourceService.addAndGetSourceId(source.getSourcePath()));
|
||||
log.info("初始化资源<--{}-->完成", source.getSourceName());
|
||||
}
|
||||
}
|
||||
|
||||
public void checkDefaultData() {
|
||||
// 查询角色表和用户表是否有数据
|
||||
if(roleService.count() == 0 && userService.count() == 0) {
|
||||
log.info("初始化默认数据");
|
||||
// 新增一个管理员角色
|
||||
Role adminRole = new Role();
|
||||
adminRole.setRoleName("管理员");
|
||||
adminRole.setDescription("系统管理员");
|
||||
adminRole.setIsSuper(1);
|
||||
roleService.save(adminRole);
|
||||
// 新增一个默认角色
|
||||
Role defaultRole = new Role();
|
||||
defaultRole.setRoleName("默认角色");
|
||||
defaultRole.setDescription("系统默认角色");
|
||||
defaultRole.setIsSuper(0);
|
||||
roleService.save(defaultRole);
|
||||
// 新增一个用户
|
||||
User user = new User();
|
||||
user.setUsername("admin");
|
||||
user.setPassword(BCrypt.hashpw("admin123", BCrypt.gensalt()));
|
||||
user.setNickname("管理员");
|
||||
user.setRoleId(adminRole.getId());
|
||||
user.setPhone("13888888888");
|
||||
userService.save(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package com.yj.earth.common.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yj.earth.annotation.SourceType;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class SourceParamsValidator {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Map<String, Class<?>> sourceTypeMap = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
public SourceParamsValidator(ObjectMapper objectMapper, Set<Class<?>> sourceParamClasses) {
|
||||
this.objectMapper = objectMapper;
|
||||
|
||||
// 初始化资源类型与参数类的映射关系
|
||||
for (Class<?> clazz : sourceParamClasses) {
|
||||
SourceType annotation = clazz.getAnnotation(SourceType.class);
|
||||
if (annotation != null) {
|
||||
sourceTypeMap.put(annotation.value(), clazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证并转换参数
|
||||
*/
|
||||
public Object validateAndConvert(String sourceType, Map<String, Object> params) {
|
||||
// 检查是否有对应的参数类
|
||||
Class<?> paramClass = sourceTypeMap.get(sourceType);
|
||||
if (paramClass == null) {
|
||||
String message = "不支持 " + sourceType + "的资源类型";
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
// 转换并验证参数
|
||||
try {
|
||||
return objectMapper.convertValue(params, paramClass);
|
||||
} catch (IllegalArgumentException e) {
|
||||
String message = "请核对类型和参数";
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有支持的资源类型
|
||||
*/
|
||||
public Set<String> getSupportedSourceTypes() {
|
||||
return sourceTypeMap.keySet();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user