package com.seeyon.apps.src_flowIntegration.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.exceptions.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import www.seeyon.com.utils.StringUtil;

import java.util.Map;

public class FlowIntegrationService {

    @Autowired
    private ThirdSysBizMapService thirdSysBizMapService;
    @Autowired
    private OaFlowCreateService oaFlowCreateService;
    @Autowired
    private OaFlowQueryService oaFlowQueryService;

    public JSONObject thirdFlowCreate(JSONObject jsonObject,String token) throws Exception {
        JSONObject res = new JSONObject();
        String tirdFlowId = jsonObject.getString("thirdFlowId");
        String thirdpartyCode = jsonObject.getString("thirdpartyCode");
        String flowStatusCallbackUrl = jsonObject.getString("flowStatusCallbackUrl");
        String flowAuditStatusCallbackUrl = jsonObject.getString("flowAuditStatusCallbackUrl");
        JSONObject relatedLinks = jsonObject.getJSONObject("relatedLinks");
        JSONObject data = jsonObject.getJSONObject("data");
        JSONObject flowParam = new JSONObject();
//        设置流程生成参数
        flowParam.put("appName","collaboration");
        String templateCode = data.getString("templateCode");

        String uniqueDataValidation = thirdSysBizMapService.uniqueDataValidation(tirdFlowId,templateCode);
        if(StringUtil.isNotEmpty(uniqueDataValidation)){
            res.put("code","1");
            res.put("msg","当前第三方系统流程ID在当前表单中已经存在，提交失败。");
            return res;
        }
//        设置流程rightId字段信息
        String rightId = thirdSysBizMapService.getRightIdByTemplateCode(templateCode);
        data.put("rightId",rightId);
//        流程默认为直接发起
        data.put("draft","0");
//        流程默认使用新模板创建
        data.put("useNewDataStructure",true);
//        流程默认直接执行触发
        data.put("doTrigger",true);
//        设置第三方信息字段内容
        JSONObject datajson = data.getJSONObject("data");
        thirdSysBizMapService.setTableData(datajson,templateCode,tirdFlowId,thirdpartyCode,flowStatusCallbackUrl,flowAuditStatusCallbackUrl,relatedLinks);
        flowParam.put("data",data);

        Map<String,Object> result = (Map<String,Object>) oaFlowCreateService.flowStart(tirdFlowId,flowParam,token);
        res.put("code","0");
        res.put("oaFlowId",(String)result.get("processId"));
        return res;
    }

    public JSONObject thirdFlowQuery(JSONObject jsonObject){
        JSONObject res = new JSONObject();
        String thirdpartyCode = jsonObject.getString("thirdpartyCode");//第三方系统标识
        String templateCode = jsonObject.getString("templateCode");//流程表单编码
        String thirdFlowId = jsonObject.getString("thirdFlowId");//第三方系统流程表单ID
            try {
//            查询summaryId
            Map<String,String> colSummary = oaFlowQueryService.getColSummaryData(thirdpartyCode,templateCode,thirdFlowId);
//            通过当前表单编码字段查询待办信息
            JSONArray todoList = oaFlowQueryService.getTodoList(String.valueOf (colSummary.get("id")));
//            设置当前待办信息
            res.put("todoList",todoList);
            JSONArray completedList = oaFlowQueryService.getCompletedList(String.valueOf (colSummary.get("process_id")));
//            设置当前已经执行流程记录
            res.put("completedList",completedList);
            JSONObject flowInfo = oaFlowQueryService.getFlowInfo(String.valueOf (colSummary.get("id")));
//            设置当前流程状态信息
            res.put("flowInfo",flowInfo);
            JSONObject formInfo = oaFlowQueryService.getFormInfo(colSummary);
//            设置流程表单数据
            res.put("formInfo",formInfo);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        return res;
    }




}
