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.Duty;
import com.cpop.oam.business.entity.Task;
import com.cpop.oam.business.entity.TaskWorkOrder;
import com.cpop.oam.business.service.DutyService;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.cpop.oam.business.entity.table.DutyTableDef.DUTY;

/**
 * 工单完结超时提醒任务
 * @author DB
 */
public class WorkOrderFinishOverTimeRemindTask 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());
        List<String> phoneList = new ArrayList<>();
        if (task != null){
            StaffService staffService = SpringUtils.getBean(StaffService.class);
            StaffInfoVo responsibleStaff = staffService.getStaffInfo(task.getResponsibleStaffId());
            StaffInfoVo recordStaff = staffService.getStaffInfo(task.getRecordStaffId());
            phoneList.add(responsibleStaff.getPhoneNumber());
            phoneList.add(recordStaff.getPhoneNumber());
            try {
                SpringUtils.getBean(WebHookSendHandler.class).webHookSendText(WebHookKeyConstant.ORDER_INFO_BOT, phoneList,
                        task.getTaskContent() + "\n您有一个工单即将完结超时,请尽快联系相关人员办结",
                        false);
            } catch (IOException e) {
                throw new ServiceException("发送工单完结超时消息通知失败!");
            }
        }
    }
}
