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.*;
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;
import static com.cpop.oam.business.entity.table.TaskStaffGroupTableDef.TASK_STAFF_GROUP;
import static com.cpop.oam.business.entity.table.TaskTableDef.TASK;

/**
 * @author DB
 */
public class WorkOrderAcceptOverTimeRemindTask 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);
        //获取工单任务创建日期的值班人员
        Duty duty = SpringUtils.getBean(DutyService.class).queryChain()
                .where(DUTY.DUTY_DATE.eq(workOrder.getCreateTime().toLocalDate()))
                .one();
        List<String> phoneList = new ArrayList<>();
        if (duty != null){
            // 获取任务
            TaskService taskService = SpringUtils.getBean(TaskService.class);
            Task task = taskService.getById(workOrder.getTaskId());
            StaffService staffService = SpringUtils.getBean(StaffService.class);
            StaffInfoVo responsibleStaff = staffService.getStaffInfo(duty.getTechnologyStaffId());
            phoneList.add(responsibleStaff.getPhoneNumber());
            try {
                SpringUtils.getBean(WebHookSendHandler.class).webHookSendText(WebHookKeyConstant.ORDER_INFO_BOT, phoneList,
                        task.getTaskContent() + "\n您有一个工单即将接收超时,请尽快接受",
                        false);
            } catch (IOException e) {
                throw new ServiceException("发送工单接受超时消息通知失败!");
            }
        }
    }
}
