TaskRunner.java 2.37 KB
package vion.cron;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 java.util.List;

/**
 * @author HlQ
 * @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("开始推送未完成工单");
        List<Task> taskList = taskService.list(Wrappers.<Task>lambdaQuery().notIn(Task::getStatus, 3, 5));
        // todo 需要异步发送吗?
        taskList.forEach(task -> {
            Long activeUser = task.getActiveUser();
            User user = userService.lambdaQuery().eq(User::getId, activeUser).one();
            dingMod.sendMessage(buildMsg(user));
        });
        log.info("推送未完成工单完成");
    }

    JSONObject buildMsg(User user) {
        JSONObject jsonObj = new JSONObject();
        jsonObj.set("agent_id", 2358374016L);
        jsonObj.set("userid_list", user.getUserid());

        JSONObject msg = new JSONObject();
        JSONObject content = new JSONObject();
        content.set("title", "请查看您未完成的工单~_~");
        content.set("markdown", "#### 请查看您未完成的工单" +
                "  \n  #### 发送时间:" + DateUtil.now());
        content.set("btn_orientation", "1");

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(new JSONObject().set("title", "查看详情").set("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);

        msg.set("msgtype", "action_card");
        msg.set("action_card", content);
        jsonObj.set("msg", msg);
        return jsonObj;
    }
}