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

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodel.vo.IotYfThingsModelPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodel.vo.IotYfThingsModelRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodel.vo.IotYfThingsModelSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.things.IotYfThingsModelDO;
import cn.iocoder.yudao.module.pms.service.yanfan.thingsmodel.IotYfThingsModelService;
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-things-model")
@Validated
public class IotYfThingsModelController {

    @Resource
    private IotYfThingsModelService iotYfThingsModelService;

    @PostMapping
    @Operation(summary = "创建yf物模型")
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model:create')")
    public CommonResult<Long> createIotThingsModel(@Valid @RequestBody IotYfThingsModelSaveReqVO createReqVO) {
        return success(iotYfThingsModelService.createIotThingsModel(createReqVO));
    }

    @PutMapping
    @Operation(summary = "更新yf物模型")
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model:update')")
    public CommonResult<Boolean> updateIotThingsModel(@Valid @RequestBody IotYfThingsModelSaveReqVO updateReqVO) {
        iotYfThingsModelService.updateIotThingsModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf物模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model:delete')")
    public CommonResult<Boolean> deleteIotThingsModel(@RequestParam("id") Long id) {
        iotYfThingsModelService.deleteIotThingsModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf物模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model:query')")
    public CommonResult<IotYfThingsModelRespVO> getIotThingsModel(@RequestParam("id") Long id) {
        IotYfThingsModelDO iotThingsModel = iotYfThingsModelService.getIotThingsModel(id);
        return success(BeanUtils.toBean(iotThingsModel, IotYfThingsModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf物模型分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model:query')")
    public CommonResult<PageResult<IotYfThingsModelRespVO>> getIotThingsModelPage(@Valid IotYfThingsModelPageReqVO pageReqVO) {
        PageResult<IotYfThingsModelDO> pageResult = iotYfThingsModelService.getIotThingsModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfThingsModelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf物模型 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotThingsModelExcel(@Valid IotYfThingsModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfThingsModelDO> list = iotYfThingsModelService.getIotThingsModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf物模型.xls", "数据", IotYfThingsModelRespVO.class,
                        BeanUtils.toBean(list, IotYfThingsModelRespVO.class));
    }

}
