package com.ruoyi.web.controller.api;
|
|
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.system.model.*;
|
import com.ruoyi.system.service.*;
|
import com.ruoyi.system.utils.util.AesSimpleUtil;
|
import com.ruoyi.system.utils.util.RsaSimpleUtil;
|
import com.ruoyi.system.utils.xiMaUtil.AesUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.UUID;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 微信小程序登录 前端控制器
|
* </p>
|
*
|
* @author xiaochen
|
* @since 2024-08-06
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("/aiCallback")
|
public class H5AICallbackController {
|
|
@Autowired
|
private TSysAiConfigService sysAiConfigService;
|
@Autowired
|
private TSysInspectionService sysInspectionService;
|
@Autowired
|
private TSysAppUserService sysAppUserService;
|
@Autowired
|
private TSysInspectionRiskEstimateService sysInspectionRiskEstimateService;
|
@Autowired
|
private TSysChronicDiseaseService sysChronicDiseaseService;
|
@Autowired
|
private TSysAiMessageService sysAiMessageService;
|
@Autowired
|
private ISysUserService sysUserService;
|
@Autowired
|
private TCrmClinicService crmClinicService;
|
|
// @PostMapping("/reportReturn")
|
// public String reportReturn(String encryptedJson, String signEncryptedJson) throws Exception {
|
// TSysAiConfig sysAiConfig = sysAiConfigService.getById(1);
|
// // 应用AES秘钥
|
// String aesKey = sysAiConfig.getAesKey();
|
// // 平台RSA公钥
|
// String rsaPublicKey = sysAiConfig.getRsaPublicKey();
|
//
|
// // 1. 先解密
|
// String source = AesSimpleUtil.decrypt(encryptedJson, aesKey);
|
// System.out.println("AI问诊回调数据=====:"+ source);
|
// JSONObject jsonObject = JSONObject.parseObject(source);
|
//
|
// // 2. 再验证签名
|
// String signSource = String.format("%s_%s",
|
// jsonObject.getString("thirdId"),
|
// jsonObject.getString("time"));
|
// boolean verify = RsaSimpleUtil.verify(signSource, signEncryptedJson, rsaPublicKey);
|
// if (!verify) {
|
// return "error";
|
// }
|
// // 3. 验证成功,则仅仅业务保存并返回success字符串
|
// /**
|
// * {
|
// * "returnType": 1,
|
// * "name": "张三",
|
// * "phone": "13344445555",
|
// * "email": null,
|
// * "sex": 1,
|
// * "age": 42,
|
// * "time": "2023-03-08 08:23:13",
|
// * "pdf": "https://labelsys-images.oss-cn-hangzhou.aliyuncs.com/tongueFile/check/2023/03/87d76220-085e-4d2e-9fd7-9f5a01da1940.pdf",
|
// * "healthIndex": 100.0,
|
// * "constitutionNames": "气虚",
|
// * "symptomName": "胃气虚证",
|
// * "thirdId": "13344445555",
|
// * "tongueFeature": "正常人舌色,舌质淡红润泽。舌苔呈现白色。",
|
// * "faceFeature": "面部颜色暗沉。"
|
// * }
|
// */
|
//
|
// Integer returnType = jsonObject.getInteger("returnType");
|
// String name = jsonObject.getString("name");
|
// String phone = jsonObject.getString("phone");
|
// String email = jsonObject.getString("email");
|
// Integer sex = jsonObject.getInteger("sex");
|
// Integer age = jsonObject.getInteger("age");
|
// String time = jsonObject.getString("time");
|
// String pdf = jsonObject.getString("pdf");
|
// Double healthIndex = jsonObject.getDouble("healthIndex");
|
// String constitutionNames = jsonObject.getString("constitutionNames");
|
// String symptomName = jsonObject.getString("symptomName");
|
// String thirdId = jsonObject.getString("thirdId");
|
// String tongueFeature = jsonObject.getString("tongueFeature");
|
// String faceFeature = jsonObject.getString("faceFeature");
|
//
|
// if(returnType == 2){
|
// log.info("用户["+phone+"]检测失败");
|
// }
|
//
|
// long count = sysInspectionService.count(Wrappers.lambdaQuery(TSysInspection.class)
|
// .eq(TSysInspection::getPersonPhone, phone)
|
// .eq(TSysInspection::getCheckTime, time));
|
// if (count == 0) {
|
// TSysInspection sysInspection = new TSysInspection();
|
// sysInspection.setPersonName(name);
|
// sysInspection.setPersonPhone(phone);
|
// sysInspection.setPersonSex(sex);
|
// sysInspection.setPersonAge(age);
|
// sysInspection.setCheckTime(time);
|
// sysInspection.setPdfUrl(pdf);
|
// sysInspection.setHealthIndex(healthIndex);
|
// sysInspection.setConstitutionNames(constitutionNames);
|
// sysInspection.setSymptomName(symptomName);
|
// sysInspection.setTongueFeature(tongueFeature);
|
// sysInspection.setFaceFeature(faceFeature);
|
// sysInspection.setAppUserId(thirdId);
|
// sysInspection.setInspectionType(2);
|
// sysInspection.setIsPay(0);
|
// sysInspectionService.save(sysInspection);
|
// }
|
// return "success";
|
// }
|
|
|
|
@PostMapping("/xiMaReportPush")
|
public JSONObject xiMaReportPush(@RequestBody JSONObject json){
|
TSysAiConfig sysAiConfig = sysAiConfigService.getById(1);
|
// 应用AES秘钥
|
String clientKey = sysAiConfig.getClientKey();
|
// 平台RSA公钥
|
String clientIV = sysAiConfig.getClientIV();
|
|
String data = json.getString("data");
|
// 解密
|
String raw = AesUtil.decrypt(clientKey, clientIV , data);
|
log.info("H5舌面报告推送,解密后:{}",raw);
|
TSysAiMessage tSysAiMessage = new TSysAiMessage();
|
tSysAiMessage.setCommitType(1);
|
tSysAiMessage.setContent(raw);
|
tSysAiMessage.setCreateTime(LocalDateTime.now());
|
|
JSONObject jsonObject = JSONObject.parseObject(raw);
|
|
// 报告信息
|
String reportTime = jsonObject.getString("reportTime"); // 报告时间
|
String reportH5Url = jsonObject.getString("reportH5Url"); // 报告H5地址URL
|
|
TSysInspection sysInspection = new TSysInspection();
|
sysInspection.setCheckTime(reportTime);
|
sysInspection.setReportPrintUrl(reportH5Url);
|
|
// 用户信息
|
JSONObject userInfo = jsonObject.getJSONObject("userInfo");
|
String outUserId = userInfo.getString("outUserId"); // 用户唯一标识
|
String birthday = userInfo.getString("birthday"); // 生日
|
String gender = userInfo.getString("gender"); // 用户性别:0男1女
|
String nickname = userInfo.getString("nickname"); // 用户姓名
|
|
// 查询用户
|
TSysAppUser sysAppUser = sysAppUserService.getOne(Wrappers.lambdaQuery(TSysAppUser.class)
|
.eq(TSysAppUser::getIdentifierNumber, outUserId)
|
.last("LIMIT 1"));
|
if(sysAppUser != null){
|
sysInspection.setAppUserId(sysAppUser.getId());
|
sysInspection.setClinicId(sysAppUser.getClinicId());
|
LocalDateTime vipEndTime = sysAppUser.getVipEndTime();
|
if(Objects.nonNull(vipEndTime) && vipEndTime.isAfter(LocalDateTime.now())){
|
sysInspection.setIsPay(1);
|
}
|
}else {
|
tSysAiMessage.setErrorMessage("未查询到患者");
|
}
|
sysAiMessageService.save(tSysAiMessage);
|
sysInspection.setOutUserId(outUserId);
|
sysInspection.setBirthday(birthday);
|
sysInspection.setSex("0".equals(gender)?1:2);
|
sysInspection.setNickName(nickname);
|
// 小程序检测
|
sysInspection.setInspectionType(2);
|
|
// 舌诊信息
|
JSONObject phyTongueInfo = jsonObject.getJSONObject("phyTongueInfo");
|
JSONObject tongueNatureColorInfo = phyTongueInfo.getJSONObject("tongueNatureColorInfo"); // 舌色信息
|
String tongueNatureColor = tongueNatureColorInfo.getString("tongueNatureColorClinical"); // 舌色临床意义
|
sysInspection.setTongueNatureColor(tongueNatureColor);
|
// 面诊信息
|
JSONObject phyFaceInfo = jsonObject.getJSONObject("phyFaceInfo");
|
JSONObject lipColorInfo = phyFaceInfo.getJSONObject("lipColorInfo"); // 唇色信息
|
String lipColorClinical = lipColorInfo.getString("lipColorClinical"); // 唇色临床意义
|
sysInspection.setLipColorClinical(lipColorClinical);
|
|
// 心理特征
|
JSONArray psychology = jsonObject.getJSONArray("psychology");
|
// 循环拼接字符串
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < psychology.size(); i++) {
|
String name = psychology.getString(i);
|
sb.append(name).append(",");
|
}
|
sb.deleteCharAt(sb.length()-1);
|
sysInspection.setPsychology(sb.toString());
|
|
// 疾病风险
|
JSONObject phyRiskInfo = jsonObject.getJSONObject("phyRiskInfo");
|
String riskTitle = phyRiskInfo.getString("riskTitle"); // 多个疾病,以#间隔
|
sysInspection.setRiskTitle(riskTitle);
|
|
sysInspectionService.save(sysInspection);
|
|
JSONObject response = new JSONObject();
|
response.put("error_code",0);
|
return response;
|
}
|
|
@PostMapping("/xiMaDoubleScreenReportPush")
|
public JSONObject xiMaDoubleScreenReportPush(@RequestBody JSONObject jsonObject){
|
log.info("双屏仪报告推送:{}",jsonObject);
|
TSysAiMessage tSysAiMessage = new TSysAiMessage();
|
tSysAiMessage.setCommitType(1);
|
tSysAiMessage.setContent(jsonObject.toJSONString());
|
tSysAiMessage.setCreateTime(LocalDateTime.now());
|
// 主体信息
|
String reportTime = jsonObject.getString("report_time"); // 报告时间
|
String report_print_url = jsonObject.getString("report_print_url"); // 报告H5页面地址
|
String present_history = jsonObject.getString("present_history"); // 患者现病史
|
String tcm_syndrome = jsonObject.getString("tcm_syndrome"); // 中医证型
|
String disease_history_memo = jsonObject.getString("disease_history_memo"); // 历史疾病补充
|
String western_medicine_diagnosis = jsonObject.getString("western_medicine_diagnosis"); // 西医诊断
|
|
TSysInspection sysInspection = new TSysInspection();
|
sysInspection.setCheckTime(reportTime);
|
sysInspection.setReportPrintUrl(report_print_url);
|
sysInspection.setPresentHistory(present_history);
|
sysInspection.setTcmSyndrome(tcm_syndrome);
|
sysInspection.setDiseaseHistoryMemo(disease_history_memo);
|
sysInspection.setWesternMedicineDiagnosis(western_medicine_diagnosis);
|
|
// 用户信息
|
JSONObject userInfo = jsonObject.getJSONObject("user_info");
|
Long id = userInfo.getLong("id"); // 用户ID
|
String phone = userInfo.getString("phone"); // 手机号
|
String user_name = userInfo.getString("user_name"); // 用户姓名
|
Long device_account_id = userInfo.getLong("device_account_id"); // 设备账号ID
|
String sex = userInfo.getString("sex"); // 性别
|
String age = userInfo.getString("age"); // 年龄
|
String device_account = userInfo.getString("device_account"); // 诊所账号
|
|
sysInspection.setThreeUserId(id);
|
sysInspection.setPhone(phone);
|
sysInspection.setNickName(user_name);
|
sysInspection.setSex("男".equals(sex) ? 1 : 2);
|
sysInspection.setAge(Integer.parseInt(age));
|
sysInspection.setDeviceAccountId(device_account_id);
|
// 硬件检测
|
sysInspection.setInspectionType(3);
|
sysInspection.setIsPay(1);
|
// 处方信息
|
JSONObject treatment_suggestion = jsonObject.getJSONObject("treatment_suggestion");
|
String medicine_print_url = treatment_suggestion.getString("medicine_print_url");
|
sysInspection.setMedicinePrintUrl(medicine_print_url);
|
|
// 通过手机号查询小程序用户
|
TSysAppUser sysAppUser = sysAppUserService.getOne(Wrappers.lambdaQuery(TSysAppUser.class)
|
.eq(TSysAppUser::getPhone, phone)
|
.last("LIMIT 1"));
|
if(sysAppUser != null){
|
sysInspection.setAppUserId(sysAppUser.getId());
|
sysInspection.setClinicId(sysAppUser.getClinicId());
|
}else {
|
sysAppUser = new TSysAppUser();
|
sysAppUser.setPhone(phone);
|
String identifierNumber = UUID.randomUUID().toString().replace("-", "");
|
sysAppUser.setIdentifierNumber(identifierNumber);
|
sysAppUser.setNickName(user_name);
|
sysAppUser.setSex("男".equals(sex) ? 1 : 2);
|
}
|
|
// 查询诊所账号
|
SysUser sysUser = sysUserService.selectByPhone(device_account);
|
if(Objects.nonNull(sysUser)){
|
// 通过账号查询诊所信息
|
TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
|
.eq(TCrmClinic::getUserId, sysUser.getUserId())
|
.last("LIMIT 1"));
|
if(Objects.nonNull(crmClinic)){
|
sysAppUser.setClinicId(crmClinic.getId());
|
}else {
|
tSysAiMessage.setErrorMessage("未查询到诊所信息");
|
sysAiMessageService.save(tSysAiMessage);
|
}
|
}else {
|
tSysAiMessage.setErrorMessage("未查询到诊所绑定账号");
|
sysAiMessageService.save(tSysAiMessage);
|
}
|
|
sysInspectionService.save(sysInspection);
|
|
// 指脉结果
|
JSONObject fingerPulseReport = jsonObject.getJSONObject("finger_pulse_report");
|
JSONArray riskEstimate = fingerPulseReport.getJSONArray("risk_estimate");
|
List<TSysInspectionRiskEstimate> sysInspectionRiskEstimates = new ArrayList<>();
|
// 查询慢性病列表
|
List<TSysChronicDisease> sysChronicDiseases = sysChronicDiseaseService.list();
|
for (int i = 0; i < riskEstimate.size(); i++) {
|
JSONObject json = riskEstimate.getJSONObject(i);
|
TSysInspectionRiskEstimate sysInspectionRiskEstimate = new TSysInspectionRiskEstimate();
|
sysInspectionRiskEstimate.setInspectionId(sysInspection.getId());
|
String name = json.getString("name"); // 疾病名称
|
String link_url = json.getString("link_url"); // 疾病H5链接
|
String value = json.getString("value"); // 风险值
|
String value_desc = json.getString("value_desc"); // 风险值描述
|
sysInspectionRiskEstimate.setRiskName(name);
|
sysInspectionRiskEstimate.setRiskLinkUrl(link_url);
|
sysInspectionRiskEstimate.setRiskValue(value);
|
sysInspectionRiskEstimate.setRiskValueDesc(value_desc);
|
sysInspectionRiskEstimates.add(sysInspectionRiskEstimate);
|
// 判断是否为慢性病患者
|
if (("中风险".equals(value) || "重风险".equals(value)) && !"急性咽炎".equals(name)){
|
// 使用名称查询慢性病
|
if(CollectionUtils.isEmpty(sysChronicDiseases)){
|
TSysChronicDisease tSysChronicDisease = new TSysChronicDisease();
|
tSysChronicDisease.setChronicName(name);
|
sysChronicDiseaseService.save(tSysChronicDisease);
|
if(StringUtils.hasLength(sysAppUser.getChronicDiseaseId())){
|
sysAppUser.setChronicDiseaseId(sysAppUser.getChronicDiseaseId() + "," + tSysChronicDisease.getId());
|
}else{
|
sysAppUser.setChronicDiseaseId(tSysChronicDisease.getId());
|
}
|
}else{
|
List<TSysChronicDisease> chronicDiseaseList = sysChronicDiseases.stream().filter(t -> t.getChronicName().equals(name)).collect(Collectors.toList());
|
if(CollectionUtils.isEmpty(chronicDiseaseList)){
|
TSysChronicDisease tSysChronicDisease = new TSysChronicDisease();
|
tSysChronicDisease.setChronicName(name);
|
sysChronicDiseaseService.save(tSysChronicDisease);
|
if(StringUtils.hasLength(sysAppUser.getChronicDiseaseId())){
|
sysAppUser.setChronicDiseaseId(sysAppUser.getChronicDiseaseId() + "," + tSysChronicDisease.getId());
|
}else{
|
sysAppUser.setChronicDiseaseId(tSysChronicDisease.getId());
|
}
|
}else {
|
TSysChronicDisease tSysChronicDisease = chronicDiseaseList.get(0);
|
if(StringUtils.hasLength(sysAppUser.getChronicDiseaseId())){
|
sysAppUser.setChronicDiseaseId(sysAppUser.getChronicDiseaseId() + "," + tSysChronicDisease.getId());
|
}else{
|
sysAppUser.setChronicDiseaseId(tSysChronicDisease.getId());
|
}
|
}
|
}
|
}
|
}
|
sysAppUserService.saveOrUpdate(sysAppUser);
|
sysInspectionRiskEstimateService.saveBatch(sysInspectionRiskEstimates);
|
|
JSONObject response = new JSONObject();
|
response.put("error_code",0);
|
return response;
|
}
|
|
}
|