package com.jeeplus.centerservice.service.ccpm.flow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jeeplus.centerservice.utils.ConvertServiceUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class FlowService {

    @Resource
    private FlowRequest flowRequest;

    /**
     * 获取流程历史
     * @param procInsId
     * @return
     */
    public List<Map<String, Object>> getHistoicFlowList(String procInsId) {
        List<Map<String, Object>> histoicFlowList = new ArrayList<>();
        Object histoicFlow = flowRequest.getHistoicFlowList(procInsId);
        if (Objects.nonNull(histoicFlow)) {
            List<Map<String, Object>> histoicFlows = (List)JSONArray.parseArray(JSON.toJSONString(histoicFlow));
            histoicFlowList.addAll(ConvertServiceUtil.convertHisFlowList(histoicFlows));
        }
        return histoicFlowList;
    }

    /**
     * 获取流程流向图
     * @param procInsId
     * @return
     */
    public List<Map<String, Object>> getFlowChart(String procInsId) {
        List<Map<String, Object>> flowCharts = new ArrayList<>();
        Object flowChart = flowRequest.getFlowChart(procInsId);
        if (Objects.nonNull(flowChart)) {
            flowCharts.addAll((List)JSONArray.parseArray(JSON.toJSONString(flowChart)));
        }
        return flowCharts;
    }
}
