TaskRunner.java 2.66 KB
package vion.cron;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.date.TimeUtil;
import org.dromara.hutool.core.lang.Opt;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import vion.model.Task;
import vion.model.User;
import vion.service.ITaskService;
import vion.service.IUserService;
import vion.third.DingMod;
import vion.utils.JsonUtil;

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

/**
 * @author vion
 * @date 2023/11/13
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class TaskRunner {

    private final IUserService userService;
    private final ITaskService taskService;
    private final DingMod dingMod;

    @Scheduled(cron = "0 0 17 * * ?")
    void pushUndoneTask() {
        log.info("开始推送未完成工单");
        var taskList = taskService.list(Wrappers.<Task>lambdaQuery().notIn(Task::getStatus, 3, 5));
        Opt.ofEmptyAble(taskList)
                .map(l -> l.stream().map(Task::getActiveUser).collect(Collectors.toSet()))
                .ifPresent(userIds -> {
                    var userList = userService.list(Wrappers.<User>lambdaQuery().in(User::getId, userIds));
                    userList.forEach(user -> dingMod.workMsg(buildMsg(user)));
                });
        log.info("推送未完成工单完成");
    }

    String buildMsg(User user) {
        var jsonObj = JsonUtil.createObj()
                .put("agent_id", 2358374016L)
                .put("userid_list", user.getUserid());

        var content = JsonUtil.createObj()
                .put("title", "请查看您未完成的工单~_~")
                .put("markdown", """
                        ### 未完成工单提醒
                        #### 请查看您未完成的工单
                        #### 发送时间:""" + TimeUtil.formatNormal(LocalDateTime.now()))
                .put("btn_orientation", "1");

        var jsonArray = JsonUtil.createArr()
                .add(JsonUtil.createObj().put("title", "查看详情")
                        .put("action_url", "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid" +
                                "=dingkrzwks0jpi2di3uo&response_type=code&scope=snsapi_auth&state=STATE&redirect_uri=https%3A%2F%2Fyunwei.vionyun.com%3A8443%2Fyunwei%2Fapi%2Fding%2Fcallback%2Finside%3FuserId%3D" + user.getId()));
        content.set("btn_json_list", jsonArray);

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