package com.seeyon.apps.src_flowIntegration.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_flowIntegration.dao.SrcFlowDao;
import com.seeyon.apps.src_flowIntegration.util.FieldUtil;
import www.seeyon.com.utils.StringUtil;

import javax.inject.Inject;

public class ThirdSysBizMapService {

    @Inject
    private SrcFlowDao srcFlowDao;

    public String getFormIdByTemplateCode(String templateCode){
        String formId = srcFlowDao.getFormIdByTemplateCode(templateCode);
        return formId;
    }

//    根据表单编号获取表单rightId
    public String getRightIdByTemplateCode(String templateCode){
//        根据templateCode查询表单rightId和固定字段信息,rightId格式为viewId.authId
//        首先通过templateCode获取表单formId
        String formId = getFormIdByTemplateCode(templateCode);
//        通过formId获取viewId
        String viewId = srcFlowDao.getViewIdByFormId(formId);
//        通过formId获取authId
        String authId = srcFlowDao.getAuthIdByFormId(formId);
        String rightId = viewId+"."+authId;
        return rightId;
    }

    /**
     * 根据流程编号，套用字段名称，设置第三方系统字段
     * @param data
     * @param templateCode
     * @param tirdFlowId
     * @param flowStatusCallbackUrl
     * @param flowAuditStatusCallbackUrl
     */
    public void setTableData(JSONObject data,String templateCode,String tirdFlowId,String thirdpartyCode,String flowStatusCallbackUrl,String flowAuditStatusCallbackUrl,JSONObject relatedLinks){
//        首先根据templateCode获取表单formId;
        String formId = getFormIdByTemplateCode(templateCode);
//        通过formId信息查询表单中的字段，并且找到字段信息为第三方信息的字段
        String fieldInfo = srcFlowDao.getFieldInfoByFormId(formId);
        JSONObject fieldjson = JSONObject.parseObject(fieldInfo);
//        通过表单字段查询固定的第三方回传字段，获取表单字段信息
        JSONObject frontFormmain = fieldjson.getJSONObject("front_formmain");
        JSONObject masterTable = data.getJSONObject("masterTable");
        JSONObject record = masterTable.getJSONObject("record");
        JSONArray fields = record.getJSONArray("fields");

//        插入第三方系统流程ID字段
        String tirdFlowIdName = FieldUtil.isDisplayExists(frontFormmain,"第三方系统流程ID");
        if(StringUtil.isNotEmpty(tirdFlowIdName)){
            JSONObject tirdFlowIdjson = new JSONObject();
            tirdFlowIdjson.put("name",tirdFlowIdName);
            tirdFlowIdjson.put("value",tirdFlowId);
            fields.add(tirdFlowIdjson);
        }
        String thirdpartyCodeName = FieldUtil.isDisplayExists(frontFormmain,"第三方系统标识");
        if(StringUtil.isNotEmpty(thirdpartyCodeName)){
            JSONObject thirdpartyCodejson = new JSONObject();
            thirdpartyCodejson.put("name",thirdpartyCodeName);
            thirdpartyCodejson.put("value",thirdpartyCode);
            fields.add(thirdpartyCodejson);
        }
//        插入流程状态回调地址字段
        String flowStatusCallbackUrlName = FieldUtil.isDisplayExists(frontFormmain,"流程状态回调地址");
        if(StringUtil.isNotEmpty(flowStatusCallbackUrlName)){
            JSONObject flowStatusCallbackUrljson = new JSONObject();
            flowStatusCallbackUrljson.put("name",flowStatusCallbackUrlName);
            flowStatusCallbackUrljson.put("value",flowStatusCallbackUrl);
            fields.add(flowStatusCallbackUrljson);
        }
//        插入流程审核状态回调地址字段
        String flowAuditStatusCallbackUrlName = FieldUtil.isDisplayExists(frontFormmain,"流程审核状态回调地址");
        if(StringUtil.isNotEmpty(flowStatusCallbackUrlName)){
            JSONObject flowAuditStatusCallbackUrljson = new JSONObject();
            flowAuditStatusCallbackUrljson.put("name",flowAuditStatusCallbackUrlName);
            flowAuditStatusCallbackUrljson.put("value",flowAuditStatusCallbackUrl);
            fields.add(flowAuditStatusCallbackUrljson);
        }
//        设置相关链接明细表信息
        JSONArray formsons = fieldjson.getJSONArray("formsons");
//        遍历所有明细表信息,查询明细表是否为相关链接
        String relatedLinkSubTableName = "";
        String linkNameFieldName ="" ,linkFieldName = "";
        for(int i = 0 ; i < formsons.size() ; i ++){
            JSONObject formson = formsons.getJSONObject(i);
            if("相关链接".equals(formson.getString("display"))){
                relatedLinkSubTableName = formson.getString("tableName");
                JSONArray subFieldInfos = formson.getJSONArray("fieldInfo");
                for(int n = 0 ; n < subFieldInfos.size() ; n++){
                    JSONObject subFieldInfo = subFieldInfos.getJSONObject(n);
                    if("跳转链接描述".equals(subFieldInfo.getString("display"))){
                        linkNameFieldName = subFieldInfo.getString("name");
                    }else if("跳转链接".equals(subFieldInfo.getString("display"))){
                        linkFieldName = subFieldInfo.getString("name");
                    }
                }
            }
        }
//        遍历第三方传递的相关链接信息，将相关链接信息按照指定字段保存到data对象中。
//        创建相关链接明细表jsonObject对象信息
        JSONObject relatedLinkFieldJson = new JSONObject();
        relatedLinkFieldJson.put("name",relatedLinkSubTableName);
        JSONArray relatedLinkRecords = new JSONArray();
        for (String key : relatedLinks.keySet()) {
            JSONObject relatedLinkRecord = new JSONObject();
            JSONArray relatedLinkFields = new JSONArray();
//            描述字段赋值
            JSONObject describeField = new JSONObject();
            describeField.put("name",linkNameFieldName);
            describeField.put("value",key);
            relatedLinkFields.add(describeField);
//            链接字段赋值
            JSONObject linkField = new JSONObject();
            linkField.put("name",linkFieldName);
            linkField.put("value",relatedLinks.getString(key));
            relatedLinkFields.add(linkField);
            relatedLinkRecord.put("fields",relatedLinkFields);
            relatedLinkRecords.add(relatedLinkRecord);
        }
        relatedLinkFieldJson.put("records",relatedLinkRecords);
//        设置明细表字段，保存link信息
        JSONArray subTables = data.getJSONArray("subTables");
        subTables.add(relatedLinkFieldJson);
    }

    public String uniqueDataValidation(String tirdFlowId,String templateCode){
//        通过流程编码查询表单ID
        String formId = getFormIdByTemplateCode(templateCode);
//        通过表单ID查询字段数据
        String fieldInfo = srcFlowDao.getFieldInfoByFormId(formId);
        JSONObject fieldjson = JSONObject.parseObject(fieldInfo);
//        查询主表相关数据
        JSONObject frontFormmain = fieldjson.getJSONObject("front_formmain");
//        获取主表名称
        String tableName = frontFormmain.getString("tableName");
//        获取指定字段名称的数据库字段名称
        String tirdFlowIdName = FieldUtil.isDisplayExists(frontFormmain,"第三方系统流程ID");
//        通过表单名称，唯一值字段，当前数据，查询当前数据是否在当前表单中是否存在
        String uniqueData = srcFlowDao.uniqueDataValidation(tableName,tirdFlowIdName,tirdFlowId);
        return uniqueData;
    }

}
