From 4884b8391e12904f20e711c3483c65345b73de5a Mon Sep 17 00:00:00 2001 From: zrh050423 <14331304+zrh050423@user.noreply.gitee.com> Date: Sat, 21 Dec 2024 01:44:29 +0800 Subject: [PATCH] 1 --- .../controller/TalkController.java | 6 +-- .../edu/zrh/healthsystem/entity/TalkInfo.java | 5 +++ .../healthsystem/entity/UserLoginInfo.java | 5 +++ .../zrh/healthsystem/entity/UserMainInfo.java | 45 +++++++++++++++++++ .../repository/UserMainInfoRepository.java | 8 ++++ .../zrh/healthsystem/service/TalkService.java | 29 +++++++++--- 6 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 src/main/java/edu/zrh/healthsystem/entity/UserMainInfo.java create mode 100644 src/main/java/edu/zrh/healthsystem/repository/UserMainInfoRepository.java diff --git a/src/main/java/edu/zrh/healthsystem/controller/TalkController.java b/src/main/java/edu/zrh/healthsystem/controller/TalkController.java index 67026f1..529471b 100644 --- a/src/main/java/edu/zrh/healthsystem/controller/TalkController.java +++ b/src/main/java/edu/zrh/healthsystem/controller/TalkController.java @@ -4,6 +4,7 @@ import edu.zrh.healthsystem.entity.TalkInfo; import edu.zrh.healthsystem.model.Talk; import edu.zrh.healthsystem.model.TalkResponse; import edu.zrh.healthsystem.service.TalkService; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -27,10 +28,9 @@ public class TalkController { public String callApi() { return talkService.callApi("http://10.10.10.44:11434/"); } - @PostMapping("/talk/main") - public Map callApiChat(@RequestBody Talk talk) { - TalkResponse result = talkService.callApiChat(talk); + public Map callApiChat(@RequestBody Talk talk, HttpServletRequest httpServletRequest) { + TalkResponse result = talkService.callApiChat(talk,httpServletRequest); if (result != null) { return Map.of("status", "success", "data", result); } else { diff --git a/src/main/java/edu/zrh/healthsystem/entity/TalkInfo.java b/src/main/java/edu/zrh/healthsystem/entity/TalkInfo.java index b2b5054..499ef41 100644 --- a/src/main/java/edu/zrh/healthsystem/entity/TalkInfo.java +++ b/src/main/java/edu/zrh/healthsystem/entity/TalkInfo.java @@ -25,5 +25,10 @@ public class TalkInfo { @Column(name="talk_user") private String talkUser; + + @ManyToOne + @JoinColumn(name = "talk_user", referencedColumnName = "user_name", insertable = false, updatable = false) + private UserMainInfo userMainInfo; + // 关联到 UserMainInfo } diff --git a/src/main/java/edu/zrh/healthsystem/entity/UserLoginInfo.java b/src/main/java/edu/zrh/healthsystem/entity/UserLoginInfo.java index 68441da..0fdc05d 100644 --- a/src/main/java/edu/zrh/healthsystem/entity/UserLoginInfo.java +++ b/src/main/java/edu/zrh/healthsystem/entity/UserLoginInfo.java @@ -34,4 +34,9 @@ public class UserLoginInfo { */ @Column(name = "permission") private String permission; + + @ManyToOne + @JoinColumn(name = "id", referencedColumnName = "user_id", insertable = false, updatable = false) + private UserMainInfo userMainInfo; + // 关联到 UserMainInfo } diff --git a/src/main/java/edu/zrh/healthsystem/entity/UserMainInfo.java b/src/main/java/edu/zrh/healthsystem/entity/UserMainInfo.java new file mode 100644 index 0000000..5e8bf4b --- /dev/null +++ b/src/main/java/edu/zrh/healthsystem/entity/UserMainInfo.java @@ -0,0 +1,45 @@ +package edu.zrh.healthsystem.entity; +import jakarta.persistence.*; +import lombok.Data; +/** + * @author han + */ +@Entity +@Data +@Table(name = "user_main") +public class UserMainInfo { + /** + * ID + */ + @Id + @Column(name = "user_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + /** + * 名字 + */ + @Column(name = "user_name") + private String username; + + /** + * 年龄 + */ + @Column(name = "user_age") + private Integer age; + + /** + * 性别 + */ + @Column(name = "user_gender") + private String gender; + + /** + * 邮箱 + */ + @Column(name = "user_email") + private String email; + + + + +} diff --git a/src/main/java/edu/zrh/healthsystem/repository/UserMainInfoRepository.java b/src/main/java/edu/zrh/healthsystem/repository/UserMainInfoRepository.java new file mode 100644 index 0000000..6279f3f --- /dev/null +++ b/src/main/java/edu/zrh/healthsystem/repository/UserMainInfoRepository.java @@ -0,0 +1,8 @@ +package edu.zrh.healthsystem.repository; + +import edu.zrh.healthsystem.entity.MedicalRecordInfo; +import edu.zrh.healthsystem.entity.UserMainInfo; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserMainInfoRepository extends JpaRepository { +} diff --git a/src/main/java/edu/zrh/healthsystem/service/TalkService.java b/src/main/java/edu/zrh/healthsystem/service/TalkService.java index fefc52f..5806c79 100644 --- a/src/main/java/edu/zrh/healthsystem/service/TalkService.java +++ b/src/main/java/edu/zrh/healthsystem/service/TalkService.java @@ -1,10 +1,15 @@ package edu.zrh.healthsystem.service; +import edu.zrh.healthsystem.controller.TalkController; import edu.zrh.healthsystem.entity.TalkInfo; +import edu.zrh.healthsystem.entity.UserMainInfo; import edu.zrh.healthsystem.model.Talk; import edu.zrh.healthsystem.model.TalkResponse; import edu.zrh.healthsystem.repository.TalkInfoRepository; +import edu.zrh.healthsystem.repository.UserMainInfoRepository; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestAttribute; import org.springframework.web.client.RestTemplate; import java.time.LocalDateTime; @@ -19,24 +24,34 @@ import java.util.Map; public class TalkService { private final RestTemplate restTemplate; private final TalkInfoRepository talkInfoRepository; - public TalkService(RestTemplate restTemplate, TalkInfoRepository talkInfoRepository) { + private final UserMainInfoRepository userMainInfoRepository; + public TalkService(RestTemplate restTemplate, TalkInfoRepository talkInfoRepository, UserMainInfoRepository userMainInfoRepository) { this.restTemplate = restTemplate; this.talkInfoRepository = talkInfoRepository; + this.userMainInfoRepository = userMainInfoRepository; } public String callApi(String url) { return restTemplate.getForObject(url, String.class); } - public TalkResponse callApiChat(Talk request) { + public TalkResponse callApiChat(Talk request, HttpServletRequest httpServletRequest) { TalkResponse response = restTemplate.postForObject("http://10.10.10.44:11434/api/generate", request, TalkResponse.class); + String userId = (String) httpServletRequest.getAttribute("user"); + UserMainInfo userMainInfo = userMainInfoRepository.findById(Integer.parseInt(userId)).orElse(null); + if (userMainInfo != null) { + TalkInfo talkInfo = new TalkInfo(); + talkInfo.setTalkPrompt(request.getPrompt()); + if (response != null) { + talkInfo.setTalkResponse(response.getResponse()); + } + talkInfo.setTalkTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); - TalkInfo talkInfo = new TalkInfo(); - talkInfo.setTalkPrompt(request.getPrompt()); - talkInfo.setTalkResponse(response.getResponse()); - talkInfo.setTalkTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + talkInfo.setTalkUser(userMainInfo.getUsername()); - talkInfoRepository.save(talkInfo); + // 保存聊天记录 + talkInfoRepository.save(talkInfo); + } return response; } public List getAllRecord() {