LogPushRunner.java 3.56 KB
package vion.cron;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.date.TimeUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.cron.CronUtil;
import org.dromara.hutool.cron.task.Task;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import vion.constant.RedisKeyEnum;
import vion.model.RStoreUser;
import vion.model.Store;
import vion.model.User;
import vion.service.IRStoreUserService;
import vion.service.IStoreService;
import vion.third.DingMod;
import vion.utils.JsonUtil;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HlQ
 * @date 2024/4/23
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class LogPushRunner {

    private final IStoreService storeService;
    private final IRStoreUserService storeUserService;
    private final DingMod dingMod;
    private final RedissonClient redissonClient;

    @Bean
    public void scheduledPush() {
        var storeList = storeService.lambdaQuery().isNotNull(Store::getCronStr).list();
        Opt.ofEmptyAble(storeList)
                .ifPresent(list -> {
                    var idList = list.stream().map(Store::getId).toList();
                    var storeUserMap = storeUserService.lambdaQuery()
                            .in(RStoreUser::getStoreId, idList).list()
                            .stream()
                            .filter(v -> v.getIsMain().equals(1))
                            .collect(Collectors.toMap(RStoreUser::getStoreId, RStoreUser::getUserId));

                    for (Store store : list) {
                        var task = getTask(store, storeUserMap);
                        CronUtil.schedule(store.getCronStr(), task);
                    }
                    if (!CronUtil.getScheduler().isStarted()) {
                        CronUtil.setMatchSecond(true);
                        CronUtil.start();
                    }
                });
    }

    public Task getTask(Store store, Map<Long, Long> storeUserMap) {
        return () -> {
            var userId = storeUserMap.get(store.getId());
            if (ObjUtil.isNull(userId)) {
                log.info("storeId:{},该项目未设置项目经理,不发送日志提醒", store.getId());
                return;
            }
            var user = (User) redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + userId).get();
            dingMod.workMsg(buildMsg(store.getName(), user.getUserid()));
            log.info("storeId:{},项目发送日志提醒,提醒人:{}({})", store.getId(), user.getUsername(), user.getUserid());
        };
    }

    String buildMsg(String storeName, String userid) {
        var jsonObj = JsonUtil.createObj()
                .put("agent_id", 2358374016L)
                .put("userid_list", userid);

        var content = JsonUtil.createObj()
                .put("title", "项目日志待提交提醒")
                .put("text", StrUtil.format("""
                        ### 项目日志待提交提醒
                        #### 项目:[{}]请及时提交日志
                        #### 发送时间:{}""", storeName, TimeUtil.formatNormal(LocalDateTime.now())));

        var msg = JsonUtil.createObj()
                .put("msgtype", "markdown")
                .set("markdown", content);
        jsonObj.set("msg", msg);
        return jsonObj.toString();
    }
}