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

import cn.iocoder.yudao.module.pms.dal.dataobject.devicegroup.IotDeviceGroupDetailDO;
import cn.iocoder.yudao.module.pms.service.devicegroup.IotDeviceGroupDetailService;
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 = "管理后台 - PMS成套明细")
@RestController
@RequestMapping("/rq/iot-device-group-detail")
@Validated
public class IotDeviceGroupDetailController {

    @Resource
    private IotDeviceGroupDetailService iotDeviceGroupDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS成套明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-group-detail:create')")
    public CommonResult<Long> createIotDeviceGroupDetail(@Valid @RequestBody IotDeviceGroupDetailSaveReqVO createReqVO) {
        return success(iotDeviceGroupDetailService.createIotDeviceGroupDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS成套明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-group-detail:update')")
    public CommonResult<Boolean> updateIotDeviceGroupDetail(@Valid @RequestBody IotDeviceGroupDetailSaveReqVO updateReqVO) {
        iotDeviceGroupDetailService.updateIotDeviceGroupDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS成套明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-device-group-detail:delete')")
    public CommonResult<Boolean> deleteIotDeviceGroupDetail(@RequestParam("id") Long id) {
        iotDeviceGroupDetailService.deleteIotDeviceGroupDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS成套明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-group-detail:query')")
    public CommonResult<IotDeviceGroupDetailRespVO> getIotDeviceGroupDetail(@RequestParam("id") Long id) {
        IotDeviceGroupDetailDO iotDeviceGroupDetail = iotDeviceGroupDetailService.getIotDeviceGroupDetail(id);
        return success(BeanUtils.toBean(iotDeviceGroupDetail, IotDeviceGroupDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS成套明细分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-group-detail:query')")
    public CommonResult<PageResult<IotDeviceGroupDetailRespVO>> getIotDeviceGroupDetailPage(@Valid IotDeviceGroupDetailPageReqVO pageReqVO) {
        PageResult<IotDeviceGroupDetailDO> pageResult = iotDeviceGroupDetailService.getIotDeviceGroupDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDeviceGroupDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS成套明细 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-group-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceGroupDetailExcel(@Valid IotDeviceGroupDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceGroupDetailDO> list = iotDeviceGroupDetailService.getIotDeviceGroupDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS成套明细.xls", "数据", IotDeviceGroupDetailRespVO.class,
                        BeanUtils.toBean(list, IotDeviceGroupDetailRespVO.class));
    }

}
