package com.engine.htxc.tycgsq.service.impl;

import com.engine.core.impl.Service;
import com.engine.htxc.tycgsq.service.TycgsqService;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.stream.Collectors;

public class TycgsqServiceImpl extends Service implements TycgsqService {
    @Override
    public ArrayList<HashMap<String, Object>> getLxxmByCgxmlx(String cgxmlx) {
        RecordSet rs = new RecordSet();
        RecordSet deptRs = new RecordSet();
        RecordSet hrmRs = new RecordSet();
        RecordSet cgzdRs = new RecordSet();
        PropBean pb = new PropBean();
        String table = pb.getPropName("tycgsq_table");
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        String sql = "select * from  uf_cgxq where zt = 1 and  CGXMLX = "+cgxmlx;
        rs.execute(sql);
        while(rs.next()){
            String id = rs.getString("id");
            Boolean complete = isComplete(table, id);
            if(complete){
                continue;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("bz",rs.getString("bz"));
            String cgzdSql = "select * from  uf_cgzdsjzd where id = " + rs.getString("cgzd") ;
            cgzdRs.execute(cgzdSql);
            String cgzd = "";
            if(cgzdRs.next()){
                cgzd =cgzdRs.getString("cgzdmc");
            }
            map.put("cgzd",cgzd);
            map.put("cgtk",rs.getString("cgtk"));
            map.put("xmbh",rs.getString("xmbh"));
            map.put("xmlb",rs.getString("xmlb"));
            map.put("cgfs",rs.getString("cgfs"));
            //预算部门改为浏览按钮格式
            ArrayList<HashMap<String, String>> deptList = new ArrayList<>();
            String deprtSql = "select * from  HRMDEPARTMENT where id in( "+rs.getString("ysssbm") + ")";
            deptRs.execute(deprtSql);
            while(deptRs.next()){
                HashMap<String, String> dept = new HashMap<>();
                dept.put("id",deptRs.getString("id"));
                dept.put("name",deptRs.getString("departmentname"));
                deptList.add(dept);
            }
            map.put("ysssbm",deptList);
            map.put("ysje",rs.getString("ysje"));
            map.put("cgnr",rs.getString("cgnr"));
            map.put("gys",rs.getString("gys"));
            map.put("cgsl",rs.getString("cgsl"));
            //采购负责人改为浏览按钮格式
            ArrayList<HashMap<String, String>> hrmList = new ArrayList<>();
            String hrmSql = "select * from hrmresource where id in ( "+rs.getString("xmfzr") + ") and status = 1";
            hrmRs.execute(hrmSql);
            while(hrmRs.next()){
                HashMap<String, String> hrm = new HashMap<>();
                hrm.put("id",hrmRs.getString("id"));
                hrm.put("name",hrmRs.getString("lastname"));
                hrmList.add(hrm);
            }
            map.put("xmfzr",hrmList);
            map.put("xmmc",rs.getString("xmmc"));
            map.put("id",rs.getString("id"));
            list.add(map);
        }
        return list;
    }

    @Override
    public boolean allowDelete(String ids) {
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_cgxmgl where id in ("+ids +")";
        rs.execute(sql);
        boolean flag = false;
        while(rs.next()){
            String zt = rs.getString("zt");
            if(!Objects.equals("0",zt)){
                flag = true;
            }
        }
        if(flag){
            return flag;
        }else{
            String deleteSsql = "delete from uf_cgxmgl where id in ("+ids +")";
            rs.execute(deleteSsql);
            return flag;
        }
    }

    @Override
    public ArrayList<String>  allowSubmit(String ids,String requestid) {
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        String table = pb.getPropName("tycgsq_table");
        ArrayList<String> list = new ArrayList<>();
        if(StringUtils.isNotBlank(ids)){
            String[] split = ids.split(",");
            for (String xmid :split) {
                Boolean complete = isComplete(table, xmid,requestid);
                if(complete){
                    String sql = "select * from  uf_cgxq where id = " +xmid ;
                    rs.execute(sql);
                    rs.next();
                    String xmmc = rs.getString("xmmc");
                    list.add(xmmc);
                }
            }
        }
        return list;
    }

    /**
     * 用来判断当前的流程是否有在途流程
     * @param table
     * @param id 采购项目id
     * @return
     */
    public Boolean isComplete(String table, String id,String request ) {
        RecordSet rs = new RecordSet();
        RecordSet mainRs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet completeRs = new RecordSet();
        String sql = "select * from "+table+"_dt4 where xmid = "+id;
        rs.execute(sql);
        ArrayList<String> list = new ArrayList<>();
        ArrayList<Boolean> bools = 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");
            //如果是当前的流程不算有在途流程
            if(Objects.equals(request,requestid)){continue;}
            //首先判断它是不是在拟稿页面保存没有提交
            String logSql = "select * from workflow_requestlog where requestid = "+requestid;
            rs1.execute(logSql);
            int logCounts = rs1.getCounts();
            rs1.next();
            String logtype = rs1.getString("logtype");
            String currentSql = "select * from workflow_currentoperator where requestid ="+requestid;
            rs1.execute(currentSql);
            int currentCounts = rs1.getCounts();
            String requestSql = "select * from workflow_requestbase where requestid = "+requestid;
            rs1.execute(requestSql);
            rs1.next();
            String isstopde = rs1.getString("isstopped");
            if((Objects.equals("1",logtype) && logCounts ==1&&currentCounts==1) || "1".equals(isstopde)){
                bools.add(false) ;
            }else{
                bools.add(true) ;
            }

            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){
                list.add("1");
            }else{
                list.add("0");
            }

        }
        //判断是不是都保存在拟稿
        List<Boolean> collect = bools.stream().filter((item) -> {
            return item == true;
        }).collect(Collectors.toList());
        if(collect.size()==0){
            return false;
        }
        //判断是否在途
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            if(Objects.equals(next,"0")){
                return true;
            }
        }
        return false;
    }
    /**
     * 用来判断当前的流程是否有在途流程
     * @param table
     * @param id 采购项目id
     * @return
     */
    public Boolean isComplete(String table, String id ) {
        RecordSet rs = new RecordSet();
        RecordSet mainRs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet completeRs = new RecordSet();
        String sql = "select * from "+table+"_dt4 where xmid = "+id;
        rs.execute(sql);
        ArrayList<String> list = new ArrayList<>();
        ArrayList<Boolean> bools = 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 logSql = "select * from workflow_requestlog where requestid = "+requestid;
            rs1.execute(logSql);
            int logCounts = rs1.getCounts();
            rs1.next();
            String logtype = rs1.getString("logtype");
            String currentSql = "select * from workflow_currentoperator where requestid ="+requestid;
            rs1.execute(currentSql);
            int currentCounts = rs1.getCounts();
            String requestSql = "select * from workflow_requestbase where requestid = "+requestid;
            rs1.execute(requestSql);
            rs1.next();
            String isstopde = rs1.getString("isstopped");
            if((Objects.equals("1",logtype) && logCounts ==1&&currentCounts==1) || "1".equals(isstopde)){
                bools.add(false) ;
            }else{
                bools.add(true) ;
            }

            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){
                list.add("1");
            }else{
                list.add("0");
            }

        }
        //判断是不是都保存在拟稿
        List<Boolean> collect = bools.stream().filter((item) -> {
            return item == true;
        }).collect(Collectors.toList());
        if(collect.size()==0){
            return false;
        }
        //判断是否在途
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            if(Objects.equals(next,"0")){
                return true;
            }
        }
        return false;
    }
}
