package org.dromara.drawing.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.drawing.domain.query.DrawingFileQuery;
import org.dromara.drawing.domain.vo.DrawingFileVo;
import org.dromara.drawing.service.IDrawingFileService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 图纸文件控制器
 *
 * @Author: Trae
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drawing/file")
public class DrawingFileController {

    private final IDrawingFileService drawingFileService;

    /**
     * 查询图纸文件列表
     */
    @GetMapping("/list")
    public R<List<DrawingFileVo>> list(DrawingFileQuery query) {
        return R.ok(drawingFileService.queryList(query));
    }

    /**
     * 根据ID查询图纸文件详情
     */
    @GetMapping("/{id}")
    public R<DrawingFileVo> getById(@PathVariable Integer id) {
        DrawingFileVo vo = drawingFileService.queryById(id);
        if (vo != null) {
            vo.setFileValue(null);
        }
        return R.ok(vo);
    }

    /**
     * 根据图纸代码批量查询JPG简图（返回 FNum -> base64 的映射）
     */
    @GetMapping("/imageByNums")
    public R<Map<String, String>> imageByNums(@RequestParam("nums") String nums) {
        List<String> numList = Arrays.asList(nums.split(","));
        return R.ok(drawingFileService.queryImagesByNums(numList));
    }

}
