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

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.var.vo.IotYfVarTempPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.var.vo.IotYfVarTempRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.var.vo.IotYfVarTempSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.var.IotYfVarTempDO;
import cn.iocoder.yudao.module.pms.service.yanfan.var.IotVarTempService;
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-var-temp")
@Validated
public class IotYfVarTempController {

    @Resource
    private IotVarTempService iotVarTempService;

    @PostMapping("/create")
    @Operation(summary = "创建yf设备采集变量模板对象")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp:create')")
    public CommonResult<Long> createIotVarTemp(@Valid @RequestBody IotYfVarTempSaveReqVO createReqVO) {
        return success(iotVarTempService.createIotVarTemp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新yf设备采集变量模板对象")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp:update')")
    public CommonResult<Boolean> updateIotVarTemp(@Valid @RequestBody IotYfVarTempSaveReqVO updateReqVO) {
        iotVarTempService.updateIotVarTemp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf设备采集变量模板对象")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp:delete')")
    public CommonResult<Boolean> deleteIotVarTemp(@RequestParam("id") Long id) {
        iotVarTempService.deleteIotVarTemp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf设备采集变量模板对象")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp:query')")
    public CommonResult<IotYfVarTempRespVO> getIotVarTemp(@RequestParam("id") Long id) {
        IotYfVarTempDO iotVarTemp = iotVarTempService.getIotVarTemp(id);
        return success(BeanUtils.toBean(iotVarTemp, IotYfVarTempRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf设备采集变量模板对象分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp:query')")
    public CommonResult<PageResult<IotYfVarTempRespVO>> getIotVarTempPage(@Valid IotYfVarTempPageReqVO pageReqVO) {
        PageResult<IotYfVarTempDO> pageResult = iotVarTempService.getIotVarTempPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfVarTempRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf设备采集变量模板对象 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-var-temp:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotVarTempExcel(@Valid IotYfVarTempPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfVarTempDO> list = iotVarTempService.getIotVarTempPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf设备采集变量模板对象.xls", "数据", IotYfVarTempRespVO.class,
                        BeanUtils.toBean(list, IotYfVarTempRespVO.class));
    }

    @Operation(summary = "根据产品id获取模板详情")
    @GetMapping("/getTempByPid")
    public CommonResult<IotYfVarTempDO> getTempByPid(Long productId) {

        return success(iotVarTempService.selectVarTempByProductId(productId));
    }
}
