|
@@ -2,9 +2,12 @@ package com.etotem.cfc.controller.mind;
|
|
|
|
|
|
|
|
import com.etotem.cfc.common.Result;
|
|
import com.etotem.cfc.common.Result;
|
|
|
import com.etotem.cfc.entity.FamilyFortune;
|
|
import com.etotem.cfc.entity.FamilyFortune;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyFortuneReport;
|
|
|
import com.etotem.cfc.service.FortuneService;
|
|
import com.etotem.cfc.service.FortuneService;
|
|
|
|
|
+import com.etotem.cfc.service.PdfReportService;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestAttribute;
|
|
import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
@@ -19,39 +22,116 @@ public class MindFortuneController {
|
|
|
@Resource
|
|
@Resource
|
|
|
private FortuneService fortuneService;
|
|
private FortuneService fortuneService;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private PdfReportService pdfReportService;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取今日家庭运势
|
|
* 获取今日家庭运势
|
|
|
- * @param familyId 家庭ID
|
|
|
|
|
- * @return 家庭运势信息
|
|
|
|
|
*/
|
|
*/
|
|
|
@PostMapping("/fortune")
|
|
@PostMapping("/fortune")
|
|
|
public Result<Map<String, Object>> getTodayFortune(@RequestAttribute("familyId") Long familyId) {
|
|
public Result<Map<String, Object>> getTodayFortune(@RequestAttribute("familyId") Long familyId) {
|
|
|
FamilyFortune fortune = fortuneService.getFamilyFortune(familyId, LocalDate.now());
|
|
FamilyFortune fortune = fortuneService.getFamilyFortune(familyId, LocalDate.now());
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
|
|
- result.put("dominantElement", fortune.getDominantElement());
|
|
|
|
|
- result.put("luckyDirection", fortune.getLuckyDirection() != null ? fortune.getLuckyDirection().replace("方", "") : "东");
|
|
|
|
|
- result.put("fortuneLevel", fortune.getFortuneLevel());
|
|
|
|
|
- result.put("fortuneLevelText", fortune.getFortuneLevelText());
|
|
|
|
|
- result.put("fortuneDescription", fortune.getFortuneDescription());
|
|
|
|
|
- result.put("moodScore", fortune.getMoodScore());
|
|
|
|
|
- result.put("moodEmoji", fortune.getMoodEmoji());
|
|
|
|
|
- result.put("moodText", fortune.getMoodText());
|
|
|
|
|
- result.put("dailyTip", fortune.getDailyTip());
|
|
|
|
|
- result.put("weeklyTip", fortune.getWeeklyTip());
|
|
|
|
|
|
|
+ Map<String, Object> result = batchBuildFortuneResponse(fortune);
|
|
|
return Result.success(result);
|
|
return Result.success(result);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 手动刷新运势(仅供测试)
|
|
* 手动刷新运势(仅供测试)
|
|
|
- * @param familyId 家庭ID
|
|
|
|
|
- * @return 刷新后的运势信息
|
|
|
|
|
*/
|
|
*/
|
|
|
@PostMapping("/fortune/refresh")
|
|
@PostMapping("/fortune/refresh")
|
|
|
public Result<FamilyFortune> refreshFortune(@RequestAttribute("familyId") Long familyId) {
|
|
public Result<FamilyFortune> refreshFortune(@RequestAttribute("familyId") Long familyId) {
|
|
|
FamilyFortune fortune = fortuneService.getFamilyFortune(familyId, LocalDate.now());
|
|
FamilyFortune fortune = fortuneService.getFamilyFortune(familyId, LocalDate.now());
|
|
|
- // 强制重新计算
|
|
|
|
|
fortune.setLuckyDirection(fortuneService.calculateLuckyDirection(familyId, LocalDate.now()));
|
|
fortune.setLuckyDirection(fortuneService.calculateLuckyDirection(familyId, LocalDate.now()));
|
|
|
fortuneService.saveFamilyFortune(fortune);
|
|
fortuneService.saveFamilyFortune(fortune);
|
|
|
return Result.success(fortune);
|
|
return Result.success(fortune);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 天盘周报导出 - 生成 PDF 报告
|
|
|
|
|
+ * @param request 包含 Base64 图片、运势数据、家庭信息
|
|
|
|
|
+ * @return PDF 文件URL
|
|
|
|
|
+ */
|
|
|
|
|
+ @PostMapping("/fortune/export")
|
|
|
|
|
+ public Result<Map<String, Object>> exportFortuneReport(
|
|
|
|
|
+ @RequestAttribute("familyId") Long familyId,
|
|
|
|
|
+ @RequestBody ExportReportRequest request) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 文件大小校验
|
|
|
|
|
+ if (request.imageBase64 == null || request.imageBase64.length() > 4 * 1024 * 1024) {
|
|
|
|
|
+ return Result.error("图片数据无效或超过4MB");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 生成 PDF 文件
|
|
|
|
|
+ byte[] pdfBytes = pdfReportService.generatePdfReport(
|
|
|
|
|
+ request.imageBase64,
|
|
|
|
|
+ request.element,
|
|
|
|
|
+ request.luckyDirection,
|
|
|
|
|
+ request.tip
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 保存到本地
|
|
|
|
|
+ String fileName = pdfReportService.savePdfReport(pdfBytes, familyId, request.element);
|
|
|
|
|
+
|
|
|
|
|
+ // 记录到数据库
|
|
|
|
|
+ FamilyFortuneReport report = new FamilyFortuneReport();
|
|
|
|
|
+ report.setFamilyId(Math.toIntExact(familyId));
|
|
|
|
|
+ report.setElement(request.element);
|
|
|
|
|
+ report.setLuckyDirection(request.luckyDirection);
|
|
|
|
|
+ report.setPdfPath(fileName);
|
|
|
|
|
+ report.setCreatedAt(new java.util.Date());
|
|
|
|
|
+ pdfReportService.saveReport(record -> {
|
|
|
|
|
+ record.setFamilyId(report.getFamilyId());
|
|
|
|
|
+ record.setElement(report.getElement());
|
|
|
|
|
+ record.setLuckyDirection(report.getLuckyDirection());
|
|
|
|
|
+ record.setPdfPath(report.getPdfPath());
|
|
|
|
|
+ record.setCreatedAt(report.getCreatedAt());
|
|
|
|
|
+ return record;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 返回响应
|
|
|
|
|
+ Map<String, Object> response = new HashMap<>();
|
|
|
|
|
+ response.put("pdfUrl", "/storage/reports/" + fileName);
|
|
|
|
|
+ response.put("fileName", fileName);
|
|
|
|
|
+ response.put("fileSize", pdfBytes.length);
|
|
|
|
|
+ response.put("element", request.element);
|
|
|
|
|
+
|
|
|
|
|
+ return Result.success(response);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return Result.error("PDF 生成失败: " + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 请求数据封装
|
|
|
|
|
+ */
|
|
|
|
|
+ public static class ExportReportRequest {
|
|
|
|
|
+ private String imageBase64;
|
|
|
|
|
+ private String element;
|
|
|
|
|
+ private String luckyDirection;
|
|
|
|
|
+ private String tip;
|
|
|
|
|
+
|
|
|
|
|
+ public String getImageBase64() { return imageBase64; }
|
|
|
|
|
+ public void setImageBase64(String imageBase64) { this.imageBase64 = imageBase64; }
|
|
|
|
|
+ public String getElement() { return element; }
|
|
|
|
|
+ public void setElement(String element) { this.element = element; }
|
|
|
|
|
+ public String getLuckyDirection() { return luckyDirection; }
|
|
|
|
|
+ public void setLuckyDirection(String luckyDirection) { this.luckyDirection = luckyDirection; }
|
|
|
|
|
+ public String getTip() { return tip; }
|
|
|
|
|
+ public void setTip(String tip) { this.tip = tip; }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> batchBuildFortuneResponse(FamilyFortune fortune) {
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("dominantElement", fortune.getDominantElement());
|
|
|
|
|
+ result.put("luckyDirection", fortune.getLuckyDirection() != null ? fortune.getLuckyDirection().replace("方", "") : "东");
|
|
|
|
|
+ result.put("fortuneLevel", fortune.getFortuneLevel());
|
|
|
|
|
+ result.put("fortuneLevelText", fortune.getFortuneLevelText());
|
|
|
|
|
+ result.put("fortuneDescription", fortune.getFortuneDescription());
|
|
|
|
|
+ result.put("moodScore", fortune.getMoodScore());
|
|
|
|
|
+ result.put("moodEmoji", fortune.getMoodEmoji());
|
|
|
|
|
+ result.put("moodText", fortune.getMoodText());
|
|
|
|
|
+ result.put("dailyTip", fortune.getDailyTip());
|
|
|
|
|
+ result.put("weeklyTip", fortune.getWeeklyTip());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|