<%@ 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 import="java.time.LocalDate" %>
<%@ page import="java.time.Period" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
    Map<String,String> data_Map = null;
    RecordSet rs = new RecordSet();
    BaseBean bb = new BaseBean();
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd" ) ;
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy" ) ;
    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd" ) ;
    int days = 30 ;

    String workflowid = "838";
    out.println("workflowid:"+workflowid+"</br>");
    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 ;
        }
    }
    out.println("formtable:"+formtable+"</br>");
    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 ) ";
    }
    out.println("where:"+where+"</br>");

//    for(int i=0;i<days;i++)
//    {
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_YEAR, i);
//        String currentDay = sdf.format(calendar.getTime()) ;
//        out.println("currentDay:"+currentDay+"</br>");
//        String currentYear = sdf2.format(calendar.getTime()) ;
//        out.println("currentYear:"+currentYear+"</br>");
//
//        String enddate = sdf3.format(calendar.getTime()) ;
//        out.println("enddate:"+enddate+"</br>");
//
//        sql =       "  select k.* from (select h.id,h.lastname,h.sex,h.departmentid,d.departmentname,s.subcompanyname,k.jobtitlename," +
//                    "   h.companystartdate,convert(varchar(50), getdate(), 23) as enddate," +
//                    "   (datediff(year,h.companystartdate,convert(varchar(50), getdate(), 23))-1) as zn" +
//                    "   from hrmresource h\n" +
//                    "   inner join HrmDepartment d on d.id = h.departmentid\n" +
//                    "   inner join HrmSubCompany s on s.id = h.subcompanyid1\n" +
//                    "   left join HrmJobTitles k on k.id = h.jobtitle\n" +
//                    "   where right(h.companystartdate,5) = '"+currentDay+"'\n" +
//                    "   and h.status in(0,1,2,3)" +
//                    "   and (h.belongto is null or  h.belongto = -1 ) " + where +
//                    "   and h.companystartdate is not null " +
//                    " ) k " ;
//
//        out.println("getCompanyAnniversary-sql:"+sql+"</br>");
//        rs.executeQuery(sql);
//        while (rs.next()){
//
//            String companystartdate = Util.null2String(rs.getString("companystartdate"));
//
//            if(StringUtils.isNotBlank(companystartdate) && StringUtils.isNotBlank(enddate)){
//                LocalDate date1 = LocalDate.parse(companystartdate);
//                LocalDate date2 = LocalDate.parse(enddate);
//
//                Period period = Period.between(date1, date2);
//                int yearsDiff = period.getYears(); // 获取年数差距
//                out.println("yearsDiff:"+yearsDiff+"</br>");
//                if(yearsDiff > 0){
//                    String userid = 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"));
//                    String jobtitlename = Util.null2String(rs.getString("jobtitlename"));
//
//                    String deptnamenew = "" ;
//                    data_Map = new HashMap<String, String>();
//                    data_Map.put("userid",userid);
//                    data_Map.put("sex",sex);
//                    data_Map.put("jobtitlename",jobtitlename);
//                    data_Map.put("lastname",lastname);
//                    data_Map.put("departmentname",departmentname);
//                    data_Map.put("subcompanyname",subcompanyname);
//                    if(StringUtils.isNotBlank(departmentid)){
//                        deptnamenew = getTopDepartment(departmentid);
//                    }
//                    if(StringUtils.isBlank(deptnamenew)){
//                        deptnamenew = departmentname ;
//                    }
//                    data_Map.put("deptnamenew",deptnamenew);
//                    data_Map.put("zn",yearsDiff+"");
//                    data_Map.put("nf",currentYear);
//                    dataList.add(data_Map);
//                }
//            }
//        }
//    }
//
//    out.println("dataList2:"+dataList.size()+"</br>");

    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()) ;
        out.println("currentDay:"+currentDay+"</br>");
        String currentYear = sdf2.format(calendar.getTime()) ;
        out.println("currentYear:"+currentYear+"</br>");

        String enddate = sdf3.format(calendar.getTime()) ;
        out.println("enddate2:"+enddate+"</br>");

        sql ="  select k.* from (select h.id,h.lastname,h.sex,h.departmentid,d.departmentname,s.subcompanyname,k.jobtitlename," +
                    "   h.companystartdate,convert(varchar(50), getdate(), 23) as enddate," +
                    "   (datediff(year,h.companystartdate,convert(varchar(50), getdate(), 23))-1) as zn" +
                    "   from hrmresource h\n" +
                    "   inner join HrmDepartment d on d.id = h.departmentid\n" +
                    "   inner join HrmSubCompany s on s.id = h.subcompanyid1\n" +
                    "   left join HrmJobTitles k on k.id = h.jobtitle\n" +
                    "   where right(h.companystartdate,5) = '"+currentDay+"'\n" +
                    "   and h.status in(0,1,2,3)" +
                    "   and (h.belongto is null or  h.belongto = -1 ) " + where +
                    "   and h.companystartdate is not null " +
                    " ) k " ;

        out.println("getCompanyAnniversary-sql:"+sql+"</br>");
        rs.executeQuery(sql);
        while (rs.next()){

            String companystartdate = Util.null2String(rs.getString("companystartdate"));

            if(StringUtils.isNotBlank(companystartdate) && StringUtils.isNotBlank(enddate)){
                LocalDate date1 = LocalDate.parse(companystartdate);
                LocalDate date2 = LocalDate.parse(enddate);

                Period period = Period.between(date1, date2);
                int yearsDiff = period.getYears(); // 获取年数差距
                out.println("yearsDiff:"+yearsDiff);
                if(yearsDiff > 0){
                    String userid = 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"));
                    String jobtitlename = Util.null2String(rs.getString("jobtitlename"));

                    String deptnamenew = "" ;
                    data_Map = new HashMap<String, String>();
                    data_Map.put("userid",userid);
                    data_Map.put("sex",sex);
                    data_Map.put("jobtitlename",jobtitlename);
                    data_Map.put("lastname",lastname);
                    data_Map.put("departmentname",departmentname);
                    data_Map.put("subcompanyname",subcompanyname);
                    if(StringUtils.isNotBlank(departmentid)){
                        deptnamenew = getTopDepartment(departmentid);
                    }
                    if(StringUtils.isBlank(deptnamenew)){
                        deptnamenew = departmentname ;
                    }
                    data_Map.put("deptnamenew",deptnamenew);
                    data_Map.put("zn",yearsDiff+"");
                    data_Map.put("nf",currentYear);
                    dataList.add(data_Map);
                }
            }
        }
    }

    out.println("dataList2:"+dataList.size()+"</br>");

    String modeid = "" ;
    String formid = "" ;
    String uftable  = "uf_rzznzfgly";
    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");
        int rzzn = Util.getIntValue(dMap.get("rzzn"));
        out.println("rzzn:"+rzzn+"</br>");

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

            sql = " select id from "+uftable+" where ry='"+ry+"' and nf ='"+nf+"'";
            rs.executeQuery(sql);
            if(rs.next()){
                billid = Util.null2String(rs.getString("id"));
            }

            out.println("billid:"+billid+"</br>");
            if(StringUtils.isBlank(billid)){
                billid = updateAnniversaryWishes(ry,modeid,nf,rzzn);
            }
            out.println("billid2:"+billid+"</br>");
        }

        dMap.put("billid",billid);
        dMap.put("modeid",modeid);
        dMap.put("formid",formid);
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("data",dataList);

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


