xieb
2023-10-19 1dd5d8f8a616f5cf3caa7828d46989c7d3dcafc4
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package cn.gistack.sm.patrol.feign;
 
import cn.gistack.sm.intelligentCall.constant.ZtConstant;
import cn.gistack.sm.intelligentCall.service.CallService;
import cn.gistack.sm.patrol.entity.PatrolTask;
import cn.gistack.sm.patrol.service.IPatrolTaskService;
import cn.gistack.sm.sjztmd.entity.PatrolResponsiblePerson;
import cn.gistack.sm.sjztmd.service.IAttResManagePersonService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * @ClassName SM服务Feign实现类
 * @Description TODO
 * @Author zhongrj
 * @Date 2023-06-03
 */
@NonDS
@ApiIgnore
@RestController
@AllArgsConstructor
@Slf4j
public class PatrolTaskImplClient implements PatrolTaskClient {
 
    //本地测试
    private static final String LOCAL = "https://sk.hubeishuiyi.cn";
    //线上
    private static final String ONLINE = "http://10.42.6.192";
    private final IPatrolTaskService patrolTaskService;
    private final IAttResManagePersonService attResManagePersonService;
 
    @Autowired
    private RestTemplate restTemplate;
 
    /**
     * 超汛限(包含汛限值0)
     */
    private static final String GET_OVER_RZ =ONLINE +  "/services/1234567890ABCDEFGHIJKLMN/over_z_last/patr_person/api?start_over=0&pageNo=1&pageSize=2000";
    /**
     * 病险水库
     */
    private static final String GET_ILL_RESERVOIR =ONLINE +  "/services/1234567890ABCDEFGHIJKLMN/res/main_st/rsvr?eng_scal=&res_nm=&is_danger=true&res_cds=&is_video=&flag=&data_sta=&gate=&is_order=1&label=&bas_nm=&pageNo=1&pageSize=10000";
 
 
    /**
     * 给巡查责任人定时创建任务执行器
     * @param processDefinitionId
     * @param taskType
     * @param title
     * @param content
     * @return
     */
    @Override
    @GetMapping(CREATE_TASK_JOB_HANDLER)
    public List<Integer> createTaskJobHandler(String processDefinitionId,
                                              String taskType,
                                              String title,
                                              String content) {
        List<PatrolResponsiblePerson> patrolResponsiblePersonList =  attResManagePersonService.getPatrolResponsiblePersonAll();
        List<Integer> taskJob = createTaskJob(processDefinitionId, taskType, title, content, patrolResponsiblePersonList);
        return taskJob;
    }
 
    @Override
    @GetMapping(CREATE_TASK_JOB_BY_CONDITION_HANDLER)
    public List<Integer> createTaskJobByConditionHandler(String processDefinitionId, String taskType, String title, String content) {
 
        List<String> reservoirList = new ArrayList<>();
 
        //获取病险水库数据
        JSONArray overList = apiRequestResult(null, GET_OVER_RZ);
        for (int i = 0; i < overList.size(); i++) {
            JSONObject reservoir = overList.getJSONObject(i);
            reservoirList.add(reservoir.getString("res_cd"));
        }
 
        JSONArray illList = apiRequestData(null, GET_ILL_RESERVOIR);
        for (int i = 0; i < illList.size(); i++) {
            JSONObject reservoir = illList.getJSONObject(i);
            reservoirList.add(reservoir.getString("res_cd"));
        }
 
        List<PatrolResponsiblePerson> patrolResponsiblePersonList =  attResManagePersonService.getPatrolResponsiblePersonByResCdList(reservoirList);
 
        List<Integer> taskJob = createTaskJob(processDefinitionId, taskType, title, content, patrolResponsiblePersonList);
 
        return taskJob;
    }
 
 
    /**
     * 请求中台接口,返回resultList结果集
     * @param params
     * @return
     */
    private JSONArray apiRequestResult(Map<String, Object> params,String url) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add(ZtConstant.header_key, ZtConstant.header_value);
        //封装请求头
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);
        try {
            //有请求头,有参数请求
            ResponseEntity<String> responseEntity =
                restTemplate.exchange(url,
                    HttpMethod.GET,
                    formEntity,
                    String.class);
            JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
            JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("resultList").toString());
            // 返回
            return jsonArray;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 请求中台接口,返回data结果集
     * @param params
     * @return
     */
    private JSONArray apiRequestData(Map<String, Object> params,String url) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add(ZtConstant.header_key, ZtConstant.header_value);
        //封装请求头
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);
        try {
            //有请求头,有参数请求
            ResponseEntity<String> responseEntity =
                restTemplate.exchange(url,
                    HttpMethod.GET,
                    formEntity,
                    String.class);
            JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
            JSONObject data = JSON.parseObject(jsonObject.get("data").toString());
 
            JSONArray dataList = data.getJSONArray("data");
 
            // 返回
            return dataList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
 
    /**
     * 创建任务的方法
     * @param processDefinitionId
     * @param taskType
     * @param title
     * @param content
     * @param patrolResponsiblePersonList
     * @return
     */
    public List<Integer> createTaskJob(String processDefinitionId,
                                       String taskType,
                                       String title,
                                       String content,List<PatrolResponsiblePerson> patrolResponsiblePersonList){
        AtomicInteger failureCount = new AtomicInteger();
        patrolResponsiblePersonList.forEach(item -> {
            log.info("当前创建任务对象:" + JSON.toJSONString(item));
            try {
                PatrolTask patrolTask = new PatrolTask();
                patrolTask.setProcessDefinitionId(processDefinitionId);
                patrolTask.setToUserId(item.getUserId());
                patrolTask.setProjectId(item.getResGuid());
                patrolTask.setReservoirName(item.getName());
                // 开始时间-结束时间
                patrolTask.setStartTime(DateUtil.format(new Date(),"yyyy-MM-dd 00:00:00"));
                patrolTask.setEndTime(DateUtil.format(new Date(),"yyyy-MM-dd 23:59:59"));
                // 标题格式 月-日 水库名称 每日巡检任务
                patrolTask.setTitle(DateUtil.format(new Date(),"MM-dd") + item.getName() + "-" + title);
                patrolTask.setContent(DateUtil.format(new Date(),"yyyyMMdd") +content);
                patrolTask.setTaskType(taskType);
                // 创建任务
                patrolTaskService.startProcess(patrolTask);
            } catch (Exception e) {
                log.error("创建任务失败:"  + e.getMessage());
                log.error("创建任务失败对象:"  + JSON.toJSONString(item));
                failureCount.getAndIncrement();
            }
 
        });
        List<Integer> list = new ArrayList<>();
        list.add(patrolResponsiblePersonList.size());
        list.add(failureCount.get());
        // 返回
        return list;
    }
 
 
}