feat(system): 生成假期信息到 Redis
- 在 AttendanceController 中添加 generateHolidayToRedis 接口- 在 AttendanceService 接口中定义 generateHolidayToRedis 方法 - 在 AttendanceServiceImpl 中实现 generateHolidayToRedis 方法 - 优化缓存更新逻辑,删除前年的假期信息
This commit is contained in:
parent
4eb18d3664
commit
b0a5a961bf
@ -10,6 +10,7 @@ import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
@ -110,4 +111,12 @@ public class AttendanceController {
|
||||
attendanceService.exportAttendanceExcel(response, dto);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/generateHolidayToRedis")
|
||||
@Operation(summary = "生成假期信息到redis(调用一次即可不需要重复调 需要开发人员手动调)")
|
||||
@PermitAll
|
||||
public CommonResult<?> generateHolidayToRedis(Integer year) {
|
||||
attendanceService.generateHolidayToRedis(year);
|
||||
return success("ok");
|
||||
}
|
||||
}
|
||||
|
@ -181,4 +181,9 @@ public interface AttendanceService {
|
||||
* @return
|
||||
*/
|
||||
Map<String, AttendanceTimeRangeInfoVO> getAttendanceInfoByTimeRange(AttendanceTimeRangeInfoDTO attendanceTimeRangeInfoDTO);
|
||||
|
||||
/**
|
||||
* 更新假期信息到redis
|
||||
*/
|
||||
void generateHolidayToRedis(Integer year);
|
||||
}
|
||||
|
@ -294,8 +294,8 @@ public class AttendanceServiceImpl implements AttendanceService {
|
||||
map.put(item.get("date").toString(), item.get("holiday").toString());
|
||||
});
|
||||
stringRedisTemplate.opsForHash().putAll(key, map);
|
||||
// -- 删除去年的
|
||||
String lastKey = "holiday_" + (Integer.parseInt(year) - 1);
|
||||
// -- 删除前年的
|
||||
String lastKey = "holiday_" + (Integer.parseInt(year) - 2);
|
||||
stringRedisTemplate.delete(lastKey);
|
||||
}
|
||||
Object o = stringRedisTemplate.opsForHash().get(key, dateStr);
|
||||
@ -1184,6 +1184,27 @@ public class AttendanceServiceImpl implements AttendanceService {
|
||||
.setAutoHolidaysFlag(activationGroup.getAutoHolidaysFlag()).setTimes(list));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateHolidayToRedis(Integer year) {
|
||||
String key = "holiday_" + year;
|
||||
Boolean flag = stringRedisTemplate.hasKey(key);
|
||||
// 缓存不存在
|
||||
if (Boolean.FALSE.equals(flag)) {
|
||||
String url = "https://timor.tech/api/holiday/year/" + year;
|
||||
String json = HttpUtil.get(url, 3000);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
JSONObject jsonObject = JSONUtil.parseObj(json);
|
||||
Object o = jsonObject.get("holiday");
|
||||
//将o转为json对象并且循环获取对象中所有的属性
|
||||
JSONObject data = JSONUtil.parseObj(o);
|
||||
data.forEach((k, v) -> {
|
||||
JSONObject item = JSONUtil.parseObj(v);
|
||||
map.put(item.get("date").toString(), item.get("holiday").toString());
|
||||
});
|
||||
stringRedisTemplate.opsForHash().putAll(key, map);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void useReplacementCardNum(Long userId) {
|
||||
userId = userId == null ? getLoginUserId() : userId;
|
||||
|
Loading…
Reference in New Issue
Block a user