package com.seeyon.apps.src_flowIntegration.node;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.plugin.vo.ConfigVo;
import com.seeyon.apps.common.workflow.constants.WorkFlowType;
import com.seeyon.apps.common.workflow.node.ACommonSuperNode;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.SuperNodeContext;
import com.seeyon.apps.src_flowIntegration.constants.FlowIntegrationConstants;
import com.seeyon.apps.src_flowIntegration.dao.SrcFlowDao;
import com.seeyon.apps.src_flowIntegration.util.AesUtil;
import com.seeyon.apps.src_flowIntegration.util.SignUtil;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.utils.http.HttpClient;
import www.seeyon.com.utils.UUIDUtil;

import javax.inject.Inject;
import java.util.Date;
import java.util.HashMap;

public class FlowStatusCallbackNode extends ACommonSuperNode {

    private static Log log = Log.get(FlowStatusCallbackNode.class);

    protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");

    @Inject
    private SrcFlowDao srcFlowDao;

    @Override
    public String getNodeId() {
        return "FlowStatusCallbackNode";
    }

    @Override
    public String getNodeName() {
        return "通用流程状态返回超级节点";
    }

    @Override
    public String getPluginId() {
        return FlowIntegrationConstants.getPluginId();
    }

    @Override
    public String getFormParse() {
        return "json";
    }

    @Override
    public WorkFlowType[] getTypes() {
        return new WorkFlowType[] {WorkFlowType.superNode};
    }

    public ConfigVo getFlowIntegrationConfig() {
        return cstConfigApi.getConfig(getPluginId());
    }

    @Override
    public SuperNodeContext proceed(String request, FormDataVo formDataVo, FormDataMasterBean formDataMasterBean) throws Exception {
        ConfigVo configVo = getFlowIntegrationConfig();
        SuperNodeContext context = new SuperNodeContext();
        context.setNeedSave(true);
        JSONObject req = JSONObject.parseObject(request);
        String url = req.getString("flowStatusCallbackUrl");
        context.setUrl(url);

        JSONObject datajson = new JSONObject();
        datajson.put("thirdFlowId",req.getString("thirdFlowId"));
        JSONObject flowInfo = new JSONObject();
        flowInfo.put("flowStatus","1");
        flowInfo.put("flowStatusTxt","流程结束");
        datajson.put("flowInfo",flowInfo);
        JSONObject formInfo = new JSONObject();
//        封装流程表单信息
        datajson.put("formInfo",formInfo);
        context.setRequest(datajson.toString());
//        进行请求参数加密
        String thirdpartyCode = req.getString("thirdpartyCode");
//        通过第三方系统编号查询第三方系统参数信息
        String paramValue = srcFlowDao.getThirdpartyParamByThirdpartyCode(thirdpartyCode);
        JSONObject paramjson = JSONObject.parseObject(paramValue);
        String secretKey = paramjson.getString("secretKey");
        String vi = paramjson.getString("vi");
        String datastr = datajson.toString();
//        加密请求参数
        String encrypted = AesUtil.encrypt(datastr,secretKey,vi);
        long timestamp = new Date().getTime();
//        生成随机数
        String nonce = UUIDUtil.getUUIDLong()+"";
        String signContent = encrypted + timestamp + nonce + secretKey;
        String sign = SignUtil.sha256(signContent);
        JSONObject param = new JSONObject();
        param.put("data",encrypted);
        param.put("timestamp",timestamp+"");
        param.put("nonce",nonce);
        param.put("sign",sign);
//        调通接口推送数据到第三方系统
        String res = HttpClient.httpPostRaw(url,param.toString(),new HashMap<String,String>(), null);
        context.setResponse(res);
        JSONObject jsonObject = JSONObject.parseObject(res);
        String code = jsonObject.getString("code");
        if("0".equals(code)){
            context.success("流程状态回调成功！");
        }else{
            context.success("流程状态回调失败！");
        }
        return context;
    }

}
