<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/11
  Time: 12:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%!
    // 获取当前一周的一星期的日期
    private static List<String> GetCurrentWeekAllDate(){
        // 获取当天的毫秒时间
        long currentTimeMillis = System.currentTimeMillis();
        List<String> list = getAllWeekDayDateByMillis(currentTimeMillis);
        return list;
    }

    // 获取指定任意日期当周的一星期的日期
    private static void GetSomedayWeekAllDate(){
        // 指定天的毫秒时间
        long currentTimeMillis = getWhatDaySomeDateMillis("2019-10-15");
        List<String> list = getAllWeekDayDateByMillis(currentTimeMillis);
        for(String date:list) {
           // System.out.println("date : "+date);
        }
    }

    // someDataStr 格式："yyyy-MM-dd"
    // 返回 时间毫秒
    private static long getWhatDaySomeDateMillis(String someDataStr) {
        Date date= new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(someDataStr);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    // 获取指定日期毫秒时间得到 星期几
    private static String getWhatDaySomeday(long timeMillis) {
        Date toDayDate = new Date(timeMillis);
        SimpleDateFormat formatE = new SimpleDateFormat("E");
        String week =null;
        try {
            week = formatE.format(toDayDate);
            //System.out.println(week);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return week;
    }

    // 根据 星期几到获取与 星期一 相差几天
    private static int getHowManyDayFromMonday(String someDay) {
        int day = 0;
        switch(someDay) {
            case "星期一":
                day=0;
                break;
            case "星期二":
                day=1;
                break;
            case "星期三":
                day=2;
                break;
            case "星期四":
                day=3;
                break;
            case "星期五":
                day=4;
                break;
            case "星期六":
                day=5;
                break;
            case "星期日":
                day=6;
                break;
            default:
                System.out.println("不存在这样的星期几 : "+ someDay);
                break;
        }

        return day;
    }

    // 获取指定日期毫秒时间的当周一星期的日期
    private static List<String> getAllWeekDayDateByMillis(long timeMills){
        List<String> list = new ArrayList<String>();
        // 得到指定时间是周几
        String week = getWhatDaySomeday(timeMills);
        //System.out.println("日期是：" + week);
        // 记录与周一的间隔天数
        int dayFromMonday=getHowManyDayFromMonday(week);
        //System.out.println("dayFromMonday : "+ dayFromMonday);
        // 获取这周第一天毫秒值
        long dayMillis = 24*60*60*1000;
        // 获取这周第一天的日子
        long firstOfWeekMillis = timeMills - dayFromMonday * dayMillis;
        // 使用 for 循环得到当前一周的日子（7天的日子）
        for(long i = firstOfWeekMillis;i<firstOfWeekMillis+ 7 *dayMillis;i+=dayMillis) {
            Date targetDate = new Date(i);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String targetDay = format.format(targetDate);
            list.add(targetDay);
        }
        return list;
    }
    public static String getMxId(ConnectionFactory cf,Connection conn,String date) {
        String mx_id="";
        Vector userList = cf.doQuery(conn,"select mx_id from zb_zdpb_bgs where b_time<=? and e_time>=? order by add_time desc limit 1",null,new Object[]{date,date});
        if(userList!=null && userList.size()>0){
            Hashtable ht1 = (Hashtable) userList.get(0);
            mx_id = (String) ht1.get("mx_id");
        }
        return mx_id;
    }
    public static String getZbTyTime(ConnectionFactory cf,Connection conn,String mx_id,String zb_type) {
        String time="";
        Vector userList = cf.doQuery(conn,"select b_time,e_time from zb_type_bgs where mx_id=? and zb_type=?"
                ,null,new Object[]{mx_id,zb_type});
        if(userList!=null && userList.size()>0){
            Hashtable ht1 = (Hashtable) userList.get(0);
            time = (String) ht1.get("b_time")+"-"+(String) ht1.get("e_time");
        }
        return time;
    }
    public static String getTel(ConnectionFactory cf,Connection conn,String user_name) {
        String tel="";
        Vector userList = cf.doQuery(conn,"select tel from zb_bgs_user where user_name=? "
                ,null,new Object[]{user_name});
        if(userList!=null && userList.size()>0){
            Hashtable ht1 = (Hashtable) userList.get(0);
            tel = (String) ht1.get("tel");
        }
        return tel;
    }
%>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Vector v_list = null;
    Vector v_list_user = null;
    Calendar cal = Calendar.getInstance();
    Date date=new Date();//现在的日期
    cal.setTime(date);
    String year=String.valueOf(cal.get(Calendar.YEAR));//获取年
    String month = String.valueOf(cal.get(Calendar.MONTH)+1);//获取月（月份从0开始，如果按照中国的习惯，需要加一）
    String day=String.valueOf(cal.get(Calendar.DAY_OF_MONTH));//获取日（月中的某一天）
    if(month.length()==1){
        month="0"+month;
    }
    month="-"+month;
    if(day.length()==1){
        day="0"+day;
    }
    day="-"+day;
    Hashtable ht = new Hashtable();
    StringBuffer sql = new StringBuffer();
    String dates="";
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        String mxId=getMxId(cf,conn,year+month+day);
        List<String> dateList=GetCurrentWeekAllDate();
        if(dateList!=null && dateList.size()>0){
            for(int n=0;n<dateList.size();n++) {
                dates=dates+dateList.get(n)+",";
            }
        }
        sql.append("select * from zb_type_bgs where mx_id=? order by order_num asc ");
        v_list = cf.doQuery(conn, sql.toString(), null, new Object[]{mxId});
%>
<style>
    .col-sm-2{
        width:14%;
    }
    .col-sm-3{
        width:19%;
    }
    .btn-green{ background-color:green;border: green}


</style>
<div >
    <a class="btn btn-primary radius" onclick="operList();" >
        导出
    </a>
</div>
<div class="">
    <form class="form form-horizontal" method="post" id="form1" name="form1" action="exportWeek.jsp" >
        <input type="hidden" value="<%=dates%>" name="exportDate" id="exportDate">
        <div style="border: solid 1px #eee;margin-top: 20px;height:550px;overflow: auto;text-align: center">
            <div style="width:1250px;height:550px;margin:0 auto;">
            <%
                for(int i=0;i<v_list.size();i++){
                    Hashtable ht_2 = (Hashtable) v_list.get(i);
                    String id=pub.trimNull((String)ht_2.get("id"));
                    String zb_type=pub.trimNull((String)ht_2.get("zb_type"));
                    String b_time=pub.trimNull((String)ht_2.get("b_time"));
                    String e_time=pub.trimNull((String)ht_2.get("e_time"));
                    StringBuffer sb = new StringBuffer();
                    sb.append("select t1.zb_user,t1.zb_type from zb_user_gl_bgs t1  where 1=1 ");
                    sb.append(" and t1.year in ( ");
                    if(dateList!=null&&dateList.size()>0){
                        for(int m=0;m<dateList.size();m++){
                            if(m==dateList.size()-1){
                                sb.append("'" + dateList.get(m) + "'");
                            }else{
                                sb.append("'" + dateList.get(m) + "',");
                            }
                        }
                    }
                    sb.append(" )");
                    sb.append(" and t1.zb_type=? order by t1.id asc");
                    v_list_user=cf.doQuery(conn,sb.toString(),null,new Object[]{zb_type});
            %>
            <%if(i==0){%>
            <div  style="height:40px;float:left;white-space: nowrap;">
                <div style="width:150px;height:40px;float:left;border:0.5px solid #000;">
                </div>
                <%
                    for(int n=0;n<dateList.size();n++){
                        String riqi="";
                        if(n==0){
                            riqi="周一";
                        }else if(n==1){riqi="周二";}
                        else if(n==2){riqi="周三";}
                        else if(n==3){riqi="周四 ";}
                        else if(n==4){riqi="周五";}
                        else if(n==5){riqi="周六";}
                        else if(n==6){riqi="周日";}
                %>
                <div style="width:150px;height:40px;line-height:40px;text-align:center;color:#fff;float:left;border:0.5px solid #000;background-color: #00b7ee">
                    <%=dateList.get(n)%>（<%=riqi%>）
                </div>
                <% }%>
            </div>
            <%}%>
            <div  style="height:160px;float:left;">
                <div style="width:150px;height:160px;float:left;border:0.5px solid #000;text-align: center;line-height: 120px;">
                    <p style="height:30px;"> <%=zb_type%></p>
                    <p style="height:30px;">(<%=b_time%>-<%=e_time%>)</p>
                </div>
                <%
                    if(v_list_user!=null && v_list_user.size()>0){
                        for(int n=0;n<v_list_user.size();n++){
                            Hashtable ht1 = (Hashtable) v_list_user.get(n);
                            String user_name=pub.trimNull((String)ht1.get("zb_user"));
                            user_name=user_name.replace(",","</br>");
                %>
                <div style="text-align:center;width:150px;height:160px;float:left;border:0.5px solid #000;position: relative">
                    <p style="height:20px;"></p>
                    <%=user_name%>
                </div>
                <%}}%>
            </div>
            <%}%>
            </div>
        </div>
    </form>
</div>
<script>
    function operList() {
        document.form1.submit();
    }
</script>
<%
    } catch (Exception e) {
        out.println("查询用户信息异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>