package com.cpop.oam.framework.tasks;

import com.cpop.common.utils.http.HttpHelper;
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.TaskStaffGroupService;
import com.cpop.oam.business.service.TaskWorkOrderService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

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

import com.cpop.oam.business.service.TaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author DB
 */
public class WorkOrderAcceptOverTimeTask 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 workOrder = taskWorkOrderService.getById(workOrderId);
        // 获取任务
        TaskService taskService = SpringUtils.getBean(TaskService.class);
        Task task = taskService.getById(workOrder.getTaskId());
        if (task != null) {
            // 更新任务为接收超时
            taskService.updateChain()
                    .set(TASK.TASK_STATUS, 7)
                    .where(TASK.ID.eq(task.getId()))
                    .update();
            // 工单超时为接收，扣除5绩点
            SpringUtils.getBean(TaskStaffGroupService.class).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();
        }
    }
}
