package com.engine.htxc.tycgrwd.judgeTheProcessInTransit;

import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class WhetherInTransit implements JudgeTheProcessInTransit {
    @Override
    public boolean isComplete(String table, String xmid) {
        RecordSet rs = new RecordSet();
        RecordSet mainRs = new RecordSet();
        RecordSet completeRs = new RecordSet();
        String sql = "select * from "+table+"_dt4 where xmid = "+xmid;
        rs.execute(sql);
        ArrayList<String> list = new ArrayList<>();
        while(rs.next()){
            String mainid = rs.getString("mainid");
            String mainSql = "select * from " + table+ " where id = "+mainid;
            mainRs.execute(mainSql);
            mainRs.next();
            String requestid = mainRs.getString("requestid");
            String stopSql = "select a.isremark,a.iscomplete ,b.isstopped from workflow_currentoperator   a right join workflow_requestbase b " +
                    "on a.requestid = b.requestid  where  a.requestid = "+requestid;
            completeRs.execute(stopSql);
            ArrayList<Integer> completes = new ArrayList<>();
            while (completeRs.next()){
                String iscomplete = completeRs.getString("iscomplete");
                String isstopped = completeRs.getString("isstopped");
                if(Objects.equals(iscomplete,"1")){
                    completes.add(1);
                } else if(Objects.equals(isstopped,"1")){
                    completes.add(0);
                }else{
                    completes.add(0);
                }
            }
            List<Integer> collect = completes.stream().filter((item) -> {
                return item > 0;
            }).collect(Collectors.toList());
            if(collect.size()>0){
                list.add("1");
            }else{
                list.add("0");
            }

        }
        if(list.size()==0){
            return true;
        }else{
            Iterator<String> iterator = list.iterator();
            while(iterator.hasNext()){
                String next = iterator.next();
                if(Objects.equals(next,"0")){
                    return true;
                }
            }
        }

        return false;

    }

    @Override
    public boolean isComplete(String requestid) {
        RecordSet completeRs = new RecordSet();
        //判断是不是在途
        ArrayList<String> list = new ArrayList<>();
        String stopSql = "select a.isremark,a.iscomplete ,b.isstopped from workflow_currentoperator   a right join workflow_requestbase b " +
                "on a.requestid = b.requestid  where  a.requestid = "+requestid;
        completeRs.execute(stopSql);
        ArrayList<Integer> completes = new ArrayList<>();
        while (completeRs.next()){
            String iscomplete = completeRs.getString("iscomplete");
            String isstopped = completeRs.getString("isstopped");
            if(Objects.equals(iscomplete,"1")){
                completes.add(0);
            } else if(Objects.equals(isstopped,"1")){
                completes.add(1);
            }else{
                completes.add(0);
            }
        }

        List<Integer> collect = completes.stream().filter((item) -> {
            return item > 0;
        }).collect(Collectors.toList());
        if(collect.size()>0){
            return false;
        }else{
            return true;
        }
    }
}
