package com.malk.cloudpure.delegate;

import com.malk.delegate.DDEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * OA审批事件 [主项目也有实现, 添加 @Primary 优先注入主项目实现]
 * -
 * 取消方案: 撤销和拒绝流程不继续执行连接器, 因此不使用连接器与轮询审批记录方案 [低效而且占用较高钉钉api调次数];
 * 优化方案: 通过事件订阅实现实时同步所有审批状态, 定时查询钉钉回调失败记录 [配置钉钉事件Delegate, 添加定时任务]
 */
@Slf4j
@Service
@Primary
public class DDDelegate implements DDEvent {

    // 审批任务回调执行业务逻辑
    @Async
    @Override
    public void executeEvent_Task_Finish(String processInstanceId, String processCode, boolean isAgree, String remark) {
        log.info("executeEvent_Task_Finish");
    }

    @Async
    @Override
    public void executeEvent_Task_Start(String processInstanceId, String processCode) {
        log.info("executeEvent_Task_Start");
    }

    @Async
    @Override
    public void executeEvent_Task_Redirect(String processInstanceId, String processCode) {
        log.info("executeEvent_Task_Redirect");
    }

    @Async
    @Override
    public void executeEvent_Instance_Start(String processInstanceId, String processCode) {
        log.info("executeEvent_Instance_Start");
    }


    // 审批实例回调执行业务逻辑
    @Async
    @Override
    public void executeEvent_Instance_Finish(String processInstanceId, String processCode, boolean isAgree, boolean isTerminate, String staffId) {
        log.info("executeEvent_Instance_Finish");
        String approveResult = isAgree ? "agree" : "refuse";
        if (isTerminate) approveResult = "terminated";
        log.info("审批实例回调执行业务逻辑, {}", approveResult);
    }
    
    // 考勤打卡事件回调
    @Override
    public void executeEvent_attendance_check(Map<String, ?> record) {
        log.info("executeEvent_attendance_check");
    }
}
