package cn.iocoder.yudao.module.mes.controller.admin.goview;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mes.controller.admin.goview.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.goview.GoviewDO;
import cn.iocoder.yudao.module.mes.service.goview.GoviewService;

@Tag(name = "管理后台 - 可视化大屏")
@RestController
@RequestMapping("/mes/goview")
@Validated
public class GoviewController {

    @Resource
    private GoviewService goviewService;

    @PostMapping("/create")
    @Operation(summary = "创建可视化大屏")
    @PreAuthorize("@ss.hasPermission('mes:goview:create')")
    public CommonResult<Long> createGoview(@Valid @RequestBody GoviewSaveReqVO createReqVO) {
        return success(goviewService.createGoview(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新可视化大屏")
    @PreAuthorize("@ss.hasPermission('mes:goview:update')")
    public CommonResult<Boolean> updateGoview(@Valid @RequestBody GoviewSaveReqVO updateReqVO) {
        goviewService.updateGoview(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除可视化大屏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:goview:delete')")
    public CommonResult<Boolean> deleteGoview(@RequestParam("id") Long id) {
        goviewService.deleteGoview(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得可视化大屏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:goview:query')")
    public CommonResult<GoviewRespVO> getGoview(@RequestParam("id") Long id) {
        GoviewDO goview = goviewService.getGoview(id);
        return success(BeanUtils.toBean(goview, GoviewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得可视化大屏分页")
    @PreAuthorize("@ss.hasPermission('mes:goview:query')")
    public CommonResult<PageResult<GoviewRespVO>> getGoviewPage(@Valid GoviewPageReqVO pageReqVO) {
        PageResult<GoviewDO> pageResult = goviewService.getGoviewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GoviewRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出可视化大屏 Excel")
    @PreAuthorize("@ss.hasPermission('mes:goview:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGoviewExcel(@Valid GoviewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GoviewDO> list = goviewService.getGoviewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "可视化大屏.xls", "数据", GoviewRespVO.class,
                        BeanUtils.toBean(list, GoviewRespVO.class));
    }

}