package com.tuoheng.admin.controller;


import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.dto.InspectionDto;
import com.tuoheng.admin.entity.FlightData;
import com.tuoheng.admin.query.InspectionFileQuery;
import com.tuoheng.admin.request.inspection.AddInspectionRequest;
import com.tuoheng.admin.request.inspection.EditInspectionRequest;
import com.tuoheng.admin.request.inspection.InspectionRequest;
import com.tuoheng.admin.request.inspection.QueryInspectionPageListRequest;
import com.tuoheng.admin.service.inspection.IInspectionService;
import com.tuoheng.common.core.common.OperationEnum;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 巡检任务前端控制器
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-16
 */
@Slf4j
@RestController
@RequestMapping("/inspection")
public class InspectionController {

    @Autowired
    private IInspectionService iInspectionService;

    /**
     * 查询巡检任务分页列表
     */
    @GetMapping("/page/list")
    public JsonResult list(QueryInspectionPageListRequest request) {
        log.info("进入查询巡检任务分页列表接口");
        return iInspectionService.selectPageList(request);
    }

    /**
     * 获取巡检任务信息信息
     */
    @GetMapping(value = "/info/{id}")
    public JsonResult getInfo(@PathVariable("id") String id) {
        log.info("进入查询任务详情接口, id={}", id);
        return iInspectionService.getInspectionInfo(id);
    }

    /**
     * 获取巡检任务详情信息
     */
    @GetMapping(value = "/details/{id}")
    public JsonResult getDetails(@PathVariable("id") String id) {
        log.info("进入查询任务详情接口, id={}", id);
        return iInspectionService.getInspectionDetails(id);
    }

    /**
     * 新增巡检任务
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody AddInspectionRequest addInspectionRequest) {
        log.info("进入添加任务接口");
        return iInspectionService.insert(addInspectionRequest);
    }

    /**
     * 修改巡检任务
     */
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody EditInspectionRequest editInspectionRequest) {
        log.info("进入修改任务接口, id={}", editInspectionRequest.getId());
        return iInspectionService.update(editInspectionRequest);
    }

    /**
     * 删除巡检任务
     */
    @DeleteMapping("/delete/{id}")
    public JsonResult delete(@PathVariable("id") String id) {
        log.info("进入删除任务接口, id={}", id);
        return iInspectionService.deleteById(id);
    }

    /**
     * 根据机场id查询任务
     * @param dto
     * @return
     */
    @GetMapping("/listByAirportId")
    public JsonResult getListByAirportId(InspectionDto dto){
        return iInspectionService.getListByAirportId(dto);
    }

    /**
     * 根据id获取任务视频回放和直播
     * @param id
     * @return
     */
    @GetMapping("/videoById/{id}")
    public JsonResult videoById(@PathVariable("id") String id){
        return iInspectionService.getVideoById(id);
    }


    /**
     * 获取飞行轨迹
     * @param id
     * @return
     */
    @GetMapping("/findFlightData/{id}")
    public JsonResult finFlightData(@PathVariable("id") String id){
        return iInspectionService.findFlightData(id);
    }

    /**
     * 根据巡检id查询最新一条遥测数据
     * @param inspectionId
     * @return
     */
    @GetMapping("/getFlightDataByInspectionId")
    public FlightData getFlightDataByInspectionId(@RequestParam("inspectionId") String inspectionId){
        return iInspectionService.getFlightDataByInspectionId(inspectionId);
    }

    /**
     * 被硬件调用，存飞行轨迹
     */
    @PostMapping("/track")
    public OperationEnum track(@RequestBody @Valid InspectionRequest inspectionRequest) {
        log.info("被硬件调用，存飞行轨迹：{}", JSONObject.toJSONString(inspectionRequest));
        return iInspectionService.track(inspectionRequest);
    }

    /**
     * 重新提交巡检任务
     */
    @PutMapping("/resubmit")
    public JsonResult resubmit(@RequestBody EditInspectionRequest editInspectionRequest) {
        log.info("进入重新提交任务接口, id={}", editInspectionRequest.getId());
        return iInspectionService.resubmit(editInspectionRequest);
    }

    /**
     * 立即执行
     * @param id
     * @return
     */
    @PostMapping("/execute/{id}")
    public JsonResult execute(@PathVariable("id") String id) {
        log.info("进入立即执行任务接口, id={}", id);
        return iInspectionService.execute(id);
    }

    /**
     * 根据用户id获取最新的五条任务信息
     * @param
     * @return
     */
    @GetMapping("/getNewInspectionList")
    public JsonResult getNewInspectionList(){
        return iInspectionService.getNewInspectionList();
    }

}
