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

import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.Tenant;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.mapper.TenantMapper;
import com.tuoheng.admin.request.third.PilotTaskRequest;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class AddPilotTaskService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    public JsonResult addTask(User user, Inspection inspection) {
        Tenant tenant = tenantMapper.selectById(inspection.getTenantId());

        //第三方请求体需要
        PilotTaskRequest pilotTaskRequest = new PilotTaskRequest();
        pilotTaskRequest.setTenantUserName(tenant.getUsername());
        pilotTaskRequest.setTaskCode(inspection.getCode());
        pilotTaskRequest.setTaskName(inspection.getName());
        pilotTaskRequest.setTenantName(tenant.getName());
        pilotTaskRequest.setPatrolLocation(inspection.getPatrolLocation() == null ? "" : inspection.getPatrolLocation());
        pilotTaskRequest.setPlatformCode(SystemConstant.PLATFORM_CODE);
        pilotTaskRequest.setSponsorName(user.getRealname());
        pilotTaskRequest.setSponsorPhone(inspection.getMobile() == null ? "" : inspection.getMobile());
        pilotTaskRequest.setTaskStartTime(inspection.getInspectionTime());
        pilotTaskRequest.setRemark(inspection.getNote());

        //直播
        pilotTaskRequest.setIsLive(inspection.getIsLive());

        //经纬度
        pilotTaskRequest.setStartLatitude(inspection.getStartLatitude());
        pilotTaskRequest.setStartLongitude(inspection.getStartLongitude());
        pilotTaskRequest.setEndLongitude(inspection.getEndLongitude());
        pilotTaskRequest.setEndLatitude(inspection.getEndLatitude());

        //第三方
        pilotTaskRequest.setInspectionType(0);
        HttpEntity httpEntity = new HttpEntity(pilotTaskRequest);

        // 设置地址(飞手平台)
        String url = CommonConfig.pilotURL + SystemConstant.ADD_PILOTTASK;
        ResponseEntity<JsonResult> response;
        try {
            response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
        } catch (Exception e) {
            log.error("请求第三方响应失败，异常!");
            log.error("请求飞手平台创建任务失败, url:{}", url);
            log.error("请求飞手平台创建任务失败, httpEntity:{}", httpEntity);
            return JsonResult.error("请求第三方响应失败，异常");
        }
        if (null == response || !response.hasBody() || response.getBody().getCode() != JsonResult.SUCCESS) {
            log.error("请求第三方响应失败，返回空!");
            log.error("请求飞手平台创建任务失败, url:{}", url);
            log.error("请求飞手平台创建任务失败, httpEntity:{}", httpEntity);
            return JsonResult.error("请求第三方响应失败，返回空");
        }
        return JsonResult.success();
    }

}
