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

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.iotryimprovedailyreportdetail.vo.IotRyImproveDailyReportDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryimprovedailyreportdetail.vo.IotRyImproveDailyReportDetailRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryimprovedailyreportdetail.vo.IotRyImproveDailyReportDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotryimprovedailyreportdetail.IotRyImproveDailyReportDetailDO;
import cn.iocoder.yudao.module.pms.service.iotryimprovedailyreportdetail.IotRyImproveDailyReportDetailService;
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-ry-improve-daily-report-detail")
@Validated
public class IotRyImproveDailyReportDetailController {

    @Resource
    private IotRyImproveDailyReportDetailService iotRyImproveDailyReportDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建瑞鹰设备整改日报明细(生产动态拆分)")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report-detail:create')")
    public CommonResult<Long> createIotRyImproveDailyReportDetail(@Valid @RequestBody IotRyImproveDailyReportDetailSaveReqVO createReqVO) {
        return success(iotRyImproveDailyReportDetailService.createIotRyImproveDailyReportDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新瑞鹰设备整改日报明细(生产动态拆分)")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report-detail:update')")
    public CommonResult<Boolean> updateIotRyImproveDailyReportDetail(@Valid @RequestBody IotRyImproveDailyReportDetailSaveReqVO updateReqVO) {
        iotRyImproveDailyReportDetailService.updateIotRyImproveDailyReportDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除瑞鹰设备整改日报明细(生产动态拆分)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report-detail:delete')")
    public CommonResult<Boolean> deleteIotRyImproveDailyReportDetail(@RequestParam("id") Long id) {
        iotRyImproveDailyReportDetailService.deleteIotRyImproveDailyReportDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得瑞鹰设备整改日报明细(生产动态拆分)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report-detail:query')")
    public CommonResult<IotRyImproveDailyReportDetailRespVO> getIotRyImproveDailyReportDetail(@RequestParam("id") Long id) {
        IotRyImproveDailyReportDetailDO iotRyImproveDailyReportDetail = iotRyImproveDailyReportDetailService.getIotRyImproveDailyReportDetail(id);
        return success(BeanUtils.toBean(iotRyImproveDailyReportDetail, IotRyImproveDailyReportDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得瑞鹰设备整改日报明细(生产动态拆分)分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report-detail:query')")
    public CommonResult<PageResult<IotRyImproveDailyReportDetailRespVO>> getIotRyImproveDailyReportDetailPage(@Valid IotRyImproveDailyReportDetailPageReqVO pageReqVO) {
        PageResult<IotRyImproveDailyReportDetailDO> pageResult = iotRyImproveDailyReportDetailService.getIotRyImproveDailyReportDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotRyImproveDailyReportDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出瑞鹰设备整改日报明细(生产动态拆分) Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotRyImproveDailyReportDetailExcel(@Valid IotRyImproveDailyReportDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotRyImproveDailyReportDetailDO> list = iotRyImproveDailyReportDetailService.getIotRyImproveDailyReportDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "瑞鹰设备整改日报明细(生产动态拆分).xls", "数据", IotRyImproveDailyReportDetailRespVO.class,
                        BeanUtils.toBean(list, IotRyImproveDailyReportDetailRespVO.class));
    }

}