<%_ if (position === 'import') { _%>
import  <%= packageName %>.domain.enumeration.LogType;
    <%_ if (ormTool === 'hibernate') { _%>
import java.util.stream.Collectors;
    <%_ } else { _%>
    <%_ } _%>
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import java.time.Instant;
<%_ } _%>
<%_ if (position === 'importInterface') { _%>
import java.util.Map;
<%_ } _%>
<%_ if (position === 'importMethod') { _%>
    <% if (ormTool === 'hibernate') { %>public abstract <% } %>Map<String, Object> logInfo();

    <% if (ormTool === 'hibernate') { %>public abstract <% } %>List<Map<String, Object>> countVisit();

<%_ } _%>
<%_ if (position === 'method') { _%>

    public Map<String, Object> logInfo() {
        Map<String, Object> result = new HashMap<>();
        // 获取一天的开始和结束时间
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date dayStart = calendar.getTime();
        calendar.add(Calendar.DATE, 1);
        Date dayEnd = calendar.getTime();
        // 获取系统访问记录
        SysLogCriteria sysLogCriteria = new SysLogCriteria();
        sysLogCriteria.logType().setEquals(LogType.LOGIN);
        Long totalVisitCount = countByCriteria(sysLogCriteria);
        result.put("totalVisitCount", totalVisitCount);
        sysLogCriteria.createdDate().setGreaterThanOrEqual(dayStart.toInstant().atZone(ZoneId.systemDefault())).setLessThan(dayEnd.toInstant().atZone(ZoneId.systemDefault()));
        Long todayVisitCount = countByCriteria(sysLogCriteria);
        result.put("todayVisitCount", todayVisitCount);
        Long todayIp = countByFieldNameAndCriteria("ip",true,sysLogCriteria);
        result.put("todayIp", todayIp);
        return result;
    }

    public List<Map<String, Object>> countVisit() {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        Date dayEnd = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        Date dayStart = calendar.getTime();
        <%_ if (ormTool === 'hibernate') { _%>
        List<Selection<?>> selections = new ArrayList<>();
        List<Expression<?>> groups = new ArrayList<>();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Tuple> q = cb.createTupleQuery();
        Root<SysLog> root = q.from(SysLog.class);
        selections.add(cb.count(root.get(SysLog_.id)).alias("visit"));
        selections.add(cb.countDistinct(root.get(SysLog_.ip)).alias("ip"));
        selections.add(cb.function("DATE_FORMAT", Instant.class, root.get(SysLog_.createdDate), cb.literal("%Y-%m-%d")).alias("day"));
        selections.add(cb.function("DATE_FORMAT", Instant.class, root.get(SysLog_.createdDate), cb.literal("%m-%d")));
        groups.add(cb.function("DATE_FORMAT", Instant.class, root.get(SysLog_.createdDate), cb.literal("%Y-%m-%d")));
        List<Order> orders = new ArrayList<>();
        orders.add(cb.asc(cb.function("DATE_FORMAT", Instant.class, root.get(SysLog_.createdDate), cb.literal("%m-%d"))));
        SysLogCriteria sysLogCriteria = new SysLogCriteria();
        sysLogCriteria.createdDate().setGreaterThanOrEqual(dayStart.toInstant().atZone(ZoneId.systemDefault())).setLessThan(dayEnd.toInstant().atZone(ZoneId.systemDefault()));
        q.where(createSpecification(sysLogCriteria).toPredicate(root, q, cb));
        q.multiselect(selections);
        groups.add(cb.function("DATE_FORMAT", Instant.class, root.get(SysLog_.createdDate), cb.literal("%m-%d")));
        q.groupBy(groups);
        q.orderBy(orders);
        TypedQuery<Tuple> query = em.createQuery(q);
        List<Tuple> resultList = query.getResultList();
        return resultList.stream().map(JpaUtil::toMap).collect(Collectors.toList());
        <%_ } else { _%>
        String[] selections = new String[4];
        selections[0] = "count(*) as visit";
        selections[1] = "count(distinct(ip)) as ip";
        selections[2] = "DATE_FORMAT(created_date, '%Y-%m-%d') as day";
        selections[3] = "DATE_FORMAT(created_date, '%m-%d') as type";
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(selections);
        queryWrapper.ge("created_date", dayStart);
        queryWrapper.lt("created_date", dayEnd);
        queryWrapper.eq("log_type", LogType.LOGIN);
        queryWrapper.groupBy("day", "type");
        queryWrapper.orderBy(true, true, "day");
        return sysLogRepository.selectMaps(queryWrapper);
        <%_ } _%>
    }
<%_ } _%>
