package weaver.interfaces.htsc.sfxzdj;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SfxzdjCustomSearchTemplate extends AbstractCustomSqlConditionJavaCode {

    /**
     * 生成SQL查询限制条件
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *
     * @return
     *  返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
     *  其中t1为表单主表表名的别名
     */
    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        User user = (User)param.get("user");

        JAVATools.writelog("司法查询登记条件开始user"+user.getUID());

        String sqlCondition = "";
        try{
            //建模人员uf_zzhgry
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            PropBean pb = new PropBean();
            String hrmSql = "select * from hrmresource where workcode = (select workcode from hrmresource where id = "+user.getUID()+")";
            rs1.execute(hrmSql);
            String ids = "";
            while(rs1.next()){
                ids = ids +rs1.getString("id")+",";
            }
            ids= ","+ids;
            JAVATools.writelog("司法查询登记ids"+ids);
            //合规人员

            ArrayList<String> hgrylist = new ArrayList<>();
            ArrayList<String> joblist = new ArrayList<>();
            ArrayList<String> hgflbfzrlist = new ArrayList<>();
            ArrayList<String> gdrlist = new ArrayList<>();

            String hgrySql = "select * from uf_zzhgry";
            rs.execute(hgrySql);
            while(rs.next()){
                String hgry = rs.getString("hgry");
                JAVATools.writelog("司法查询登记条件hgry"+hgry);
                hgrylist.add(hgry);
            }
            //岗位2737
            String jobSql = "select * from hrmresource where jobtitle   in (select id from hrmjobtitles where jobtitlecode = '2737') and status = 1";
            rs.execute(jobSql);
            while(rs.next()){
                String id = rs.getString("id");
                joblist.add(id);


            }
            //合规法律部负责人
            String hgflbfzrSql = "select * from  hrmresource where workcode = '" +pb.getPropName("sfxzfj_hgflbfzr_workcode")+"' and status = 1";
            JAVATools.writelog("合规法律部负责人hgflbfzrSql"+hgflbfzrSql);
            rs.execute(hgflbfzrSql);
            while(rs.next()){
                hgflbfzrlist.add(rs.getString("id"));
            }
            //016185,011659 固定人员
            String gdSql = "select * from  hrmresource where workcode in ('"+pb.getPropName("sfxzdj_fltdfzr_workcode")+"','"+pb.getPropName("sfxzdj_fsg_workcode")+"') and status = 1";
            JAVATools.writelog("合规法律部负责人gdSql"+gdSql);
            rs.execute(gdSql);
            while(rs.next()){
                gdrlist.add(rs.getString("id"));
            }
            //判断当前人员是不是符合五种要求
//            分公司合规负责人	查询、查看分公司辖区内所有流程
//            找人逻辑：岗位编码为2737（OA的岗位业务code编码，不是泛微的id）
//            注意：过滤离职人员
//            合规法律部负责人	查询、查看所有流程
//            法务团队负责人	查询、查看所有流程	王铁勋（016185）
//            分送岗	        查询、查看所有流程	马元（011659）
//            营业部合规人员 查询、查看本营业部下所有流程


            if(getTrueOrFalse(ids,hgrylist) || getTrueOrFalse(ids,joblist) || getTrueOrFalse(ids,hgflbfzrlist) ||getTrueOrFalse(ids,gdrlist)){
                RecordSet rs2 = new RecordSet();
                for (String hgry :hgrylist) {
                    if(ids.indexOf(","+hgry+",")>-1){
                        JAVATools.writelog("合规人员条件hgry"+hgry);
                        String sql = "select * from uf_zzhgry where to_char(hgry) = '"+hgry+"'";
                        rs2.execute(sql);
                        rs2.next();
                        String yyb = rs2.getString("yyb");
                        sqlCondition += sqlCondition .equals("")?"(t1.szbm = '" +yyb+ "'" :" or t1.szbm = '" +yyb+ "'";
                        JAVATools.writelog("合规人员条件"+sqlCondition);
                    }
                }
                sqlCondition += sqlCondition .equals("")?"" :")";
                for (String id :joblist) {
                    if(ids.indexOf(","+id+",")>-1){
                        String sql = "select * from hrmresource where id = '"+id+"'";
                        rs2.execute(sql);
                        rs2.next();
                        String departmentid = rs2.getString("departmentid");
                        sqlCondition +=sqlCondition.equals("")? " (select supdepid from hrmdepartment where id = t1.szbm)='"+departmentid+"'":" or (select supdepid from hrmdepartment where id = t1.szbm)='"+departmentid+"'";
                        JAVATools.writelog("分公司负责人"+sqlCondition);
                    }
                }
                for (String id :hgflbfzrlist) {
                    if(ids.indexOf(","+id+",")>-1){

                        sqlCondition +=sqlCondition.equals("")? " 1=1 ":" and 1=1";
                        JAVATools.writelog("合规法律部负责人"+sqlCondition);
                    }
                }
                for (String id :gdrlist) {
                    if(ids.indexOf(","+id+",")>-1){
                        sqlCondition +=sqlCondition.equals("")? " 1=1 ":" and 1=1";
                        JAVATools.writelog("法务团队负责人,分送岗"+sqlCondition);
                    }
                }
            }else{
                sqlCondition = "1=2";
            }


        }catch (Exception e){
            JAVATools.writelog("司法查询登记条件报错r"+e.getMessage());
        }
        return sqlCondition;
    }

    public boolean getTrueOrFalse(String ids , List<String> list){
        for (String str :list) {
            if(ids.indexOf(","+str+",")>-1){
                return true;
            }
        }
        return false;
    }

}

