package com.ruoyi.record.controller;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.record.domain.RecordInspectionCabinet;
import com.ruoyi.record.service.IRecordInspectionCabinetService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import org.springframework.http.HttpHeaders;

import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.util.RandomAccess;

/**
 * 电柜巡检记录Controller
 * 
 * @author zangch
 * @date 2024-11-07
 */
@RestController
@RequestMapping("/record/recordInspectionCabinet")
public class RecordInspectionCabinetController extends BaseController
{
    @Autowired
    private IRecordInspectionCabinetService recordInspectionCabinetService;

    /**
     * 查询电柜巡检记录列表
     */
    @PreAuthorize("@ss.hasPermi('record:recordInspectionCabinet:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordInspectionCabinet recordInspectionCabinet)
    {
        startPage();
        List<RecordInspectionCabinet> list = recordInspectionCabinetService.selectRecordInspectionCabinetList(recordInspectionCabinet);
        return getDataTable(list);
    }

    /**
     * 导出电柜巡检记录列表
     */
    @PreAuthorize("@ss.hasPermi('record:recordInspectionCabinet:export')")
    @Log(title = "电柜巡检记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordInspectionCabinet recordInspectionCabinet)
    {
        List<RecordInspectionCabinet> list = recordInspectionCabinetService.selectRecordInspectionCabinetList(recordInspectionCabinet);
        ExcelUtil<RecordInspectionCabinet> util = new ExcelUtil<RecordInspectionCabinet>(RecordInspectionCabinet.class);
        util.exportExcel(response, list, "电柜巡检记录数据");
    }

    /**
     * 获取电柜巡检记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:recordInspectionCabinet:query')")
    @GetMapping(value = "/{objId}")
    public AjaxResult getInfo(@PathVariable("objId") Long objId)
    {
        return success(recordInspectionCabinetService.selectRecordInspectionCabinetByObjId(objId));
    }

    /**
     * 新增电柜巡检记录
     */
    @PreAuthorize("@ss.hasPermi('record:recordInspectionCabinet:add')")
    @Log(title = "电柜巡检记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordInspectionCabinet recordInspectionCabinet)
    {
        return toAjax(recordInspectionCabinetService.insertRecordInspectionCabinet(recordInspectionCabinet));
    }

    /**
     * 修改电柜巡检记录
     */
    @PreAuthorize("@ss.hasPermi('record:recordInspectionCabinet:edit')")
    @Log(title = "电柜巡检记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordInspectionCabinet recordInspectionCabinet)
    {
        return toAjax(recordInspectionCabinetService.updateRecordInspectionCabinet(recordInspectionCabinet));
    }

    /**
     * 删除电柜巡检记录
     */
    @PreAuthorize("@ss.hasPermi('record:recordInspectionCabinet:remove')")
    @Log(title = "电柜巡检记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{objIds}")
    public AjaxResult remove(@PathVariable Long[] objIds)
    {
        return toAjax(recordInspectionCabinetService.deleteRecordInspectionCabinetByObjIds(objIds));
    }



    /**
     * 处理热成像视频文件的HTTP GET请求。
     * @param filePath 热成像视频文件的路径
     * @return 包含视频文件的ResponseEntity对象，如果文件不存在则返回null
     */
    @GetMapping("/getThermalVideo/{filePath}")
    public ResponseEntity<FileSystemResource> getThermalVideo(@PathVariable String filePath) {
        // 获取配置文件中的路径application.yml的profile
        String myPath = RuoYiConfig.getMyPath();
        // 构建完整的文件路径
        String fileUrl = myPath + "/日志信息/巡检录像/热成像/" + filePath;
        // 创建文件对象
        File file = new File(fileUrl);
        // 检查文件是否存在
        if (file.exists()) {
            // 创建文件系统资源对象，包装文件，http头不能使用file，所以使用FileSystemResource
            FileSystemResource resource = new FileSystemResource(file);
            // 设置响应头，指定内容类型为视频格式
            HttpHeaders headers = new HttpHeaders();
            // 设置文件长度的内容类型
            headers.setContentLength(file.length());
            // 动态检测文件的MIME类型
            try {
                String contentType = Files.probeContentType(file.toPath());
                if (contentType != null) {
                    headers.setContentType(MediaType.parseMediaType(contentType));
                } else {
                    headers.setContentType(MediaType.parseMediaType("video/*"));
                }
            } catch (IOException e) {
                headers.setContentType(MediaType.parseMediaType("video/*"));
            }
            // 返回包含文件资源的响应实体
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } else {
            // 如果文件不存在，返回404状态
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 处理GET请求，返回指定路径的视频文件。
     * @param filePath 视频文件的路径
     * @return ResponseEntity<FileSystemResource> 包含视频文件资源的响应实体
     */
    @GetMapping("/getVisibleVideo/{filePath}")
    public ResponseEntity<FileSystemResource> getVisibleVideo(@PathVariable String filePath) {
        // 获取配置文件中的路径application.yml的profile
        String myPath = RuoYiConfig.getMyPath();
        // 构建完整的文件路径
        String fileUrl = myPath + "/日志信息/巡检录像/可见光/" + filePath;
        // 创建文件对象
        File file = new File(fileUrl);
        // 检查文件是否存在
        if (file.exists()) {
            // 创建文件系统资源对象，包装文件，http头不能使用file，所以使用FileSystemResource
            FileSystemResource resource = new FileSystemResource(file);
            // 设置HTTP头信息
            HttpHeaders headers = new HttpHeaders();
            // 设置文件长度
            headers.setContentLength(file.length());
            // 动态检测文件的MIME类型
            try {
                String contentType = Files.probeContentType(file.toPath());
                if (contentType != null) {
                    headers.setContentType(MediaType.parseMediaType(contentType));
                } else {
                    headers.setContentType(MediaType.parseMediaType("video/*"));
                }
            } catch (IOException e) {
                headers.setContentType(MediaType.parseMediaType("video/*"));
            }
            // 返回包含文件资源的响应实体
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } else {
            // 如果文件不存在，返回404状态
            return ResponseEntity.notFound().build();
        }
    }

}
