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

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.iotsapsynclog.vo.IotSapSyncLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotsapsynclog.vo.IotSapSyncLogRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotsapsynclog.vo.IotSapSyncLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotsapsynclog.IotSapSyncLogDO;
import cn.iocoder.yudao.module.pms.service.iotsapsynclog.IotSapSyncLogService;
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 = "管理后台 - 操作日志记录 V2 版本")
@RestController
@RequestMapping("/pms/iot-sap-sync-log")
@Validated
public class IotSapSyncLogController {

    @Resource
    private IotSapSyncLogService iotSapSyncLogService;

    @PostMapping("/create")
    @Operation(summary = "创建操作日志记录 V2 版本")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-sync-log:create')")
    public CommonResult<Long> createIotSapSyncLog(@Valid @RequestBody IotSapSyncLogSaveReqVO createReqVO) {
        return success(iotSapSyncLogService.createIotSapSyncLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新操作日志记录 V2 版本")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-sync-log:update')")
    public CommonResult<Boolean> updateIotSapSyncLog(@Valid @RequestBody IotSapSyncLogSaveReqVO updateReqVO) {
        iotSapSyncLogService.updateIotSapSyncLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除操作日志记录 V2 版本")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-sync-log:delete')")
    public CommonResult<Boolean> deleteIotSapSyncLog(@RequestParam("id") Long id) {
        iotSapSyncLogService.deleteIotSapSyncLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得操作日志记录 V2 版本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-sync-log:query')")
    public CommonResult<IotSapSyncLogRespVO> getIotSapSyncLog(@RequestParam("id") Long id) {
        IotSapSyncLogDO iotSapSyncLog = iotSapSyncLogService.getIotSapSyncLog(id);
        return success(BeanUtils.toBean(iotSapSyncLog, IotSapSyncLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得操作日志记录 V2 版本分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-sync-log:query')")
    public CommonResult<PageResult<IotSapSyncLogRespVO>> getIotSapSyncLogPage(@Valid IotSapSyncLogPageReqVO pageReqVO) {
        PageResult<IotSapSyncLogDO> pageResult = iotSapSyncLogService.getIotSapSyncLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotSapSyncLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出操作日志记录 V2 版本 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-sync-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotSapSyncLogExcel(@Valid IotSapSyncLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotSapSyncLogDO> list = iotSapSyncLogService.getIotSapSyncLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "操作日志记录 V2 版本.xls", "数据", IotSapSyncLogRespVO.class,
                        BeanUtils.toBean(list, IotSapSyncLogRespVO.class));
    }

}