//package com.jay.monitor.data.server.services.impl;
//
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.jay.monitor.data.core.model.response.ResponseCommonModel;
//import com.jay.monitor.data.core.utils.ResponseUtils;
//import com.jay.monitor.data.server.daos.BackstageRouteMapper;
//import com.jay.monitor.data.server.models.entity.BackstageRoute;
//import com.jay.monitor.data.server.models.entity.BackstageRouteExample;
//import com.jay.monitor.data.server.models.request.route.RouteDataRequest;
//import com.jay.monitor.data.server.models.response.RouteDataResponse;
//import com.jay.monitor.data.server.services.IBackstageRouteService;
//import com.jay.monitor.data.server.services.IRouteDataService;
//import com.jay.monitor.data.server.services.IRoutesService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
///**
// * 后台路由管理Service
// *
// * @author liukx
// * @Date 2019-02-28 15:16
// */
//@Service("defaultBackstageRouteService")
//public class DefaultBackstageRouteService extends ServiceImpl<BackstageRouteMapper, BackstageRoute> implements IBackstageRouteService, IRoutesService {
//
//
//    private Logger logger = LoggerFactory.getLogger(DefaultBackstageRouteService.class);
//
//    @Autowired
//    private BackstageRouteMapper backstageRouteMapper;
//
//    @Value("${spring.application.name}")
//    private String domainName;
//
////    @Autowired
////    private EurekaClient eurekaClient;
//
//    @Autowired
//    private List<IRouteDataService> routeDataServiceList;
//
//    @Autowired
//    private Map<String, IRouteDataService> routeDataServiceMap;
//
//    @Transactional
//    public int insert(BackstageRoute request) {
//        return 0;
//    }
//
//    public List<String> getRoutes() {
////        List<String> routeList = getValidApplicationName();
//        return null;
//    }
//
//    /**
//     * 执行路由刷新的对象
//     *
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @Override
//    @Transactional
//    public ResponseCommonModel executeRefresh(RouteDataRequest request) throws Exception {
//        String projectName = request.getProjectName();
//        ResponseUtils.checkParamNull(projectName, "project");
//
////        Application application = eurekaClient.getApplication(projectName);
////        InstanceInfo applicationInfo = getApplicationInfo(application);
//
//        // 如果eureka拿不到该项目,表示该项目未存活
////        ResponseUtils.checkNull(applicationInfo, CommonEnums.ROUTE_EUREKA_APP_ERROR);
//
//        // 根据服务注册的地址去指定的服务内去获取api的文档
////        String homePageUrl = applicationInfo.getHomePageUrl();
////        IRouteDataService routeDataService = routeDataServiceList.get(0);
////        RouteDataRequest routeDataRequest = new RouteDataRequest();
////        routeDataRequest.setUrl(homePageUrl);
////        routeDataRequest.setProjectName(projectName);
////        List<RouteDataResponse> data = routeDataService.getData(routeDataRequest);
//        List<RouteDataResponse> data = null;
//
//        StringBuffer sb = new StringBuffer();
//
//        // 拿到注册文档开始分析对比数据变化
//        BackstageRouteExample example = new BackstageRouteExample();
//        example.createCriteria().andProjectEqualTo(projectName);
//        List<BackstageRoute> backstageRouteEntityList = backstageRouteMapper.selectByExample(example);
//        if (backstageRouteEntityList != null && backstageRouteEntityList.size() > 0) {
//            logger.info(" 开始分析数据对比 ... ");
//            Map<String, BackstageRoute> urlMap = new HashMap<>();
//            for (int i = 0; i < backstageRouteEntityList.size(); i++) {
//                BackstageRoute backstageRoute = backstageRouteEntityList.get(i);
//                urlMap.put(backstageRoute.getUrl(), backstageRoute);
//            }
//
//            // 构建两个容器分别存放修改过的和需要新增的数据
//            List<BackstageRoute> updateEntityList = new ArrayList<>();
//            List<BackstageRoute> insertEntityList = new ArrayList<>();
//
//            for (int i = 0; i < data.size(); i++) {
//                RouteDataResponse response = data.get(i);
//                String url = response.getUrl();
//                BackstageRoute oldEntity = urlMap.get(url);
//                if (oldEntity == null) {
//                    insertRoute(projectName, response);
//                    insertEntityList.add(response);
//                } else {
//                    RouteDataResponse routeDataResponse = new RouteDataResponse(oldEntity);
//                    if (!response.equals(routeDataResponse)) {
//                        logger.info(oldEntity.getUrl() + " 触发修改 ... ");
//                        response.setId(oldEntity.getId());
//                        response.setUpdated(new Date());
//                        backstageRouteMapper.updateByPrimaryKey(response);
//                        updateEntityList.add(response);
//                    }
//                }
//            }
//            sb.append(" 新增 [" + insertEntityList.size() + "] 条 , 修改 [" + updateEntityList.size() + "] 条");
//            logger.info(sb.toString());
//
//        } else {
//            logger.info(" 直接执行添加操作 ... 数据大小 : " + data.size());
//            // 直接执行添加操作
//            for (int i = 0; i < data.size(); i++) {
//                RouteDataResponse response = data.get(i);
//                insertRoute(projectName, response);
//            }
//            sb.append(" 新增 [" + data.size() + "] 条 ");
//        }
//
//        ResponseCommonModel responseCommonModel = new ResponseCommonModel();
//        responseCommonModel.setSuccess(true);
//        responseCommonModel.setMessage(sb.toString());
//
//        return responseCommonModel;
//    }
//
//    /**
//     * 添加路由到数据库
//     *
//     * @param project
//     * @param response
//     * @throws Exception
//     */
//    private void insertRoute(String project, RouteDataResponse response) throws Exception {
//        response.setProject(project);
//        response.setCreated(new Date());
//        response.setIsValid(1);
//        response.setStatus(1);
//        backstageRouteMapper.insert(response);
//    }
//
//    /**
//     * 获取Eureka有效的应用名称
//     *
//     * @return
//     */
////    private List<String> getValidApplicationName() {
////        List<String> routeList = new ArrayList<>();
////        Applications applications = eurekaClient.getApplications();
////        List<Application> registeredApplications = applications.getRegisteredApplications();
////        if (applications != null && registeredApplications.size() > 0) {
////            for (int i = 0; i < registeredApplications.size(); i++) {
////                Application application = registeredApplications.get(i);
////                InstanceInfo applicationInfo = getApplicationInfo(application);
////                String vipAddress = applicationInfo.getVIPAddress();
////                if (applicationInfo != null && StringUtils.isNotEmpty(vipAddress) && filterApplication(vipAddress)) {
////                    routeList.add(vipAddress);
////                }
////            }
////        }
////        return routeList;
////    }
//
//    public boolean filterApplication(String applicationName) {
//        return !(applicationName.indexOf("eureka") > 0 || applicationName.equals(domainName));
//    }
//
//    /**
//     * 获取指定的应用对象信息
//     *
//     * @param application
//     * @return
//     */
////    private InstanceInfo getApplicationInfo(Application application) {
////        if (application != null && application.size() > 0) {
////            for (int j = 0; j < application.size(); j++) {
////                List<InstanceInfo> instances = application.getInstances();
////                if (instances != null && instances.size() > 0) {
////                    InstanceInfo instanceInfo = instances.get(0);
////                    return instanceInfo;
////                }
////            }
////        }
////        return null;
////    }
//
//}
