//package com.red.circle.console.infra;
//
//import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
//import com.red.circle.console.infra.database.rds.entity.admin.Resource;
//import com.red.circle.console.infra.database.rds.enums.AuthTypeEnum;
//import com.red.circle.console.infra.database.rds.service.admin.ResourceService;
//import com.red.circle.tool.crypto.SecurityUtils;
//import jakarta.annotation.PostConstruct;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.Collections;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Objects;
//import java.util.Set;
//import java.util.stream.Collectors;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
//
///**
// * <p>
// * 服务mapping扫描.
// * </p>
// *
// * @author pengliang
// */
//@Service
//@RequiredArgsConstructor
//public class ScanMappings {
//
//  private final ResourceService resourceService;
//  private final String[] emptyArray = new String[]{""};
//  private final RequestMappingHandlerMapping requestMappingHandlerMapping;
//
//
//  /**
//   * 扫描资源插入数据库
//   */
//  @PostConstruct
//  public void doScan() {
//    resourceService.saveOrUpdateBatch(
//        requestMappingHandlerMapping.getHandlerMethods()
//            .values()
//            .stream()
//            .map(this::getResources)
//            .flatMap(Collection::stream)
//            .collect(Collectors.toList())
//    );
//  }
//
//  /**
//   * 获取Resource.
//   */
//  public List<Resource> getResources(HandlerMethod handlerMethod) {
//    // Resources res = handlerMethod.getMethodAnnotation(Resources.class);
//    RequestMapping requestMappingAnnotation = handlerMethod.getBeanType()
//        .getAnnotation(RequestMapping.class);
//    RequestMapping methodMappingAnnotation = handlerMethod
//        .getMethodAnnotation(RequestMapping.class);
//    if (Objects.isNull(requestMappingAnnotation) && Objects.isNull(methodMappingAnnotation)) {
//      return Collections.emptyList();
//    }
//    String[] requestMappings =
//        Objects.nonNull(requestMappingAnnotation) ? requestMappingAnnotation.value() : emptyArray;
//    String[] methodMappings =
//        Objects.nonNull(methodMappingAnnotation) ? methodMappingAnnotation.path() : emptyArray;
//    RequestMethod[] method =
//        Objects.nonNull(methodMappingAnnotation) ? methodMappingAnnotation.method()
//            : new RequestMethod[0];
//    requestMappings = ArrayUtils.isEmpty(requestMappings) ? emptyArray : requestMappings;
//    methodMappings = ArrayUtils.isEmpty(methodMappings) ? emptyArray : methodMappings;
//    Set<String> mappings = new HashSet<>(1);
//    for (String reqMapping : requestMappings) {
//      for (String methodMapping : methodMappings) {
//        mappings.add(reqMapping + methodMapping);
//      }
//    }
//    List<Resource> resources = new ArrayList<>(1);
//    for (RequestMethod requestMethod : method) {
//      for (String mapping : mappings) {
//        //接口描述
//        Resource resource = new Resource();
//        resource.setResourceName("未命名资源路径");
//        resource.setMapping(mapping);
//        resource.setMethod(requestMethod.name());
//        // resource.setAuthType(Objects.isNull(res) ? AuthTypeEnum.LOGIN : res.auth());
//        resource.setAuthType(AuthTypeEnum.AUTH);
//        resource.setPerm(resourceService.getResourcePermTag(requestMethod.name(), mapping));
//        resource.setId(SecurityUtils.md5(resource.getPerm()));
//        resources.add(resource);
//      }
//    }
//    return resources;
//  }
//
//}
