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

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.iotsapstocklog.vo.IotSapStockLogPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotsapstocklog.vo.IotSapStockLogRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotsapstocklog.vo.IotSapStockLogSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotsapstocklog.IotSapStockLogDO;
import cn.iocoder.yudao.module.pms.service.iotsapstocklog.IotSapStockLogService;
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 = "管理后台 - PMS SAP 库存(sap同步的原始数据日志)")
@RestController
@RequestMapping("/pms/iot-sap-stock-log")
@Validated
public class IotSapStockLogController {

    @Resource
    private IotSapStockLogService iotSapStockLogService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS SAP 库存(sap同步的原始数据日志)")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-stock-log:create')")
    public CommonResult<Long> createIotSapStockLog(@Valid @RequestBody IotSapStockLogSaveReqVO createReqVO) {
        return success(iotSapStockLogService.createIotSapStockLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS SAP 库存(sap同步的原始数据日志)")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-stock-log:update')")
    public CommonResult<Boolean> updateIotSapStockLog(@Valid @RequestBody IotSapStockLogSaveReqVO updateReqVO) {
        iotSapStockLogService.updateIotSapStockLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS SAP 库存(sap同步的原始数据日志)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-stock-log:delete')")
    public CommonResult<Boolean> deleteIotSapStockLog(@RequestParam("id") Long id) {
        iotSapStockLogService.deleteIotSapStockLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS SAP 库存(sap同步的原始数据日志)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-stock-log:query')")
    public CommonResult<IotSapStockLogRespVO> getIotSapStockLog(@RequestParam("id") Long id) {
        IotSapStockLogDO iotSapStockLog = iotSapStockLogService.getIotSapStockLog(id);
        return success(BeanUtils.toBean(iotSapStockLog, IotSapStockLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS SAP 库存(sap同步的原始数据日志)分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-stock-log:query')")
    public CommonResult<PageResult<IotSapStockLogRespVO>> getIotSapStockLogPage(@Valid IotSapStockLogPageReqVO pageReqVO) {
        PageResult<IotSapStockLogDO> pageResult = iotSapStockLogService.getIotSapStockLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotSapStockLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS SAP 库存(sap同步的原始数据日志) Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-stock-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotSapStockLogExcel(@Valid IotSapStockLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotSapStockLogDO> list = iotSapStockLogService.getIotSapStockLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS SAP 库存(sap同步的原始数据日志).xls", "数据", IotSapStockLogRespVO.class,
                        BeanUtils.toBean(list, IotSapStockLogRespVO.class));
    }

}