package com.cpop.oam.framework.tasks;

import com.cpop.core.utils.SpringUtils;
import com.cpop.oam.business.entity.Task;
import com.cpop.oam.business.entity.TaskWorkOrder;
import com.cpop.oam.business.service.TaskService;
import com.cpop.oam.business.service.TaskWorkOrderService;
import com.cpop.oam.business.service.TaskStaffGroupService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

import java.time.LocalDateTime;

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

/**
 * @author DB
 * @Date: 2023/07/12/17:11
 * @Description: 工单任务超时检查
 */
public class WorkOrderOvertimeTask implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        // 获取工单id
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String workOrderId = jobDataMap.getString("WorkOrderId");
        TaskWorkOrderService taskWorkOrderService = SpringUtils.getBean(TaskWorkOrderService.class);
        TaskWorkOrder taskWorkOrder = taskWorkOrderService.getById(workOrderId);
        // 修改任务逾期
        TaskService taskService = SpringUtils.getBean(TaskService.class);
        taskService.updateChain().set(TASK.TASK_STATUS, 4).where(TASK.ID.eq(taskWorkOrder.getTaskId())).update();
        Task task = taskService.getById(taskWorkOrder.getTaskId());
        // 扣除5点绩点
        SpringUtils.getBean(TaskStaffGroupService.class).updateChain()
                .setRaw(TASK_STAFF_GROUP.GRADE_POINT, "grade_point - 5")
                .setRaw(TASK_STAFF_GROUP.REMARK, "concat(IFNULL(remark,''),'工单完结超时扣除5绩点')")
                .where(TASK_STAFF_GROUP.TASK_ID.eq(task.getId()))
                .and(TASK_STAFF_GROUP.STAFF_ID.eq(task.getResponsibleStaffId()))
                .update();
    }
}