<%!

    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;
    }



    public String updateAnniversaryWishes(String user_id,String modeid,String currentYear,int rzzn){

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

        String uftable  = "uf_rzznzfgly";
        RecordSet rs = new RecordSet();

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

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

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

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

            bb.writeLog("updateAnniversaryWishes-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 companystartdate = Util.null2String(rs.getString("companystartdate"));
                String zfy =getZfy(lastname,rzzn);
                String rzyr = Util.null2String(rs.getString("startday"));

                dataMap = new HashMap<String, String>();
                dataMap.put("ry",userid);
                dataMap.put("xm",lastname);
                dataMap.put("zfy",zfy);
                dataMap.put("bm",departmentid);
                dataMap.put("rzrq",companystartdate);
                dataMap.put("fb",subcompanyid1);
                dataMap.put("nf",currentYear);
                dataMap.put("rzzn",rzzn+"");
                dataMap.put("rzyr",rzyr);

            }

            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 = updateWishesInfo(uftable,dataMap,modeid);
                }
            }
        }catch (Exception e){

        }
        return billid;
    }


    public String updateWishesInfo(String fromtable,Map<String,String> datas,String formmodeid){
        RecordSet rs = new RecordSet();
        String dataid = "" ;
        BaseBean bb = new BaseBean();
        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";

        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;
            String 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 static String convert(int number) {
        if (number <= 0) {
            return "";
        }
        //数字对应的汉字
        String[] num = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
        //单位
        String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
        //将输入数字转换为字符串
        String result = String.valueOf(number);
        //将该字符串分割为数组存放
        char[] ch = result.toCharArray();
        //结果 字符串
        String str = "";
        int length = ch.length;
        for (int i = 0; i < length; i++) {
            int c = (int) ch[i] - 48;
            if (c != 0) {
                str += num[c - 1] + unit[length - i - 1];
            }
        }
        if (number < 20 && number > 9) {
            str = str.substring(1);
        }
//		System.out.println(str);
        return str ;
    }


    public String getZfy(String lastname,int rzzn){
        String rzzzdx = "" ;
        if(rzzn > 0){
            rzzzdx = convert(rzzn);
        }
        String zfy =" <div style=\"width: 500px;text-align: left;font-size: 14px !important;\">\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;\">衷心祝贺您与铭沣同行<span class=\"s1\">"+rzzzdx+"</span>周年快乐！</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;
    }

%>




