package com.customization.htsc.cus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.util.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-06-13
 * @Description: 对接IT管理平台，IT需求管理平台流程，IT测试和上线申请流程，流程终止时向上游同步状态
 * @ModifyLog:
 **/
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交处理业务")
public class ITManagementPlatformCmd  extends AbstractCommandProxy<Map<String, Object>> {
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        JAVATools.writelog("------ITManagementPlatformCmd------");
        // 从request中可以取到参数，不用以前的方法了
        HttpServletRequest request = requestSubmitCmd.getRequest();
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String requestid = Util.null2String(request.getParameter("requestid"));
        JAVATools.writelog("requestid:"+requestid);
        // 终止：2
        String oaStatus = "2";
        String tablename = JAVATools.getMaintable(requestid);
        String src = Util.null2String(request.getParameter("src"));
        JAVATools.writelog("src:"+src);
        String dt = "";

        PropBean pb = new PropBean();
        String itxqglpt_workflowid = pb.getPropName("itxqglpt_workflowid");
        String itcshsxsqglpt_workflowid = pb.getPropName("itcshsxsqglpt_workflowid");
        JAVATools.writelog("IT需求管理平台流程itxqglpt_workflowid"+itxqglpt_workflowid);
        JAVATools.writelog("IT测试和上线申请流程itcshsxsqglpt_workflowid"+itcshsxsqglpt_workflowid);

        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
        String executeResult = resultBean.getType().name();
        JAVATools.writelog("executeResult:"+executeResult);

        if ("SUCCESS".equals(executeResult) && "dostopc".equals(src)) {
            if(workflowid.equals(itxqglpt_workflowid)){
                dt = tablename + "_dt7";
                JAVATools.writelog("-----ITManagementPlatformCmd--itxqglpt--" + getUnnumberAndXqhb(workflowid,tablename,dt,requestid,oaStatus));
            }else if (workflowid.equals(itcshsxsqglpt_workflowid)){
                dt = tablename + "_dt4";
                JAVATools.writelog("-----ITManagementPlatformCmd--itcshsxsqglpt--" + getUnnumberAndXqhb(workflowid,tablename,dt,requestid,oaStatus));
            }
        }
        return result;
    }

    /**
     * 获取流程唯一标识 和 需求编号list,并调用上游接口同步状态
     */
    public String getUnnumberAndXqhb(String workflowid,String tablename,String dt,String requestid,String oaStatus){
        String result = "";
        String sql = "";
        String wfUnnumber = "";
        RecordSet rs = new RecordSet();

        // 根据workflowid拿到流程唯一标识
        sql = "select UNNUMBER from WORKFLOW_BASE where ID = " + workflowid;
        JAVATools.writelog(sql);
        rs.execute(sql);
        if (rs.next()){
            wfUnnumber = Util.null2String(rs.getString("UNNUMBER"));
        }

        // 获取该requestid流程中的所有需求编号
        List<String> demandcodeList = new ArrayList<>();
        sql = "select distinct d.XQBH from WORKFLOW_REQUESTBASE w\n" +
                "        join "+tablename+" m on w.REQUESTID = m.REQUESTID\n" +
                "        join "+dt+" d on d.MAINID = m.ID\n" +
                "        where m.REQUESTID = " + requestid;
        JAVATools.writelog(sql);
        rs.execute(sql);
        while (rs.next()){
            demandcodeList.add(Util.null2String(rs.getString("XQBH")));
        }

        // 调用上游接口传状态
        result = sendPost(demandcodeList,oaStatus,wfUnnumber);
        return result;
    }

    /**
     * 调用上游接口同步状态
     */
    public String sendPost(List<String> demandcodeList,String oaStatus,String wfUnnumber){

        String result = "";
        HttpRequestUtil httpRequestUtil = new HttpRequestUtil();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("demandcodeList",demandcodeList);
        jsonObject.put("oaStatus",oaStatus);
        jsonObject.put("unNumber",wfUnnumber);
        String params = JSONObject.toJSONString(jsonObject);
        JAVATools.writelog(params);

        String url = "http://168.63.17.245:8900/newirm/require/syncOAProcessStatus";
        String response = httpRequestUtil.sendPost(url, params,"UTF-8");
        JAVATools.writelog(response);
        JSONObject responseJSON = JSON.parseObject(response);
        boolean error = Boolean.parseBoolean(Util.null2String(responseJSON.getString("error")));
        if (error){
            // true:同步失败
            result = "同步失败";
        }else {
            // false:同步成功
            result = "同步成功";
        }
        return result;
    }

}
