package com.engine.htsc.yghmzczysq.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.yghmzczysq.service.ZysqWorkflowService;
import com.weaver.general.TimeUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.record.DVALRecord;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;

public class ZysqWorkflowServiceImpl extends Service implements ZysqWorkflowService {
//    //营业部总经理
//    static  ArrayList<String> deptCodeList = new ArrayList<>();
//    //分公司负责人、总部部门负责人
//    static  ArrayList<String> companyDeptCodeList = new ArrayList<>();
//
//    static {
//        PropBean pb = new PropBean();
//        String codeList = pb.getPropName("deptCodeList");
//        if(StringUtils.isNotBlank(codeList)){
//            String[] split = codeList.split(",");
//            for (String code:split) {
//                deptCodeList.add(code);
//
//            }
//        }
//        codeList = pb.getPropName("companyDeptCodeList");
//        if(StringUtils.isNotBlank(codeList)){
//            String[] split = codeList.split(",");
//            for (String code:split) {
//                companyDeptCodeList.add(code);
//
//            }
//        }
//
//    }

    @Override
    public HashMap<String, String> getSeclevel(String userid) {
        HashMap<String, String> map = new HashMap<>();
        BaseBean bb = new BaseBean();

        try {
            RecordSet rs = new RecordSet();
            String sql = "select * from hrmresource where id = " + userid;
            bb.writeLog("通过人员的id查询安全级别sql"+sql);
            rs.execute(sql);
            rs.next();
            map.put("code","200");
            map.put("msg","success");
            bb.writeLog("人员的安全级别"+rs.getString("seclevel"));
            map.put("data",rs.getString("seclevel"));

        }catch (Exception e){
           map.put("code","500");
            map.put("msg","error");
            map.put("data",null);
       }
        return map;
    }

