package com.ruoyi.scientific.listener;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.flowable.common.constant.ProcessConstants;
import com.ruoyi.flowable.common.enums.ProcessStatus;
import com.ruoyi.scientific.service.IProjectApplicationService;
import com.ruoyi.scientific.service.IProjectHandbookService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.flowable.engine.HistoryService;
import com.ruoyi.scientific.domain.bo.ProjectHandbookBo;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.scientific.domain.vo.ProjectHandbookVo;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.Map;
import java.util.HashMap;

//@Component
//public class HandbookUpdateListener implements ExecutionListener {
//
//    @Override
//    public void notify(DelegateExecution execution) {
//        System.out.println("当前活动名称: " + execution.getProcessInstanceBusinessStatus());
//
//        if ("end".equals(execution.getEventName())) {
//            if (execution.isEnded()) {
//                System.out.println("任务被终止: " + execution.getId());
//            } else {
//                System.out.println("任务正常完成: " + execution.getId());
//            }
//        }
//    }
//}

@Component
public class HandbookUpdateListener implements ExecutionListener {
    /**
     * 注入字段（名称与流程设计时字段名称一致）
     */

    private final IProjectHandbookService iProjectHandbookService =
        SpringUtils.getBean(IProjectHandbookService.class);
    private final HistoryService historyService =
        SpringUtils.getBean(HistoryService.class);

    private final RuntimeService runtimeService =
        SpringUtils.getBean(RuntimeService.class);

    @Override
    public void notify(DelegateExecution execution) {
        System.out.println("科研模块：执行指南更新任务监听器...");

        String handbookProcId = execution.getProcessInstanceId();
        ProjectHandbookBo bo = new ProjectHandbookBo();
        bo.setHandbookProcId(handbookProcId);
        PageQuery pageQuery = new PageQuery();
        TableDataInfo<ProjectHandbookVo> result = iProjectHandbookService.queryPageList(bo, pageQuery);
        Long handbookId = result.getRows().get(0).getHandbookId();

        // zqjia:获取在task complete时注入的review状态
        String reviewStatus = (String) execution.getVariable("review");
        // 获取流程状态
        String status = (String) runtimeService.getVariable(handbookProcId, ProcessConstants.PROCESS_STATUS_KEY);

        // zqjia: runtimeService获取是否terminated，判断注入review字段可以区分是complete还是cancel
        // 退回有bug，暂时不知道会不会到这来
        if("terminated".equals(status)) {
            iProjectHandbookService.updateHandbookInfoById(handbookId, "2");
        }
        else {
            if("1".equals(reviewStatus)) {
                iProjectHandbookService.updateHandbookInfoById(handbookId, "3");
            }
            else {
                iProjectHandbookService.updateHandbookInfoById(handbookId, "0");
            }
        }
    }
}
