package com.tuoheng.admin.service.third.airport;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.Tenant;
import com.tuoheng.admin.enums.code.inspection.ExecuteInspectionCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.TenantMapper;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.HttpUtils;
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.Service;

@Slf4j
@Service
public class ExecuteTaskService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    public JsonResult executeTask(String userId, Inspection inspection) {
        log.info("进入调用机场平台方法，执行任务");
        // 读取不同租户的机场平台url
        Tenant tenant = tenantMapper.selectById(inspection.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            log.info("进入调用机场平台方法: 租户不存在");
            return JsonResult.error(ExecuteInspectionCodeEnum.TENANT_IS_NOT_EXIST.getCode(), ExecuteInspectionCodeEnum.TENANT_IS_NOT_EXIST.getMsg());
        }
        String url = CommonConfig.airportURL + SystemConstant.API_AIRPORT_EXECUTE_TASK;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskId", inspection.getInspectionLine());
        jsonObject.put("requestId", inspection.getId());
        jsonObject.put("requestName", inspection.getName());
        jsonObject.put("code", "gs"); // 与机场平台约定好的
        jsonObject.put("tenantCode", tenant.getCode());

        String airPortStr = HttpUtils.doSend(url, jsonObject, null, "POST");
        if (StringUtils.isEmpty(airPortStr)) {
            log.info("进入调用机场平台方法: url:{}", url);
            log.info("进入调用机场平台方法: jsonObject:{}", jsonObject);
            log.info("立即执行任务业务：机场接口返回数据为空，飞行失败，任务id：{}，任务名称：{}，机场id：{}，机场名称：{}， 路线id：{}，路线名称：{}",
                    inspection.getId(), inspection.getName(), inspection.getAirportId(), inspection.getAirportName(), inspection.getInspectionLine(), inspection.getInspectionLineName());
            return JsonResult.error(ExecuteInspectionCodeEnum.AIRPORT_RETURN_DATA_IS_NULL.getCode(), ExecuteInspectionCodeEnum.AIRPORT_RETURN_DATA_IS_NULL.getMsg());
        }

        // 修改任务执行状态，防止手动执行和定时重复
        this.updateInspectionExecutionStatus(userId, inspection);

        log.info("进入调用机场平台方法: 等待机场平台回调，修改执行任务状态");
        return JsonResult.success();
    }

    /**
     * 修改任务执行状态
     *
     * @param userId
     * @param inspection
     */
    private void updateInspectionExecutionStatus(String userId, Inspection inspection) {
        inspection.setExecutionStatus(2);
        inspection.setStatus(inspection.getStatus());
        inspection.setUpdateUser(userId);
        inspection.setUpdateTime(DateUtils.now());
        inspectionMapper.update(inspection);
    }
}