group by and union all

sql 复制代码
 SELECT
        'vacation' AS keyCode,
        staff.staff_id AS staffId,
        staff.staff_name AS staffName,
        SUM(DATEDIFF(LEAST(staff.end_time, #{endDate}), GREATEST(staff.begin_time, #{startDate}))) AS vacationDays,
        NULL AS lateDays,
        NULL AS absenceDays,
        NULL AS attendanceDays
        FROM tb_staff_stop_receive_time AS staff
        WHERE staff.stop_type = 1
        AND (staff.begin_time BETWEEN #{startDate} AND #{endDate} OR staff.end_time BETWEEN #{startDate} AND #{endDate})
        GROUP BY staff.staff_id

        UNION ALL

        SELECT
        'late' AS keyCode,
        staff.id AS staffId,
        staff.staff_name AS staffName,
        NULL AS vacationDays,
        COUNT(*) AS lateDays,
        NULL AS absenceDays,
        NULL AS attendanceDays
        FROM tb_staff_stop_receive_time AS staff
        JOIN eb_store_order AS O ON staff.id = O.delivery_id
        WHERE O.staff_late = 1
        AND O.create_time BETWEEN #{startDate} AND #{endDate}
        GROUP BY staff.id

        UNION ALL

        SELECT
        'absence' AS keyCode,
        staff.id AS staffId,
        staff.staff_name AS staffName,
        NULL AS vacationDays,
        NULL AS lateDays,
        COUNT(*) AS absenceDays,
        NULL AS attendanceDays
        FROM tb_staff_stop_receive_time AS staff
        JOIN eb_store_order AS O ON staff.id = O.delivery_id
        WHERE O.status NOT IN (2, 3, 4, 5) AND O.order_service_time < CURRENT_TIMESTAMP
        AND O.create_time BETWEEN #{startDate} AND #{endDate}
        GROUP BY staff.id

        UNION ALL

        SELECT
        'attendance' AS keyCode,
        staff.id AS staffId,
        staff.staff_name AS staffName,
        NULL AS vacationDays,
        NULL AS lateDays,
        NULL AS absenceDays,
        COUNT(*) AS attendanceDays
        FROM tb_staff_stop_receive_time AS staff
        JOIN eb_store_order AS O ON staff.id = O.delivery_id
        WHERE O.status = 2
        AND O.create_time BETWEEN #{startDate} AND #{endDate}
        GROUP BY staff.id
java 复制代码
    /*同比出勤*/
    List<AbsenceThisWeekVo> getAbsenceThisWeek(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
java 复制代码
        int totalAbsenceCount = lateOrders.stream()
                .filter(order -> order.getState() == 0 || order.getState() == 1)
                .mapToInt(AbsenceThisWeekVo::getCount)
                .sum();
        //未出勤的
        int totalAbsenceCountLast = lateOrders.stream()
                .filter(order -> order.getState() == 2 || order.getState() == 3
                        || order.getState() == 4 || order.getState() == 5)
                .mapToInt(AbsenceThisWeekVo::getCount)
                .sum();
java 复制代码
    public String getStaffPercentage(double count, double totalCount){

        String rate = "0%";


        if (totalCount == 0) {

            throw new RuntimeException("分母/数据为0,无法计算!");
        } else {
            // 使用BigDecimal进行精确的数值计算
            BigDecimal countBD = BigDecimal.valueOf(count);
            BigDecimal totalCountBD = BigDecimal.valueOf(totalCount);

            // 计算百分比
            BigDecimal percentageBD = NumberUtil.div(countBD.toString(), totalCountBD.toString(), 2)
                    .multiply(BigDecimal.valueOf(100));

            rate = percentageBD.intValue() + "%";
        }


        return rate;
    }
java 复制代码
    public List<StaffAttendanceResponse> getStaffAttendance(Date startDate, Date endDate) {

//会查询出一个id有多条记录,要做的就是,将这些多条的数据,根据唯一id合成一条完整的
        List<StaffAttendanceResponse> attendanceDaysList2 = statisticsStaffDao.getAttendanceDays(startDate, endDate);

        // 创建一个 Map 用于存储每个员工的 StaffAttendanceResponse
        Map<Integer, StaffAttendanceResponse> responseMap = new HashMap<>();
        for (StaffAttendanceResponse response : attendanceDaysList2) {
            Integer  staffId = response.getStaffId();

            StaffAttendanceResponse individualResponse;

            if (CollectionUtil.isNotEmpty(responseMap) &&responseMap.containsKey(staffId) ) {
                individualResponse = responseMap.get(staffId);
            } else {
                individualResponse = new StaffAttendanceResponse();
                BeanUtils.copyProperties(response,individualResponse);
            }

            // 没有id创建一个新的 StaffAttendanceResponse 对象
//           StaffAttendanceResponse individualResponse = responseMap.getOrDefault(staffId, new StaffAttendanceResponse());

            switch (response.getKeyCode()) {
                case "vacation":
                    individualResponse.setVacationDays(response.getVacationDays());
                    break;
                case "late":
                    individualResponse.setLateDays(response.getLateDays());
                    break;
                case "absence":
                    individualResponse.setAbsenceDays(response.getAbsenceDays());
                    break;
                case "attendance":
                    individualResponse.setAttendanceDays(response.getAttendanceDays());
                    break;
                default:
                   throw new ServiceException("数据错误!");


            }
            // 更新 Map
            responseMap.put(staffId, individualResponse);
        }
        // 转换 Map 的值为 List 并返回
        return new ArrayList<>(responseMap.values());
    }


   List<StaffAttendanceResponse> attendanceResponse = getVacationDaysByType(type,this::getStaffAttendance);
        StaffCircleVo StaffCircleResponse = getVacationDaysByType(typeCircle,this::getTotalVacationDays);

得到固定时间

java 复制代码
    public interface StaffAttendanceFetcher<T>  {
        T fetch(Date startDate, Date endDate);
    }

    public <T> T getVacationDaysByType(String type, StaffAttendanceFetcher<T> fetcher) {
        Calendar cal = Calendar.getInstance();
        Date start = null, end = null;
        switch (type) {
            case "TODAY":
                LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
                LocalDateTime endOfDay = LocalDate.now().atTime(23, 59, 59);
                start = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
                end = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
                break;
            case "WEEK":
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                start = cal.getTime();
                cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                end = cal.getTime();
                break;
            case "MONTH":
                cal.set(Calendar.DAY_OF_MONTH, 1);
                start = cal.getTime();
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                end = cal.getTime();
                break;
            case "YEAR":
                cal.set(Calendar.DAY_OF_YEAR, 1);
                start = cal.getTime();
                cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
                end = cal.getTime();
                break;
            default:
                throw new IllegalArgumentException("请输入正确的参数: " + type + "TODAY,WEEK, MONTH, YEAR.");

        }
        return fetcher.fetch(start, end);
    }
相关推荐
欧先生^_^1 小时前
Linux内核可配置的参数
linux·服务器·数据库
问道飞鱼1 小时前
【数据库知识】Mysql进阶-高可用MHA(Master High Availability)方案
数据库·mysql·adb·高可用·mha
tiging1 小时前
centos7.x下,使用宝塔进行主从复制的原理和实践
数据库·mysql·adb·主从复制
wangcheng86992 小时前
Oracle常用函数-日期时间类型
数据库·sql·oracle
zizisuo2 小时前
面试篇:Spring Security
网络·数据库·安全
一只fish2 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(2)
数据库·mysql
StarRocks_labs2 小时前
从InfluxDB到StarRocks:Grab实现Spark监控平台10倍性能提升
大数据·数据库·starrocks·分布式·spark·iris·物化视图
搞不懂语言的程序员2 小时前
Redis的Pipeline和Lua脚本适用场景是什么?使用时需要注意什么?
数据库·redis·lua
王RuaRua2 小时前
[数据结构]5. 栈-Stack
linux·数据结构·数据库·链表
Lw老王要学习4 小时前
Linux数据库篇、第一章_02_MySQL的使用增删改查
linux·运维·数据库·mysql·云计算·it