package weaver.interfaces.htsc.itxqlc.util;

import com.engine.htsc.itxqlc.web.ItxqlcAction;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class ItxqLcUtil {

    public String getBshqbmData(String szbm,String hgfxdj,String sfxyw,String sfcyxxxt,String sfsjywxghgfkgn,String ywlb,String xqxgxt)
    {
        ItxqlcAction itxqlcAction = new ItxqlcAction();
        String hqbmvalue = "" ;
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        String bshq = pb.getPropName("ITXQ_BSHQ");
        String bshq2 = pb.getPropName("ITXQ_BSHQ2");
        String ywlx_bm = pb.getPropName("ITXQ_YWLX_BM");
        List<String> ywlxList = new ArrayList<String>();
        if(!"".equals(ywlx_bm)){
            ywlx_bm = "'"+ywlx_bm.replace(",","','")+"'";
            String sql =   " select id from hrmdepartment d where d.departmentcode in("+ywlx_bm+") ";
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                ywlxList.add(id);
            }
        }

        List<String> xtmcList = new ArrayList<String>();
        String xtmc_bm = pb.getPropName("ITXQ_XTMC_BM");
        if(!"".equals(xtmc_bm)){
            xtmc_bm = "'"+xtmc_bm.replace(",","','")+"'";
            String sql =   " select id from hrmdepartment d where d.departmentcode in("+xtmc_bm+") ";
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                xtmcList.add(id);
            }
        }

        String xgxt_code = pb.getPropName("ITXQ_XGXT_CODE");
        //需求相关系统：电子印章管理平台
        String xgxt_code2 = pb.getPropName("xqxgxt2");
        int ishq = 0;
        int ishq2 = 0;
        if("2".equals(hgfxdj)){
            ishq++;
        }
        if("1".equals(sfxyw)){
            ishq++;
        }
        if(("1".equals(sfcyxxxt)) && ("1".equals(sfsjywxghgfkgn) || "2".equals(sfsjywxghgfkgn))){
            ishq++;
        }


        if(ywlxList.contains(szbm)){
            ishq++;
        }


        //if((xtmcList.contains(szbm)) &&((","+xgxt_code+",").indexOf(","+xqxgxt+",")>-1 )){
        //    ishq++;
        //}
        List<String> xgxt_codeList = itxqlcAction.convertStringToList(xgxt_code);

        List<String> xqxgxtList = new ArrayList<>();
        if (!StringUtils.isBlank(xqxgxt)){
            xqxgxtList = itxqlcAction.convertStringToList(xqxgxt);
        }
        if((xtmcList.contains(szbm)) && (!Collections.disjoint(xgxt_codeList, xqxgxtList))){
            ishq++;
        }

        List<String> xgxt_codeList2 = itxqlcAction.convertStringToList(xgxt_code2);
        // 判断两个集合是否有相同元素 有的话返回false，没有返回true
        if (!Collections.disjoint(xgxt_codeList2, xqxgxtList)){
            ishq2++;
        }

        if(ishq > 0)
        {
            if(!"".equals(bshq))
            {
                bshq = "'"+bshq.replace(",","','")+"'";
                String sql =   " select id,departmentname from hrmdepartment d where d.departmentcode in("+bshq+") ";
                rs.execute(sql);
                while (rs.next()){
                    String id = Util.null2String(rs.getString("id"));
                    hqbmvalue += hqbmvalue== "" ? id : ","+id ;
                }
            }
        }

        if(ishq2 > 0)
        {
            if(!"".equals(bshq2))
            {
                bshq2 = "'"+bshq2.replace(",","','")+"'";
                String sql =   " select id,departmentname from hrmdepartment d where d.departmentcode in("+bshq2+") ";
                rs.execute(sql);
                while (rs.next()){
                    String id = Util.null2String(rs.getString("id"));
                    hqbmvalue += hqbmvalue== "" ? id : ","+id ;
                }
            }
        }

        // 去重
        List<String> hqbmValueList = new ArrayList<>();
        if(!StringUtils.isBlank(hqbmvalue)){
            hqbmValueList = itxqlcAction.convertStringToList(hqbmvalue);
        }
        List<String> newHqbmValueList = itxqlcAction.delRepeat1(hqbmValueList);
        String newHqbmValue = newHqbmValueList.stream().collect(Collectors.joining(","));

        return newHqbmValue;
    }
}