    /**
     *拟稿人是营业部总经理，根据营业部层级结构找到分公司总经理审；
     *
     * 拟稿人是分公司负责人、总部部门负责人，找拟稿人所在部门的分管领导；
     *
     * 拟稿人是资管子公司总经理聂挺进018850，到资管董事长崔春006600；
     *
     * 拟稿人是紫金子公司总经理樊欣008426，到紫金董事长曹群001645；
     *
     * 拟稿人是联合子公司总经理马骁009400，到联合董事长江禹009169；
     *
     * 拟稿人是香港子公司普通部门负责人，到部门的分管领导；
     *
     * 拟稿人是香港子公司首席执行官CEO王磊010377，到香港首席运营官COO王迎曦014582；
     *
     * 拟稿人是香港子公司首席运营官COO王迎曦014582，到香港首席执行官CEO王磊010377；
     * @param userid
     * @return
     */
    @Override
    public HashMap<String, Object> getLeader(String userid) {
        BaseBean bb = new BaseBean();
        PropBean pb = new PropBean();
        //营业部总经理
        ArrayList<String> deptCodeList = new ArrayList<>();
        //分公司负责人、总部部门负责人
        ArrayList<String> companyDeptCodeList = new ArrayList<>();
        //特殊人员
        HashMap<String, String> proples = new HashMap<>();

        String codeList = pb.getPropName("deptCodeList");
        if(StringUtils.isNotBlank(codeList)){
            String[] split = codeList.split(",");
            for (String code:split) {
                deptCodeList.add(code);

            }
        }
        codeList = pb.getPropName("companyDeptCodeList");
        if(StringUtils.isNotBlank(codeList)){
            String[] split = codeList.split(",");
            for (String code:split) {
                companyDeptCodeList.add(code);

            }
        }
        codeList = pb.getPropName("specialPreson");
        if(StringUtils.isNotBlank(codeList)){
            String[] split = codeList.split(",");
            for (String code:split) {
                String[] codes = code.split(":");

                proples.put(codes[0],codes[1]);
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<HashMap<String, String>> resultList = new ArrayList<>();
        try {
            RecordSet rs = new RecordSet();
            //判断是不是上面指定的5个人
            String getLoginId = "select * from hrmresource where id = " + userid +" and status = '1'";
            rs.execute(getLoginId);
            rs.next();
            String loginid = rs.getString("loginid");
            String workcode = rs.getString("workcode");
            if(proples.keySet().contains(loginid)){
                String leader = proples.get(loginid);
                String getId = "select * from hrmresource where loginid = '" + leader+"'"+" and status = '1'" ;
                rs.execute(getId);
                rs.next();
                map.put("code","200");
                map.put("msg","success");
                String id = rs.getString("id");
                map.put("data",getPerson(rs.getString("id")));
                return map;
            }
            //获取拟稿人的部门类型
            String getDeptCodeSql = "select * from  hrmdepartmentdefined where deptid =" +
                    " (select departmentid from  hrmresource where id = "+userid+" and status = '1')";
            rs.execute(getDeptCodeSql);
            rs.next();
            String deptCode = rs.getString("depttypecode");//部门类型
            String director = rs.getString("deptdirectors");//部门经理
            String deptid = rs.getString("deptid");//部门
            ArrayList<String> list = new ArrayList<>();
            if(director.contains(",")){
                String[] split = director.split(",");
                for (String id:split) {
                    String getWorkCode = "select * from hrmresource where id = " + id +" and status = '1'" ;
                    rs.execute(getWorkCode);
                    if(rs.next()){
                        list.add(rs.getString("workcode"));
                    }
                }
            }else{
                String getWorkCode = "select * from hrmresource where id = " + director +" and status = '1'";
                rs.execute(getWorkCode);
                if(rs.next()){
                    list.add(rs.getString("workcode"));
                }
            }
            //判断当前人员是不是负责人。不是的就直接返回空
            if(list.contains(workcode)){
                if(deptCodeList.contains(deptCode)){
                    //分公司营业部
                    String getSupdepidSql = "select * from  hrmdepartment where id =" +
                            " (select departmentid from  hrmresource where id = "+userid+" and status = '1')";
                    rs.execute(getSupdepidSql);
                    rs.next();
                    String supdepid = rs.getString("supdepid");//上级部门
                    String getsupdepLeaderSql = "select * from hrmdepartmentdefined where deptid =" +supdepid;
                    rs.execute(getsupdepLeaderSql);
                    rs.next();

                    map.put("code","200");
                    map.put("msg","success");
                    String deptdirectors = rs.getString("deptdirectors");
                    if(deptdirectors.indexOf(",")>-1){
                        String[] split = deptdirectors.split(",");
                        for (String deptdirector:split) {
                            resultList.add(getPerson(deptdirector));
                        }
                    }else{
                        resultList.add(getPerson(deptdirectors));
                    }

                    map.put("data",resultList);
                    return map;

                }else if(companyDeptCodeList.contains(deptCode)){
                  //分公司负责人、总部部门负责人取分管领导
                    String getDeptLeaderSql = "select * from hrmdepartmentdefined where deptid =" +deptid;
                    rs.execute(getDeptLeaderSql);
                    rs.next();
                    map.put("code","200");
                    map.put("msg","success");
                    String deptleaders = rs.getString("deptleaders");
                    if(deptleaders.indexOf(",")>-1){
                        String[] split = deptleaders.split(",");
                        for (String deptleader:split) {
                            resultList.add(getPerson(deptleader));
                        }
                    }else{
                        resultList.add(getPerson(deptleaders));
                    }

                    map.put("data",resultList);
                    return map;

                }
            }else{
                map.put("code","200");
                map.put("msg","success");
                map.put("data","");
                return map;
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","500");
            map.put("msg",e.getMessage());
            map.put("data","");
        }
        map.put("code","200");
        map.put("msg","success");
        map.put("data","");
        return map;
    }

    @Override
    public HashMap<String, Object> getNbshUser(String deptid) {
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        try {
            String getSql = "select id from hrmresource where departmentid = (select deptid from hrmdepartmentdefined where deptid = "+deptid+" and  depttypecode = 'JGLX03')  and jobtitle in (select id from hrmjobtitles where jobtitlecode = '2737') and status = 1" +
                    " union all " +
                    "select id from hrmresource where departmentid = (select deptid from hrmdepartmentdefined where deptid = "+deptid+" and  (depttypecode = 'JGLX02' or depttypecode = 'JGLX04') )  and jobtitle in (select id from hrmjobtitles where jobtitlecode = '2768') and status = 1";

            System.out.println(getSql);
            rs.execute(getSql);
            while(rs.next()){
                list.add(getPerson(rs.getString("id")));
            }
            map.put("code","200");
            map.put("msg","success");
            map.put("data",list);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","500");
            map.put("msg",e.getMessage());
            map.put("data",null);
        }
        map.put("code","200");
        map.put("msg","success");
        map.put("data","");
        return map;
    }

    @Override
    public HashMap<String, Object> getNbsh(String requestid,String formid,String deptid) {
        HashMap<String, Object> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        try {
            String getSql = "select id from hrmresource where departmentid = (select deptid from hrmdepartmentdefined where deptid = "+deptid+" and  depttypecode = 'JGLX03')  and jobtitle in (select id from hrmjobtitles where jobtitlecode = '2737') and status = 1" +
                    " union all " +
                    "select id from hrmresource where departmentid = (select deptid from hrmdepartmentdefined where deptid = "+deptid+" and  (depttypecode = 'JGLX02' or depttypecode = 'JGLX04') )  and jobtitle in (select id from hrmjobtitles where jobtitlecode = '2768') and status = 1";

            System.out.println(getSql);
            rs.execute(getSql);
            String userid ="";
            String ids = "";
            while(rs.next()){
                if(StringUtils.isNotBlank(rs.getString("id"))){
                    userid = rs.getString("id");
                    list.add(getPerson(userid));
                }
                ids = ids + rs.getString("id");
                ids +=",";

            }
            String getNbshSql = "select * from formtable_main_" + formid.substring(1) + " where requestid = " + requestid;
            rs.execute(getNbshSql);
            if(rs.next()){
                String nbsh = rs.getString("nbsh");
                if(nbsh.indexOf(",")>-1){
                    String[] split = nbsh.split(",");
                    for (String id:split) {
                        //去重
                        if(!Objects.equals(userid,id)){
                            list.add(getPerson(id));
                        }
                    }
                }else{
                    if(!((","+ids).indexOf(","+nbsh+",")  >-1)){
                        list.add(getPerson(nbsh));
                    }
                }
            }
            map.put("code","200");
            map.put("msg","success");
            map.put("data",list);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","500");
            map.put("msg",e.getMessage());
            map.put("data",null);
        }
        map.put("code","200");
        map.put("msg","success");
        map.put("data","");
        return map;
    }

    public HashMap<String,String> getPerson(String userid ){
        HashMap<String, String> person = new HashMap<>();
        if(StringUtils.isNotBlank(userid)){
            RecordSet rs = new RecordSet();
            String getPerson = "select * from hrmresource where id = " + userid;
            rs.execute(getPerson);
            if(rs.next()){
                person.put("id",rs.getString("id"));
                person.put("lastname",rs.getString("lastname"));
                person.put("departmentid",rs.getString("departmentid"));
                person.put("name",rs.getString("lastname"));
            }

        }

        return person;
    }
//    /**
//     * 会签部门联席负责人抄送
//     * @param requestid
//     * @param nodeid
//     * @param deptid
//     */
//    @Override
//    public Map<String,Object> noticeSignDeptCoDirector(String requestid, String nodeid, String deptid){
//        RecordSet rs = new RecordSet();
//        BaseBean bs = new BaseBean();
//        Map<String,Object> resultMap = new HashMap<>();
//
//        //联席负责人
//        String cohead = "";
//        String sql = "select cohead from hrmdepartmentdefined where deptid = '"+deptid+"'";
//        bs.writeLog(sql);
//        rs.executeQuery(sql);
//        if(rs.next()){
//            cohead = Util.null2String(rs.getString("cohead"));
//        }
//
//        if(StringUtils.isNotEmpty(cohead)){
//            for(String id:cohead.split(",")){
//                String workflowid = "";
//                String workflowtype = "";
//                sql = "select t1.id,t1.workflowtype from workflow_base t1,workflow_requestbase t2 where t2.workflowid = t1.id and t2.requestid = '"+requestid+"'";
//                bs.writeLog(sql);
//                rs.executeQuery(sql);
//                if(rs.next()){
//                    workflowid = Util.null2String(rs.getString("id"));
//                    workflowtype = Util.null2String(rs.getString("workflowtype"));
//                }
//
//                char flag = com.weaver.general.Util.getSeparator();
//                String operatorgroup = "0";
//                String creatertype = "0";
//                String currentdate = TimeUtil.getCurrentDateString();
//                String currenttime =  TimeUtil.getCurrentTimeString().substring(11,19);
//                String isremarktemp = "9";
//                String lastnodeid = ""+nodeid;
//                String groupdetailid = "-1";
//                String useridtemp = id;//抄送人
//                String Procparatemp = "" + requestid + flag + useridtemp + flag + operatorgroup + flag + workflowid + flag
//                        + workflowtype + flag + creatertype + flag + isremarktemp + flag + lastnodeid + flag + -1
//                        + flag + "0" + flag + -1 + flag + groupdetailid+flag+currentdate+flag+currenttime;
//                bs.writeLog("chaosong Procparatemp====="+Procparatemp);
//                boolean executesuccess = rs.executeProc("workflow_CurrentOperator_I2", Procparatemp);
//                resultMap.put("result",executesuccess);
//
//                if(executesuccess){
//                    bs.writeLog("CC SUCCESS:"+executesuccess);
//                }else{
//                    bs.writeLog("CC FAIL:"+executesuccess);
//                }
//            }
//        }
//        return resultMap;
//    }
//    @Override
//    public Map<String,Object> checkCoDirectorNotice(String requestid, String nodename){
//        RecordSet rs = new RecordSet();
//        BaseBean bs = new BaseBean();
//        PropBean pb = new PropBean();
//        Map<String,Object> resultMap = new HashMap<>();
//
//        try{
//            ResourceComInfo resourceComInfo = new ResourceComInfo();
//
//            String nodeid = "";
//            if("bmfzr".equals(nodename)){
//                nodeid = pb.getNodeIdsByUnNumber(pb.getPropName("lhcd.nodeunnumber.lhcd_bmfzrsh"));
//            }else if("hq".equals(nodename)){
//                nodeid = pb.getNodeIdsByUnNumber(pb.getPropName("lhcd.nodeunnumber.lhcd_fxgl"));
//            }
//
//            List<Map<String,String>> userMapList = new ArrayList<>();
//            String sql = "select userid from workflow_currentoperator where requestid = '"+requestid+"' and nodeid = '"+nodeid+"' and isremark = '9'";
//            bs.writeLog(sql);
//            rs.executeQuery(sql);
//            while(rs.next()){
//                Map<String,String> userMap = new HashMap<>();
//                String userid = Util.null2String(rs.getString("userid"));
//                userMap.put("id",userid);
//                userMap.put("name",resourceComInfo.getLastname(userid));
//                userMapList.add(userMap);
//            }
//
//            resultMap.put("result",userMapList);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return resultMap;
//    }

}
