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

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.iotwebtopoprojectdevice.vo.IotWebtopoProjectDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotwebtopoprojectdevice.vo.IotWebtopoProjectDeviceRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotwebtopoprojectdevice.vo.IotWebtopoProjectDeviceSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotwebtopoprojectdevice.IotWebtopoProjectDeviceDO;
import cn.iocoder.yudao.module.pms.service.iotwebtopoprojectdevice.IotWebtopoProjectDeviceService;
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-webtopo-project-device")
@Validated
public class IotWebtopoProjectDeviceController {

    @Resource
    private IotWebtopoProjectDeviceService iotWebtopoProjectDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建组态项目关联设备")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-project-device:create')")
    public CommonResult<Long> createIotWebtopoProjectDevice(@Valid @RequestBody IotWebtopoProjectDeviceSaveReqVO createReqVO) {
        return success(iotWebtopoProjectDeviceService.createIotWebtopoProjectDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组态项目关联设备")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-project-device:update')")
    public CommonResult<Boolean> updateIotWebtopoProjectDevice(@Valid @RequestBody IotWebtopoProjectDeviceSaveReqVO updateReqVO) {
        iotWebtopoProjectDeviceService.updateIotWebtopoProjectDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组态项目关联设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-project-device:delete')")
    public CommonResult<Boolean> deleteIotWebtopoProjectDevice(@RequestParam("id") Long id) {
        iotWebtopoProjectDeviceService.deleteIotWebtopoProjectDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组态项目关联设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-project-device:query')")
    public CommonResult<IotWebtopoProjectDeviceRespVO> getIotWebtopoProjectDevice(@RequestParam("id") Long id) {
        IotWebtopoProjectDeviceDO iotWebtopoProjectDevice = iotWebtopoProjectDeviceService.getIotWebtopoProjectDevice(id);
        return success(BeanUtils.toBean(iotWebtopoProjectDevice, IotWebtopoProjectDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组态项目关联设备分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-project-device:query')")
    public CommonResult<PageResult<IotWebtopoProjectDeviceRespVO>> getIotWebtopoProjectDevicePage(@Valid IotWebtopoProjectDevicePageReqVO pageReqVO) {
        PageResult<IotWebtopoProjectDeviceDO> pageResult = iotWebtopoProjectDeviceService.getIotWebtopoProjectDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotWebtopoProjectDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组态项目关联设备 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-webtopo-project-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotWebtopoProjectDeviceExcel(@Valid IotWebtopoProjectDevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotWebtopoProjectDeviceDO> list = iotWebtopoProjectDeviceService.getIotWebtopoProjectDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组态项目关联设备.xls", "数据", IotWebtopoProjectDeviceRespVO.class,
                        BeanUtils.toBean(list, IotWebtopoProjectDeviceRespVO.class));
    }

}