package com.seeyon.apps.src_flowIntegration.event;

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.event.ACommonWorkflowEvent;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.WorkflowEventContext;
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 FlowStatusCallbackEvent extends ACommonWorkflowEvent {

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

    @Inject
    private SrcFlowDao srcFlowDao;

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

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

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

    @Override
    public String getId() {
        return "FlowStatusCallbackEvent";
    }

    @Override
    public String getLabel() {
        return "通用流程状态返回终止前监听";
    }


    @Override
    protected WorkflowEventContext proceed(String request, FormDataVo formDataVo, WorkFlowType workFlowType, FormDataMasterBean formDataMasterBean) throws Exception {
        WorkflowEventContext context = new WorkflowEventContext();
        ConfigVo configVo = cstConfigApi.getConfig(getPluginId());
        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","2");
        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.setSuccess(true);
            context.setErrMsg("终止成功，第三方流程已回退");
        }else{
            context.setSuccess(true);
            context.setErrMsg("终止失败");
        }

        return context;
    }





}
