package com.project.zcustom.controller.backManager.engineering;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.enums.BusinessType;
import com.project.common.utils.poi.ExcelUtil;
import com.project.zcustom.domain.addional.LargeIssue;
import com.project.zcustom.service.unit.ILargeIssueService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 上报问题Controller
 *
 * @author change
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/zcustom/issue")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class LargeIssueController extends BaseController {

    private final ILargeIssueService largeIssueService;

    @GetMapping("/list")
    public AjaxResult list(LargeIssue entity) {
        return AjaxResult.success("查询成功", largeIssueService.selectList(entity));
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(LargeIssue entity) {
        return AjaxResult.success("查询成功", largeIssueService.selectLargeIssueList(entity));
    }

    @Log(title = "上报问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LargeIssue entity) {
        List<LargeIssue> list = largeIssueService.selectLargeIssueList(entity);
        ExcelUtil<LargeIssue> util = new ExcelUtil<>(LargeIssue.class);
        util.exportExcel(response, list, "上报问题数据");
    }

    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", largeIssueService.selectLargeIssueById(id));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:issue:add')")
    @Log(title = "上报问题", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody LargeIssue entity) {
        return toAjax(largeIssueService.save(entity));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:issue:edit')")
    @Log(title = "上报问题", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody LargeIssue entity) {
        return toAjax(largeIssueService.updateById(entity));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:issue:remove')")
    @Log(title = "上报问题", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(largeIssueService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @GetMapping("/downloadFile")
    public ResponseEntity<InputStreamResource> downloadFile(@RequestParam String filePath) throws IOException {
        // 获取服务器根目录下的 xx 文件夹中的文件
        File file = new File(System.getProperty("user.dir") + filePath);

        if (!file.exists()) {
            return ResponseEntity.notFound().build();  // 如果文件不存在，返回 404
        }

        // 创建文件流
        FileInputStream fileInputStream = new FileInputStream(file);

        // 设置 HTTP 响应头，返回文件流
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())  // 设置文件下载
                .contentType(MediaType.APPLICATION_OCTET_STREAM)  // 文件类型为二进制流
                .contentLength(file.length())  // 设置文件大小
                .body(new InputStreamResource(fileInputStream));  // 返回文件流
    }
}
