package com.zy.acs.manager.core.third; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zy.acs.framework.common.Cools; import com.zy.acs.manager.core.utils.HttpHandler; import com.zy.acs.manager.manager.entity.TaskReport; import com.zy.acs.manager.manager.entity.TaskReportLog; import com.zy.acs.manager.manager.service.TaskReportLogService; import com.zy.acs.manager.manager.service.TaskReportService; import com.zy.acs.manager.system.service.ConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.List; @Slf4j @Component public class ReportThirdScheduler { @Autowired private TaskReportService taskReportService; @Autowired private TaskReportLogService taskReportLogService; @Autowired private ConfigService configService; @Scheduled(fixedDelay = 3000) public void execute() { Boolean report = configService.getVal("REPORT", Boolean.class, false); if (report) { String wmsUrl = configService.getVal("WMS_URL", String.class); String wmsPath = configService.getVal("WMS_PATH", String.class); String codes = configService.getVal("CODES", String.class, "1457;1612"); List list = taskReportService.list(new LambdaQueryWrapper().eq(TaskReport::getCompleted, 0).le(TaskReport::getReportTimes, 3)); for (TaskReport taskReport : list) { if (report(taskReport, wmsUrl, wmsPath)) { taskReport.setReportTimes((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) + 1); taskReport.setUpdateTime(new Date()); taskReport.setCompleted(1); } else { taskReport.setReportTimes((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) + 1); taskReport.setUpdateTime(new Date()); } taskReportService.updateById(taskReport); } } } @Scheduled(fixedDelay = 3000) public void execute2() { Calendar instance = Calendar.getInstance(); instance.add(Calendar.DATE, 1); List list = taskReportService.list(new LambdaQueryWrapper().eq(TaskReport::getCompleted, 1).ge(TaskReport::getUpdateTime, instance.getTime())); Integer times = configService.getVal("REPORT_TIMES", Integer.class, 3); for (TaskReport taskReport : list) { if ((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) > times) { toLog(taskReport); } } list = taskReportService.list(new LambdaQueryWrapper().eq(TaskReport::getReportTimes, 4).ge(TaskReport::getUpdateTime, instance.getTime())); for (TaskReport taskReport : list) { if ((Cools.isEmpty(taskReport.getReportTimes()) ? 0 : taskReport.getReportTimes()) > times) { toLog(taskReport); } } } @Transactional public boolean report(TaskReport taskReport, String wmsUrl, String wmsPath) { String response = null; try { response = new HttpHandler.Builder() .setUri(wmsUrl) .setPath(wmsPath) .setJson(JSON.toJSONString(taskReport)) .build() .doPost(); log.info("返回参数:{}", response); JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("code").equals(200)) { return true; } } catch (IOException e) { log.info("报错了,{}", e); taskReport.setMemo(e.getMessage()); e.printStackTrace(); } return false; } @Transactional public void toLog(TaskReport taskReport) { TaskReportLog taskReportLog = new TaskReportLog(); BeanUtils.copyProperties(taskReport, taskReportLog); taskReportService.removeById(taskReport.getId()); taskReportLog.setId(null); taskReportLogService.save(taskReportLog); } }