package com.malk.chuizi.delegate;

import com.malk.chuizi.service.ChuiZiService;
import com.malk.delegate.DDEvent;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Personnel;
import com.malk.service.dingtalk.DDClient_Workflow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 {

    @Autowired
    ChuiZiService chuiZiService;

    @Autowired
    private DDClient ddClient;

    @Autowired
    private DDClient_Workflow ddClient_workflow;
    // 审批任务回调执行业务逻辑

    @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");
    }


    /// dingtalk
    final String APP_EKY = "ding9seotugcsfjlgy8h";
    final String APP_SECRET = "4k6hoCDUwwpUG5WXijfWcnzIm97j_zGquJqXJgrkBcyp2vo7ceV6dO-nSDUUDMhE";
    // 审批实例回调执行业务逻辑
    @Async
    @Override
    public void executeEvent_Instance_Finish(String processInstanceId, String processCode, boolean isAgree, boolean isTerminate, String staffId) {
        log.info("executeEvent_Instance_Finish:{}",processCode);
        String approveResult = isAgree ? "agree" : "refuse";
//        if (isTerminate) approveResult = "terminated";
        if  (isAgree){
            String token = ddClient.getAccessToken(APP_EKY, APP_SECRET);
            Map processData = ddClient_workflow.getProcessInstanceId(token, processInstanceId);
            log.info("开始执行回调:{}",processInstanceId);
            //差旅报销
            String TRAVEL_REIMBURSEMENT = "PROC-D362D97E-916B-4865-B932-13D4A3B4DF1C";
            //油卡充值
            String RECHARGE_FUEL_CARD = "PROC-65D446B1-EF01-4E6D-A66A-519B4C62A4D6";
            //付款申请
            String PAYMENT_APPLICATION = "PROC-82233BD3-A274-4B90-8710-97D9F9663634";

            if (PAYMENT_APPLICATION.equals(processCode)){
                chuiZiService.saveAccountsPayable(processData);
            }else if (TRAVEL_REIMBURSEMENT.equals(processCode)){
                chuiZiService.saveOtherMeet(processData);
            } else if (RECHARGE_FUEL_CARD.equals(processCode)) {
                chuiZiService.rechargeFuelCard(processData);
            }
        }
        log.info("审批实例回调执行业务逻辑, {}", approveResult);
    }
}
