package cn.iocoder.yudao.module.pms.controller.admin.qhse.environment;

import cn.iocoder.yudao.module.pms.controller.admin.qhse.environment.vo.IotEnvironmentRecognizePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.environment.vo.IotEnvironmentRecognizeRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.environment.vo.IotEnvironmentRecognizeSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.environment.IotEnvironmentRecognizeDO;
import cn.iocoder.yudao.module.pms.service.qhse.environment.IotEnvironmentRecognizeService;
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 = "管理后台 - 环境因素识别")
@RestController
@RequestMapping("/rq/iot-environment-recognize")
@Validated
public class IotEnvironmentRecognizeController {

    @Resource
    private IotEnvironmentRecognizeService iotEnvironmentRecognizeService;

    @PostMapping("/create")
    @Operation(summary = "创建环境因素识别")
    @PreAuthorize("@ss.hasPermission('rq:iot-environment-recognize:create')")
    public CommonResult<Long> createIotEnvironmentRecognize(@Valid @RequestBody IotEnvironmentRecognizeSaveReqVO createReqVO) {
        return success(iotEnvironmentRecognizeService.createIotEnvironmentRecognize(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新环境因素识别")
    @PreAuthorize("@ss.hasPermission('rq:iot-environment-recognize:update')")
    public CommonResult<Boolean> updateIotEnvironmentRecognize(@Valid @RequestBody IotEnvironmentRecognizeSaveReqVO updateReqVO) {
        iotEnvironmentRecognizeService.updateIotEnvironmentRecognize(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除环境因素识别")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-environment-recognize:delete')")
    public CommonResult<Boolean> deleteIotEnvironmentRecognize(@RequestParam("id") Long id) {
        iotEnvironmentRecognizeService.deleteIotEnvironmentRecognize(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得环境因素识别")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-environment-recognize:query')")
    public CommonResult<IotEnvironmentRecognizeRespVO> getIotEnvironmentRecognize(@RequestParam("id") Long id) {
        IotEnvironmentRecognizeDO iotEnvironmentRecognize = iotEnvironmentRecognizeService.getIotEnvironmentRecognize(id);
        return success(BeanUtils.toBean(iotEnvironmentRecognize, IotEnvironmentRecognizeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得环境因素识别分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-environment-recognize:query')")
    public CommonResult<PageResult<IotEnvironmentRecognizeRespVO>> getIotEnvironmentRecognizePage(@Valid IotEnvironmentRecognizePageReqVO pageReqVO) {
        PageResult<IotEnvironmentRecognizeDO> pageResult = iotEnvironmentRecognizeService.getIotEnvironmentRecognizePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotEnvironmentRecognizeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出环境因素识别 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-environment-recognize:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotEnvironmentRecognizeExcel(@Valid IotEnvironmentRecognizePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotEnvironmentRecognizeDO> list = iotEnvironmentRecognizeService.getIotEnvironmentRecognizePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "环境因素识别.xls", "数据", IotEnvironmentRecognizeRespVO.class,
                        BeanUtils.toBean(list, IotEnvironmentRecognizeRespVO.class));
    }

}