package cn.iocoder.yudao.module.pms.controller.admin.yanfan.group;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.group.vo.IotYfGroupPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.group.vo.IotYfGroupRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.group.vo.IotYfGroupSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.group.IotYfGroupDO;
import cn.iocoder.yudao.module.pms.service.yanfan.group.IotYfGroupService;
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-group")
@Validated
public class IotYfGroupController {

    @Resource
    private IotYfGroupService iotGroupService;

    @PostMapping
    @Operation(summary = "创建yf设备分组")
    @PreAuthorize("@ss.hasPermission('rq:iot-group:create')")
    public CommonResult<Long> createIotGroup(@Valid @RequestBody IotYfGroupSaveReqVO createReqVO) {
        return success(iotGroupService.createIotGroup(createReqVO));
    }

    @PutMapping
    @Operation(summary = "更新yf设备分组")
    @PreAuthorize("@ss.hasPermission('rq:iot-group:update')")
    public CommonResult<Boolean> updateIotGroup(@Valid @RequestBody IotYfGroupSaveReqVO updateReqVO) {
        iotGroupService.updateIotGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf设备分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-group:delete')")
    public CommonResult<Boolean> deleteIotGroup(@RequestParam("id") Long id) {
        iotGroupService.deleteIotGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf设备分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-group:query')")
    public CommonResult<IotYfGroupRespVO> getIotGroup(@RequestParam("id") Long id) {
        IotYfGroupDO iotGroup = iotGroupService.getIotGroup(id);
        return success(BeanUtils.toBean(iotGroup, IotYfGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf设备分组分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-group:query')")
    public CommonResult<PageResult<IotYfGroupRespVO>> getIotGroupPage(@Valid IotYfGroupPageReqVO pageReqVO) {
        PageResult<IotYfGroupDO> pageResult = iotGroupService.getIotGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf设备分组 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotGroupExcel(@Valid IotYfGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfGroupDO> list = iotGroupService.getIotGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf设备分组.xls", "数据", IotYfGroupRespVO.class,
                        BeanUtils.toBean(list, IotYfGroupRespVO.class));
    }

}
