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

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.iotmodel.vo.*;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmodel.IotModelDO;
import cn.iocoder.yudao.module.pms.service.iotmodel.IotModelService;
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 = "管理后台 - PMS 功能优化 规格型号")
@RestController
@RequestMapping("/rq/iot-model")
@Validated
public class IotModelController {

    @Resource
    private IotModelService iotModelService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 功能优化 规格型号")
    @PreAuthorize("@ss.hasPermission('rq:iot-model:create')")
    public CommonResult<Long> createIotModel(@Valid @RequestBody IotModelSaveReqVO createReqVO) {
        return success(iotModelService.createIotModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 功能优化 规格型号")
    @PreAuthorize("@ss.hasPermission('rq:iot-model:update')")
    public CommonResult<Boolean> updateIotModel(@Valid @RequestBody IotModelSaveReqVO updateReqVO) {
        iotModelService.updateIotModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 功能优化 规格型号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-model:delete')")
    public CommonResult<Boolean> deleteIotModel(@RequestParam("id") Long id) {
        iotModelService.deleteIotModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 功能优化 规格型号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-model:query')")
    public CommonResult<IotModelRespVO> getIotModel(@RequestParam("id") Long id) {
        IotModelDO iotModel = iotModelService.getIotModel(id);
        return success(BeanUtils.toBean(iotModel, IotModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 功能优化 规格型号分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-model:query')")
    public CommonResult<PageResult<IotModelRespVO>> getIotModelPage(@Valid IotModelPageReqVO pageReqVO) {
        PageResult<IotModelDO> pageResult = iotModelService.getIotModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotModelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 功能优化 规格型号 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotModelExcel(@Valid IotModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotModelDO> list = iotModelService.getIotModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 功能优化 规格型号.xls", "数据", IotModelRespVO.class,
                        BeanUtils.toBean(list, IotModelRespVO.class));
    }

}