package com.tuoheng.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.request.inspection.AddInspectionRequest;
import com.tuoheng.admin.request.inspection.EditInspectionRequest;
import com.tuoheng.admin.request.inspection.MissionStatusRequest;
import com.tuoheng.admin.request.inspection.QueryInspectionPageListRequest;
import com.tuoheng.admin.service.inspection.IInspectionService;
import com.tuoheng.admin.service.inspection.update.status.UpdateFlightStatusService;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class InspectionServiceTest {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private IInspectionService iInspectionService;

    @Autowired
    private UpdateFlightStatusService updateFlightStatusService;

    @Test
    public void testPageList() {
        QueryInspectionPageListRequest request = new QueryInspectionPageListRequest();
//        request.setKey("1");
        request.setAirportId(1);   // 巡检机场id
        request.setInspectionLine(1);  // 巡检线路Id
        request.setType(1);  // 巡检任务类型: 1 临时巡检(目前只有该一种类型)
        request.setStatus(5); // 任务状态 5任务待飞行 7飞行失败 10任务飞行中 15任务飞行完成
        request.setInspectionType(1);  // 1 无人机 2机场
        request.setDeptId("");
        request.setPage(1);
        request.setLimit(2);

        JSONObject json = (JSONObject) JSONObject.toJSON(request);
        log.info("-----> 获取分页列表请求参数：json={}", json);

        JsonResult result = iInspectionService.selectPageList(request);
        System.out.println(">>>>>>>>>>>>>" + result);
    }

    @Test
    public void testAdd() {
        AddInspectionRequest request = new AddInspectionRequest();
        request.setType(1);
        request.setName("test-2022112501-11");
        request.setChannelId("1");
        request.setChannelName("G2");
        request.setLegId("1");
        request.setLegName("航段1");
        request.setInspectionType(1);
        request.setAirportId(1);
        request.setAirportName("禄口-2022112501");
        request.setInspectionLine(1);
        request.setInspectionLineName("路线1");
        request.setInspectionTime(DateUtils.now());

        JSONObject json = (JSONObject) JSONObject.toJSON(request);
        log.info("-----> 新增任务请求参数：json={}", json);

        JsonResult result = iInspectionService.insert(request);
        System.out.println(">>>>>>>>>>>>>" + result);
    }

    @Test
    public void testEdit() {
        EditInspectionRequest request = new EditInspectionRequest();
        request.setId("54601a9f13236dd3b5151db5ebdb0794");
        request.setType(1);
        request.setName("test1");
        request.setChannelId("1");
        request.setLegId("1");
        request.setInspectionType(1);
        request.setAirportId(1);
        request.setAirportName("禄口1");
        request.setInspectionLine(1);
        request.setInspectionLineName("路线11");
        request.setInspectionTime(DateUtils.now());
        request.setNote("test");

        JsonResult result = iInspectionService.update(request);
        System.out.println(">>>>>>>>>>>>>" + result);
    }

    @Test
    public void testDelete() {
        String id = "e69b6f5050c9106dfa37fea1a5c3fc15";
        JsonResult result = iInspectionService.deleteById(id);
        System.out.println(">>>>>>>>>>>>>" + result);
    }

    @Test
    public void testResubmit() {
        EditInspectionRequest request = new EditInspectionRequest();
        request.setId("1f2ed1f784eb36165785d5235350ef7d");
        request.setCode("XJRW20221130092941");
        request.setType(1);
        request.setInspectionType(2);
        request.setName("test1");
        request.setChannelId("1");
        request.setChannelName("G2");
        request.setLegId("1");
        request.setLegName("航段1");
        request.setAirportId(1);
        request.setAirportName("禄口1");
        request.setInspectionLine(1);
        request.setInspectionLineName("路线11");
        request.setIsLive(1);
        request.setStartLatitude("11");
        request.setStartLongitude("11");
        request.setEndLongitude("22");
        request.setEndLatitude("22");
        request.setInspectionTime(DateUtils.now());
        request.setNote("test");

        JSONObject json = (JSONObject) JSONObject.toJSON(request);
        log.info("-----> 重新提交请求参数：json={}", json);

        JsonResult result = iInspectionService.resubmit(request);
        System.out.println(">>>>>>>>>>>>>" + result);
    }

    @Test
    public void test22() {
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                .eq(Inspection::getId, "4fb3d92882b908a760b23f3b6431b32a")
                .eq(Inspection::getInspectionLine, 55));
        MissionStatusRequest missionStatusRequest = new MissionStatusRequest();
        missionStatusRequest.setStatus(2);
        updateFlightStatusService.updateFlightStatus(inspection, missionStatusRequest);
    }
}
