<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.api.doc.detail.util.DocDownloadCheckUtil" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%

    RecordSet rs = new RecordSet();
    String lx = Util.null2String(request.getParameter("lx"));
    User user  = HrmUserVarify.getUser(request , response) ;
    int userid = user.getUID();
    String billid = "";
    String pxfs = "";
    String sql = " select * from uf_zdyszb where ry = ?" ;
    rs.executeQuery(sql,new Object[]{userid});
    if(rs.next()){
        billid = Util.null2String(rs.getString("id"));
        pxfs = Util.null2String(rs.getString("pxfs"));
    }

    List<Map<String,String>> data = new ArrayList<Map<String,String>>();

    if(StringUtils.isEmpty(billid)){
        data = getCustomMenuInfoByDefault(lx);
    }else{
        data = getCustomMenuInfoByBillid(billid,pxfs);
        if(data.size() == 0){
            data = getCustomMenuInfoByDefault(lx);
        }
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("data",data);
    jsonObject.put("billid",billid);
    jsonObject.put("pxfs",pxfs);
    jsonObject.put("count",data.size());
    jsonObject.put("userid",user.getUID());
    jsonObject.put("lastname",user.getLastname());
%>
<%=jsonObject.toString()  %>


<%!
    /***
     *
     * @param lx
     * @return
     */
    public List<Map<String,String>> getCustomMenuInfoByDefault(String lx)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        try {
            Map<String,String> map = null;
            String sql =   " select id,mc,tb,djhtb,url from uf_tbpzb where lx = ? order by isnull(sx,0)  ";
            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql,new Object[]{lx});
            while (rs.next()){
                String id = Util.null2String(rs.getString("mc"));
                String mc = Util.null2String(rs.getString("mc"));
                String lj = Util.null2String(rs.getString("url"));
                String djqtb = Util.null2String(rs.getString("tb"));
                String djhtb = Util.null2String(rs.getString("djhtb"));
                map = new HashMap<String,String>();
                map.put("dataid",id);
                map.put("lcmc",mc);
                map.put("djqtb",djqtb);
                map.put("djhtb",djhtb);
                map.put("lj",lj);
                list.add(map);
            }

            for(int i=0;i<list.size();i++)
            {
                Map<String,String> dataMap = list.get(i);
                String djqtb = Util.null2String(dataMap.get("djqtb"));
                String djhtb = Util.null2String(dataMap.get("djhtb"));
                String imagefileid = "" ;
                if(StringUtils.isNotEmpty(djqtb)){
                    sql =   " select k.imagefileid\n" +
                            " from docdetail t\n" +
                            " inner join docimagefile l on l.docid = t.id\n" +
                            " inner join imagefile k on k.imagefileid = l.imagefileid\n" +
                            " where docid= "+ djqtb;
                    rs.executeQuery(sql);
                    if(rs.next()){
                        imagefileid = Util.null2String(rs.getString("imagefileid"));
                    }

                    String fileid = "";
                    if(StringUtils.isNotEmpty(imagefileid)){
                        try{
                            fileid = DocDownloadCheckUtil.checkPermission(imagefileid+"",null);
                        }catch (Exception e){
                            bb.writeLog("e:"+e);
                        }
                    }
                    String mrzstburl = "/weaver/weaver.file.FileDownload?fileid="+fileid+"";
                    dataMap.put("djqtburl",mrzstburl);
                }

                if(StringUtils.isNotEmpty(djhtb)){
                    sql =   " select k.imagefileid\n" +
                            " from docdetail t\n" +
                            " inner join docimagefile l on l.docid = t.id\n" +
                            " inner join imagefile k on k.imagefileid = l.imagefileid\n" +
                            " where docid= "+ djhtb;
                    rs.executeQuery(sql);
                    if(rs.next()){
                        imagefileid = Util.null2String(rs.getString("imagefileid"));
                    }

                    String fileid = "";
                    if(StringUtils.isNotEmpty(imagefileid)){
                        try{
                            fileid = DocDownloadCheckUtil.checkPermission(imagefileid+"",null);
                        }catch (Exception e){
                            bb.writeLog("e:"+e);
                        }
                    }

                    String sbxftburl = "/weaver/weaver.file.FileDownload?fileid="+fileid+"";
                    dataMap.put("djhtburl",sbxftburl);
                }
            }
        }catch (Exception e){

        }
        return list;
    }


    /***
     *
     * @return
     */
    public List<Map<String,String>> getCustomMenuInfoByBillid(String billid,String pxfs)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        try {
            Map<String,String> map = null;
            String orderby  = "" ;
            if("1".equals(pxfs)){
                orderby = " order by isnull(djcs,0) desc,isnull(sx,0)";
            }else{
                orderby = " order by isnull(sx,0) ";
            }
            String sql =   " select * from uf_zdyszb_dt1 where mainid = ? " +orderby;
            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql,new Object[]{billid});
            while (rs.next()){
                String menunamemultlang = Util.null2String(rs.getString("menunamemultlang"));
                String menulink = Util.null2String(rs.getString("menulink"));

                String iconurl = Util.null2String(rs.getString("iconurl"));
                String id = Util.null2String(rs.getString("uuid"));

                map = new HashMap<String,String>();
                map.put("dataid",id);
                map.put("lcmc",menunamemultlang);
                map.put("lj",menulink);
                map.put("djqtburl",iconurl);
                map.put("djhtburl",iconurl);
                list.add(map);
            }
        }catch (Exception e){

        }
        return list;
    }
%>



