package com.tuoheng.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.entity.request.inspection.AddInspectionRequest;
import com.tuoheng.admin.entity.request.inspection.EditInspectionRequest;
import com.tuoheng.admin.entity.request.inspection.MissionStatusRequest;
import com.tuoheng.admin.entity.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;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@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.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.setInspectionType(1);
        request.setAirportId(1);
        request.setAirportName("禄口-2022112501");
        request.setInspectionLine(1);
        request.setInspectionLineName("路线1");


        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");

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

    @Test
    public void testDelete() {
    }

    @Test
    public void testResubmit() {
        EditInspectionRequest request = new EditInspectionRequest();
        request.setId("1f2ed1f784eb36165785d5235350ef7d");
        request.setInspectionType(2);
        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());

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

        System.out.println(">>>>>>>>>>>>>");
    }

    @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);
    }

    @Test
    public void test33() {

        StringBuffer ids = new StringBuffer();
        String airportIds;
        //查询当前租户下的任务列表
        List<Inspection> inspectionList = inspectionMapper.selectList(Wrappers.<Inspection>lambdaQuery()
                .eq(Inspection::getTenantId, "1")
                .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
        Set<Integer> set = new HashSet<>();
        for (Inspection inspection : inspectionList) {
            if (set.add(inspection.getAirportId())) {
                ids.append(inspection.getAirportId()).append(",");
            }
        }
        airportIds = ids.substring(0,ids.length() - 1);
        System.out.println(airportIds);
    }
}
