package com.engine.htxc.eseal.responseaction;

import com.alibaba.fastjson.JSONObject;
import com.engine.htxc.commonutil.base.RequestJsonUtil;
import static com.engine.htxc.commonutil.log.CustomizeLog.printLog;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.eseal.responseaction.num.EsealClassType;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>用印流程回参的入口类</p>
 * @title SealPesponseAction
 * @author K1810015
 * @updateTime 2022/9/13 9:41
 */
public class SealResponseAction {
    public PropBean pb = new PropBean();
    private DbTools dbtools=new DbTools();

    /**
     * <p>返回参数</p>
     * @title receiveSeal
     * @author K1810015
     * @updateTime 2022/9/13 9:50
     */
    @POST
    @Path("/receiveSeal")
    @Produces({MediaType.TEXT_PLAIN})
    public String receiveSeal(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject apidatas = new JSONObject();
        printLog("获取到消息！！！！");
        String yywjSignfile = pb.getPropName("yywj_signfile");
        printLog("获取用印文件路径:" + yywjSignfile);
          try {
             String recordvalue = Util.null2String(RequestJsonUtil.getRequestJsonString(request));
             if (!StringUtils.isBlank(recordvalue)) {

                printLog("接收到用印文件请求消息！！！！");
                JSONObject recordsObject = JSONObject.parseObject(recordvalue);
                printLog("接收到用印文件请求消息！！！！"+ recordvalue);
                String flowNum = recordsObject.getString("flowNum");
                //判断流程-如果是用印登记单流程就继续，如果不是就转到service
                 printLog("接收到用印文件请flowNum:" + flowNum);
                String value=getnumber(flowNum);
                EsealClassType esealClassType=EsealClassType.where(value);
                if(StringUtils.isBlank(esealClassType.getValue())|| "-1".equals(esealClassType.getValue())){
                    apidatas.put("resultMsg", "接口参数为空");
                    apidatas.put("status", "-1");
                }
                Class calss= Class.forName(esealClassType.getValue());
                Object ob=calss.getConstructor(String.class).newInstance(value);
                Method m = calss.getMethod("receivestatus",JSONObject.class);
                m.invoke(ob,recordsObject);
             } else {
                apidatas.put("resultMsg", "接口参数为空");
                apidatas.put("status", "-1");
            }
        } catch (Exception e) {
            apidatas.put("resultMsg", "异常");
            apidatas.put("status", "-1");
        }
        return apidatas.toJSONString();
    }

    /**
     * <p>通过flownum取流程表名和主键</p>
     * @title getnumber
     * @author K1810015
     * @updateTime 2022/9/13 9:45
     */
    public String getnumber(String flowNum){
       String value=dbtools.getRequestidByRequestMark(flowNum);
       String tablenumber="";
        if(!StringUtils.isBlank(value)){
            //流程走向
            tablenumber=dbtools.getmainunnumber(value);
        }else{
            //建模走向
            String tablennumKey=othertable(flowNum);
            if(!StringUtils.isBlank(tablennumKey)){
                tablenumber=tablennumKey;
            }
        }
        return  tablenumber;
    }

    /**
     * <p>判断流程是否存在建模</p>
     * @title othertable
     * @author K1810015
     * @updateTime 2022/9/13 9:44
     */
    public String othertable(String flowNum){
        if(StringUtils.isBlank(flowNum)){
            return "";
        }
        Map<String,String> map=new HashMap<>(1);
        map.put("flownum",flowNum);
        Map<String,Object> retmap= dbtools.gettabletomap("uf_esealmodeinfo",map);
        if(retmap==null || retmap.size()<=0){
            return "";
        }
        return Util.null2String(retmap.get("glzb"));

    }

}
