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

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.devicetemplate.vo.IotYfDeviceTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.devicetemplate.vo.IotYfDeviceTemplateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.devicetemplate.vo.IotYfDeviceTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.devicetemplate.IotYfDeviceTemplateDO;
import cn.iocoder.yudao.module.pms.service.yanfan.devicetemplate.IotYfDeviceTemplateService;
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("/yf/iot-device-template")
@Validated
public class IotYfDeviceTemplateController {

    @Resource
    private IotYfDeviceTemplateService iotYfDeviceTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建yf设备采集点模板关联对象")
    @PreAuthorize("@ss.hasPermission('yf:iot-device-template:create')")
    public CommonResult<Long> createIotDeviceTemplate(@Valid @RequestBody IotYfDeviceTemplateSaveReqVO createReqVO) {
        return success(iotYfDeviceTemplateService.createIotDeviceTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新yf设备采集点模板关联对象")
    @PreAuthorize("@ss.hasPermission('yf:iot-device-template:update')")
    public CommonResult<Boolean> updateIotDeviceTemplate(@Valid @RequestBody IotYfDeviceTemplateSaveReqVO updateReqVO) {
        iotYfDeviceTemplateService.updateIotDeviceTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf设备采集点模板关联对象")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yf:iot-device-template:delete')")
    public CommonResult<Boolean> deleteIotDeviceTemplate(@RequestParam("id") Long id) {
        iotYfDeviceTemplateService.deleteIotDeviceTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf设备采集点模板关联对象")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yf:iot-device-template:query')")
    public CommonResult<IotYfDeviceTemplateRespVO> getIotDeviceTemplate(@RequestParam("id") Long id) {
        IotYfDeviceTemplateDO iotDeviceTemplate = iotYfDeviceTemplateService.getIotDeviceTemplate(id);
        return success(BeanUtils.toBean(iotDeviceTemplate, IotYfDeviceTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf设备采集点模板关联对象分页")
    @PreAuthorize("@ss.hasPermission('yf:iot-device-template:query')")
    public CommonResult<PageResult<IotYfDeviceTemplateRespVO>> getIotDeviceTemplatePage(@Valid IotYfDeviceTemplatePageReqVO pageReqVO) {
        PageResult<IotYfDeviceTemplateDO> pageResult = iotYfDeviceTemplateService.getIotDeviceTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfDeviceTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf设备采集点模板关联对象 Excel")
    @PreAuthorize("@ss.hasPermission('yf:iot-device-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceTemplateExcel(@Valid IotYfDeviceTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfDeviceTemplateDO> list = iotYfDeviceTemplateService.getIotDeviceTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf设备采集点模板关联对象.xls", "数据", IotYfDeviceTemplateRespVO.class,
                        BeanUtils.toBean(list, IotYfDeviceTemplateRespVO.class));
    }

}
