package cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.relation;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.relation.vo.IotYfSipRelationPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.relation.vo.IotYfSipRelationRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.relation.vo.IotYfSipRelationSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.relation.IotYfSipRelationDO;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.relation.IotYfSipRelationService;
import org.springframework.web.bind.annotation.*;
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 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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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


@Tag(name = "管理后台 - yf监控设备关联")
@RestController
@RequestMapping("/rq/iot-sip-relation")
@Validated
public class IotYfSipRelationController {

    @Resource
    private IotYfSipRelationService iotYfSipRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建yf监控设备关联")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-relation:create')")
    public CommonResult<Long> createIotSipRelation(@Valid @RequestBody IotYfSipRelationSaveReqVO createReqVO) {
        return success(iotYfSipRelationService.createIotSipRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新yf监控设备关联")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-relation:update')")
    public CommonResult<Boolean> updateIotSipRelation(@Valid @RequestBody IotYfSipRelationSaveReqVO updateReqVO) {
        iotYfSipRelationService.updateIotSipRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf监控设备关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-relation:delete')")
    public CommonResult<Boolean> deleteIotSipRelation(@RequestParam("id") Long id) {
        iotYfSipRelationService.deleteIotSipRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf监控设备关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-relation:query')")
    public CommonResult<IotYfSipRelationRespVO> getIotSipRelation(@RequestParam("id") Long id) {
        IotYfSipRelationDO iotSipRelation = iotYfSipRelationService.getIotSipRelation(id);
        return success(BeanUtils.toBean(iotSipRelation, IotYfSipRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf监控设备关联分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-relation:query')")
    public CommonResult<PageResult<IotYfSipRelationRespVO>> getIotSipRelationPage(@Valid IotYfSipRelationPageReqVO pageReqVO) {
        PageResult<IotYfSipRelationDO> pageResult = iotYfSipRelationService.getIotSipRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfSipRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf监控设备关联 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotSipRelationExcel(@Valid IotYfSipRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfSipRelationDO> list = iotYfSipRelationService.getIotSipRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf监控设备关联.xls", "数据", IotYfSipRelationRespVO.class,
                        BeanUtils.toBean(list, IotYfSipRelationRespVO.class));
    }

}
