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

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.iotdeviceassociate.vo.IotDeviceAssociatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdeviceassociate.vo.IotDeviceAssociateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdeviceassociate.vo.IotDeviceAssociateSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdeviceassociate.IotDeviceAssociateDO;
import cn.iocoder.yudao.module.pms.service.iotdeviceassociate.IotDeviceAssociateService;
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-associate")
@Validated
public class IotDeviceAssociateController {

    @Resource
    private IotDeviceAssociateService iotDeviceAssociateService;

    @PostMapping("/create")
    @Operation(summary = "创建设备关联表(如：瑞都主要设备)")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-associate:create')")
    public CommonResult<Long> createIotDeviceAssociate(@Valid @RequestBody IotDeviceAssociateSaveReqVO createReqVO) {
        return success(iotDeviceAssociateService.createIotDeviceAssociate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备关联表(如：瑞都主要设备)")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-associate:update')")
    public CommonResult<Boolean> updateIotDeviceAssociate(@Valid @RequestBody IotDeviceAssociateSaveReqVO updateReqVO) {
        iotDeviceAssociateService.updateIotDeviceAssociate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备关联表(如：瑞都主要设备)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-device-associate:delete')")
    public CommonResult<Boolean> deleteIotDeviceAssociate(@RequestParam("id") Long id) {
        iotDeviceAssociateService.deleteIotDeviceAssociate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备关联表(如：瑞都主要设备)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-associate:query')")
    public CommonResult<IotDeviceAssociateRespVO> getIotDeviceAssociate(@RequestParam("id") Long id) {
        IotDeviceAssociateDO iotDeviceAssociate = iotDeviceAssociateService.getIotDeviceAssociate(id);
        return success(BeanUtils.toBean(iotDeviceAssociate, IotDeviceAssociateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备关联表(如：瑞都主要设备)分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-associate:query')")
    public CommonResult<PageResult<IotDeviceAssociateRespVO>> getIotDeviceAssociatePage(@Valid IotDeviceAssociatePageReqVO pageReqVO) {
        PageResult<IotDeviceAssociateDO> pageResult = iotDeviceAssociateService.getIotDeviceAssociatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDeviceAssociateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备关联表(如：瑞都主要设备) Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-associate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceAssociateExcel(@Valid IotDeviceAssociatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceAssociateDO> list = iotDeviceAssociateService.getIotDeviceAssociatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备关联表(如：瑞都主要设备).xls", "数据", IotDeviceAssociateRespVO.class,
                        BeanUtils.toBean(list, IotDeviceAssociateRespVO.class));
    }

}