package cn.iocoder.yudao.module.system.controller.admin.positionmap;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapLineRespVO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapLineSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapLineDO;
import cn.iocoder.yudao.module.system.service.positionmap.PositionMapLineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 仓库点位地图连线")
@RestController
@RequestMapping("/system/position-map-line")
@Validated
@Slf4j
public class PositionMapLineController {

    @Resource
    private PositionMapLineService positionMapLineService;

    @PostMapping("/createOrEditOrDel")
    @Operation(summary = "创建编辑删除仓库点位地图连线")
    @PreAuthorize("@ss.hasPermission('system:position-map-line:create')")
    public CommonResult<Boolean> createOrEditOrDel(@RequestParam Long positionMapId, @Valid @RequestBody List<PositionMapLineSaveReqVO> createReqVO) {
        positionMapLineService.createOrEditPositionMapLine(positionMapId, createReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库点位地图连线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:position-map-line:delete')")
    public CommonResult<Boolean> deletePositionMapLine(@RequestParam("id") Long id) {
        positionMapLineService.deletePositionMapLine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库点位地图连线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:position-map-line:query')")
    public CommonResult<PositionMapLineRespVO> getPositionMapLine(@RequestParam("id") Long id) {
        PositionMapLineDO positionMapLine = positionMapLineService.getPositionMapLine(id);
        return success(BeanUtils.toBean(positionMapLine, PositionMapLineRespVO.class));
    }

    /**
     * 以下两个方法区别 - 优化
     * 方法一：
     * 查询后需在内存中转换并序列化全部数据
     * 客户端必须等待所有数据处理完成才能接收响应。
     *
     * 方法二：
     * 查询后直接流式输出，序列化与传输并行。
     * 客户端几乎在查询完成后立即开始接收数据（无内存堆积延迟）。
     */

//    @GetMapping("/list")
//    @Operation(summary = "通过地图id获取连线列表")
//    @PreAuthorize("@ss.hasPermission('system:position-map-line:query')")
//    public CommonResult<List<PositionMapLineDO>> list(@RequestParam Long positionMapId) {
//        List<PositionMapLineDO> list = positionMapLineService.getByMapId(positionMapId);
//        return success(list);
//    }

    @GetMapping("/list")
    @Operation(summary = "通过地图id获取连线列表")
    @PreAuthorize("@ss.hasPermission('system:position-map-line:query')")
    public void list(@RequestParam Long positionMapId, HttpServletResponse response) {
        // 设置响应头
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        try (OutputStream out = response.getOutputStream()) {
            // 查询数据
            List<PositionMapLineDO> list = positionMapLineService.getByMapId(positionMapId);
            // 构造返回结果
            CommonResult<List<PositionMapLineDO>> result = success(list);
            // 使用 Jackson ObjectMapper 流式写入
            SpringUtils.getBean(com.fasterxml.jackson.databind.ObjectMapper.class).writeValue(out, result);
        } catch (IOException e) {
            log.error("流式返回数据失败", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}
