/* 
 * 作者：钟勋 (e-mail:zhongxunking@163.com)
 */

/*
 * 修订记录:
 * @author 钟勋 2017-09-16 16:05 创建
 */
package org.antframework.configcenter.biz.service;

import org.antframework.common.util.facade.BizException;
import org.antframework.common.util.facade.CommonResultCode;
import org.antframework.common.util.facade.EmptyResult;
import org.antframework.common.util.facade.Status;
import org.antframework.common.util.zookeeper.ZkTemplate;
import org.antframework.configcenter.common.ZkUtils;
import org.antframework.configcenter.dal.dao.AppDao;
import org.antframework.configcenter.dal.dao.ProfileDao;
import org.antframework.configcenter.dal.entity.App;
import org.antframework.configcenter.dal.entity.Profile;
import org.antframework.configcenter.facade.api.ConfigService;
import org.antframework.configcenter.facade.order.TriggerClientRefreshOrder;
import org.bekit.service.annotation.service.Service;
import org.bekit.service.annotation.service.ServiceExecute;
import org.bekit.service.engine.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 触发客户端刷新配置服务
 */
@Service
public class TriggerClientRefreshService {
    @Autowired
    private AppDao appDao;
    @Autowired
    private ProfileDao profileDao;
    @Autowired
    private ZkTemplate zkTemplate;

    @ServiceExecute
    public void execute(ServiceContext<TriggerClientRefreshOrder, EmptyResult> context) {
        TriggerClientRefreshOrder order = context.getOrder();

        App app = getApp(order);
        List<Profile> profiles = getProfiles(order);
        for (Profile profile : profiles) {
            zkTemplate.setData(ZkTemplate.buildPath(profile.getProfileId(), app.getAppId()), ZkUtils.getCurrentDate());
        }
    }

    // 获取需要刷新的应用
    private App getApp(TriggerClientRefreshOrder order) {
        String appId = order.getAppId();
        if (appId == null) {
            appId = ConfigService.COMMON_APP_ID;
        }
        App app = appDao.findByAppId(appId);
        if (app == null) {
            throw new BizException(Status.FAIL, CommonResultCode.INVALID_PARAMETER.getCode(), String.format("不存在应用[%s]", appId));
        }
        return app;
    }

    // 获取需要刷新的环境
    private List<Profile> getProfiles(TriggerClientRefreshOrder order) {
        List<Profile> profiles = new ArrayList<>();
        if (order.getProfileId() == null) {
            profiles.addAll(profileDao.findAll());
        } else {
            Profile profile = profileDao.findByProfileId(order.getProfileId());
            if (profile == null) {
                throw new BizException(Status.FAIL, CommonResultCode.INVALID_PARAMETER.getCode(), String.format("不存在环境[%s]", order.getProfileId()));
            }
            profiles.add(profile);
        }

        return profiles;
    }
}

@@ -703,71 +703,8 @@
 le;%0A
-import org.antframework.configcenter.facade.api.ConfigService;%0A
 impo
@@ -1452,23 +1452,30 @@
         
+List%3C
 App
+%3E
  app
+s
  = getAp
@@ -1475,16 +1475,17 @@
 = getApp
+s
 (order);
@@ -1493,37 +1493,36 @@
         
-List%3C
+for (
 Profile
-%3E
  profile
 s = getP
@@ -1513,19 +1513,18 @@
  profile
-s =
+ :
  getProf
@@ -1526,33 +1526,35 @@
 tProfiles(order)
-;
+) %7B
 %0A        for (Pr
@@ -1550,43 +1550,39 @@
     
+    
 for (
-Profile profile : profile
+App app : app
 s) %7B%0A
+    
     
@@ -1693,24 +1693,38 @@
 entDate());%0A
+            %7D%0A
         %7D%0A  
@@ -1761,18 +1761,25 @@
 ate 
+List%3C
 App
+%3E
  getApp
+s
 (Tri
@@ -1822,37 +1822,40 @@
     
-String
+List%3CApp%3E
  app
-Id = order.getAppId
+s = new ArrayList%3C%3E
 ();%0A
@@ -1866,21 +1866,32 @@
     if (
-a
+order.getA
 ppId
+()
  == null
@@ -1913,52 +1913,57 @@
  app
-Id = ConfigService.COMMON_APP_ID;%0A        %7D%0A
+s.addAll(appDao.findAll());%0A        %7D else %7B%0A    
     
@@ -1999,16 +1999,31 @@
 pId(
-a
+order.getA
 ppId
+()
 );%0A
+    
     
@@ -2037,32 +2037,36 @@
 (app == null) %7B%0A
+    
             thro
@@ -2174,17 +2174,32 @@
 %5D%22, 
-a
+order.getA
 ppId
+(
 ))
+)
 ;%0A
+    
     
@@ -2196,32 +2196,70 @@
 ;%0A            %7D%0A
+            apps.add(app);%0A        %7D%0A%0A
         return a
@@ -2260,16 +2260,17 @@
 turn app
+s
 ;%0A    %7D%0A

重构成树结构：触发客户端刷新