|
@@ -18,14 +18,27 @@ public class DietMealConfigService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private MealConfigMapper mealConfigMapper;
|
|
private MealConfigMapper mealConfigMapper;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取共餐配置
|
|
|
|
|
+ * dateType: "weekday"/"weekend" → 查默认配置;"today" → 查当天临时配置
|
|
|
|
|
+ */
|
|
|
public Map<String, Object> getConfig(Long familyId, String dateType, String mealType) {
|
|
public Map<String, Object> getConfig(Long familyId, String dateType, String mealType) {
|
|
|
LambdaQueryWrapper<MealConfig> wrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<MealConfig> wrapper = new LambdaQueryWrapper<>();
|
|
|
wrapper.eq(MealConfig::getFamilyId, familyId)
|
|
wrapper.eq(MealConfig::getFamilyId, familyId)
|
|
|
- .eq(MealConfig::getConfigDateType, dateType)
|
|
|
|
|
.eq(MealConfig::getMealType, mealType);
|
|
.eq(MealConfig::getMealType, mealType);
|
|
|
-
|
|
|
|
|
- MealConfig config = mealConfigMapper.selectOne(wrapper);
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ MealConfig config;
|
|
|
|
|
+ if ("today".equals(dateType)) {
|
|
|
|
|
+ // 当天临时配置:优先查当天日期
|
|
|
|
|
+ wrapper.eq(MealConfig::getConfigDate, LocalDate.now());
|
|
|
|
|
+ config = mealConfigMapper.selectOne(wrapper);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 工作日/周末默认配置:config_date 必须为 NULL
|
|
|
|
|
+ wrapper.eq(MealConfig::getConfigDateType, dateType)
|
|
|
|
|
+ .isNull(MealConfig::getConfigDate);
|
|
|
|
|
+ config = mealConfigMapper.selectOne(wrapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
if (config != null) {
|
|
if (config != null) {
|
|
|
result.put("id", config.getId());
|
|
result.put("id", config.getId());
|
|
@@ -34,14 +47,20 @@ public class DietMealConfigService {
|
|
|
result.put("mealType", config.getMealType());
|
|
result.put("mealType", config.getMealType());
|
|
|
result.put("participantMemberIds", config.getParticipantMemberIds());
|
|
result.put("participantMemberIds", config.getParticipantMemberIds());
|
|
|
result.put("notes", config.getNotes());
|
|
result.put("notes", config.getNotes());
|
|
|
|
|
+ result.put("configDate", config.getConfigDate());
|
|
|
} else {
|
|
} else {
|
|
|
result.put("id", null);
|
|
result.put("id", null);
|
|
|
result.put("participantMemberIds", "[]");
|
|
result.put("participantMemberIds", "[]");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存共餐配置
|
|
|
|
|
+ * dateType="today" 时保存当天临时配置(config_date=当天,config_date_type=NULL)
|
|
|
|
|
+ * dateType="weekday"/"weekend" 时保存默认配置(config_date=NULL)
|
|
|
|
|
+ */
|
|
|
public void saveConfig(Long familyId, Map<String, Object> request) {
|
|
public void saveConfig(Long familyId, Map<String, Object> request) {
|
|
|
String dateType = request.get("date_type") != null ? request.get("date_type").toString() : null;
|
|
String dateType = request.get("date_type") != null ? request.get("date_type").toString() : null;
|
|
|
if (dateType == null || dateType.isEmpty()) {
|
|
if (dateType == null || dateType.isEmpty()) {
|
|
@@ -50,7 +69,6 @@ public class DietMealConfigService {
|
|
|
|
|
|
|
|
Object configsObj = request.get("configs");
|
|
Object configsObj = request.get("configs");
|
|
|
if (configsObj instanceof Map) {
|
|
if (configsObj instanceof Map) {
|
|
|
- // 批量格式: {"date_type":"weekday","configs":{"breakfast":"[1,2]","lunch":"[]","dinner":"[3]"}}
|
|
|
|
|
Map<?, ?> configs = (Map<?, ?>) configsObj;
|
|
Map<?, ?> configs = (Map<?, ?>) configsObj;
|
|
|
for (Map.Entry<?, ?> entry : configs.entrySet()) {
|
|
for (Map.Entry<?, ?> entry : configs.entrySet()) {
|
|
|
if (entry.getKey() == null || entry.getValue() == null) {
|
|
if (entry.getKey() == null || entry.getValue() == null) {
|
|
@@ -61,7 +79,6 @@ public class DietMealConfigService {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 单条格式(兼容旧调用): {"date_type":"weekday","meal_type":"breakfast","participant_member_ids":"[1,2]","notes":"..."}
|
|
|
|
|
String mealType = request.get("meal_type") != null ? request.get("meal_type").toString() : null;
|
|
String mealType = request.get("meal_type") != null ? request.get("meal_type").toString() : null;
|
|
|
String participantMemberIds = request.get("participant_member_ids") != null
|
|
String participantMemberIds = request.get("participant_member_ids") != null
|
|
|
? request.get("participant_member_ids").toString() : null;
|
|
? request.get("participant_member_ids").toString() : null;
|
|
@@ -77,26 +94,44 @@ public class DietMealConfigService {
|
|
|
saveOneConfig(familyId, dateType, mealType, participantMemberIds, notes);
|
|
saveOneConfig(familyId, dateType, mealType, participantMemberIds, notes);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void saveOneConfig(Long familyId, String dateType, String mealType, String participantMemberIds, String notes) {
|
|
|
|
|
|
|
+ private void saveOneConfig(Long familyId, String dateType, String mealType,
|
|
|
|
|
+ String participantMemberIds, String notes) {
|
|
|
|
|
+ saveOneConfig(familyId, dateType, mealType, participantMemberIds, notes, null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void saveOneConfig(Long familyId, String dateType, String mealType,
|
|
|
|
|
+ String participantMemberIds, String notes, LocalDate configDate) {
|
|
|
LambdaQueryWrapper<MealConfig> wrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<MealConfig> wrapper = new LambdaQueryWrapper<>();
|
|
|
wrapper.eq(MealConfig::getFamilyId, familyId)
|
|
wrapper.eq(MealConfig::getFamilyId, familyId)
|
|
|
- .eq(MealConfig::getConfigDateType, dateType)
|
|
|
|
|
.eq(MealConfig::getMealType, mealType);
|
|
.eq(MealConfig::getMealType, mealType);
|
|
|
-
|
|
|
|
|
|
|
+ if ("today".equals(dateType)) {
|
|
|
|
|
+ wrapper.eq(MealConfig::getConfigDate, configDate != null ? configDate : LocalDate.now());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ wrapper.eq(MealConfig::getConfigDateType, dateType)
|
|
|
|
|
+ .isNull(MealConfig::getConfigDate);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
MealConfig config = mealConfigMapper.selectOne(wrapper);
|
|
MealConfig config = mealConfigMapper.selectOne(wrapper);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (config == null) {
|
|
if (config == null) {
|
|
|
config = new MealConfig();
|
|
config = new MealConfig();
|
|
|
config.setFamilyId(familyId);
|
|
config.setFamilyId(familyId);
|
|
|
- config.setConfigDateType(dateType);
|
|
|
|
|
config.setMealType(mealType);
|
|
config.setMealType(mealType);
|
|
|
config.setCreatedAt(new java.util.Date());
|
|
config.setCreatedAt(new java.util.Date());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if ("today".equals(dateType)) {
|
|
|
|
|
+ config.setConfigDate(configDate != null ? configDate : LocalDate.now());
|
|
|
|
|
+ config.setConfigDateType(null);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ config.setConfigDateType(dateType);
|
|
|
|
|
+ config.setConfigDate(null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
config.setParticipantMemberIds(participantMemberIds);
|
|
config.setParticipantMemberIds(participantMemberIds);
|
|
|
config.setNotes(notes);
|
|
config.setNotes(notes);
|
|
|
config.setUpdatedAt(new java.util.Date());
|
|
config.setUpdatedAt(new java.util.Date());
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (config.getId() == null) {
|
|
if (config.getId() == null) {
|
|
|
mealConfigMapper.insert(config);
|
|
mealConfigMapper.insert(config);
|
|
|
} else {
|
|
} else {
|
|
@@ -104,17 +139,69 @@ public class DietMealConfigService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当天共餐参与者 IDs(优先当天临时配置,降级到工作日/周末默认配置)
|
|
|
|
|
+ */
|
|
|
|
|
+ public List<Long> queryTodayParticipantIds(Long familyId, LocalDate date) {
|
|
|
|
|
+ boolean isWeekend = date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY;
|
|
|
|
|
+ String defaultDateType = isWeekend ? "weekend" : "weekday";
|
|
|
|
|
+ Set<Long> ids = new HashSet<>();
|
|
|
|
|
+
|
|
|
|
|
+ // 优先查当天临时配置
|
|
|
|
|
+ for (String mealType : Arrays.asList("breakfast", "lunch", "dinner")) {
|
|
|
|
|
+ LambdaQueryWrapper<MealConfig> w = new LambdaQueryWrapper<>();
|
|
|
|
|
+ w.eq(MealConfig::getFamilyId, familyId)
|
|
|
|
|
+ .eq(MealConfig::getConfigDate, date)
|
|
|
|
|
+ .eq(MealConfig::getMealType, mealType);
|
|
|
|
|
+ MealConfig config = mealConfigMapper.selectOne(w);
|
|
|
|
|
+ if (config != null && config.getParticipantMemberIds() != null) {
|
|
|
|
|
+ ids.addAll(parseMemberIds(config.getParticipantMemberIds()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 当天无临时配置时,降级到工作日/周末默认配置
|
|
|
|
|
+ if (ids.isEmpty()) {
|
|
|
|
|
+ for (String mealType : Arrays.asList("breakfast", "lunch", "dinner")) {
|
|
|
|
|
+ LambdaQueryWrapper<MealConfig> w = new LambdaQueryWrapper<>();
|
|
|
|
|
+ w.eq(MealConfig::getFamilyId, familyId)
|
|
|
|
|
+ .eq(MealConfig::getConfigDateType, defaultDateType)
|
|
|
|
|
+ .isNull(MealConfig::getConfigDate)
|
|
|
|
|
+ .eq(MealConfig::getMealType, mealType);
|
|
|
|
|
+ MealConfig config = mealConfigMapper.selectOne(w);
|
|
|
|
|
+ if (config != null && config.getParticipantMemberIds() != null) {
|
|
|
|
|
+ ids.addAll(parseMemberIds(config.getParticipantMemberIds()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new ArrayList<>(ids);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Set<Long> parseMemberIds(String json) {
|
|
|
|
|
+ Set<Long> ids = new HashSet<>();
|
|
|
|
|
+ if (json == null || json.isEmpty()) return ids;
|
|
|
|
|
+ try {
|
|
|
|
|
+ com.fasterxml.jackson.databind.JsonNode nodes = new com.fasterxml.jackson.databind.ObjectMapper().readTree(json);
|
|
|
|
|
+ if (nodes.isArray()) {
|
|
|
|
|
+ for (com.fasterxml.jackson.databind.JsonNode node : nodes) ids.add(node.asLong());
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("解析 participantMemberIds 失败: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ return ids;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public Map<String, Object> getMealSummary(Long familyId, LocalDate date) {
|
|
public Map<String, Object> getMealSummary(Long familyId, LocalDate date) {
|
|
|
boolean isWeekend = date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY;
|
|
boolean isWeekend = date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY;
|
|
|
String dateType = isWeekend ? "weekend" : "weekday";
|
|
String dateType = isWeekend ? "weekend" : "weekday";
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Map<String, Object> summary = new HashMap<>();
|
|
Map<String, Object> summary = new HashMap<>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
for (String mealType : Arrays.asList("breakfast", "lunch", "dinner")) {
|
|
for (String mealType : Arrays.asList("breakfast", "lunch", "dinner")) {
|
|
|
Map<String, Object> config = getConfig(familyId, dateType, mealType);
|
|
Map<String, Object> config = getConfig(familyId, dateType, mealType);
|
|
|
summary.put(mealType + "_config", config);
|
|
summary.put(mealType + "_config", config);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return summary;
|
|
return summary;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|