package com.engine.htsc.otherSystemWorkflow;


import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.workflow.util.WorkflowUtil;
import weaver.conn.RecordSet;
import weaver.crm.Maint.CustomerInfoComInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 *
 */
public class OtherWorkflowTodoAction {


    /**
     * 查询人员待办
     * @param request
     * @return
     */
    @POST
    @Path("/todos")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
//    @Produces({MediaType.TEXT_PLAIN})
    public String getPortalTodoData(Map<String, String> request){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        //获取用户
        String userId = Util.null2String(request.get("userId"));
        int num = Util.getIntValue(request.get("num"));
        String type = Util.null2String(request.get("type"));
        JSONObject jsonObject=new JSONObject();
        StringBuilder stringBuilder = new StringBuilder();
        //标题
        String requestName = Util.null2String(request.get("title"));
        //流程编号
        String workFlowNum = Util.null2String(request.get("workFlowNum"));
        //创建人
        String drafterName = Util.null2String(request.get("drafterName"));
        //创建人ID
        String drafterId = "";
        int pageNumber = Integer.parseInt(Util.null2String(request.get("pageNumber")));

        int pageSize = Integer.parseInt(Util.null2String(request.get("pageSize")));



        PropBean pb = new PropBean();
        String oahost = pb.getPropName("OAHOST");


        List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
        try {
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select * from hrmresource where workcode=? and (accounttype is null or accounttype='' or accounttype='0')",userId);
            if(rs.next()){

                int id = rs.getInt("id");
                User user = User.getUser(id,0);
                String useridAll = id+"";
                String belongtoids = user.getBelongtoids();
                if(!"".equals(belongtoids)){
                    useridAll += ","+belongtoids;
                }

                //查询接口
               // List<String> liststr=gethideAttention(id);
                apidatas.put("userCode",rs.getString("workcode"));
                apidatas.put("userId",id);
                apidatas.put("userName",user.getLastname());
                String getTotalNumSql = this.getTotalNumSql(useridAll, type, num, requestName, pageNumber, pageSize);
                rs.execute(getTotalNumSql);
                while(rs.next()) {
                    apidatas.put("total", Integer.valueOf(rs.getString("total")));
                }
                if (!"".equals(drafterName) && drafterName != null){
                    String nameConditionSql = "select id from hrmresource where lastname like '%"+drafterName+"%'";
                    rs.execute(nameConditionSql);
                    while(rs.next()){
                        stringBuilder.append(rs.getString("id"));
                        stringBuilder.append(",");
                    }
                    drafterId = stringBuilder.substring(0,stringBuilder.length()-1).toString();
                }

                ResourceComInfo resourceComInfo = new ResourceComInfo();
                String sql = getWorkflowDataSql(useridAll,type,num,requestName,drafterName,workFlowNum,pageNumber,pageSize,drafterId);
                //System.out.println("data sql===="+sql);
                rs.execute(sql);
                while(rs.next()){
                    //String workcode = Util.null2String(rs.getString("workcode"));
                    String userid = Util.null2String(rs.getString("userid"));
                    String creater = Util.null2String(rs.getString("creater"));
                    String uuid = Util.null2String(rs.getString("uuid"));
                    String workcode  = resourceComInfo.getWorkcode(creater);
                    String lastname = resourceComInfo.getLastname(creater);
                  //  String workcode="";
                //    String lastname="";

                    String requestid = Util.null2String(rs.getString("requestid"));
                    String priority = Util.null2String(rs.getString("requestlevel"));

                    if("0".equals(priority)){
                        priority = "一般";
                    }else if("1".equals(priority)){
                        priority = "紧急";
                    }else if("2".equals(priority)){
                        priority = "特急";
                    }else{
                        priority = "一般";
                    }

                    String title = Util.null2String(rs.getString("requestname"));
                    String workflowname = Util.null2String(rs.getString("workflowname"));
                    //String lastname = Util.null2String(rs.getString("lastname"));
                    String action = WorkflowUtil.getNodeName(Util.null2String(rs.getString("currentnodeid"))) ;
                    String createTime = Util.null2String(rs.getString("createtime"));
                    String createDate = Util.null2String(rs.getString("createdate"));
                    String referencenumber = Util.null2String(rs.getString("requestmark"));
                    String workflowid = Util.null2String(rs.getString("workflowid"));

                    Map<String,String> itemMap = new HashMap<String,String>();
                    itemMap.put("createrCode",workcode);
                    itemMap.put("priority",priority);
                    String marks = "" ;
                    try{
                        marks = getAttentionType(requestid,useridAll);
                    }catch (Exception e){

                    }
                    if(!"".equals(marks)){
                        itemMap.put("title",marks + title);
                    }else{
                        itemMap.put("title",title);
                    }

                    itemMap.put("uuid",uuid);
                    itemMap.put("type",workflowname);
                    itemMap.put("createrName",lastname);
                    itemMap.put("action",action);
                    itemMap.put("createTime",createTime);
                    itemMap.put("createDate",createDate);
                    itemMap.put("referencenumber",referencenumber);
                    itemMap.put("fromSystem","数字办公平台");
                    itemMap.put("requestid",requestid);

                    long sjc = System.currentTimeMillis();
                    //&_workflowid=4024&_workflowtype=&isovertime=0&preloadkey=1615367484147&timestamp=1615367484147&_key=w3aabh
//                    itemMap.put("url",oahost+"workflow/request/ViewRequestForwardSPA.jsp?isrequest=1&requestid="+requestid+"" +
//                               "&f_weaver_belongto_userid="+userid+"&f_weaver_belongto_usertype=0&isovertime=0&preloadkey="+sjc+"&timestamp="+sjc);

//                    itemMap.put("url",oahost+"workflow/request/ViewRequestForwardSPA.jsp?isrequest=1&requestid="+requestid+"" +
//                            "&_workflowid="+workflowid+"&f_weaver_belongto_userid="+userid+"&f_weaver_belongto_usertype=0"+"&isovertime=0");
                    itemMap.put("url",oahost);


                  //  if(liststr==null || liststr.size()<=0 || !liststr.contains(requestid) || (liststr.contains(requestid) && !(type.equals("2") || type.equals("3")) ) ){
                            dataList.add(itemMap);
                  //  }



                }
                apidatas.put("data",dataList);
            }

            apidatas.put("success", true);
            apidatas.put("status","200");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 格局ids查询流程信息
     * @param request
     * @return
     */
    @POST
    @Path("/getRequestData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String getRequestData(Map<String, String> request){
        PropBean pb = new PropBean();
        String oahost = pb.getPropName("OAHOST");
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String requestIds = Util.null2String(request.get("requestIds"));
        String xtmc = Util.null2String(request.get("xtmc"));

        String sql = "select requestid,creatertype,requestname,requestmark,requestnamenew,creater,createdate,createtime from workflow_requestbase where requestid in (" + requestIds + ")";
        try {
            RecordSet recordSet = new RecordSet();
            recordSet.executeSql(sql);
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            CustomerInfoComInfo comInfo = new CustomerInfoComInfo();
            List<Map<String,String>> dataList = new ArrayList<>();
            while (recordSet.next()){
                String requestid = Util.null2String(recordSet.getString("requestid"));
                String creater = Util.null2String(recordSet.getString("creater"));
                String createdate = Util.null2String(recordSet.getString("createdate"));
                String createtime = Util.null2String(recordSet.getString("createtime"));
                String requestname = Util.null2String(recordSet.getString("requestname"));
                String createtype = Util.null2String(recordSet.getString("createtype"));
                String requestmark =  Util.null2String(recordSet.getString("requestmark"));
                if ("1".equals(createtype)) {
                    creater = comInfo.getCustomerInfoname(creater);
                } else {
                    creater = resourceComInfo.getResourcename(creater);
                }
                HashMap dataMap = new HashMap();
                dataMap.put("id", oahost+"^"+requestid);
                dataMap.put("title", requestname);
                dataMap.put("workflownum", requestmark);
                dataMap.put("creater", creater);
                dataMap.put("createdate", createdate + " " + createtime);
                dataMap.put("url",oahost);
                dataList.add(dataMap);
            }
            apidatas.put("data",dataList);
            apidatas.put("success", true);
            apidatas.put("status","200");
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("success", false);
        }

        return JSONObject.toJSONString(apidatas);
    }










    /**
     * @param requstid
     * @return
     */
    public String getAttentionType(String requstid,String userids){
        String mark = "" ;
        RecordSet rs = new RecordSet();
        String sql =" select requestid\n" +
                    "  from workflow_attention\n" +
                    "  where atypeid in (select id\n" +
                    "                     from workflow_attentionTypeSet\n" +
                    "                    where showname = '电子用印'\n" +
                    "                      and userid in ("+userids+"))\n" +
                    "  and userid in ("+userids+") \n" +
                    "  and usertype = 0 \n " +
                    "  and requestid="+requstid ;

        //System.out.println("sql:"+sql);
        rs.execute(sql);
        if(rs.next()){
            mark = "[E]";
        }

        sql =" select requestid\n" +
                "  from workflow_attention\n" +
                "  where atypeid in (select id\n" +
                "                     from workflow_attentionTypeSet\n" +
                "                    where showname = '邮寄'\n" +
                "                      and userid in ("+userids+"))\n" +
                "  and userid in ("+userids+") \n" +
                "  and usertype = 0 \n " +
                "  and requestid="+requstid ;

        //System.out.println("sql:"+sql);
        rs.execute(sql);
        if(rs.next()){
            mark = mark + "[邮]";
        }

        return mark;
    }


//
//    public List<String> gethideAttention(int userid){
//        PropBean propBean=new PropBean();
//        String jgxtworkflowid= propBean.getPropName("jgxt.workflowid");
//        String showname= propBean.getPropName("jgxt.label.showname");
//        String userids=getAgentInfoByResouce(jgxtworkflowid,Utils.null2String(userid));
//         if(StringUtil.isBlank(userids)){
//            userids= Utils.null2String(userid);
//        }
//
//        Map<String,Object> map=new HashMap<>();
//        map.put("userid",userid);
//        map.put("showname",showname);
//        String usertype=  ServiceUtil.getService(WfAttentionTypeSetServiceImpl.class).getAttentionTypeSet(map);
//
//        RecordSet rs=new RecordSet();
//        List<String> list=new ArrayList<>();
//        userids = userids.replace("\'", "");
//        String sql ="   select t.* from workflow_attention t where t.userid in ("+userids+") and atypeid='"+usertype+"' ";
//        rs.execute(sql);
//        while(rs.next()){
//            String requestid=Util.null2String(rs.getString("requestid"));
//            if(!"".equals(requestid)){
//                list.add(requestid);
//            }
//        }
//        return list;
//    }

    public String getAgentInfoByResouce(String workflowid, String userid) {
        String value="";
        if (!org.apache.commons.lang.StringUtils.isBlank(workflowid) && !org.apache.commons.lang.StringUtils.isBlank(userid)) {
            try {
                RecordSet rs = new RecordSet();
                Calendar calendar = Calendar.getInstance();
                String endDate = Util.add0(calendar.get(1), 4) + "-" + Util.add0(calendar.get(2) + 1, 2) + "-" + Util.add0(calendar.get(5), 2);
                String beginTime = Util.add0(calendar.get(11), 2) + ":" + Util.add0(calendar.get(12), 2) + ":" + Util.add0(calendar.get(13), 2);
                String sql = " select wm_concat_old(a.bagentuid) as bagentuid from workflow_agentConditionSet a,Workflow_Agent b where a.workflowId in (" + workflowid + ") and (" + Util.getSubINClause(userid, "a.agentuid", "IN") + ") and b.agenttype = '1' and a.agentid=b.agentId and a.workflowid = b.workflowid and a.isproxydeal='1'   and ( ( (a.endDate = '" + endDate + "' and (a.endTime='' or a.endTime is null)) or (a.endDate = '" + endDate + "' and a.endTime > '" + beginTime + "' ) )  or a.endDate > '" + endDate + "' or a.endDate = '' or a.endDate is null) and ( ( (a.beginDate = '" + endDate + "' and (a.beginTime='' or a.beginTime is null)) or (a.beginDate = '" + endDate + "' and a.beginTime < '" + beginTime + "' ) )  or a.beginDate < '" + endDate + "' or a.beginDate = '' or a.beginDate is null)  order by a.agentbatch asc,a.id asc  ";
                rs.execute(sql);
                if(rs.getColCounts()<=0){
                    return "";
                }
                rs.next();
                value= Util.null2String(rs.getString("bagentuid"));

            } catch (Exception exo) {
            }
        } else {
            return  value;
        }
        return value;
    }


    public String getWorkflowDataSql(String useridAll,String type,int num,String requestName,String drafterName,String workFlowNum,int pageNumber,int pageSize,String drafterId){
        String sql = "";
        String excludeFlow = new PropBean().getPropName("noSendWorkflowId");
        String excludeSqlWhere = "";
        if(!"".equals(excludeFlow)){
            excludeSqlWhere = " and t3.id not in("+excludeFlow+") ";
        }
        String requestNameCondition = "";
        if(requestName != null && !"".equals(requestName)){
            requestNameCondition = " AND requestname like '%"+requestName+"%'";
        }
        if(drafterName != null && !"".equals(drafterName)){
            requestNameCondition = " AND  creater in ( " + drafterId+" )";
        }
        if(workFlowNum != null && !"".equals(workFlowNum)){
            requestNameCondition = " AND requestmark like '%"+workFlowNum+"%'";
        }
        String paging = " AND rn between "+((pageNumber-1)*pageSize+1)+ " and " + pageNumber*pageSize;


        //type 1、待阅  2、待办 3、在办 4、搁置 5、草稿
        if("1".equals(type)){
            sql = " select * from ( " +
                    " select t2.userid,t1.workflowid,t1.requestid,t3.workflowname,t1.requestname,t1.requestmark,\n" +
                    " t1.creater,t1.createdate, t1.createtime,t1.requestlevel,t1.currentnodeid\n" +
                    " from workflow_requestbase t1,workflow_currentoperator t2,workflow_base t3   \n" +
                    " where t1.requestid = t2.requestid \n" +
                    " and t1.workflowid=t3.id \n" +
                    " and t2.userid in ("+useridAll+") and t2.usertype=0 \n" +
                    " and (nvl(t1.currentstatus,-1) = -1 or (nvl(t1.currentstatus,-1)=0 and t1.creater in ("+useridAll+")))   \n" +
                    " and (t1.deleted<>1 or t1.deleted is null or t1.deleted='')  \n" +
                    " and t2.preisremark in ('1','8','9','11') \n" +
                    " and t2.takisremark is null  and nvl(t1.isstopped,0) != 1  and t2.viewtype = '0' \n" +
                    " and (t1.currentnodetype <> '3' or (t2.isremark in ('1', '8', '9','11') and t1.currentnodetype = '3')) \n" +
                    " and (isprocessing = '' or isprocessing is null)  and t3.isvalid in(1,3) "+excludeSqlWhere+" order by t2.receivedate desc,t2.receivetime desc " +
                    " ) where rownum<="+num;
        }else if("2".equals(type)){ //待办
//            sql = "select DISTINCT(requestid),requestname,workflowid,workflowname,requestname,requestmark,uuid,creater,createdate,createtime,requestlevel,currentnodeid from (\n" +
//                    " select  t2.userid,t1.workflowid,t1.requestid,t3.workflowname,t1.requestname,t1.requestmark,t3.uuid,\n" +
//                    " t1.creater,t1.createdate, t1.createtime,t1.requestlevel,t1.currentnodeid  \n" +
//                    " from workflow_requestbase t1,workflow_currentoperator t2,workflow_base t3 \n" +
//                    " where t1.requestid = t2.requestid and t1.workflowid=t3.id \n" +
//                    " and t2.userid in ("+useridAll+") and t2.usertype=0 \n" +
//                    " and (nvl(t1.currentstatus,-1) = -1 or (nvl(t1.currentstatus,-1)=0 and t1.creater in ("+useridAll+"))) \n" +
//                    " and (t1.deleted<>1 or t1.deleted is null or t1.deleted='') \n" +
//                    " and t2.islasttimes=1 and t2.isremark not in('5') \n" +
//                    " and t2.isprocessed is null \n" +
//                    " and t1.currentnodetype='3' \n"+
//                    " and (isprocessing = '' or isprocessing is null) and t3.isvalid in(1,3) " +excludeSqlWhere+
//                    " and ( (t2.isremark not in ('1','8','9') )or (t2.isremark = '1' and t2.takisremark = '2' ) )\n" +
//                    " order by t2.receivedate desc,t2.receivetime desc "+
//                    " ) where 1=1 "+requestNameCondition;
            sql = "select DISTINCT(requestid),requestname,workflowid,workflowname,requestmark,uuid,creater,createdate,createtime,requestlevel,currentnodeid from(SELECT\n" +
                    "t1.requestid,\n" +
                    "t1.requestmark,\n" +
                    "t1.createdate,\n" +
                    "t1.createtime,\n" +
                    "t1.creater,\n" +
                    "t1.creatertype,\n" +
                    "t1.workflowid,\n" +
                    "t1.requestname,\n" +
                    "t1.requestnamenew,\n" +
                    "t1.status,\n" +
                    "t1.requestlevel,\n" +
                    "t1.currentnodeid,\n" +
                    "t2.viewtype,\n" +
                    "t2.userid,\n" +
                    "t2.receivedate,\n" +
                    "t2.receivetime,\n" +
                    "t2.isremark,\n" +
                    "t2.nodeid,\n" +
                    "t2.agentorbyagentid,\n" +
                    "t2.agenttype,\n" +
                    "t2.isprocessed,\n" +
                    "t3.uuid,\n" +
                    "t3.workflowname,\n" +
                    "( CASE WHEN t2.operatedate IS NULL THEN t2.receivedate ELSE t2.operatedate END ) operatedate,\n" +
                    "( CASE WHEN t2.operatetime IS NULL THEN t2.receivetime ELSE t2.operatetime END ) operatetime,\n" +
                    "t1.seclevel,\n" +
                    "'0' AS systype,\n" +
                    "t2.workflowtype,\n" +
                    "t2.viewDate,\n" +
                    "t2.viewTime,\n" +
                    "t1.lastFeedBackDate,\n" +
                    "t1.lastFeedBackTime,\n" +
                    "t2.needwfback,\n" +
                    "t1.lastFeedBackOperator,\n" +
                    "t2.operatedate AS operatedateNew,\n" +
                    "t2.operatetime AS operatetimeNew \n" +
                    "FROM\n" +
                    "workflow_requestbase t1,\n" +
                    "workflow_currentoperator t2,\n" +
                    "workflow_base t3 \n" +
                    "WHERE\n" +
                    "t1.requestid = t2.requestid \n" +
                    "AND t1.workflowid = t3.id \n" +
                    "AND t2.userid IN ( "+useridAll+" ) \n" +
                    "AND t2.usertype = 0 \n" +
                    "AND (\n" +
                    "nvl( t1.currentstatus,- 1 ) = - 1 \n" +
                    "OR (\n" +
                    "nvl( t1.currentstatus,- 1 ) = 0 \n" +
                    "AND t1.creater IN ( "+useridAll+" ))) \n" +
                    "AND ( t1.deleted <> 1 OR t1.deleted IS NULL OR t1.deleted = '' ) \n" +
                    "AND (\n" +
                    "t2.isremark IN ( '2', '4' ) \n" +
                    "OR ( t2.isremark = '0' AND t2.takisremark =- 2 )) \n" +
                    "AND t2.islasttimes = 1 \n" +
                    "AND t3.isvalid IN ( 1, 3 )) where 1=1 "+requestNameCondition;

        }else if("3".equals(type)){ //在办
            //收文等待
            String where  = " " ;
            sql = " select * from (\n" +
                    " select  t2.userid,t1.workflowid,t1.requestid,t3.workflowname,t1.requestname,t1.requestmark,\n" +
                    " t1.creater,t1.createdate, t1.createtime,t1.requestlevel,t1.currentnodeid \n" +
                    " from workflow_requestbase t1,workflow_currentoperator t2,workflow_base t3 \n" +
                    " where t1.requestid = t2.requestid and t1.workflowid=t3.id \n" +
                    " and t2.userid in ("+useridAll+") and t2.usertype=0 \n" +
                    " and (nvl(t1.currentstatus,-1) = -1 or (nvl(t1.currentstatus,-1)=0 and t1.creater in ("+useridAll+"))) \n" +
                    " and (t1.deleted<>1 or t1.deleted is null or t1.deleted='') \n" +
                    " and ((t2.isremark='0' and (t2.takisremark is null or t2.takisremark=0 )) or t2.isremark in('1','5','8','9','7','11')) \n" +
                    " and t2.islasttimes=1 and t2.viewtype != '0' \n" +
                    " and (t1.currentnodetype <> '3' or (t2.isremark in ('1', '8', '9','11') and t1.currentnodetype = '3')) \n" +
                    " and (isprocessing = '' or isprocessing is null) and t3.isvalid in(1,3) " +excludeSqlWhere+
                    " and ( exists (select 1 from workflow_requestlog log where log.requestid=t2.requestid \n" +
                    " and log.logtype in ('0','2','i') ) or nvl(t1.notdraft,0)=1 )\n" +
                    " and not exists ( select 1 from uf_lcgzb t4 where t4.lcid=t1.requestid and lcdqzt=1 and gzr in ("+useridAll+") ) \n"+
                    " and ( (t2.isremark not in ('1','8','9') )or (t2.isremark = '1' and t2.takisremark = '2' ) )\n" +
                    " and t1.requestid not in(" +
                    "    select requestid from workflow_attention where atypeid in(select id from workflow_attentionTypeSet where showname='收文等待' and userid in("+useridAll+"))"+
                    "     and userid in("+useridAll+") and usertype=0 ) \n"+
                    " order by t2.receivedate desc,t2.receivetime desc\n" +
                    " ) where rownum<="+num;

            //System.out.println("swdd:"+sql);

        }else if("4".equals(type)){//搁置
            sql = "  select * from (\n" +
                    "  select gzr userid,t2.requestid,t2.workflowid,t3.workflowname,t2.requestname,t2.requestmark,\n" +
                    " t2.creater,t2.createdate, t2.createtime,t2.requestlevel,t2.currentnodeid   \n" +
                    " from uf_lcgzb t1,workflow_requestbase t2, workflow_base  t3\n" +
                    " where t3.id=t2.workflowid and t2.requestid=t1.lcid " +excludeSqlWhere+
                    " and gzr in ("+useridAll+") and lcdqzt = 1 order by t1.gzrq desc,t1.gzsj desc ) where  rownum<="+num;

        }else if("5".equals(type)){//草稿
            sql = " select * from ("+
                    "select t2.userid,t1.workflowid,t1.requestid,t3.workflowname,t1.requestname,t1.requestmark,\n" +
                    "t1.creater,t1.createdate, t1.createtime,t1.requestlevel,t1.currentnodeid \n" +
                    "from workflow_requestbase t1,workflow_currentoperator t2,workflow_base t3 \n" +
                    "where t1.requestid = t2.requestid and t1.workflowid=t3.id \n" +
                    "and t2.userid in ("+useridAll+" ) and t2.usertype=0 \n" +
                    "and (nvl(t1.currentstatus,-1) = -1 or (nvl(t1.currentstatus,-1)=0 and t1.creater in ("+useridAll+")))   \n" +
                    "and (t1.deleted<>1 or t1.deleted is null or t1.deleted='')  \n" +
                    "and t1.creater in ("+useridAll+") and t1.creatertype = 0 and t1.creater = t2.userid   \n" +
                    "and (not exists (select 1 from workflow_requestlog wr where wr.logtype in ('0','2','i') and wr.requestid = t1.requestid  ) \n" +
                    "and t1.currentnodetype != '3' ) and t3.isvalid in(1,3) "+excludeSqlWhere+" order by t1.createdate desc,t1.createtime desc " +
                    ") where  rownum<="+num;
        }else if("6".equals(type)){
            sql = "select * from ( \n" +
                    "select t2.userid,t1.workflowid,t1.requestid,t3.workflowname,t1.requestname,t1.requestmark,\n" +
                    "t1.creater,t1.createdate, t1.createtime,t1.requestlevel,t1.currentnodeid \n" +
                    "from workflow_requestbase t1,workflow_currentoperator t2,workflow_base t3   \n" +
                    "where t1.requestid = t2.requestid and t1.workflowid=t3.id and t2.userid in ("+useridAll+" ) and t2.usertype=0 \n" +
                    "and (nvl(t1.currentstatus,-1) = -1 or (nvl(t1.currentstatus,-1)=0 and t1.creater in ("+useridAll+")))   \n" +
                    "and (t1.deleted<>1 or t1.deleted is null or t1.deleted='')  and t2.preisremark in ('1','8','9','11') \n" +
                    "and t2.takisremark is null  and nvl(t1.isstopped,0) != 1  and t2.islasttimes=1   and t2.viewtype != '0'  \n" +
                    "and t2.isremark not in ('4','2') and (t1.currentnodetype <> '3' or (t2.isremark in ('1', '8', '9','11') \n" +
                    "and t1.currentnodetype = '3')) and (isprocessing = '' or isprocessing is null)  and t3.isvalid in(1,3) "+excludeSqlWhere+
                    "order by t2.receivedate desc,t2.receivetime desc \n" +
                    ") where rownum<="+num;
        }
        return sql;
    }

    public String getTotalNumSql(String useridAll, String type, int num, String requestName, int pageNumber, int pageSize) {
        String sql = "";
        String excludeFlow = (new PropBean()).getPropName("noSendWorkflowId");

        if ("2".equals(type)) {
            //sql = " select  count(1) as total \n from workflow_requestbase t1,workflow_currentoperator t2,workflow_base t3 \n where t1.requestid = t2.requestid and t1.workflowid=t3.id \n and t2.userid in (" + useridAll + ") and t2.usertype=0 \n" + " and (nvl(t1.currentstatus,-1) = -1 or (nvl(t1.currentstatus,-1)=0 and t1.creater in (" + useridAll + "))) \n" + " and (t1.deleted<>1 or t1.deleted is null or t1.deleted='') \n" + " and ((t2.isremark='0' and (t2.takisremark is null or t2.takisremark=0 )) or t2.isremark in('1','5','8','9','7','11')) \n" + " and t2.islasttimes=1 and t2.isremark not in('5') \n" + " and t2.isprocessed is null and t2.viewtype = '0' \n" + " and (t1.currentnodetype <> '3' or (t2.isremark in ('1', '8', '9','11') and t1.currentnodetype = '3')) \n" + " and (isprocessing = '' or isprocessing is null) and t3.isvalid in(1,3) " + excludeSqlWhere + " and ( exists (select 1 from workflow_requestlog log where log.requestid=t2.requestid \n" + " and log.logtype in ('0','2','i') ) or nvl(t1.notdraft,0)=1 )\n" + " and ( (t2.isremark not in ('1','8','9') )or (t2.isremark = '1' and t2.takisremark = '2' ) )";
            sql = "SELECT\n" +
                    "count(1) as total\n" +
                    "FROM\n" +
                    "workflow_requestbase t1,\n" +
                    "workflow_currentoperator t2,\n" +
                    "workflow_base t3 \n" +
                    "WHERE\n" +
                    "t1.requestid = t2.requestid \n" +
                    "AND t1.workflowid = t3.id \n" +
                    "AND t2.userid IN ( "+useridAll+" ) \n" +
                    "TokenUtilAND t2.usertype = 0 \n" +
                    "TokenUtilAND (\n" +
                    "TokenUtilnvl( t1.currentstatus,- 1 ) = - 1 \n" +
                    "TokenUtilOR (\n" +
                    "TokenUtilnvl( t1.currentstatus,- 1 ) = 0 \n" +
                    "TokenUtilAND t1.creater IN ( "+useridAll+" ))) \n" +
                    "TokenUtilAND ( t1.deleted <> 1 OR t1.deleted IS NULL OR t1.deleted = '' ) \n" +
                    "TokenUtilAND (\n" +
                    "TokenUtilt2.isremark IN ( '2', '4' ) \n" +
                    "TokenUtilOR ( t2.isremark = '0' AND t2.takisremark =- 2 )) \n" +
                    "TokenUtilAND t2.islasttimes = 1 \n" +
                    "TokenUtilAND t3.isvalid IN ( 1, 3 )";
        }

        return sql;
    }













}
