<%@ page import="weaver.general.*" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.systeminfo.SystemEnv" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="com.engine.kq.util.PageUidFactory" %>
<%@ page import="com.cloudstore.dev.api.util.Util_TableMap" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="com.engine.kq.service.KQMyAttendanceService" %>
<%@ page import="com.engine.common.util.ServiceUtil" %>
<%@ page import="com.engine.kq.service.impl.KQMyAttendanceServiceImpl" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    Map<String,Object> data = new HashMap<String,Object>();

    User user = HrmUserVarify.getUser(request , response);
    Map<String, Object> params =  ParamUtil.request2Map(request);

    String kqtype = Util.null2String(params.get("kqtype"));
    if("BELATE".equalsIgnoreCase(kqtype) || "graveBeLate".equalsIgnoreCase(kqtype)){
        data = getBeLateList(params,user);
        data.put("api_status", true);
    }else{
        data = getService(user).getHrmKQReportInfo(ParamUtil.request2Map(request), user);
        data.put("api_status", true);
    }
    JSONObject jsonObject = new JSONObject(data);
%>
<%=jsonObject.toJSONString()  %>

<%!
    private KQMyAttendanceService getService(User user){
        return (KQMyAttendanceService) ServiceUtil.getService(KQMyAttendanceServiceImpl.class, user);
    }

    /**
     * 获取迟到明细列表
     *
     * @return
     */
    public Map<String, Object> getBeLateList(Map<String, Object> params,User user)
    {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String nowdate = weaver.common.DateUtil.getCurrentDate();
            String type = Util.null2String(params.get("type"));//是查看一年的数据还是一月的数据：1-年、2-月
            String typevalue = Util.null2String(params.get("typevalue"));//指定的年份或者指定的月份
            String fromDate = Util.null2String(params.get("fromDate"));//指定日期起点
            String toDate = Util.null2String(params.get("toDate"));//指定日期终点
            int subCompanyId = Util.getIntValue((String) params.get("subCompanyId"), 0);//指定查看的人员的所属分部
            int departmentId = Util.getIntValue((String) params.get("departmentId"), 0);//指定查看的人员的所属部门
            String resourceId = Util.null2String(params.get("resourceId"));//指定查看的人员ID
            String status = Util.null2String(params.get("status"));//?
            String tabKey = Util.null2String(params.get("tabKey"));//是迟到还是严重迟到：2-严重迟到、其他-迟到

            /**
             * 若未指定查看人员，则默认为当前登录人员
             */
            if (resourceId.length() == 0) {
                resourceId = "" + user.getUID();
            }

            /**
             * 获取指定日期的起点和终点
             */
            if (type.equals("1")) {//年
                if (typevalue.length() == 0 || typevalue.length() != 4) {
                    typevalue = weaver.common.DateUtil.getYear();
                }
                fromDate = typevalue + "-01-01";
                toDate = weaver.common.DateUtil.getLastDayOfYear(weaver.common.DateUtil.parseToDate(fromDate));
            } else if (type.equals("2")) {//月
                if (typevalue.length() == 0) {
                    typevalue = weaver.common.DateUtil.getYear() + "-" + weaver.common.DateUtil.getMonth();
                }
                fromDate = typevalue + "-01";
                toDate = weaver.common.DateUtil.getLastDayOfMonthToString(DateUtil.parseToDate(fromDate));
            }

            /**获取今天的日期*/
            Calendar today = Calendar.getInstance();
            String currentdate = Util.add0(today.get(Calendar.YEAR), 4) + "-"
                    + Util.add0(today.get(Calendar.MONTH) + 1, 2) + "-"
                    + Util.add0(today.get(Calendar.DAY_OF_MONTH), 2);

            /**
             * 获取明细列表
             */
            String backFields = " a.id, b.resourceid,a.departmentid, a.lastname, a.workcode, a.status, a.dsporder, kqdate, serialid, serialid as serialid1," +
                                " workbegintime,workendtime, signintime,signouttime, (beLateMins+graveBeLateMins) as  beLateMins";
            String sqlFrom = "from hrmresource a, kq_format_detail b  ";
            String sqlWhere = " where a.id = b.resourceid";
            String orderBy = " kqdate asc, workbegintime asc ";
            if (fromDate.length() > 0) {
                sqlWhere += " and kqdate >= '" + fromDate + "'";
            }

            if (toDate.length() > 0) {
                sqlWhere += " and kqdate <= '" + toDate + "'";
            }

            if (resourceId.length() > 0) {
                sqlWhere += " and resourceid = " + resourceId;
            }

            sqlWhere += " and (graveBeLateMins>0  or beLateMins>0 )";

            String pageUid = PageUidFactory.getHrmPageUid("KQReportDetialList");
            String tableString ="" +
                                " <table pageUid=\"" + pageUid + "\" pagesize=\"10\" tabletype=\"none\">" +
                                " <sql backfields=\"" + backFields + "\" sqlform=\"" + Util.toHtmlForSplitPage(sqlFrom) + "\" sqlprimarykey=\"a.id\" sqlorderby=\"" + orderBy + "\" sqlsortway=\"asc\" sqldistinct=\"true\" sqlwhere=\"" + Util.toHtmlForSplitPage(sqlWhere) + "\"/>" +
                                " <head>" +
                                "				<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(413, user.getLanguage()) + "\" column=\"lastname\" orderkey=\"lastname\"/>" +
                                "				<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(714, user.getLanguage()) + "\" column=\"workcode\" orderkey=\"workcode\"/>" +
                                "				<col width=\"15%\" text=\"" + SystemEnv.getHtmlLabelName(124, user.getLanguage()) + "\" column=\"departmentid\" orderkey=\"departmentid\" transmethod=\"weaver.hrm.company.DepartmentComInfo.getDepartmentname\"/>" +
                                "				<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(602, user.getLanguage()) + "\" column=\"status\" orderkey=\"status\" transmethod=\"weaver.hrm.resource.ResourceComInfo.getStatusName\" otherpara=\"" + user.getLanguage() + "\"/>" +
                                "				<col width=\"15%\" text=\"" + SystemEnv.getHtmlLabelName(97, user.getLanguage()) + "\" column=\"kqdate\" orderkey=\"kqdate\"/>" +
                                "				<col width=\"20%\" text=\"" + SystemEnv.getHtmlLabelName(390054, user.getLanguage()) + "\" column=\"serialid\" orderkey=\"serialid\" transmethod=\"com.engine.kq.util.TransMethod.getSerailName\" otherpara=\"column:workbegintime+column:workendtime\"/>" +
                                "				<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(18949, user.getLanguage()) + "\" column=\"serialid1\" orderkey=\"serialid1\" transmethod=\"com.engine.kq.util.TransMethod.getReportDetialSignTime\" otherpara=\"column:signintime++column:kqdate+column:resourceid+" + user.getLanguage() + "\"/>";
            tableString += "				<col width=\"10%\" text=\"" + SystemEnv.getHtmlLabelName(391413, user.getLanguage()) + "\" column=\"beLateMins\" orderkey=\"beLateMins\" transmethod=\"com.engine.kq.util.TransMethod.getReportDetialMinToHour\" />";
            tableString += "</head>" +
                    "</table>";

            //主要用于 显示定制列以及 表格 每页展示记录数选择
            String sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
            Util_TableMap.setVal(sessionkey, tableString);

            resultMap.put("sessionkey", sessionkey);
            resultMap.put("status", "1");
        } catch (Exception e) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
            e.printStackTrace();
        }
        return resultMap;
    }


%>


