<%@ page import="weaver.general.*" %>
<%@ page import="weaver.conn.RecordSet"%>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%
    List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
    Map<String,String> dataMap = null;
    RecordSet rs = new RecordSet();
    BaseBean bb = new BaseBean();
    SimpleDateFormat sdf = new SimpleDateFormat("MM") ;
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy" ) ;


    String workflowid = "890";
    String formtable = "" ;
    String sql = "select abs(formid) as formid from workflow_base where id = "+workflowid ;
    rs.executeQuery(sql);
    if(rs.next()){
        String formid = Util.null2String(rs.getString("formid"));
        if(StringUtils.isNotBlank(formid)){
            formtable = "formtable_main_"+formid ;
        }
    }

    String where  = "" ;
    if(StringUtils.isNotBlank(formtable)){
        where = " and h.id not in(select k.lzyg \n" +
                " from workflow_requestbase p\n" +
                " inner join "+formtable+" k on k.requestid = p.requestid\n" +
                " where workflowid = "+workflowid+"\n" +
                " and p.currentnodetype = 3 ) ";
    }

    String currentMonth = sdf.format(new Date());
    bb.writeLog("currentMonth:"+currentMonth);
    String currentYear = sdf2.format(new Date());
    bb.writeLog("currentYear:"+currentYear);

    sql =" select h.id,h.lastname,h.sex,h.departmentid,d.departmentname,s.subcompanyname from hrmresource h\n" +
                " inner join HrmDepartment d on d.id = h.departmentid\n" +
                " inner join HrmSubCompany s on s.id = h.subcompanyid1\n" +
                " where right(left(h.birthday,7),2) ='"+currentMonth+"'\n" +
                " and h.status in(0,1,2,3) and h.birthday is not null " + where+
                " and (h.belongto is null or  h.belongto = -1 ) " ;
    bb.writeLog("sql:"+sql);
    rs.executeQuery(sql);
    while (rs.next()){
        String id = Util.null2String(rs.getString("id"));
        String lastname = Util.null2String(rs.getString("lastname"));
        String sex = Util.null2String(rs.getString("sex"));
        String departmentname = Util.null2String(rs.getString("departmentname"));
        String subcompanyname = Util.null2String(rs.getString("subcompanyname"));
        String departmentid = Util.null2String(rs.getString("departmentid"));

        dataMap = new HashMap<String, String>();
        dataMap.put("userid",id);
        dataMap.put("departmentid",departmentid);
        dataMap.put("sex",sex);
        dataMap.put("lastname",lastname);
        dataMap.put("departmentname",departmentname);
        dataMap.put("subcompanyname",subcompanyname);
        dataMap.put("nf",currentYear);
        dataList.add(dataMap);
    }


    String modeid = "" ;
    String formid = "" ;
    String uftable  = "uf_srzf";

    sql =" select k.id,k.formid from modeinfo k  \n" +
            " inner join workflow_bill l on formid = l.id\n" +
            " where l.tablename = '"+uftable+"' " ;

    rs.execute(sql);
    if(rs.next()){
        modeid = Util.null2String(rs.getString("id"));
        formid = Util.null2String(rs.getString("formid"));
    }

    for(int i=0;i<dataList.size();i++){
        String billid = "" ;

        Map<String,String> dMap = dataList.get(i);
        String nf = dMap.get("nf");
        String ry = dMap.get("userid");
        String departmentid = dMap.get("departmentid");
        String departmentname = dMap.get("departmentname");

        if(StringUtils.isNotBlank(ry) && StringUtils.isNotBlank(nf)){

            sql = " select id from "+uftable+" where ry='"+ry+"' and nf ='"+nf+"'";
            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql);
            if(rs.next()){
                billid = Util.null2String(rs.getString("id"));
            }
            bb.writeLog("billid:"+billid);
            if(StringUtils.isBlank(billid)){
                billid = updateUserBrithdayWishes(ry,uftable,nf);
            }
        }
        String deptnamenew = "";
        if(StringUtils.isNotBlank(departmentid)){
            deptnamenew = getTopDepartment(departmentid);
        }

        if(StringUtils.isBlank(deptnamenew)){
            deptnamenew = departmentname ;
        }
        dMap.put("deptnamenew",deptnamenew);
        dMap.put("billid",billid);
        dMap.put("modeid",modeid);
        dMap.put("formid",formid);
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("data",dataList);

