package com.cpop.oam.web;

import com.cpop.core.utils.QuartzUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.oam.business.entity.Task;
import com.cpop.oam.business.service.TaskService;
import com.cpop.oam.business.service.TaskStaffGroupService;
import com.cpop.oam.framework.enums.QuartzEnums;
import org.junit.jupiter.api.Test;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;

import static com.cpop.oam.business.entity.table.TaskStaffGroupTableDef.TASK_STAFF_GROUP;

/**
 * @author DB
 * @version 1.0.0
 * @since 2023-12-07 11:55
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class CpopQrtzTest {

    @Autowired
    private QuartzUtils quartzUtils;

    /**
     * 定时任务删除
     * @author DB
     * @since 2023/12/07
     */
    @Test
    public void jobRemove() throws SchedulerException {
        quartzUtils.deleteJob("WorkOrderAcceptOvertimeTask:90214101954240512","WorkOrder");
    }

    @Test
    public void workOrderAcceptOverTimeTask() {
        // 获取任务
        TaskService taskService = SpringUtils.getBean(TaskService.class);
        Task task = taskService.getById("90263826384441344");
        // 工单超时为接收，扣除5绩点
        TaskStaffGroupService taskStaffGroupService = SpringUtils.getBean(TaskStaffGroupService.class);
        taskStaffGroupService.updateChain()
                .set(TASK_STAFF_GROUP.GRADE_POINT, -5)
                .set(TASK_STAFF_GROUP.REMARK, "工单接收超时扣除5绩点 ")
                .where(TASK_STAFF_GROUP.TASK_ID.eq(task.getId()))
                .and(TASK_STAFF_GROUP.STAFF_ID.eq(task.getResponsibleStaffId()))
                .update();
    }

    @Test
    public void workOrderOverTimeTask() {
        // 获取任务
        TaskService taskService = SpringUtils.getBean(TaskService.class);
        Task task = taskService.getById("90282358270763008");
        // 扣除5点绩点
        SpringUtils.getBean(TaskStaffGroupService.class).updateChain()
                .setRaw(TASK_STAFF_GROUP.GRADE_POINT, "grade_point - 5")
                .setRaw(TASK_STAFF_GROUP.REMARK, "concat(remark,'工单完结超时扣除5绩点')")
                .where(TASK_STAFF_GROUP.TASK_ID.eq(task.getId()))
                .and(TASK_STAFF_GROUP.STAFF_ID.eq(task.getResponsibleStaffId()))
                .update();
    }

    @Test
    public void getJobInfo() throws SchedulerException {
        LocalDateTime jobExecuteTime = quartzUtils.getJobExecuteTime(QuartzEnums.WORK_ORDER_OVERTIME_TASK.getName() + "118089232185462784", QuartzEnums.WORK_ORDER_OVERTIME_TASK.getGroup());
        System.out.println(jobExecuteTime);
    }

}
