<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.api.login.util.LoginUtil" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="com.icbc.api.internal.apache.http.impl.cookie.S" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.general.StringUtil" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
        //获取当前用户
        User user = HrmUserVarify.getUser(request, response);
        RecordSet rs = new RecordSet();
        RecordSet recordSet = new RecordSet();
        JSONArray conArr = new JSONArray();
        JSONObject dataObj = new JSONObject();
        String userid = user.getUID() + "";
        boolean isdialog = true;
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String nowStr = now.format(formatter);
        String id = "";
        String title = "";
        String backgroundAddress = "";
        String sql = "select * from uf_MsgTopDialog where startDate <= ? and endDate >= ? order by id desc";
        rs.executeQuery(sql, nowStr, nowStr);
        while (rs.next()) {
            String dataid = Util.null2String(rs.getString("id"));
            String scope = Util.null2String(rs.getString("scope"));
            String role = Util.null2String(rs.getString("role"));
            boolean isLatest = getLatestData(dataid, user, recordSet, scope, role);
            if (isLatest) {
                id = Util.null2String(rs.getString("id"));
                title = Util.null2String(rs.getString("title"));
                backgroundAddress = Util.null2String(rs.getString("backgroundAddress"));
                break;
            }
        }
        if (!"".equals(id)) {
            sql = "select count(1) as sl from uf_MsgTopDialog_dt2 where mainid = '" + id + "' and hrmid = '" + userid + "'";
            rs.execute(sql);
            while (rs.next()) {
                if (Util.getIntValue(rs.getString("sl")) > 0) {
                    isdialog = false;
                }
            }
        } else {
            isdialog = false;
        }
        if (isdialog){
            dataObj.put("id", id);
            dataObj.put("tilte", title);
            dataObj.put("backgroundAddress", backgroundAddress);
        }
        apidatas.put("data", dataObj);
        apidatas.put("api_status", true);
    } catch (Exception e) {
        e.printStackTrace();
        apidatas.put("api_status", false);
        apidatas.put("api_errormsg", "getVersionInfo catch exception : " + e.getMessage());
    }
    out.print(JSONObject.toJSONString(apidatas));
%>
<%!

    private boolean getLatestData(String id, User user, RecordSet recordSet, String scope, String role) {
        HrmUserVarify hrmUserVarify = new HrmUserVarify();

        if ("0".equals(scope)) {
            //角色
            if (!StringUtil.isEmpty(role)) {
                String[] roleArr = role.split(",");
                for (String roleid : roleArr) {
                    boolean b = hrmUserVarify.checkUserRole(roleid, user, user.getUserDepartment() + "");
                    if(b){
                        return true;
                    }
                }
            }
        } else if ("1".equals(scope)) {
            //多人力
            String sql = "select count(main.id) cnt from uf_MsgTopDialog main left join uf_MsgTopDialog_dt1 dt  on main.id = dt.mainid where main.id = ? and dt.hrmid = ?";
            recordSet.executeQuery(sql,id,user.getUID());
            if(recordSet.next()){
                int cnt = recordSet.getInt("cnt");
                if(cnt > 0 ){
                    return true;
                }
            }
        }


        return false;
    }

%>