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

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.iotrddailyreportitem.vo.IotRdDailyReportItemPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotrddailyreportitem.vo.IotRdDailyReportItemRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotrddailyreportitem.vo.IotRdDailyReportItemSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotrddailyreportitem.IotRdDailyReportItemDO;
import cn.iocoder.yudao.module.pms.service.iotrddailyreportitem.IotRdDailyReportItemService;
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-item")
@Validated
public class IotRdDailyReportItemController {

    @Resource
    private IotRdDailyReportItemService iotRdDailyReportItemService;

    @PostMapping("/create")
    @Operation(summary = "创建瑞都日报 (工作量)明细")
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report-item:create')")
    public CommonResult<Long> createIotRdDailyReportItem(@Valid @RequestBody IotRdDailyReportItemSaveReqVO createReqVO) {
        return success(iotRdDailyReportItemService.createIotRdDailyReportItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新瑞都日报 (工作量)明细")
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report-item:update')")
    public CommonResult<Boolean> updateIotRdDailyReportItem(@Valid @RequestBody IotRdDailyReportItemSaveReqVO updateReqVO) {
        iotRdDailyReportItemService.updateIotRdDailyReportItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除瑞都日报 (工作量)明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report-item:delete')")
    public CommonResult<Boolean> deleteIotRdDailyReportItem(@RequestParam("id") Long id) {
        iotRdDailyReportItemService.deleteIotRdDailyReportItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得瑞都日报 (工作量)明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report-item:query')")
    public CommonResult<IotRdDailyReportItemRespVO> getIotRdDailyReportItem(@RequestParam("id") Long id) {
        IotRdDailyReportItemDO iotRdDailyReportItem = iotRdDailyReportItemService.getIotRdDailyReportItem(id);
        return success(BeanUtils.toBean(iotRdDailyReportItem, IotRdDailyReportItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得瑞都日报 (工作量)明细分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report-item:query')")
    public CommonResult<PageResult<IotRdDailyReportItemRespVO>> getIotRdDailyReportItemPage(@Valid IotRdDailyReportItemPageReqVO pageReqVO) {
        PageResult<IotRdDailyReportItemDO> pageResult = iotRdDailyReportItemService.getIotRdDailyReportItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotRdDailyReportItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出瑞都日报 (工作量)明细 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotRdDailyReportItemExcel(@Valid IotRdDailyReportItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotRdDailyReportItemDO> list = iotRdDailyReportItemService.getIotRdDailyReportItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "瑞都日报 (工作量)明细.xls", "数据", IotRdDailyReportItemRespVO.class,
                        BeanUtils.toBean(list, IotRdDailyReportItemRespVO.class));
    }

}