package cn.iocoder.yudao.module.pms.controller.admin.iotwebtopodevicesvgnode;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotwebtopodevicesvgnode.vo.IotWebtopoDeviceSvgnodePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotwebtopodevicesvgnode.vo.IotWebtopoDeviceSvgnodeRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotwebtopodevicesvgnode.vo.IotWebtopoDeviceSvgnodeSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotwebtopodevicesvgnode.IotWebtopoDeviceSvgnodeDO;
import cn.iocoder.yudao.module.pms.service.iotwebtopodevicesvgnode.IotWebtopoDeviceSvgnodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 组态-svg与设备绑定")
@RestController
@RequestMapping("/pms/iot-webtopo-device-svgnode")
@Validated
public class IotWebtopoDeviceSvgnodeController {

    @Resource
    private IotWebtopoDeviceSvgnodeService iotWebtopoDeviceSvgnodeService;

    @PostMapping("/create")
    @Operation(summary = "创建组态-svg与设备绑定")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-device-svgnode:create')")
    public CommonResult<Long> createIotWebtopoDeviceSvgnode(@Valid @RequestBody IotWebtopoDeviceSvgnodeSaveReqVO createReqVO) {
        return success(iotWebtopoDeviceSvgnodeService.createIotWebtopoDeviceSvgnode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组态-svg与设备绑定")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-device-svgnode:update')")
    public CommonResult<Boolean> updateIotWebtopoDeviceSvgnode(@Valid @RequestBody IotWebtopoDeviceSvgnodeSaveReqVO updateReqVO) {
        iotWebtopoDeviceSvgnodeService.updateIotWebtopoDeviceSvgnode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组态-svg与设备绑定")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-device-svgnode:delete')")
    public CommonResult<Boolean> deleteIotWebtopoDeviceSvgnode(@RequestParam("id") Long id) {
        iotWebtopoDeviceSvgnodeService.deleteIotWebtopoDeviceSvgnode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组态-svg与设备绑定")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-device-svgnode:query')")
    public CommonResult<IotWebtopoDeviceSvgnodeRespVO> getIotWebtopoDeviceSvgnode(@RequestParam("id") Long id) {
        IotWebtopoDeviceSvgnodeDO iotWebtopoDeviceSvgnode = iotWebtopoDeviceSvgnodeService.getIotWebtopoDeviceSvgnode(id);
        return success(BeanUtils.toBean(iotWebtopoDeviceSvgnode, IotWebtopoDeviceSvgnodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组态-svg与设备绑定分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-device-svgnode:query')")
    public CommonResult<PageResult<IotWebtopoDeviceSvgnodeRespVO>> getIotWebtopoDeviceSvgnodePage(@Valid IotWebtopoDeviceSvgnodePageReqVO pageReqVO) {
        PageResult<IotWebtopoDeviceSvgnodeDO> pageResult = iotWebtopoDeviceSvgnodeService.getIotWebtopoDeviceSvgnodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotWebtopoDeviceSvgnodeRespVO.class));
    }

    @GetMapping("/deviceSvgNodes")
    @Operation(summary = "获得组态-svg与设备绑定列表")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-device-svgnode:query')")
    public CommonResult<List<IotWebtopoDeviceSvgnodeRespVO>> deviceSvgNodes(@Valid IotWebtopoDeviceSvgnodePageReqVO pageReqVO) {
        List<IotWebtopoDeviceSvgnodeRespVO> resultDeviceSvgNodes = new ArrayList<>();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<IotWebtopoDeviceSvgnodeDO> pageResult = iotWebtopoDeviceSvgnodeService.getIotWebtopoDeviceSvgnodePage(pageReqVO);
        if (ObjUtil.isNotEmpty(pageResult)) {
            List<IotWebtopoDeviceSvgnodeDO> deviceSvgNodes = pageResult.getList();
            return success(BeanUtils.toBean(deviceSvgNodes, IotWebtopoDeviceSvgnodeRespVO.class));
        }
        return success(resultDeviceSvgNodes);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组态-svg与设备绑定 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-device-svgnode:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotWebtopoDeviceSvgnodeExcel(@Valid IotWebtopoDeviceSvgnodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotWebtopoDeviceSvgnodeDO> list = iotWebtopoDeviceSvgnodeService.getIotWebtopoDeviceSvgnodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组态-svg与设备绑定.xls", "数据", IotWebtopoDeviceSvgnodeRespVO.class,
                        BeanUtils.toBean(list, IotWebtopoDeviceSvgnodeRespVO.class));
    }

}