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

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodeltemplate.vo.IotYfThingsModelTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodeltemplate.vo.IotYfThingsModelTemplateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodeltemplate.vo.IotYfThingsModelTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.thingsmodeltemplate.IotYfThingsModelTemplateDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.var.IotYfVarTempDO;
import cn.iocoder.yudao.module.pms.service.yanfan.thingsmodeltemplate.IotThingsModelTemplateService;
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-template")
@Validated
public class IotYfThingsModelTemplateController {

    @Resource
    private IotThingsModelTemplateService iotThingsModelTemplateService;

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

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

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

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

    @GetMapping("/list")
    @Operation(summary = "获得yf物模型模板分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model-template:query')")
    public CommonResult<PageResult<IotYfThingsModelTemplateRespVO>> getIotThingsModelTemplatePage(@Valid IotYfThingsModelTemplatePageReqVO pageReqVO) {
        PageResult<IotYfThingsModelTemplateDO> pageResult = iotThingsModelTemplateService.getIotThingsModelTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfThingsModelTemplateRespVO.class));
    }

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

    @GetMapping("/getPoints")
    @Operation(summary = "根据模板id查询采集点数据")
    @PreAuthorize("@ss.hasPermission('rq:iot-things-model-template:query')")
    public CommonResult<List<IotYfThingsModelTemplateDO>> getPoints(@Valid IotYfVarTempDO iotYfVarTempDO) {
        //todo yf
        List<IotYfThingsModelTemplateDO> iotYfThingsModelTemplateDOS = iotThingsModelTemplateService.selectAllByTemplateId(iotYfVarTempDO.getTemplateId());
        return success(iotYfThingsModelTemplateDOS);
    }
}
