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

import cn.reghao.devops.web.mgr.app.db.repository.log.BuildLogRepository;
import cn.reghao.devops.web.mgr.app.model.po.AppBuilding;
import cn.reghao.devops.web.mgr.app.model.po.log.BuildLog;
import cn.reghao.devops.web.mgr.app.model.po.log.DeployLog;
import cn.reghao.devops.web.mgr.app.service.bd.BuildDeployNotify;
import cn.reghao.devops.web.mgr.app.model.vo.BuildNotifyMsg;
import cn.reghao.devops.web.mgr.app.model.vo.DeployNotifyMsg;
import cn.reghao.devops.web.admin.sys.service.NotifyService;
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 BuildLogRepository buildLogRepository;
    private final NotifyService notifyService;

    public BuildDeployNotifyImpl(BuildLogRepository buildLogRepository, NotifyService notifyService) {
        this.buildLogRepository = buildLogRepository;
        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 = buildLogRepository.findByBuildLogId(buildLogId);
        DeployNotifyMsg deployNotifyMsg = new DeployNotifyMsg(buildLog, deployLog);
        notifyService.notify(deployNotifyMsg.dingMsg());
    }
}