%>
<%=jsonObject.toJSONString() %>


<%!


    public String getTopDepartment(String departmentid){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String departmentname = "" ;
        if(StringUtils.isNotBlank(departmentid)){
            String sql = " with tem_table(department_id,department_name,supdep_id,curlevel) as (\n" +
                    "  select id,departmentname,supdepid, 1 as level \n" +
                    "   from  HrmDepartment  where id = " + departmentid+
                    "  union all \n" +
                    "  select  a.id,a.departmentname,a.supdepid, b.curlevel+1 \n" +
                    "  from HrmDepartment  a \n" +
                    "  inner join tem_table b on (a.id = b.supdep_id)\n" +
                    " ) \n" +
                    " select * from tem_table ";

            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql);
            while (rs.next()){
                String department_id = Util.null2String(rs.getString("department_id"));
                String department_name = Util.null2String(rs.getString("department_name"));
                String supdep_id = Util.null2String(rs.getString("supdep_id"));
                if(StringUtils.isBlank(supdep_id) || "0".equals(supdep_id)){
                    departmentname = department_name ;
                }
            }
        }
        bb.writeLog("departmentname:"+departmentname);

        return departmentname;
    }

    /***
     *
     * @param user_id
     * @param uftable
     * @return
     */
    public String updateUserBrithdayWishes(String user_id,String uftable,String currentYear){

        String billid = "";
        BaseBean log = new BaseBean();
        log.writeLog("updateUserBrithdayWishes! ");

        RecordSet rs = new RecordSet();

        BaseBean bb = new BaseBean();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy" ) ;

        try{
            Map<String,String> dataMap = new HashMap<String,String>();

            bb.writeLog("updateUserBrithdayWishes-currentYear:"+currentYear);

            String sql ="	select h.id,h.lastname,h.sex,h.birthday,h.departmentid,h.subcompanyid1,right(h.birthday,5) startday " +
                    "   from hrmresource h " +
                    "   where id = "+user_id+" " +
                    "   and h.status in(0,1,2,3) " +
                    "   and (h.belongto is null or  h.belongto = -1 ) " +
                    "	and h.birthday is not null " +
                    "   order by id " ;

            bb.writeLog("updateUserBrithdayWishes-sql:"+sql);

            rs.executeQuery(sql);
            if (rs.next()){
                String userid = Util.null2String(rs.getString("id"));
                String lastname = Util.null2String(rs.getString("lastname"));
                String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1"));
                String departmentid = Util.null2String(rs.getString("departmentid"));
                String birthday = Util.null2String(rs.getString("birthday"));
                String sryr = Util.null2String(rs.getString("startday"));

                String zfy = getZfy(lastname);

                dataMap = new HashMap<String, String>();
                dataMap.put("ry",userid);
                dataMap.put("rymc",lastname);
                dataMap.put("ryid",userid);
                dataMap.put("zfy",zfy);
                dataMap.put("bm",departmentid);
                dataMap.put("sr",birthday);
                dataMap.put("fb",subcompanyid1);
                dataMap.put("nf",currentYear);
                dataMap.put("sryr",sryr);

            }

            if(!dataMap.isEmpty()){
                String ry = dataMap.get("ry");
                String nf = dataMap.get("nf");
                sql = " select id from "+uftable+" where ry='"+ry+"' and nf = '"+nf+"'";
                rs.executeQuery(sql);
                if(rs.next()){
                    billid = Util.null2String(rs.getString("id"));
                }
                if(StringUtils.isBlank(billid)){
                    billid = updateBrithdayInfo(uftable,dataMap);
                }
            }
        }catch (Exception e){

        }
        return billid;
    }

    public String updateBrithdayInfo(String fromtable,Map<String,String> datas){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();

        String dataid = "";

        ModeRightInfo modeRightInfo = new ModeRightInfo();
        String uuid = UUID.randomUUID().toString().replace("-","");
        bb.writeLog("uuid:"+uuid);

        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
        String currentdate = formatter1.format(new Date());
        String currenttime = formatter2.format(new Date());
        String modedatacreater = "1";
        String modedatacreatertype = "0";

        String formmodeid = "" ;
        String sql =" select k.id from modeinfo k  \n" +
                " inner join workflow_bill l on formid = l.id\n" +
                " where l.tablename = '"+fromtable+"' " ;

        rs.execute(sql);
        if(rs.next()){
            formmodeid = Util.null2String(rs.getString("id"));
        }

        bb.writeLog("formmodeid:"+formmodeid);
        datas.put("formmodeid", formmodeid);
        datas.put("modedatacreater", modedatacreater);
        datas.put("modedatacreatertype", modedatacreatertype);
        datas.put("modedatacreatedate", currentdate);
        datas.put("modedatacreatetime", currenttime);
        datas.put("uuid", uuid);

        String tablenvalue = "";
        String tablename = "";
        for (Map.Entry<String, String> entry : datas.entrySet()) {
            String fieldname = Util.null2String(entry.getKey()).trim();
            String fieldvalue = Util.null2String(entry.getValue()).replace("'", "''").trim();
            tablename += tablename == "" ? fieldname : "," + fieldname;
            tablenvalue += tablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
        }
        if (tablename != "" && tablenvalue != "") {
            int count  = 0;
            sql = " insert into " + fromtable + "(" + tablename + ") values (" + tablenvalue + ")";
            bb.writeLog("sql:" + sql);
            boolean boo = rs.executeUpdate(sql);
            if (boo) {
                count++;
            }
            bb.writeLog("count:" + count);
            if (count > 0) {

                sql = " select id from " + fromtable + " where uuid='" + uuid + "'";
                bb.writeLog("sql:" + sql);
                rs.execute(sql);
                if (rs.next()) {
                    dataid = Util.null2String(rs.getString("id"));
                }
                if (!"".equals(dataid)) {
                    modeRightInfo.editModeDataShare(Integer.valueOf(modedatacreater), Util.getIntValue(formmodeid), Integer.parseInt(dataid));
                }
            }
        }
        return dataid;
    }


    public String getZfy(String lastname){

        String zfy =" <div style=\"width: 500px;font-size: 14px !important;text-align: left;\">\n" +
                "    <p class=\"p1\" style=\"color: #202e7f;font-size: 18px;font-weight: 600;\">亲爱的<span class=\"s1\">"+lastname+"</span>：</p>\n" +
                "    <p class=\"p2\">&nbsp;</p>\n" +
                "    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">时光是一把神奇的钥匙，让每个脚印都坚实而有力量；</p>\n" +
                "    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">岁月是看不见的年轮，让每次付出都收获希望与果实；</p>\n" +
                "    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">感谢您的全力奔跑，感恩您的笃定前行；</p>\n" +
                "    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">今天是您的生日！</p>\n" +
                "    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">在这个没有流星也可以许愿的日子里，祝您生日快乐！</p>\n" +
                "    <p class=\"p3\" style=\"color: #202e7f;line-height: 30px;\">愿您心之所向，光芒万丈，所盼皆所成，所思终所拥！</p>\n" +
                "    <p class=\"p2\">&nbsp;</p>\n" +
                "    <p class=\"p2\">&nbsp;</p>\n" +
                "    <p class=\"p4\" style=\"float: right;color: #202e7f;font-size: 18px;font-weight: 600;\">&nbsp;MATFRON</p>\n" +
                "</div>";
        return zfy;
    }


%>