|
|
@@ -0,0 +1,88 @@
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.FoodRecommendIndex;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.FoodRecommendIndexMapper;
|
|
|
+import com.etotem.cfc.service.UserCurrentFoodService;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 管理端维护:用户当前食材推荐(user_current_food_recommend)数据一致性回填。
|
|
|
+ *
|
|
|
+ * <p>对 food_recommend_idx 中已有报告基线的每个用户,复用 UserCurrentFoodService.rebuildForUser
|
|
|
+ * 按同一套问卷规则重建当前表;幂等可重复执行,不修改报告原始分。</p>
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/admin/user-current-food")
|
|
|
+public class AdminUserCurrentFoodController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FoodRecommendIndexMapper foodRecommendIndexMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserCurrentFoodService userCurrentFoodService;
|
|
|
+
|
|
|
+ @PostMapping("/rebuild-all")
|
|
|
+ public Result<Map<String, Object>> rebuildAll(
|
|
|
+ @RequestAttribute(value = "role", required = false) String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error(403, "无权限");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<FoodRecommendIndex> rows = foodRecommendIndexMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<FoodRecommendIndex>()
|
|
|
+ .select(FoodRecommendIndex::getUserId)
|
|
|
+ .groupBy(FoodRecommendIndex::getUserId));
|
|
|
+ Set<Long> userIds = new LinkedHashSet<Long>();
|
|
|
+ if (rows != null) {
|
|
|
+ for (FoodRecommendIndex row : rows) {
|
|
|
+ if (row != null && row.getUserId() != null) {
|
|
|
+ userIds.add(row.getUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int rebuilt = 0;
|
|
|
+ int failed = 0;
|
|
|
+ List<String> errors = new ArrayList<String>();
|
|
|
+ for (Long uid : userIds) {
|
|
|
+ try {
|
|
|
+ FamilyMember member = familyMemberMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<FamilyMember>()
|
|
|
+ .eq(FamilyMember::getUserId, uid)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ userCurrentFoodService.rebuildForUser(uid, member == null ? null : member.getId());
|
|
|
+ rebuilt++;
|
|
|
+ } catch (Exception e) {
|
|
|
+ failed++;
|
|
|
+ if (errors.size() < 10) {
|
|
|
+ errors.add(uid + ": " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> data = new LinkedHashMap<String, Object>();
|
|
|
+ data.put("baselineUsers", userIds.size());
|
|
|
+ data.put("rebuilt", rebuilt);
|
|
|
+ data.put("failed", failed);
|
|
|
+ data.put("errors", errors);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+}
|