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

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.iotattachment.vo.IotAttachmentPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotattachment.vo.IotAttachmentRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotattachment.vo.IotAttachmentSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotattachment.IotAttachmentDO;
import cn.iocoder.yudao.module.pms.service.iotattachment.IotAttachmentService;
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("/rq/iot-attachment")
@Validated
public class IotAttachmentController {

    @Resource
    private IotAttachmentService iotAttachmentService;

    @PostMapping("/create")
    @Operation(summary = "创建附件")
    @PreAuthorize("@ss.hasPermission('rq:iot-attachment:create')")
    public CommonResult<Long> createIotAttachment(@Valid @RequestBody IotAttachmentSaveReqVO createReqVO) {
        return success(iotAttachmentService.createIotAttachment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新附件")
    @PreAuthorize("@ss.hasPermission('rq:iot-attachment:update')")
    public CommonResult<Boolean> updateIotAttachment(@Valid @RequestBody IotAttachmentSaveReqVO updateReqVO) {
        iotAttachmentService.updateIotAttachment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除附件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-attachment:delete')")
    public CommonResult<Boolean> deleteIotAttachment(@RequestParam("id") Long id) {
        iotAttachmentService.deleteIotAttachment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得附件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-attachment:query')")
    public CommonResult<IotAttachmentRespVO> getIotAttachment(@RequestParam("id") Long id) {
        IotAttachmentDO iotAttachment = iotAttachmentService.getIotAttachment(id);
        return success(BeanUtils.toBean(iotAttachment, IotAttachmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得附件分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-attachment:query')")
    public CommonResult<PageResult<IotAttachmentRespVO>> getIotAttachmentPage(@Valid IotAttachmentPageReqVO pageReqVO) {
        PageResult<IotAttachmentDO> pageResult = iotAttachmentService.getIotAttachmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotAttachmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出附件 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-attachment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotAttachmentExcel(@Valid IotAttachmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotAttachmentDO> list = iotAttachmentService.getIotAttachmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "附件.xls", "数据", IotAttachmentRespVO.class,
                        BeanUtils.toBean(list, IotAttachmentRespVO.class));
    }

}