parent
3e617d2daa
commit
72c98e3069
@ -0,0 +1,120 @@ |
||||
package com.cjy.back.ybsjAppointment.task; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.alibaba.fastjson.TypeReference; |
||||
import com.cjy.back.ybsjAppointment.dao.YbsjyAppointmentRecordMapper; |
||||
import com.cjy.back.ybsjAppointment.entity.AppointmentRequestTurnstileLogEntity; |
||||
import com.cjy.util.HttpUtil; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.scheduling.annotation.Scheduled; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author liangjiawei |
||||
* @createDate 2023/8/2 |
||||
*/ |
||||
|
||||
@Component("processRequestFailRecordTask") |
||||
public class ProcessRequestFailRecordTask { |
||||
|
||||
@Autowired |
||||
YbsjyAppointmentRecordMapper ybsjyAppointmentRecordMapper; |
||||
|
||||
// @Scheduled(cron = "0 0/1 * * * ?")
|
||||
@Scheduled(cron = "0 0/60 * * * ?") |
||||
public void processRequestFailRecordTask() { |
||||
//第一步 获取请求失败的数据
|
||||
List<Map<String,Object>> list= ybsjyAppointmentRecordMapper.getProcessRequestFailRecordList(); |
||||
|
||||
List<AppointmentRequestTurnstileLogEntity> updateDateList=new ArrayList<>(); |
||||
list.stream().forEach(item ->{ |
||||
//第二步 发送请求
|
||||
if(item.get("type").equals("post")){ |
||||
Map<String, String> map = stringToMap(item.get("requestParams").toString()); |
||||
String responseString = HttpUtil.sendPost(item.get("requestUrl").toString(),map); |
||||
JSONObject responseJson = JSONObject.parseObject(responseString); |
||||
if(responseJson.get("code").equals("200")){ |
||||
AppointmentRequestTurnstileLogEntity appointmentRequestTurnstileLogEntity=new AppointmentRequestTurnstileLogEntity(); |
||||
appointmentRequestTurnstileLogEntity.setResponseBody(responseString); |
||||
appointmentRequestTurnstileLogEntity.setId(item.get("id").toString()); |
||||
appointmentRequestTurnstileLogEntity.setState("200"); |
||||
if(updateDateList.size()<500){ |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
}else { |
||||
ybsjyAppointmentRecordMapper.updateProcessRequestFailRecord(updateDateList); |
||||
updateDateList.clear(); |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
} |
||||
//修改状态
|
||||
}else { |
||||
AppointmentRequestTurnstileLogEntity appointmentRequestTurnstileLogEntity=new AppointmentRequestTurnstileLogEntity(); |
||||
appointmentRequestTurnstileLogEntity.setResponseBody(responseString); |
||||
appointmentRequestTurnstileLogEntity.setId(item.get("id").toString()); |
||||
appointmentRequestTurnstileLogEntity.setState("500"); |
||||
//重试次数+1
|
||||
if(updateDateList.size()<500){ |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
}else { |
||||
ybsjyAppointmentRecordMapper.updateProcessRequestFailRecord(updateDateList); |
||||
updateDateList.clear(); |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
} |
||||
} |
||||
}else { |
||||
String responseString = HttpUtil.doGet(item.get("requestUrl").toString()); |
||||
JSONObject responseJson = JSONObject.parseObject(responseString); |
||||
if(responseJson.get("status").equals("200")){ |
||||
AppointmentRequestTurnstileLogEntity appointmentRequestTurnstileLogEntity=new AppointmentRequestTurnstileLogEntity(); |
||||
appointmentRequestTurnstileLogEntity.setResponseBody(responseString); |
||||
appointmentRequestTurnstileLogEntity.setId(item.get("id").toString()); |
||||
appointmentRequestTurnstileLogEntity.setState("200"); |
||||
if(updateDateList.size()<500){ |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
}else { |
||||
ybsjyAppointmentRecordMapper.updateProcessRequestFailRecord(updateDateList); |
||||
updateDateList.clear(); |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
} |
||||
//修改状态
|
||||
}else { |
||||
AppointmentRequestTurnstileLogEntity appointmentRequestTurnstileLogEntity=new AppointmentRequestTurnstileLogEntity(); |
||||
appointmentRequestTurnstileLogEntity.setResponseBody(responseString); |
||||
appointmentRequestTurnstileLogEntity.setId(item.get("id").toString()); |
||||
appointmentRequestTurnstileLogEntity.setState("500"); |
||||
//重试次数+1
|
||||
if(updateDateList.size()<500){ |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
}else { |
||||
ybsjyAppointmentRecordMapper.updateProcessRequestFailRecord(updateDateList); |
||||
updateDateList.clear(); |
||||
updateDateList.add(appointmentRequestTurnstileLogEntity); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
if(!updateDateList.isEmpty()){ |
||||
ybsjyAppointmentRecordMapper.updateProcessRequestFailRecord(updateDateList); |
||||
} |
||||
//第三步 成功修改状态 失败尝试次数加一
|
||||
} |
||||
public static Map<String, String> stringToMap(String input) { |
||||
String jsonString = input.substring(1, input.length() - 1); |
||||
String[] keyValuePairs = jsonString.split(", "); |
||||
Map<String, String> map = new HashMap<>(); |
||||
|
||||
for (String pair : keyValuePairs) { |
||||
String[] keyValue = pair.split("="); |
||||
String key = keyValue[0]; |
||||
String value = keyValue[1]; |
||||
map.put(key, value); |
||||
} |
||||
|
||||
return map; |
||||
} |
||||
} |
Loading…
Reference in new issue