• group by and union all


    1. SELECT
    2. 'vacation' AS keyCode,
    3. staff.staff_id AS staffId,
    4. staff.staff_name AS staffName,
    5. SUM(DATEDIFF(LEAST(staff.end_time, #{endDate}), GREATEST(staff.begin_time, #{startDate}))) AS vacationDays,
    6. NULL AS lateDays,
    7. NULL AS absenceDays,
    8. NULL AS attendanceDays
    9. FROM tb_staff_stop_receive_time AS staff
    10. WHERE staff.stop_type = 1
    11. AND (staff.begin_time BETWEEN #{startDate} AND #{endDate} OR staff.end_time BETWEEN #{startDate} AND #{endDate})
    12. GROUP BY staff.staff_id
    13. UNION ALL
    14. SELECT
    15. 'late' AS keyCode,
    16. staff.id AS staffId,
    17. staff.staff_name AS staffName,
    18. NULL AS vacationDays,
    19. COUNT(*) AS lateDays,
    20. NULL AS absenceDays,
    21. NULL AS attendanceDays
    22. FROM tb_staff_stop_receive_time AS staff
    23. JOIN eb_store_order AS O ON staff.id = O.delivery_id
    24. WHERE O.staff_late = 1
    25. AND O.create_time BETWEEN #{startDate} AND #{endDate}
    26. GROUP BY staff.id
    27. UNION ALL
    28. SELECT
    29. 'absence' AS keyCode,
    30. staff.id AS staffId,
    31. staff.staff_name AS staffName,
    32. NULL AS vacationDays,
    33. NULL AS lateDays,
    34. COUNT(*) AS absenceDays,
    35. NULL AS attendanceDays
    36. FROM tb_staff_stop_receive_time AS staff
    37. JOIN eb_store_order AS O ON staff.id = O.delivery_id
    38. WHERE O.status NOT IN (2, 3, 4, 5) AND O.order_service_time < CURRENT_TIMESTAMP
    39. AND O.create_time BETWEEN #{startDate} AND #{endDate}
    40. GROUP BY staff.id
    41. UNION ALL
    42. SELECT
    43. 'attendance' AS keyCode,
    44. staff.id AS staffId,
    45. staff.staff_name AS staffName,
    46. NULL AS vacationDays,
    47. NULL AS lateDays,
    48. NULL AS absenceDays,
    49. COUNT(*) AS attendanceDays
    50. FROM tb_staff_stop_receive_time AS staff
    51. JOIN eb_store_order AS O ON staff.id = O.delivery_id
    52. WHERE O.status = 2
    53. AND O.create_time BETWEEN #{startDate} AND #{endDate}
    54. GROUP BY staff.id
    1. /*同比出勤*/
    2. List getAbsenceThisWeek(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
    1. int totalAbsenceCount = lateOrders.stream()
    2. .filter(order -> order.getState() == 0 || order.getState() == 1)
    3. .mapToInt(AbsenceThisWeekVo::getCount)
    4. .sum();
    5. //未出勤的
    6. int totalAbsenceCountLast = lateOrders.stream()
    7. .filter(order -> order.getState() == 2 || order.getState() == 3
    8. || order.getState() == 4 || order.getState() == 5)
    9. .mapToInt(AbsenceThisWeekVo::getCount)
    10. .sum();
    1. public String getStaffPercentage(double count, double totalCount){
    2. String rate = "0%";
    3. if (totalCount == 0) {
    4. throw new RuntimeException("分母/数据为0,无法计算!");
    5. } else {
    6. // 使用BigDecimal进行精确的数值计算
    7. BigDecimal countBD = BigDecimal.valueOf(count);
    8. BigDecimal totalCountBD = BigDecimal.valueOf(totalCount);
    9. // 计算百分比
    10. BigDecimal percentageBD = NumberUtil.div(countBD.toString(), totalCountBD.toString(), 2)
    11. .multiply(BigDecimal.valueOf(100));
    12. rate = percentageBD.intValue() + "%";
    13. }
    14. return rate;
    15. }
    1. public List getStaffAttendance(Date startDate, Date endDate) {
    2. //会查询出一个id有多条记录,要做的就是,将这些多条的数据,根据唯一id合成一条完整的
    3. List attendanceDaysList2 = statisticsStaffDao.getAttendanceDays(startDate, endDate);
    4. // 创建一个 Map 用于存储每个员工的 StaffAttendanceResponse
    5. Map responseMap = new HashMap<>();
    6. for (StaffAttendanceResponse response : attendanceDaysList2) {
    7. Integer staffId = response.getStaffId();
    8. StaffAttendanceResponse individualResponse;
    9. if (CollectionUtil.isNotEmpty(responseMap) &&responseMap.containsKey(staffId) ) {
    10. individualResponse = responseMap.get(staffId);
    11. } else {
    12. individualResponse = new StaffAttendanceResponse();
    13. BeanUtils.copyProperties(response,individualResponse);
    14. }
    15. // 没有id创建一个新的 StaffAttendanceResponse 对象
    16. // StaffAttendanceResponse individualResponse = responseMap.getOrDefault(staffId, new StaffAttendanceResponse());
    17. switch (response.getKeyCode()) {
    18. case "vacation":
    19. individualResponse.setVacationDays(response.getVacationDays());
    20. break;
    21. case "late":
    22. individualResponse.setLateDays(response.getLateDays());
    23. break;
    24. case "absence":
    25. individualResponse.setAbsenceDays(response.getAbsenceDays());
    26. break;
    27. case "attendance":
    28. individualResponse.setAttendanceDays(response.getAttendanceDays());
    29. break;
    30. default:
    31. throw new ServiceException("数据错误!");
    32. }
    33. // 更新 Map
    34. responseMap.put(staffId, individualResponse);
    35. }
    36. // 转换 Map 的值为 List 并返回
    37. return new ArrayList<>(responseMap.values());
    38. }
    39. List attendanceResponse = getVacationDaysByType(type,this::getStaffAttendance);
    40. StaffCircleVo StaffCircleResponse = getVacationDaysByType(typeCircle,this::getTotalVacationDays);

    得到固定时间
     

    1. public interface StaffAttendanceFetcher {
    2. T fetch(Date startDate, Date endDate);
    3. }
    4. public T getVacationDaysByType(String type, StaffAttendanceFetcher fetcher) {
    5. Calendar cal = Calendar.getInstance();
    6. Date start = null, end = null;
    7. switch (type) {
    8. case "TODAY":
    9. LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
    10. LocalDateTime endOfDay = LocalDate.now().atTime(23, 59, 59);
    11. start = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    12. end = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    13. break;
    14. case "WEEK":
    15. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    16. start = cal.getTime();
    17. cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    18. end = cal.getTime();
    19. break;
    20. case "MONTH":
    21. cal.set(Calendar.DAY_OF_MONTH, 1);
    22. start = cal.getTime();
    23. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    24. end = cal.getTime();
    25. break;
    26. case "YEAR":
    27. cal.set(Calendar.DAY_OF_YEAR, 1);
    28. start = cal.getTime();
    29. cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
    30. end = cal.getTime();
    31. break;
    32. default:
    33. throw new IllegalArgumentException("请输入正确的参数: " + type + "TODAY,WEEK, MONTH, YEAR.");
    34. }
    35. return fetcher.fetch(start, end);
    36. }

  • 相关阅读:
    【我的OpenGL学习进阶之旅】 C++ 长行字符串多行书写的方法以及如何书写正确的OpenGL Shader着色器代码
    2022/7/27 算力-价格明细
    月薪12.8K,转行测试斩获3份过万offer,分享一些我的小秘招
    uniapp启动微信小程序报错---initialize
    Yolov3-v5正负样本匹配机制
    http协议之digest(摘要)认证,详细讲解并附Java SpringBoot源码
    BSN长话短说之十:如何保证NFT的安全
    java实现带有html格式和附件的符合RFC822规范的eml格式的信件原文组装
    C#基础知识
    【付费推广】常见问题合集,推荐榜单FAQ
  • 原文地址:https://blog.csdn.net/jiang2360/article/details/132776079