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

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.iotrydailyreportdetail.vo.IotRyDailyReportDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotrydailyreportdetail.vo.IotRyDailyReportDetailRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotrydailyreportdetail.vo.IotRyDailyReportDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotrydailyreportdetail.IotRyDailyReportDetailDO;
import cn.iocoder.yudao.module.pms.service.iotrydailyreportdetail.IotRyDailyReportDetailService;
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-daily-report-detail")
@Validated
public class IotRyDailyReportDetailController {

    @Resource
    private IotRyDailyReportDetailService iotRyDailyReportDetailService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得瑞鹰日报明细(生产动态拆分)分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-daily-report-detail:query')")
    public CommonResult<PageResult<IotRyDailyReportDetailRespVO>> getIotRyDailyReportDetailPage(@Valid IotRyDailyReportDetailPageReqVO pageReqVO) {
        PageResult<IotRyDailyReportDetailDO> pageResult = iotRyDailyReportDetailService.getIotRyDailyReportDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotRyDailyReportDetailRespVO.class));
    }

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

}