package com.engine.htsc.wdlb.web;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WdlbControlAction {

    /**
     * 文档建模查询列表是否可以删除
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/wdlbIfDelete")
    @Produces({MediaType.TEXT_PLAIN})
    public String wdlbIfDelete(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        JSONObject jsonObject = new JSONObject();
        Integer idNew = Util.getIntValue(request.getParameter("id"),1);
        try{
            Integer ifcomplete = 0;
            Integer iscompleteTrue = 0;
            Integer isDetele = 0;
            String btNew = "";
            String tablename=JAVATools.GetUnumbertotable("mbwj");
            RecordSet wdReuqestId= new RecordSet();
            wdReuqestId.execute("SELECT requestid FROM  "+tablename+"  WHERE id in (SELECT MAINID FROM "+tablename+"_DT1 WHERE mbmc = '"+idNew+"'\n" +
                    "UNION ALL \n" +
                    "SELECT MAINID FROM "+tablename+"_DT3 WHERE mbmc = '"+idNew+"'\n" +
                    "UNION ALL \n" +
                    "SELECT mainid FROM "+tablename+"_DT4 fmd WHERE mbmc = '"+idNew+"')");
            while (wdReuqestId.next()){
                ifcomplete++;
                //判断是否归档
                Integer requestidNew = Util.getIntValue(wdReuqestId.getString("requestid"));
                RecordSet iscompleteFind = new RecordSet();
                String workflowsql = "select * from workflow_requestbase where requestid = " + requestidNew;
                iscompleteFind.execute(workflowsql);
                String workflowid = iscompleteFind.getString("workflowid");
                iscompleteFind.execute("SELECT count(*) AS iscomplete  FROM WORKFLOW_CURRENTOPERATOR WHERE requestid \n" +
                        "= "+requestidNew+" AND workflowid = "+workflowid+" AND iscomplete = 1");
                if(iscompleteFind.next()){
                    Integer iscompleteNew = Util.getIntValue(iscompleteFind.getString("iscomplete"));
                    if(iscompleteNew >0){
                        iscompleteTrue++;
                    }else{
                        RecordSet sqlFindWgd = new RecordSet();
                        sqlFindWgd.execute("SELECT REQUESTNAME FROM WORKFLOW_REQUESTBASE wr WHERE REQUESTID  = "+requestidNew);
                        if (sqlFindWgd.next()){
                            btNew = Util.null2String(sqlFindWgd.getString("REQUESTNAME"));
                        }
                    }
                }
                //判断是否终止
                RecordSet sqlIfStop = new RecordSet();
                sqlIfStop.execute("SELECT ISSTOPPED FROM workflow_requestbase WHERE REQUESTID  = "+requestidNew);
                if (sqlIfStop.next()){
                    String isStop = Util.null2String(sqlIfStop.getString("ISSTOPPED"));
                    if (isStop.equals("1")){
                        iscompleteTrue++;
                    }
                }
            }
            if(ifcomplete == iscompleteTrue){
                //可以删除
                isDetele = 1;
            }else {
                //有调用此模板的流程未归档
                isDetele = 0;
            }
            //判断是否有模板文件
            Integer numDt = 0;
            RecordSet findDtNumber = new RecordSet();
            findDtNumber.execute("SELECT count(*) num FROM UF_WDZX_DT1 uwd WHERE MAINID = "+idNew+" AND zt IN (0,1)");
            if (findDtNumber.next()){
                 numDt = Util.getIntValue(findDtNumber.getString("num"));
            }
            jsonObject.put("code","200");
            jsonObject.put("msg","success");
            jsonObject.put("isDetele",isDetele);
            jsonObject.put("numDt",numDt);
            jsonObject.put("btNew",btNew);
        }catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code","500");
            jsonObject.put("msg","fail");
        }
        return JSONObject.toJSONString(jsonObject);
    }

    /**
     * 文档建模查询列表删除
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/wdlbDelete")
    @Produces({MediaType.TEXT_PLAIN})
    public String wdlbDelete(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        JSONObject jsonObject = new JSONObject();
        Integer idNew = Util.getIntValue(request.getParameter("id"),1);
        try{
            RecordSet wdDelete = new RecordSet();
            wdDelete.execute("DELETE FROM uf_wdzx WHERE id = "+idNew);
            jsonObject.put("code","200");
            jsonObject.put("msg","success");
        }catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code","500");
            jsonObject.put("msg","fail");
        }
        return JSONObject.toJSONString(jsonObject);
    }

    /**
     * 文档建模查询列表是否可以入库
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/wdlbIfRuku")
    @Produces({MediaType.TEXT_PLAIN})
    public String wdlbIfRuku(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//        public String wdlbIfRuku() {
        JSONObject jsonObject = new JSONObject();
        Integer idNew = Util.getIntValue(request.getParameter("id"),1);
//        Integer idNew = 402;
        String tablename=JAVATools.GetUnumbertotable("mbwj");
        try{
            Integer ifcomplete = 0;
            Integer iscompleteTrue = 0;
            Integer isRuku = 0;
            String btNew = "";
            RecordSet wdReuqestId= new RecordSet();
                    wdReuqestId.execute("SELECT requestid FROM "+tablename+" WHERE id in (SELECT MAINID FROM "+tablename+"_DT1 WHERE mbmc = '"+idNew+"')");
//            int counts = wdReuqestId.getCounts();
//            if(counts<=0){
//                ifcomplete =1;
//            }
            while (wdReuqestId.next()){
//                ifcomplete++;
                //判断是否归档
                Integer requestidNew = Util.getIntValue(wdReuqestId.getString("requestid"));
                RecordSet iscompleteFind = new RecordSet();
                String workflowsql = "select * from workflow_requestbase where requestid = " + requestidNew;
                iscompleteFind.execute(workflowsql);
                iscompleteFind.next();
                String workflowid = iscompleteFind.getString("workflowid");
                String currentnodetype = iscompleteFind.getString("currentnodetype");
                if("3".equals(currentnodetype)){
//                    ifcomplete = 1;
                    btNew =  iscompleteFind.getString("requestname");
                }else if(Util.null2String(iscompleteFind.getString("ISSTOPPED")).equals("1")){
                    btNew =  iscompleteFind.getString("requestname");
                }else{
                    ifcomplete++;
                    btNew =  iscompleteFind.getString("requestname");
                }
            }
            if(ifcomplete <= 0){
                //可以入库
                isRuku = 1;
            }else {
                //有调用此模板的流程未归档
                isRuku = 0;


            }
            jsonObject.put("code","200");
            jsonObject.put("msg","success");
            jsonObject.put("isRuku",isRuku);
            jsonObject.put("btNew",btNew);
        }catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code","500");
            jsonObject.put("msg","fail");
        }
        return JSONObject.toJSONString(jsonObject);
    }

    /**
     * 文档建模查询列表是否可以变更
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/wdlbIfBianGeng")
    @Produces({MediaType.TEXT_PLAIN})
    public String wdlbIfBianGeng(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//    public String wdlbIfBianGeng( ){
        JSONObject jsonObject = new JSONObject();
        Integer idNew = Util.getIntValue(request.getParameter("id"),1);
//        Integer idNew =420;
        String tablename=JAVATools.GetUnumbertotable("mbwj");
        try{
            Integer ifcomplete = 0;
            Integer iscompleteTrue = 0;
            Integer isBianGeng = 0;
            String btNew = "";
            RecordSet wdReuqestId= new RecordSet();
            wdReuqestId.execute("SELECT requestid FROM "+tablename+" WHERE id in ("+

                    " SELECT mainid FROM "+tablename+"_DT3 fmd WHERE mbmc = '"+idNew+"' "+
                    " UNION ALL " +
                    " SELECT mainid FROM "+tablename+"_DT4 fmd WHERE mbmc = '"+idNew+"') ");

//            int counts = wdReuqestId.getCounts();
//            if(counts<=0){
//                ifcomplete=1;
//            }
            while (wdReuqestId.next()){
//                ifcomplete++;
                //判断是否归档
                Integer requestidNew = Util.getIntValue(wdReuqestId.getString("requestid"));
                RecordSet iscompleteFind = new RecordSet();
                String workflowsql = "select * from workflow_requestbase where requestid = " + requestidNew;
                iscompleteFind.execute(workflowsql);
                iscompleteFind.next();
                String workflowid = iscompleteFind.getString("workflowid");
                String currentnodetype = iscompleteFind.getString("currentnodetype");
                if("3".equals(currentnodetype)){
                    btNew = iscompleteFind.getString("requestname");
                }else if(Util.null2String(iscompleteFind.getString("ISSTOPPED")) == "1"){
                    btNew = iscompleteFind.getString("requestname");
                }else{
                    ifcomplete++;
                    btNew = iscompleteFind.getString("requestname");
                }
            }

            if(ifcomplete <=0){
                //可以变更
                isBianGeng = 1;
            }else {
                //有调用此模板的流程未归档
                isBianGeng = 0;
            }
            jsonObject.put("code","200");
            jsonObject.put("msg","success");
            jsonObject.put("isBianGeng",isBianGeng);
            jsonObject.put("btNew",btNew);
        }catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code","500");
            jsonObject.put("msg","fail");
        }
        return JSONObject.toJSONString(jsonObject);
    }

}
