From 215159dd2e4a74df039be6e2de72ebb94ea2ce24 Mon Sep 17 00:00:00 2001 From: NeuroLogeKCW Date: Fri, 11 Apr 2025 23:39:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AlarmController.java | 17 ++- .../kkz/kpersonalre/entity/BaseEntity.java | 5 +- .../entity/TbAlarmClockConfigEntity.java | 2 + .../entity/TbAlarmPersonConfigEntity.java | 2 +- .../entity/TbLocationUploadEntity.java | 5 + .../kkz/kpersonalre/service/AlarmService.java | 6 +- .../service/impl/AlarmServiceImpl.java | 117 +++++++++++++++++- 7 files changed, 143 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/kkz/kpersonalre/controller/AlarmController.java b/src/main/java/com/kkz/kpersonalre/controller/AlarmController.java index 44ac17a..35e0f51 100644 --- a/src/main/java/com/kkz/kpersonalre/controller/AlarmController.java +++ b/src/main/java/com/kkz/kpersonalre/controller/AlarmController.java @@ -6,9 +6,12 @@ import com.kkz.kpersonalre.service.AlarmService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.time.LocalTime; + /** * @program: KPersonal * @description: 闹钟入口 @@ -28,10 +31,10 @@ public class AlarmController { * @param location * @return */ - @GetMapping("/uploadLocation") - public R uploadLocation(String location) { + @GetMapping("/uploadLocation/{person}/{location}") + public R uploadLocation(@PathVariable String person,@PathVariable String location) { log.info("进入[上传位置信息接口],入参:{}", location); - R result = alarmService.uploadLocation(location); + R result = alarmService.uploadLocation(person, location); log.info("离开[上传位置信息接口],出参:{}", result); return result; } @@ -43,4 +46,12 @@ public class AlarmController { public void checkWorkDay() { alarmService.checkWorkDay(); } + + @GetMapping("/getAlarm/{personId}") + public R getAlarm(@PathVariable String personId) { + log.info("进入[获取闹钟信息接口],入参:{}", personId); + R result = alarmService.getAlarm(personId); + log.info("离开[获取闹钟信息接口],出参:{}", result); + return result; + } } diff --git a/src/main/java/com/kkz/kpersonalre/entity/BaseEntity.java b/src/main/java/com/kkz/kpersonalre/entity/BaseEntity.java index 1904930..8f28f1c 100644 --- a/src/main/java/com/kkz/kpersonalre/entity/BaseEntity.java +++ b/src/main/java/com/kkz/kpersonalre/entity/BaseEntity.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.time.LocalDateTime; @@ -42,7 +43,7 @@ public class BaseEntity { * 创建时间 */ @TableField(value = "create_time", fill = FieldFill.INSERT) -// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private LocalDateTime createTime; /** @@ -55,6 +56,6 @@ public class BaseEntity { * 更新时间 */ @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE,update = "now()") -// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private LocalDateTime updateTime; } diff --git a/src/main/java/com/kkz/kpersonalre/entity/TbAlarmClockConfigEntity.java b/src/main/java/com/kkz/kpersonalre/entity/TbAlarmClockConfigEntity.java index e4e222e..0931af5 100644 --- a/src/main/java/com/kkz/kpersonalre/entity/TbAlarmClockConfigEntity.java +++ b/src/main/java/com/kkz/kpersonalre/entity/TbAlarmClockConfigEntity.java @@ -1,6 +1,7 @@ package com.kkz.kpersonalre.entity; import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -31,6 +32,7 @@ public class TbAlarmClockConfigEntity extends BaseEntity { /** * 闹钟时间 */ + @JsonFormat(pattern = "HH:mm", timezone = "GMT+8") private LocalTime alarmTime; } \ No newline at end of file diff --git a/src/main/java/com/kkz/kpersonalre/entity/TbAlarmPersonConfigEntity.java b/src/main/java/com/kkz/kpersonalre/entity/TbAlarmPersonConfigEntity.java index 10d5b71..0aaed2a 100644 --- a/src/main/java/com/kkz/kpersonalre/entity/TbAlarmPersonConfigEntity.java +++ b/src/main/java/com/kkz/kpersonalre/entity/TbAlarmPersonConfigEntity.java @@ -33,7 +33,7 @@ public class TbAlarmPersonConfigEntity extends BaseEntity { /** * 配置描述 */ - private String describe; + private String describes; /** * json类型 diff --git a/src/main/java/com/kkz/kpersonalre/entity/TbLocationUploadEntity.java b/src/main/java/com/kkz/kpersonalre/entity/TbLocationUploadEntity.java index 9043e3e..f17d0c2 100644 --- a/src/main/java/com/kkz/kpersonalre/entity/TbLocationUploadEntity.java +++ b/src/main/java/com/kkz/kpersonalre/entity/TbLocationUploadEntity.java @@ -30,4 +30,9 @@ public class TbLocationUploadEntity extends BaseEntity { */ private Integer locationConfigId; + /** + * 人员配置表id + */ + private Integer personConfigId; + } diff --git a/src/main/java/com/kkz/kpersonalre/service/AlarmService.java b/src/main/java/com/kkz/kpersonalre/service/AlarmService.java index 23a04c2..366163f 100644 --- a/src/main/java/com/kkz/kpersonalre/service/AlarmService.java +++ b/src/main/java/com/kkz/kpersonalre/service/AlarmService.java @@ -3,6 +3,8 @@ package com.kkz.kpersonalre.service; import com.kkz.kpersonalre.api.R; +import java.time.LocalTime; + public interface AlarmService { /** @@ -10,10 +12,12 @@ public interface AlarmService { * @param location * @return */ - R uploadLocation(String location); + R uploadLocation(String person, String location); /** * 查询工作日 */ void checkWorkDay(); + + R getAlarm(String username); } diff --git a/src/main/java/com/kkz/kpersonalre/service/impl/AlarmServiceImpl.java b/src/main/java/com/kkz/kpersonalre/service/impl/AlarmServiceImpl.java index a2a5121..d3cb660 100644 --- a/src/main/java/com/kkz/kpersonalre/service/impl/AlarmServiceImpl.java +++ b/src/main/java/com/kkz/kpersonalre/service/impl/AlarmServiceImpl.java @@ -6,10 +6,15 @@ import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import com.kkz.kpersonalre.api.R; +import com.kkz.kpersonalre.entity.TbAlarmClockConfigEntity; +import com.kkz.kpersonalre.entity.TbAlarmPersonConfigEntity; import com.kkz.kpersonalre.entity.TbDateWeekdayEntity; import com.kkz.kpersonalre.entity.TbLocationConfigEntity; import com.kkz.kpersonalre.entity.TbLocationUploadEntity; +import com.kkz.kpersonalre.entity.TbPersonConfigEntity; import com.kkz.kpersonalre.mapper.TbAlarmClockConfigMapper; import com.kkz.kpersonalre.mapper.TbAlarmPersonConfigMapper; import com.kkz.kpersonalre.mapper.TbDateWeekdayMapper; @@ -22,8 +27,12 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.time.LocalDate; +import java.time.LocalTime; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; /** * @program: KPersonal @@ -64,15 +73,28 @@ public class AlarmServiceImpl implements AlarmService { * @return */ @Override - public R uploadLocation(String location) { + public R uploadLocation(String person, String location) { - TbLocationConfigEntity locationConfigEntity = locationConfigMapper.selectOne(new LambdaQueryWrapper() - .eq(TbLocationConfigEntity::getName, location)); + TbLocationConfigEntity locationConfigEntity = null; + List tbLocationConfigEntities = locationConfigMapper.selectList(new LambdaQueryWrapper()); + for (TbLocationConfigEntity tbLocationConfigEntity : tbLocationConfigEntities) { + if (location.contains(tbLocationConfigEntity.getName())) { + locationConfigEntity = tbLocationConfigEntity; + break; + } + } if (locationConfigEntity == null) { return R.failed("位置不存在"); } + TbPersonConfigEntity personConfigEntity = personConfigMapper.selectOne(new LambdaQueryWrapper().eq(TbPersonConfigEntity::getPerson, person)); + if (BeanUtil.isEmpty(personConfigEntity)) { + return R.failed("用户不存在"); + } locationUploadMapper.insert(new TbLocationUploadEntity() - .setLocationConfigId(locationConfigEntity.getId().intValue())); + .setLocationConfigId(locationConfigEntity.getId().intValue()) + .setPersonConfigId(personConfigEntity.getId().intValue())); + //检查一下工作日信息 + checkWorkDay(); return R.ok("上传成功"); } @@ -94,4 +116,91 @@ public class AlarmServiceImpl implements AlarmService { } } } + + @Override + public R getAlarm(String username) { + + //判断今天是不是工作日 + TbDateWeekdayEntity dateWeekdayEntity = dateWeekdayMapper.selectOne(new LambdaQueryWrapper() + .eq(TbDateWeekdayEntity::getDate, LocalDate.now())); + if (BeanUtil.isEmpty(dateWeekdayEntity) || dateWeekdayEntity.getHolidayFlag()) { + return R.failed("今天放假"); + } + //找到人 + TbPersonConfigEntity personConfigEntity = personConfigMapper.selectOne(new LambdaQueryWrapper() + .eq(TbPersonConfigEntity::getPerson, username)); + //找到配置 + List alarmPersonConfigEntities = alarmPersonConfigMapper.selectList(new LambdaQueryWrapper<>()); + List configs = alarmPersonConfigEntities.stream().map(TbAlarmPersonConfigEntity::getConfig).toList(); + //先判断是不是自己的,不是自己的就没有配置 + //personId,localtionId,alarmClockId + for (String configStr : configs) { + Integer alarmClockId = findAlarmClockId(configStr, personConfigEntity.getId().intValue()); + if (alarmClockId != null) { + TbAlarmClockConfigEntity entity = alarmClockConfigMapper.selectById(alarmClockId); + if (BeanUtil.isEmpty(entity)){ + return R.failed("不响铃"); + } + return R.ok(entity.getAlarmTime()); + } + } + return R.failed("自定义闹钟"); + } + + public Integer findAlarmClockId(String json, Integer personId) { + ObjectMapper objectMapper = new ObjectMapper(); + Integer myClockId = null; + try { + List> configs = objectMapper.readValue(json, new TypeReference<>() { + }); + + // 优先处理组合模式 + if (configs.size() > 1) { + for (List config : configs) { + if (matchCondition(config)) { + //需要每个都查一遍 + if (Objects.equals(personId, config.get(0))) { + myClockId = config.get(2); + } + } else { + //走到这里说明另一个没有匹配上 + return null; + } + } + return myClockId; + } + + // 处理单条模式 + if (!configs.isEmpty() && matchCondition(configs.get(0))) { + return configs.get(0).get(2); + } + + return null; + } catch (Exception e) { + throw new RuntimeException("JSON解析失败", e); + } + } + + private boolean matchCondition(List config) { + Integer configPersonId = config.get(0); + Integer locationId = config.get(1); + + return locationExists(configPersonId, locationId); // 验证location存在性 + } + + private boolean locationExists(Integer personId, Integer locationId) { + // MyBatis Plus查询:检查location_config_id是否存在 + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query + .eq(TbLocationUploadEntity::getLocationConfigId, locationId) + .eq(TbLocationUploadEntity::getPersonConfigId, personId); + List locationUploadEntities = locationUploadMapper.selectList(query); + if (locationUploadEntities.isEmpty()) { + return false; + } + //要最新的一条 + TbLocationUploadEntity locationUploadEntity = locationUploadEntities.get(locationUploadEntities.size() - 1); + //最新的一条必须符合条件 + return Objects.equals(locationUploadEntity.getLocationConfigId(), locationId) && Objects.equals(locationUploadEntity.getPersonConfigId(), personId); + } }