package com.cpop.oam.framework.tasks;

import com.cpop.api.tencent.wxWork.handler.WebHookSendHandler;
import com.cpop.core.base.exception.ServiceException;
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.StaffService;
import com.cpop.oam.business.service.TaskService;
import com.cpop.oam.business.service.TaskWorkOrderService;
import com.cpop.oam.business.vo.StaffInfoVo;
import com.cpop.oam.framework.constant.WebHookKeyConstant;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

/**
 * 工单暂停回复任务
 * @author DB
 * @version 1.0.0
 * @since 2023-12-21 21:38
 */
public class WorkOrderPauseRecoverTask implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 获取工单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);
        Task task = taskService.getById(taskWorkOrder.getTaskId());
        if (task.getTaskStatus() == 5) {
            taskService.updateChain().set(TASK.TASK_STATUS, 2).where(TASK.ID.eq(taskWorkOrder.getTaskId())).update();
            // 发送消息通知
            List<String> phoneList = new ArrayList<String>();
            StaffInfoVo responsibleStaff = SpringUtils.getBean(StaffService.class).getStaffInfo(task.getResponsibleStaffId());
            StaffInfoVo recordStaff = SpringUtils.getBean(StaffService.class).getStaffInfo(task.getRecordStaffId());
            phoneList.add(responsibleStaff.getPhoneNumber());
            phoneList.add(recordStaff.getPhoneNumber());
            try {
                SpringUtils.getBean(WebHookSendHandler.class)
                        .webHookSendText(WebHookKeyConstant.ORDER_INFO_BOT,
                                phoneList,
                                "==========您有一条工单已经到期,请及时处理==========\n" + task.getTaskContent(),
                                false);
            } catch (IOException e) {
                throw new ServiceException("发送消息通知失败!");
            }
        }
    }
}
