package cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplateattrs;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplateattrs.vo.IotDeviceCategoryTemplateAttrsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplateattrs.vo.IotDeviceCategoryTemplateAttrsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplateattrs.vo.IotDeviceCategoryTemplateAttrsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicecategorytemplateattrs.IotDeviceCategoryTemplateAttrsDO;
import cn.iocoder.yudao.module.pms.service.iotdevicecategorytemplateattrs.IotDeviceCategoryTemplateAttrsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 设备分类公共模板属性")
@RestController
@RequestMapping("/pms/iot-device-category-template-attrs")
@Validated
public class IotDeviceCategoryTemplateAttrsController {

    @Resource
    private IotDeviceCategoryTemplateAttrsService iotDeviceCategoryTemplateAttrsService;

    @PostMapping("/create")
    @Operation(summary = "创建设备分类公共模板属性")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template-attrs:create')")
    public CommonResult<Long> createIotDeviceCategoryTemplateAttrs(@Valid @RequestBody IotDeviceCategoryTemplateAttrsSaveReqVO createReqVO) {
        return success(iotDeviceCategoryTemplateAttrsService.createIotDeviceCategoryTemplateAttrs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备分类公共模板属性")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template-attrs:update')")
    public CommonResult<Boolean> updateIotDeviceCategoryTemplateAttrs(@Valid @RequestBody IotDeviceCategoryTemplateAttrsSaveReqVO updateReqVO) {
        iotDeviceCategoryTemplateAttrsService.updateIotDeviceCategoryTemplateAttrs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备分类公共模板属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template-attrs:delete')")
    public CommonResult<Boolean> deleteIotDeviceCategoryTemplateAttrs(@RequestParam("id") Long id) {
        iotDeviceCategoryTemplateAttrsService.deleteIotDeviceCategoryTemplateAttrs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备分类公共模板属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template-attrs:query')")
    public CommonResult<IotDeviceCategoryTemplateAttrsRespVO> getIotDeviceCategoryTemplateAttrs(@RequestParam("id") Long id) {
        IotDeviceCategoryTemplateAttrsDO iotDeviceCategoryTemplateAttrs = iotDeviceCategoryTemplateAttrsService.getIotDeviceCategoryTemplateAttrs(id);
        return success(BeanUtils.toBean(iotDeviceCategoryTemplateAttrs, IotDeviceCategoryTemplateAttrsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备分类公共模板属性分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template-attrs:query')")
    public CommonResult<PageResult<IotDeviceCategoryTemplateAttrsRespVO>> getIotDeviceCategoryTemplateAttrsPage(@Valid IotDeviceCategoryTemplateAttrsPageReqVO pageReqVO) {
        PageResult<IotDeviceCategoryTemplateAttrsDO> pageResult = iotDeviceCategoryTemplateAttrsService.getIotDeviceCategoryTemplateAttrsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDeviceCategoryTemplateAttrsRespVO.class));
    }

    @GetMapping("/list-by-device-category-id")
    @Operation(summary = "根据设备分类id获取模板属性列表")
    @Parameter(name = "deviceCategoryId", description = "设备分类id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-category-template-attrs:query')")
    public CommonResult<List<IotDeviceCategoryTemplateAttrsRespVO>> getAttrsByDeviceCategoryId(@RequestParam("deviceCategoryId") Long deviceCategoryId) {
        List<IotDeviceCategoryTemplateAttrsDO> deviceTemplateAttrs = iotDeviceCategoryTemplateAttrsService.getTemplateAttrsByDeviceCategoryId(deviceCategoryId);
        return success(BeanUtils.toBean(deviceTemplateAttrs, IotDeviceCategoryTemplateAttrsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备分类公共模板属性 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template-attrs:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceCategoryTemplateAttrsExcel(@Valid IotDeviceCategoryTemplateAttrsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceCategoryTemplateAttrsDO> list = iotDeviceCategoryTemplateAttrsService.getIotDeviceCategoryTemplateAttrsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备分类公共模板属性.xls", "数据", IotDeviceCategoryTemplateAttrsRespVO.class,
                        BeanUtils.toBean(list, IotDeviceCategoryTemplateAttrsRespVO.class));
    }

}