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

/*
 * 修订记录:
 * @author 钟勋 2019-08-29 22:48 创建
 */
package org.antframework.configcenter.biz.service;

import lombok.AllArgsConstructor;
import org.antframework.common.util.facade.BizException;
import org.antframework.common.util.facade.CommonResultCode;
import org.antframework.common.util.facade.Status;
import org.antframework.configcenter.biz.util.Branches;
import org.antframework.configcenter.dal.dao.BranchDao;
import org.antframework.configcenter.dal.dao.MergenceDao;
import org.antframework.configcenter.dal.entity.Branch;
import org.antframework.configcenter.dal.entity.Mergence;
import org.antframework.configcenter.facade.info.ReleaseInfo;
import org.antframework.configcenter.facade.order.MergeBranchOrder;
import org.antframework.configcenter.facade.result.MergeBranchResult;
import org.bekit.service.annotation.service.Service;
import org.bekit.service.annotation.service.ServiceExecute;
import org.bekit.service.engine.ServiceContext;
import org.springframework.beans.BeanUtils;

/**
 * 合并分支服务
 */
@Service(enableTx = true)
@AllArgsConstructor
public class MergeBranchService {
    // 分支dao
    private final BranchDao branchDao;
    // 合并dao
    private final MergenceDao mergenceDao;

    @ServiceExecute
    public void execute(ServiceContext<MergeBranchOrder, MergeBranchResult> context) {
        MergeBranchOrder order = context.getOrder();
        // 校验
        Branch branch = branchDao.findLockByAppIdAndProfileIdAndBranchId(order.getAppId(), order.getProfileId(), order.getBranchId());
        if (branch == null) {
            throw new BizException(Status.FAIL, CommonResultCode.INVALID_PARAMETER.getCode(), String.format("分支[appId=%s,profileId=%s,branchId=%s]不存在", order.getAppId(), order.getProfileId(), order.getBranchId()));
        }
        Branch sourceBranch = branchDao.findLockByAppIdAndProfileIdAndBranchId(order.getAppId(), order.getProfileId(), order.getSourceBranchId());
        if (sourceBranch == null) {
            throw new BizException(Status.FAIL, CommonResultCode.INVALID_PARAMETER.getCode(), String.format("源分支[appId=%s,profileId=%s,branchId=%s]不存在", order.getAppId(), order.getProfileId(), order.getSourceBranchId()));
        }
        // 计算分支合并的配置变更
        Branches.ReleaseDifference difference = Branches.computeBranchMergence(
                order.getAppId(),
                order.getProfileId(),
                order.getBranchId(),
                order.getSourceBranchId());
        // 发布变更
        ReleaseInfo release = Branches.releaseBranch(
                order.getAppId(),
                order.getProfileId(),
                order.getBranchId(),
                difference.getAddOrModifiedProperties(),
                difference.getRemovedPropertyKeys(),
                String.format("merge from branch[%s] with version[%d]", order.getSourceBranchId(), sourceBranch.getReleaseVersion()))
                .getRelease();
        // 保存合并
        Mergence mergence = buildMergence(order, release.getVersion(), sourceBranch.getReleaseVersion());
        mergenceDao.save(mergence);
    }

    // 构建合并
    private Mergence buildMergence(MergeBranchOrder order, long releaseVersion, Long sourceReleaseVersion) {
        Mergence mergence = new Mergence();
        BeanUtils.copyProperties(order, mergence);
        mergence.setReleaseVersion(releaseVersion);
        mergence.setSourceReleaseVersion(sourceReleaseVersion);

        return mergence;
    }
}

@@ -402,16 +402,73 @@
 anches;%0A
+import org.antframework.configcenter.biz.util.Refreshes;%0A
 import o
@@ -936,24 +936,82 @@
 ce.Service;%0A
+import org.bekit.service.annotation.service.ServiceAfter;%0A
 import org.b
@@ -2940,16 +2940,24 @@
 s%5D with 
+release 
 version%5B
@@ -3583,11 +3583,265 @@
 e;%0A    %7D
+%0A%0A    @ServiceAfter%0A    public void after(ServiceContext%3CMergeBranchOrder, MergeBranchResult%3E context) %7B%0A        MergeBranchOrder order = context.getOrder();%0A        // %E5%88%B7%E6%96%B0%E5%AE%A2%E6%88%B7%E7%AB%AF%0A        Refreshes.refreshClients(order.getAppId(), order.getProfileId());%0A    %7D
 %0A%7D%0A

合并分支后，通知客户端刷新配置