¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.yuanchu.mom.service.impl; |
| | | |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.yuanchu.mom.dto.PerformanceShiftAddDto; |
| | | import com.yuanchu.mom.mapper.PerformanceShiftMapper; |
| | | import com.yuanchu.mom.pojo.Enums; |
| | | import com.yuanchu.mom.pojo.PerformanceShift; |
| | | import com.yuanchu.mom.service.EnumService; |
| | | import com.yuanchu.mom.service.PerformanceShiftService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * <p> |
| | | * 绩æç®¡ç-çæ¬¡ æå¡å®ç°ç±» |
| | | * </p> |
| | | * |
| | | * @author æ±èéµ·éç½ç»ç§ææéå
¬å¸ |
| | | * @since 2024-05-08 09:12:04 |
| | | */ |
| | | @Service |
| | | public class PerformanceShiftServiceImpl extends ServiceImpl<PerformanceShiftMapper, PerformanceShift> implements PerformanceShiftService { |
| | | |
| | | @Autowired |
| | | private EnumService enumService; |
| | | |
| | | public List<PerformanceShift> list = new ArrayList<>(); |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void performanceShiftAdd(PerformanceShiftAddDto performanceShiftAddDto) { |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | String formattedDateTime = performanceShiftAddDto.getStartWeek().format(formatter); |
| | | boolean exists = baseMapper.exists(Wrappers.<PerformanceShift>lambdaQuery() |
| | | .eq(PerformanceShift::getWorkTime, formattedDateTime)); |
| | | String[] splitUserId = performanceShiftAddDto.getUserId().split(","); |
| | | // 妿ä¸å卿·»å æ°æ® |
| | | if (!exists) { |
| | | LocalDate firstDayOfMonth = performanceShiftAddDto.getEndWeek().toLocalDate().withDayOfMonth(1); |
| | | LocalDate lastDayOfMonth = performanceShiftAddDto.getEndWeek().toLocalDate().with(TemporalAdjusters.lastDayOfMonth()); |
| | | List<LocalDateTime> localDateTimesBetween = getLocalDateTimesBetween(firstDayOfMonth.atStartOfDay(), lastDayOfMonth.atStartOfDay()); |
| | | localDateTimesBetween.forEach(i -> { |
| | | for (String s : splitUserId) { |
| | | PerformanceShift performanceShift = new PerformanceShift(); |
| | | performanceShift.setUserId(Integer.valueOf(s)); |
| | | performanceShift.setWorkTime(i); |
| | | performanceShift.setShift(""); |
| | | list.add(performanceShift); |
| | | } |
| | | if (list.size() >= 1000) { |
| | | baseMapper.insertBatchSomeColumn(list); |
| | | list.clear(); |
| | | } |
| | | }); |
| | | if (!list.isEmpty()) { |
| | | baseMapper.insertBatchSomeColumn(list); |
| | | } |
| | | } |
| | | // 忬¡æ´æ° |
| | | List<LocalDateTime> datesBetween = getLocalDateTimesBetween(performanceShiftAddDto.getStartWeek(), performanceShiftAddDto.getEndWeek()); |
| | | for (LocalDateTime date : datesBetween) { |
| | | for (String s : splitUserId) { |
| | | PerformanceShift performanceShift = new PerformanceShift(); |
| | | performanceShift.setShift(performanceShiftAddDto.getShift()); |
| | | performanceShift.setUserId(Integer.valueOf(s)); |
| | | performanceShift.setWorkTime(date); |
| | | String formatterDateTime = date.format(formatter); |
| | | baseMapper.update(new PerformanceShift(), Wrappers.<PerformanceShift>lambdaUpdate() |
| | | .set(PerformanceShift::getShift, performanceShiftAddDto.getShift()) |
| | | .eq(PerformanceShift::getUserId, s) |
| | | .eq(PerformanceShift::getWorkTime, formatterDateTime)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public IPage<Map<String, Object>> performanceShiftPage(Page<Object> page, String time, String userName, String laboratory) { |
| | | IPage<Map<String, Object>> mapIPage = baseMapper.performanceShiftPage(page, time, userName, laboratory); |
| | | List<Enums> shiftType = enumService.selectEnumByCategory("çæ¬¡ç±»å"); |
| | | for (Map<String, Object> i : mapIPage.getRecords()) { |
| | | String[] shiftTimes = i.get("shiftTime").toString().split(";"); |
| | | int totalAttendance = 0; |
| | | List<Object> map = new ArrayList<>(); |
| | | // å岿¥æ |
| | | for (String shiftTime : shiftTimes) { |
| | | Map<Object, Object> hashMap = new HashMap<>(); |
| | | String[] shiftTimeAndShift = shiftTime.split("ï¼"); |
| | | for (Enums enums : shiftType) { |
| | | if (!i.containsKey(enums.getLabel())) { |
| | | i.put(enums.getLabel(), 0); |
| | | } |
| | | if (enums.getValue().equals(shiftTimeAndShift[1])) { |
| | | Integer num = (Integer) i.get(enums.getLabel()); |
| | | i.put(enums.getLabel(), num += 1); |
| | | } |
| | | } |
| | | if (shiftTimeAndShift[1].equals("1") || shiftTimeAndShift[1].equals("2") || shiftTimeAndShift[1].equals("0")) { |
| | | i.put("totalAttendance", totalAttendance += 1); |
| | | } |
| | | hashMap.put("id", shiftTimeAndShift[2]); |
| | | hashMap.put("shift", shiftTimeAndShift[1]); |
| | | hashMap.put("weekly", DateUtil.weekOfYear(DateUtil.parse(shiftTimeAndShift[0]))); |
| | | hashMap.put("time", shiftTimeAndShift[0]); |
| | | hashMap.put("headerTime", getWeek(shiftTimeAndShift[0])); |
| | | map.add(hashMap); |
| | | } |
| | | i.put("list", map); |
| | | i.remove("shiftTime"); |
| | | } |
| | | return mapIPage; |
| | | } |
| | | |
| | | @Override |
| | | public void performanceShiftUpdate(String shift, Integer id) { |
| | | PerformanceShift performanceShift = new PerformanceShift(); |
| | | performanceShift.setId(id); |
| | | performanceShift.setShift(shift); |
| | | baseMapper.updateById(performanceShift); |
| | | } |
| | | |
| | | // è·å两个localDateTimeçæ¯ä¸å¤© |
| | | public static List<LocalDateTime> getLocalDateTimesBetween(LocalDateTime start, LocalDateTime end) { |
| | | List<LocalDateTime> localDateTimes = new ArrayList<>(); |
| | | LocalDate currentDate = start.toLocalDate(); |
| | | LocalDateTime currentLocalDateTime = start; |
| | | while (!currentDate.isAfter(end.toLocalDate())) { |
| | | localDateTimes.add(currentLocalDateTime); |
| | | currentLocalDateTime = currentLocalDateTime.plusDays(1); |
| | | currentDate = currentDate.plusDays(1); |
| | | } |
| | | return localDateTimes; |
| | | } |
| | | |
| | | public static String getWeek(String dayStr) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | try { |
| | | Date date = sdf.parse(dayStr); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); |
| | | int day = calendar.get(Calendar.DAY_OF_MONTH); |
| | | return day + " " + getWeekDay(dayOfWeek); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static String getWeekDay(int dayOfWeek) { |
| | | switch (dayOfWeek) { |
| | | case Calendar.MONDAY: |
| | | return "å¨ä¸"; |
| | | case Calendar.TUESDAY: |
| | | return "å¨äº"; |
| | | case Calendar.WEDNESDAY: |
| | | return "å¨ä¸"; |
| | | case Calendar.THURSDAY: |
| | | return "å¨å"; |
| | | case Calendar.FRIDAY: |
| | | return "å¨äº"; |
| | | case Calendar.SATURDAY: |
| | | return "å¨å
"; |
| | | case Calendar.SUNDAY: |
| | | return "卿¥"; |
| | | default: |
| | | return "æªç¥"; |
| | | } |
| | | } |
| | | } |