|
|
@ -4,13 +4,11 @@ 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.ApplicationContext; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
@Configuration |
|
|
|
public class PermissionInitializer { |
|
|
@ -23,38 +21,49 @@ public class PermissionInitializer { |
|
|
|
*/ |
|
|
|
@PostConstruct |
|
|
|
public void initOpePermissions() { |
|
|
|
Set<String> operationKeys = getExistsOperationKeys(); |
|
|
|
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<>(); |
|
|
|
|
|
|
|
RequirePermissionEnum[] requirePermissionEnums = RequirePermissionEnum.values(); |
|
|
|
Arrays.stream(requirePermissionEnums).forEach(perm -> { |
|
|
|
// 1.收集需要添加的
|
|
|
|
List<RequirePermissionEnum> permEnums = Arrays.asList(RequirePermissionEnum.values()); |
|
|
|
for (RequirePermissionEnum perm : permEnums) { |
|
|
|
String key = perm.getKey(); |
|
|
|
if (!operationKeys.contains(key)) { |
|
|
|
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 (!ope.getOperationName().equals(name) || !ope.getBrief().equals(brief)) { |
|
|
|
// name或者brief发生过变化的,需要更新
|
|
|
|
ope.setBrief(brief); |
|
|
|
ope.setOperationName(name); |
|
|
|
operations2Update.add(ope); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//3.批量添加和修改
|
|
|
|
if (!CollectionUtils.isEmpty(operations2Create)) { |
|
|
|
operationService.createBatch(operations2Create); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取现有的操作key列表 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public Set<String> getExistsOperationKeys() { |
|
|
|
List<OperationEntity> operationEntities = operationService.listAllOperations(); |
|
|
|
if (!CollectionUtils.isEmpty(operationEntities)) { |
|
|
|
return operationEntities.stream().map(ope -> ope.getOperationKey()).collect(Collectors.toSet()); |
|
|
|
if (!CollectionUtils.isEmpty(operations2Update)) { |
|
|
|
operationService.updateBatch(operations2Update); |
|
|
|
} |
|
|
|
return new HashSet<>(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|