package cn.reghao.bnt.web.devops.app.service.bd.impl;

import cn.reghao.bnt.web.admin.service.NotifyService;
import cn.reghao.bnt.web.devops.app.db.query.AppBuildQuery;
import cn.reghao.bnt.web.devops.app.model.po.AppBuilding;
import cn.reghao.bnt.web.devops.app.model.po.log.BuildLog;
import cn.reghao.bnt.web.devops.app.model.po.log.DeployLog;
import cn.reghao.bnt.web.devops.app.model.vo.msg.BuildNotifyMsg;
import cn.reghao.bnt.web.devops.app.model.vo.msg.CdnRereshNotifyMsg;
import cn.reghao.bnt.web.devops.app.model.vo.msg.DeployNotifyMsg;
import cn.reghao.bnt.web.devops.app.service.bd.BuildDeployNotify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2023-02-24 21:17:52
 */
@Slf4j
@Service
public class BuildDeployNotifyImpl implements BuildDeployNotify {
    private final AppBuildQuery appBuildQuery;
    private final NotifyService notifyService;

    public BuildDeployNotifyImpl(AppBuildQuery appBuildQuery, NotifyService notifyService) {
        this.appBuildQuery = appBuildQuery;
        this.notifyService = notifyService;
    }

    @Override
    public void buildNotify(AppBuilding appBuilding) {
        BuildNotifyMsg buildNotifyMsg = new BuildNotifyMsg(appBuilding);
        notifyService.notify(buildNotifyMsg.dingMsg());
    }

    @Override
    public void deployNotify(DeployLog deployLog) {
        String buildLogId = deployLog.getBuildLog().getBuildLogId();
        BuildLog buildLog = appBuildQuery.getBuildLog(buildLogId);
        DeployNotifyMsg deployNotifyMsg = new DeployNotifyMsg(buildLog, deployLog);
        notifyService.notify(deployNotifyMsg.dingMsg());
    }

    @Override
    public void refreshNotify(CdnRereshNotifyMsg cdnRereshNotifyMsg) {
        notifyService.notify(cdnRereshNotifyMsg.dingMsg());
    }
}
