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

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.iotrddailyreportdetail.vo.IotRdDailyReportDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotrddailyreportdetail.vo.IotRdDailyReportDetailRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotrddailyreportdetail.vo.IotRdDailyReportDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotrddailyreportdetail.IotRdDailyReportDetailDO;
import cn.iocoder.yudao.module.pms.service.iotrddailyreportdetail.IotRdDailyReportDetailService;
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-rd-daily-report-detail")
@Validated
public class IotRdDailyReportDetailController {

    @Resource
    private IotRdDailyReportDetailService iotRdDailyReportDetailService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得瑞都日报明细(生产动态拆分)分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report-detail:query')")
    public CommonResult<PageResult<IotRdDailyReportDetailRespVO>> getIotRdDailyReportDetailPage(@Valid IotRdDailyReportDetailPageReqVO pageReqVO) {
        PageResult<IotRdDailyReportDetailDO> pageResult = iotRdDailyReportDetailService.getIotRdDailyReportDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotRdDailyReportDetailRespVO.class));
    }

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

}