package com.ruoyi.flowable.oa;

import com.ruoyi.common.utils.SnowFlake;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.oa.domain.CmcAssess;
import com.ruoyi.oa.domain.CmcAssessContent;
import com.ruoyi.oa.domain.CmcAssessContentWork;
import com.ruoyi.oa.mapper.CmcAssessContentMapper;
import com.ruoyi.oa.mapper.CmcAssessContentWorkMapper;
import com.ruoyi.oa.mapper.CmcAssessMapper;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.engine.task.Comment;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyServiceTask implements JavaDelegate {
    public List<String> fieldList = Arrays.asList("role", "familiar", "remark");

    @Override
    public void execute(DelegateExecution execution) {
        CmcAssessMapper cmcAssessMapper = SpringUtils.getBean(CmcAssessMapper.class);
        CmcAssessContentMapper cmcAssessContentMapper = SpringUtils.getBean(CmcAssessContentMapper.class);
        CmcAssessContentWorkMapper cmcAssessContentWorkMapper = SpringUtils.getBean(CmcAssessContentWorkMapper.class);
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String,Object> variables = runtimeService.getVariables(execution.getId());
        String processInstance = execution.getProcessInstanceId();
        CmcAssess cmcAssess = new CmcAssess();
        SnowFlake snowFlake = new SnowFlake();
//        cmcAssess = cmcAssessMapper.selectCmcAssessByAssessId("0");
        cmcAssess.setAssessId(snowFlake.generateId());
        cmcAssessMapper.insertCmcAssess(cmcAssess);
        for(String key : variables.keySet()){
            if (removeDigital(key).equals("role") && !variables.get(key).toString().equals("")) {
                CmcAssessContent cmcAssessContent = new CmcAssessContent();
                cmcAssessContent.setContentId(cmcAssess.getAssessId() + key.substring(4));
                cmcAssessContentMapper.insertCmcAssessContent(cmcAssessContent);
            }
        }
        for(String key : variables.keySet()){
            if (key.length() == 5 && key.startsWith("f") && !variables.get(key).toString().equals("")) {
                CmcAssessContentWork cmcAssessContentWork = new CmcAssessContentWork();
                cmcAssessContentWork.setWorkId(cmcAssess.getAssessId() + key.substring(1));
                cmcAssessContentWork.setWorkLoad(variables.get(key).toString());
                cmcAssessContentWorkMapper.insertCmcAssessContentWork(cmcAssessContentWork);
            }
            if (key.equals("INITIATOR"))
                cmcAssess.setUserId((Long) variables.get(key));
            if (removeDigital(key).equals(fieldList.get(0))) {
                CmcAssessContent cmcAssessContent = cmcAssessContentMapper.selectCmcAssessContentByContentId(cmcAssess.getAssessId() + key.substring(4)).get(0);
                if (cmcAssessContent != null) {
                    cmcAssessContent.setRole(variables.get(key).toString());
                    cmcAssessContentMapper.updateCmcAssessContent(cmcAssessContent);
                }
            }
            if (removeDigital(key).equals(fieldList.get(1))) {
                CmcAssessContent cmcAssessContent = cmcAssessContentMapper.selectCmcAssessContentByContentId(cmcAssess.getAssessId() + key.substring(8)).get(0);
                if (cmcAssessContent != null) {
                    cmcAssessContent.setFamiliar(variables.get(key).toString());
                    cmcAssessContentMapper.updateCmcAssessContent(cmcAssessContent);
                }
            }
            if (removeDigital(key).equals(fieldList.get(2))) {
                CmcAssessContent cmcAssessContent = cmcAssessContentMapper.selectCmcAssessContentByContentId(cmcAssess.getAssessId() + key.substring(6)).get(0);
                if (cmcAssessContent != null) {
                    cmcAssessContent.setRemark(variables.get(key).toString());
                    cmcAssessContentMapper.updateCmcAssessContent(cmcAssessContent);
                }
            }

        }
        for (Comment comment: processEngine.getTaskService().getProcessInstanceComments(processInstance))
            System.out.println(comment.getFullMessage());
        System.out.println("========MyServiceTask==========");
        System.out.println(variables);
        System.out.println(processInstance);
    }

    /**
     * 剔除数字
     * @param value
     */
    public String removeDigital(String value){
        Pattern p = Pattern.compile("[\\d]");
        Matcher matcher = p.matcher(value);
        return matcher.replaceAll("");
    }

}
