<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<jsp:useBean id="ResourceComInfo" class="weaver.hrm.resource.ResourceComInfo" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    out.println("start");
    JSONObject jsonObject = new JSONObject();

    updateUserBrithdayWishes();
    out.println("end");
    jsonObject.put("result",true);
%>
<%=jsonObject.toJSONString()  %>

<%!

    public void updateUserBrithdayWishes(){

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

        String uftable  = "uf_srzf";
        RecordSet rs = new RecordSet();
        List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
        Map<String,String> dataMap = null;

        BaseBean bb = new BaseBean();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd" ) ;
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy" ) ;
        int days = 15 ;
        try{

            for(int i=0;i<days;i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_YEAR, 0-i);
                String currentDay = sdf.format(calendar.getTime());
                bb.writeLog("currentDay:" + currentDay);

                String currentYear = sdf2.format(calendar.getTime());
                bb.writeLog("updateBirthdayWishes-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 right(birthday,5) = '"+currentDay+"' " +
                            "   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("updateAnniversaryWishes1-sql:"+sql);

                rs.executeQuery(sql);
                while (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);
                    dataList.add(dataMap);

                }
            }

            for(int i=0;i<days;i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_YEAR, i);
                String currentDay = sdf.format(calendar.getTime());
                bb.writeLog("currentDay:" + currentDay);

                String currentYear = sdf2.format(calendar.getTime());
                bb.writeLog("updateBirthdayWishes2-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 right(birthday,5) = '"+currentDay+"' " +
                            "   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("updateAnniversaryWishes-sql:"+sql);

                rs.executeQuery(sql);
                while (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);
                    dataList.add(dataMap);

                }
            }




            for(int i=0;i<dataList.size();i++){
                Map<String,String> dMap = dataList.get(i);
                String ry = dMap.get("ry");
                String nf = dMap.get("nf");
                String sryr = dMap.get("sryr");
                String ufid = "" ;
                String sql = " select id from "+uftable+" where ry='"+ry+"' and nf = '"+nf+"'";
                bb.writeLog("sql666:"+sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    ufid = Util.null2String(rs.getString("id"));
                }
                if(StringUtils.isBlank(ufid)){
                    updateBrithdayInfo(uftable,dMap);
                }else{
                    String zfy = dMap.get("zfy");
                    String sr = dMap.get("sr");
                    sql = " update "+uftable+" set zfy='"+zfy+"',sr='"+sr+"',sryr='"+sryr+"' where id = "+ ufid ;
                    boolean isTrue = rs.executeUpdate(sql);
                    bb.writeLog("isTrue:"+isTrue);
                }
            }

        }catch (Exception e){

        }
    }

    public int updateBrithdayInfo(String fromtable,Map<String,String> datas){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        int count = 0;
        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 != "") {

            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) {
                String dataid = "";
                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 count;
    }


    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>\n" ;

        return zfy;
    }


%>



