<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="com.engine.kq.biz.KQGroupMemberComInfo" %>
<%@ page import="com.engine.kq.biz.KQGroupComInfo" %>
<%@ page import="com.engine.kq.biz.KQGroupBiz" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%

    User user = HrmUserVarify.getUser(request, response);
    if(user==null) {
        response.sendRedirect("/login/Login.jsp");
        return;
    }

    BaseBean bb = new BaseBean();
    RecordSet rs = new RecordSet();
    String cjrbm = request.getParameter("cjrbm");
    String pbyf = request.getParameter("pbyf");

    List<Map<String,String>> hrmList = new ArrayList<Map<String,String>>();
    Map<String,Object> valueMap = new HashMap<>();
    List<Map<String,Object>> dt1List = new ArrayList<Map<String,Object>>();
    if(StringUtils.isNotBlank(cjrbm)) {
        String sql =" select id,lastname from hrmresource where departmentid = "+cjrbm +" and status in(0,1,2,3) and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理')) and id not in(select xm from uf_ygddxx where left(ydsxrq,7) = '"+pbyf+"')"+
                    " union "+
                    " select id,lastname from hrmresource where status in(0,1,2,3) and id in(select xm from uf_ygddxx where left(ydsxrq,7) = '"+pbyf+"' and ydhbm ="+cjrbm+") and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理')) "+
                    " union "+
                    " select id,lastname from hrmresource where status in(0,1,2,3) and id in(select xm from uf_ygddxx where left(ydsxrq,7) = '"+pbyf+"' and ydsxrq >='"+pbyf+"-02' and bm ="+cjrbm+") and jobtitle not in(select id from hrmjobtitles where jobtitlename in('营运经理')) " ;

        hrmList = queryListBySql(sql);
        valueMap = getMainUserDate(hrmList);
        dt1List = getDt1UserDate(hrmList);
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("data",valueMap);
    jsonObject.put("dt1",dt1List);

    String userid = user.getUID()+"" ;
//    if(hrmList !=null && hrmList.size() > 0){
//        userid = hrmList.get(0).get("id");
//    }

    if(StringUtils.isNotBlank(userid)){
        String activeGroupId = getUserActiveGroupId(userid);
        String serialids = "";
        if(StringUtils.isNotBlank(activeGroupId)){
            String sql = " select serialids from kq_group where id = "+activeGroupId ;
            rs.executeQuery(sql);
            if(rs.next()){
                serialids = Util.null2String(rs.getString("serialids"));
            }
        }
        jsonObject.put("group",activeGroupId);
        jsonObject.put("serialids",serialids);
    }

%>

<%=jsonObject.toJSONString() %>


<%!

    public List<Map<String,Object>> getDt1UserDate(List<Map<String,String>> hrmList){
        List<Map<String,Object>> dt1List = new ArrayList<Map<String,Object>>();
        for(int i=0;i<hrmList.size();i++){
            Map<String,String> hrmMap = hrmList.get(i);

            String userid = Util.null2String(hrmMap.get("id"));
            String lastname = Util.null2String(hrmMap.get("lastname"));

            Map<String,Object> dt1Map = new HashMap<String,Object>();
            dt1Map.put("id",userid);
            dt1Map.put("name",lastname);

            List<Map<String,String>> specialobjList2 = new ArrayList<Map<String,String>>();
            Map<String,String> specialobjMap2 = new HashMap<String,String>();
            specialobjMap2.put("id",userid);
            specialobjMap2.put("name",lastname);
            specialobjList2.add(specialobjMap2);

            Map<String,Object> valueMap2 = new HashMap<String,Object>();
            valueMap2.put("value",userid);
            valueMap2.put("specialobj",specialobjList2);
            dt1Map.put("value",valueMap2);
            dt1List.add(dt1Map);
        }
        return dt1List;
    }
    /***
     *
     * @param hrmList
     * @return
     */
    public Map<String,Object> getMainUserDate(List<Map<String,String>> hrmList){
        String userids = "";
        List<Map<String,String>> specialobjList = new ArrayList<Map<String,String>>();
        for(int i=0;i<hrmList.size();i++){
            Map<String,String> hrmMap = hrmList.get(i);
            String userid = Util.null2String(hrmMap.get("id"));
            String lastname = Util.null2String(hrmMap.get("lastname"));
            userids += StringUtils.isBlank(userids) ? userid : ","+userid ;

            Map<String,String> specialobjMap = new HashMap<String,String>();
            specialobjMap.put("id",userid);
            specialobjMap.put("name",lastname);
            specialobjList.add(specialobjMap);
        }

        Map<String,Object> valueMap = new HashMap<String,Object>();
        valueMap.put("value",userids);
        valueMap.put("specialobj",specialobjList);
        return valueMap;
    }

    public List<Map<String,String>> queryListBySql(String sql){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        try{
            if(StringUtils.isNotBlank(sql)){
                Map<String,String> map = new HashMap<String,String>();
                rs.executeQuery(sql);
                String[] column = rs.getColumnName();
                while (rs.next()){
                    map = new HashMap<String,String>();
                    for(int i=0;i<column.length;i++){
                        String value = rs.getString(column[i]);
                        map.put(column[i],value);
                    }
                    if(!map.isEmpty() && map.size()>0){
                        list.add(map);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("queryListBySql-e:"+e);
        }
        return list;
    }


    public String getUserActiveGroupId(String userid){
        BaseBean bb = new BaseBean();

        KQGroupMemberComInfo kqGroupMemberComInfo = null;
        KQGroupComInfo kqGroupComInfo = null ;
        String activeGroupId = "";
        try{
            kqGroupMemberComInfo = new KQGroupMemberComInfo();
            kqGroupComInfo = new KQGroupComInfo();

            Map<String, List<String>> groupMemberMap = getUserGroup();

            String activeGroup = "";
            if(groupMemberMap.containsKey(userid)){
                List<String> groupIds = groupMemberMap.get(userid);
                bb.writeLog("groupIds:"+groupIds.size());

                for(String i : groupIds){
                    String isDelete = kqGroupComInfo.getIsdelete(i);
                    if(!"1".equals(isDelete)) {
                        String currentGroupId = Util.null2String(kqGroupMemberComInfo.getKQGroupId(userid, DateUtil.getCurrentDate()));
                        if (i.equals(currentGroupId)) {
                            activeGroupId =  i;
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog(e);
        }
        return activeGroupId;
    }


    /***
     *
     * @return
     */
    public Map<String, List<String>> getUserGroup(){
        BaseBean bb = new BaseBean();
        Map<String, List<String>> groupMemberMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        String viewScope = "3";
        try {
            KQGroupBiz kqGroupBiz = new KQGroupBiz();
            Map<String, Object> groupParams = new HashMap<String, Object>();
            if("3".equals(viewScope)){
                groupParams.put("isNoAccount", "1");
            }
            rs.executeQuery(kqGroupBiz.getGroupMemberSql(groupParams));
            while (rs.next()) {
                String id = rs.getString("resourceid");
                String gid = rs.getString("groupid");
                if (!groupMemberMap.containsKey(id))
                    groupMemberMap.put(id, new ArrayList<>());
                groupMemberMap.get(id).add(gid);
            }
        } catch (Exception e) {
        }
        return groupMemberMap;
    }


%>


