|
|
|
|
|
|
|
|
|
|
|
package com.tuoheng.admin.task; |
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
|
import com.tuoheng.admin.constant.SystemConstant; |
|
|
|
|
|
import com.tuoheng.admin.entity.Inspection; |
|
|
|
|
|
import com.tuoheng.admin.entity.Tenant; |
|
|
|
|
|
import com.tuoheng.admin.enums.InspectionStatusEnum; |
|
|
|
|
|
import com.tuoheng.admin.enums.InspectionTypeEnum; |
|
|
|
|
|
import com.tuoheng.admin.enums.MarkTypeEnum; |
|
|
|
|
|
import com.tuoheng.admin.enums.TaskStatusEnum; |
|
|
|
|
|
import com.tuoheng.admin.mapper.InspectionMapper; |
|
|
|
|
|
import com.tuoheng.admin.mapper.TenantMapper; |
|
|
|
|
|
import com.tuoheng.admin.service.inspection.IInspectionService; |
|
|
|
|
|
import com.tuoheng.common.core.enums.ServiceExceptionEnum; |
|
|
|
|
|
import com.tuoheng.common.core.exception.ServiceException; |
|
|
|
|
|
import com.tuoheng.common.core.utils.HttpUtils; |
|
|
|
|
|
import com.tuoheng.common.core.utils.JacksonUtil; |
|
|
|
|
|
import com.tuoheng.common.core.utils.JsonResult; |
|
|
|
|
|
import com.tuoheng.common.core.utils.StringUtils; |
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 定时扫描,然后飞行 |
|
|
|
|
|
*/ |
|
|
|
|
|
@Component |
|
|
|
|
|
@Slf4j |
|
|
|
|
|
public class AirportTask { |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private InspectionMapper inspectionMapper; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private IInspectionService inspectionService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TenantMapper tenantMapper; |
|
|
|
|
|
|
|
|
|
|
|
// @Scheduled(fixedRate = 60000) |
|
|
|
|
|
// @XxlJob("airportTaskHandler") |
|
|
|
|
|
public void airportTaskHandler() { |
|
|
|
|
|
//查询当前时间正负1分钟的时间跨度,xxljob执行频率为1分钟1次 |
|
|
|
|
|
long start = new Date().getTime() - 1 * 60000L; |
|
|
|
|
|
long end = new Date().getTime() + 1 * 60000L; |
|
|
|
|
|
Date startTime = new Date(start); |
|
|
|
|
|
Date endTime = new Date(end); |
|
|
|
|
|
|
|
|
|
|
|
log.info("执行定时执行飞行任务:" + LocalDateTime.now()); |
|
|
|
|
|
List<Inspection> inspectionList = inspectionMapper.selectList(new LambdaQueryWrapper<Inspection>() |
|
|
|
|
|
.eq(Inspection::getMark, MarkTypeEnum.VALID.getCode()) |
|
|
|
|
|
.between(Inspection::getInspectionTime, startTime, endTime) |
|
|
|
|
|
//查询巡检方式为机场的任务 |
|
|
|
|
|
.eq(Inspection::getInspectionType, InspectionTypeEnum.AIRPORT.getCode()) |
|
|
|
|
|
//查询未执行任务 |
|
|
|
|
|
.eq(Inspection::getExecutionStatus, 1) |
|
|
|
|
|
//任务待飞行 |
|
|
|
|
|
.eq(Inspection::getStatus, InspectionStatusEnum.WAIT_FLIGHT.getCode())); |
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(inspectionList)) { |
|
|
|
|
|
for (Inspection inspection : inspectionList) { |
|
|
|
|
|
log.info("执行定时执行飞行任务开始:" + inspection.getId()); |
|
|
|
|
|
|
|
|
|
|
|
JsonResult jsonResult = this.executeTask(inspection); |
|
|
|
|
|
|
|
|
|
|
|
Inspection inspectionUpdate = new Inspection(); |
|
|
|
|
|
inspectionUpdate.setId(inspection.getId()); |
|
|
|
|
|
inspectionUpdate.setExecutionStatus(2); |
|
|
|
|
|
if (jsonResult.getCode() != 0 && ObjectUtil.isEmpty(jsonResult.getData())) { |
|
|
|
|
|
// 12任务飞行失败(机场) |
|
|
|
|
|
inspectionUpdate.setStatus(TaskStatusEnum.FAIL.getCode()); |
|
|
|
|
|
} |
|
|
|
|
|
inspectionMapper.updateById(inspectionUpdate); |
|
|
|
|
|
|
|
|
|
|
|
log.info("执行定时执行飞行任务结束"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private JsonResult executeTask(Inspection inspection) { |
|
|
|
|
|
//读取不同租户的机场平台url |
|
|
|
|
|
Tenant tenant = tenantMapper.selectById(inspection.getTenantId()); |
|
|
|
|
|
if (ObjectUtil.isEmpty(tenant) && StringUtils.isEmpty(tenant.getAirportUrl())) { |
|
|
|
|
|
throw new ServiceException(ServiceExceptionEnum.GET_NO_DATA); |
|
|
|
|
|
} |
|
|
|
|
|
String url = tenant.getAirportUrl() + SystemConstant.API_AIRPORT_EXECUTE_TASK; |
|
|
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
|
|
jsonObject.put("taskId", inspection.getInspectionLine()); |
|
|
|
|
|
jsonObject.put("requestId", String.valueOf(inspection.getId())); |
|
|
|
|
|
jsonObject.put("code", "gs"); // 与机场平台约定好的 |
|
|
|
|
|
jsonObject.put("tenantName", tenant.getName()); |
|
|
|
|
|
|
|
|
|
|
|
String airPortStr = HttpUtils.doSend(url, jsonObject, null, "POST"); |
|
|
|
|
|
if (StringUtils.isEmpty(airPortStr)) { |
|
|
|
|
|
return JsonResult.error("机场接口返回数据为空"); |
|
|
|
|
|
} |
|
|
|
|
|
JsonResult jsonResult = JacksonUtil.json2pojo(airPortStr, JsonResult.class); |
|
|
|
|
|
if (jsonResult.getCode() != 0) { |
|
|
|
|
|
return JsonResult.error(jsonResult.getMsg()); |
|
|
|
|
|
} |
|
|
|
|
|
return JsonResult.success(); |
|
|
|
|
|
} |
|
|
|
|
|
} |