forked from gzzfw/backEnd/gz-dyh

xusd
2024-09-11 4d273f93f82f0be873f386db2fc2f6c792ad4236
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cn.huge.module.ai.controller;
 
import cn.huge.base.common.utils.GuavaCacheUtils;
import cn.huge.base.common.utils.HttpClientUtils;
import cn.huge.base.common.utils.ObjectUtils;
import cn.huge.base.common.utils.ReturnSucUtils;
import cn.huge.module.ai.controller.domain.dto.CaseInfoDetailDto;
import cn.huge.module.ai.controller.domain.dto.CaseInfoDto;
import cn.huge.module.ai.controller.domain.dto.CaseRiskDto;
import cn.huge.module.ai.controller.domain.dto.LawInfoDto;
import cn.huge.module.ai.controller.domain.vo.AiRequestVo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * AI请求调用/get-case获取案件列表(一般包含3个纠纷类和3个判决类),/get-law获取法条推荐列表;案件详情在根据案件列表的id及type调用/get-case-detail获取案件详情
 */
@Slf4j
@RestController
@RequestMapping("/api/ai/case-law")
public class AiController {
 
    @Value("${ai.url}")
    private String aiUrl;
 
    @PostMapping("/get-case")
    public Object getCase(@RequestBody AiRequestVo aiRequestVo) {
        Object cacheAi = GuavaCacheUtils.getCacheAi("get-case"+aiRequestVo.getCaseId());
        if(ObjectUtils.isNotEmpty(cacheAi)){
            return ReturnSucUtils.getRepInfo("请求成功", cacheAi);
        }
        List<CaseInfoDto> caseInfoDtoList = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        params.put("caseDes", aiRequestVo.getCaseDes());//案件描述
        params.put("caseClaim", aiRequestVo.getCaseClaim());//案件诉求
        params.put("caseId", aiRequestVo.getCaseId());//案件id
        params.put("topK", "2");
        params.put("metricType", "IP");
        String s = HttpClientUtils.httpPostForm(aiUrl + "/queryConciliationCase", params, new HashMap<>(), "utf-8");
        JSONObject object = JSONObject.parseObject(s);
        int code = object.getIntValue("code");
        if (code == 200) {
            JSONArray data = object.getJSONArray("data");
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                CaseInfoDto caseInfoDto = new CaseInfoDto();
                caseInfoDto.setCaseId(jsonObject.getString("id"));
                caseInfoDto.setCaseName(jsonObject.getString("case_title"));
                caseInfoDto.setCaseType(1);//表示调解类案件
                caseInfoDtoList.add(caseInfoDto);
            }
        }
        if (caseInfoDtoList.size() < 4) {
            s = HttpClientUtils.httpPostForm(aiUrl + "/queryJudgmentCase", params, new HashMap<>(), "utf-8");
            object = JSONObject.parseObject(s);
            code = object.getIntValue("code");
            if (code == 200) {
                JSONArray data = object.getJSONArray("data");
                for (int i = 0; i < data.size(); i++) {
                    JSONObject jsonObject = data.getJSONObject(i);
                    CaseInfoDto caseInfoDto = new CaseInfoDto();
                    caseInfoDto.setCaseId(jsonObject.getString("cpws_case_info_id"));
                    caseInfoDto.setCaseName(jsonObject.getString("case_name"));
                    caseInfoDto.setCaseType(2);//表示判决类案件
                    caseInfoDtoList.add(caseInfoDto);
                    if (caseInfoDtoList.size() == 3)
                        break;
                }
            }
        }
        GuavaCacheUtils.putCacheAi("get-case"+aiRequestVo.getCaseId(), caseInfoDtoList);
        return ReturnSucUtils.getRepInfo("请求成功", caseInfoDtoList);
    }
 
 
    @PostMapping("/get-law")
    public Object getLaw(@RequestBody AiRequestVo aiRequestVo) {
        Object cacheAi = GuavaCacheUtils.getCacheAi("get-law"+aiRequestVo.getCaseId());
        if(ObjectUtils.isNotEmpty(cacheAi)){
            return ReturnSucUtils.getRepInfo("请求成功", cacheAi);
        }
        List<LawInfoDto> lawInfoDtos = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        params.put("caseDes", aiRequestVo.getCaseDes());//案件描述
        params.put("caseClaim", aiRequestVo.getCaseClaim());//案件诉求
        params.put("caseId", aiRequestVo.getCaseId());//案件id
        String s = HttpClientUtils.httpPostForm(aiUrl + "/queryLawProvision", params, new HashMap<>(), "utf-8");
        JSONObject object = JSONObject.parseObject(s);
        int code = object.getIntValue("code");
        if (code == 200) {
            JSONArray data = object.getJSONArray("data");
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                LawInfoDto lawInfoDto = new LawInfoDto();
                lawInfoDto.setLawInfoId(jsonObject.getString("law_info_id"));
                lawInfoDto.setLawIndex(jsonObject.getString("provision_index"));
                lawInfoDto.setLawDesc(jsonObject.getString("provision_text"));
                lawInfoDto.setLawTitle(jsonObject.getString("law_title"));
                lawInfoDtos.add(lawInfoDto);
            }
        }
        GuavaCacheUtils.putCacheAi("get-law"+aiRequestVo.getCaseId(), lawInfoDtos);
        return ReturnSucUtils.getRepInfo("请求成功", lawInfoDtos);
    }
 
    @RequestMapping("/get-case-detail")
    public Object getCaseLawDetail(@RequestParam String caseId, @RequestParam int type) {
        String s = "";
        CaseInfoDetailDto caseInfoDetailDto = new CaseInfoDetailDto();
        if (type == 1) {//表示纠纷类案件
            s = HttpClientUtils.httpGet(aiUrl + "/getConciliationCaseText?caseId=" + caseId, new HashMap<>(), "utf-8");
            JSONObject object = JSONObject.parseObject(s);
            int code = object.getIntValue("code");
            if (code == 200) {
                JSONObject data = object.getJSONObject("data");
                caseInfoDetailDto.setCaseId(data.getString("id"));
                caseInfoDetailDto.setCaseTitle(data.getString("case_title"));
                caseInfoDetailDto.setCaseDesc(data.getString("case_des"));
                caseInfoDetailDto.setCaseClaim(data.getString("case_claim"));
                caseInfoDetailDto.setAgreeContent(data.getString("agree_content"));
            }
        } else {
            s = HttpClientUtils.httpGet(aiUrl + "/getJudgmentCaseText?caseId=" + caseId, new HashMap<>(), "utf-8");
            JSONObject object = JSONObject.parseObject(s);
            int code = object.getIntValue("code");
            if (code == 200) {
                JSONObject data = object.getJSONObject("data");
                caseInfoDetailDto.setCaseId(data.getString("idcpws_case_info_id"));
                caseInfoDetailDto.setCaseTitle(data.getString("basic_case_info"));
                caseInfoDetailDto.setCaseDesc(data.getString("full_text"));
                caseInfoDetailDto.setAgreeContent(data.getString("judgment"));
                caseInfoDetailDto.setLegalBasis(data.getString("legal_basis"));
            }
        }
        return ReturnSucUtils.getRepInfo("请求成功", caseInfoDetailDto);
    }
 
    @PostMapping("/get-clain-rise-case")
    public Object getClaimRiskByCase(@RequestBody AiRequestVo aiRequestVo) {
        Object cacheAi = GuavaCacheUtils.getCacheAi("get-clain-rise-case"+aiRequestVo.getCaseId());
        if(ObjectUtils.isNotEmpty(cacheAi)){
            return ReturnSucUtils.getRepInfo("请求成功", cacheAi);
        }
        CaseRiskDto caseRiskDto  = new CaseRiskDto();
        Map<String, String> params = new HashMap<>();
        params.put("caseDes", aiRequestVo.getCaseDes());//案件描述
        params.put("caseClaim", aiRequestVo.getCaseClaim());//案件诉求
        params.put("caseId", aiRequestVo.getCaseId());//案件id
        String s = HttpClientUtils.httpPostForm(aiUrl + "/getClaimRiskByCase", params, new HashMap<>(), "utf-8");
        JSONObject object = JSONObject.parseObject(s);
        int code = object.getIntValue("code");
        if (code == 200) {
            JSONObject data = object.getJSONObject("data");
            caseRiskDto.setIsClaimRisk(data.getIntValue("is_claim_risk"));
            caseRiskDto.setAnalysisProcess(data.getString("analysis_process"));
        }
        GuavaCacheUtils.putCacheAi("get-clain-rise-case"+aiRequestVo.getCaseId(), caseRiskDto);
        return ReturnSucUtils.getRepInfo("请求成功", caseRiskDto);
    }
 
}