package com.ruoyi.system.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FlightPaths;
import com.ruoyi.system.service.IFlightPathsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 航线文件信息Controller
 *
 * @author ruoyi
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/system/paths")
public class FlightPathsController extends BaseController {
    @Autowired
    private IFlightPathsService flightPathsService;

    /**
     * 查询航线文件信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:paths:list')")
    @GetMapping("/list")
    public TableDataInfo list(FlightPaths flightPaths) {
        startPage();
        if (flightPaths.getOrder() == null) {
            flightPaths.setOrder(1);
        }
        List<FlightPaths> list = flightPathsService.selectFlightPathsList(flightPaths);
        return getDataTable(list);
    }

    /**
     * 导出航线文件信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:paths:export')")
    @Log(title = "航线文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightPaths flightPaths) {
        List<FlightPaths> list = flightPathsService.selectFlightPathsList(flightPaths);
        ExcelUtil<FlightPaths> util = new ExcelUtil<FlightPaths>(FlightPaths.class);
        util.exportExcel(response, list, "航线文件信息数据");
    }

    /**
     * 获取航线文件信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:paths:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(flightPathsService.selectFlightPathsById(id));
    }

    /**
     * 新增航线文件信息
     */
    // @PreAuthorize("@ss.hasPermi('system:paths:add')")
    @Log(title = "航线文件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlightPaths flightPaths) {
        return toAjax(flightPathsService.insertFlightPaths(flightPaths));
    }

    /**
     * 修改航线文件信息
     */
    // @PreAuthorize("@ss.hasPermi('system:paths:edit')")
    @Log(title = "航线文件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlightPaths flightPaths) {
        return toAjax(flightPathsService.updateFlightPaths(flightPaths));
    }

    /**
     * 删除航线文件信息
     */
    // @PreAuthorize("@ss.hasPermi('system:paths:remove')")
    @Log(title = "航线文件信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            FlightPaths flightPaths = flightPathsService.selectFlightPathsById(id);
            String md5FileName = flightPaths.getFileMd5();
            Path filePath = Paths.get(System.getProperty("user.dir") + "/upload", md5FileName + ".kmz");
            try {
                // 删除文件
                Files.deleteIfExists(filePath);
                // 删除数据库
                flightPathsService.deleteFlightPathsById(id);
            } catch (IOException e) {
                return AjaxResult.warn("删除文件失败: " + e.getMessage());
            }
        }
        return AjaxResult.success();
    }
}
