You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

69 lines
2.6 KiB

package com.epmet.config;
import com.epmet.commons.tools.enums.RequirePermissionEnum;
import com.epmet.entity.OperationEntity;
import com.epmet.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.util.*;
@Configuration
public class PermissionInitializer {
@Autowired
private OperationService operationService;
/**
* 初始化操作权限
*/
@PostConstruct
public void initOpePermissions() {
final Map<String, OperationEntity> existsOpesMap = new HashMap<>();
List<OperationEntity> allExistsOpeEntities = operationService.listAllOperations();
allExistsOpeEntities.stream().forEach(ope -> existsOpesMap.put(ope.getOperationKey(), ope));
ArrayList<OperationEntity> operations2Create = new ArrayList<>();
ArrayList<OperationEntity> operations2Update = new ArrayList<>();
// 1.收集需要添加的
List<RequirePermissionEnum> permEnums = Arrays.asList(RequirePermissionEnum.values());
for (RequirePermissionEnum perm : permEnums) {
String key = perm.getKey();
if (!existsOpesMap.containsKey(key)) {
OperationEntity operationEntity = new OperationEntity();
operationEntity.setOperationKey(key);
operationEntity.setBrief(perm.getBrief());
operationEntity.setOperationName(perm.getName());
operations2Create.add(operationEntity);
}
}
//2.收集需要修改的
for (RequirePermissionEnum perm : permEnums) {
String key = perm.getKey();
String name = perm.getName();
String brief = perm.getBrief();
if (existsOpesMap.containsKey(key)) {
OperationEntity ope = existsOpesMap.get(key);
if (!name.equals(ope.getOperationName()) || !brief.equals(ope.getBrief())) {
// name或者brief发生过变化的,需要更新
ope.setBrief(brief);
ope.setOperationName(name);
operations2Update.add(ope);
}
}
}
//3.批量添加和修改
if (!CollectionUtils.isEmpty(operations2Create)) {
operationService.createBatch(operations2Create);
}
if (!CollectionUtils.isEmpty(operations2Update)) {
operationService.updateBatch(operations2Update);
}
}
}