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

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.iotdailyreportfuel.vo.IotDailyReportFuelPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdailyreportfuel.vo.IotDailyReportFuelRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdailyreportfuel.vo.IotDailyReportFuelSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdailyreportfuel.IotDailyReportFuelDO;
import cn.iocoder.yudao.module.pms.service.iotdailyreportfuel.IotDailyReportFuelService;
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-daily-report-fuel")
@Validated
public class IotDailyReportFuelController {

    @Resource
    private IotDailyReportFuelService iotDailyReportFuelService;

    @PostMapping("/create")
    @Operation(summary = "创建日报-油耗")
    @PreAuthorize("@ss.hasPermission('pms:iot-daily-report-fuel:create')")
    public CommonResult<Long> createIotDailyReportFuel(@Valid @RequestBody IotDailyReportFuelSaveReqVO createReqVO) {
        return success(iotDailyReportFuelService.createIotDailyReportFuel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新日报-油耗")
    @PreAuthorize("@ss.hasPermission('pms:iot-daily-report-fuel:update')")
    public CommonResult<Boolean> updateIotDailyReportFuel(@Valid @RequestBody IotDailyReportFuelSaveReqVO updateReqVO) {
        iotDailyReportFuelService.updateIotDailyReportFuel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除日报-油耗")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-daily-report-fuel:delete')")
    public CommonResult<Boolean> deleteIotDailyReportFuel(@RequestParam("id") Long id) {
        iotDailyReportFuelService.deleteIotDailyReportFuel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日报-油耗")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-daily-report-fuel:query')")
    public CommonResult<IotDailyReportFuelRespVO> getIotDailyReportFuel(@RequestParam("id") Long id) {
        IotDailyReportFuelDO iotDailyReportFuel = iotDailyReportFuelService.getIotDailyReportFuel(id);
        return success(BeanUtils.toBean(iotDailyReportFuel, IotDailyReportFuelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得日报-油耗分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-daily-report-fuel:query')")
    public CommonResult<PageResult<IotDailyReportFuelRespVO>> getIotDailyReportFuelPage(@Valid IotDailyReportFuelPageReqVO pageReqVO) {
        PageResult<IotDailyReportFuelDO> pageResult = iotDailyReportFuelService.getIotDailyReportFuelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDailyReportFuelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出日报-油耗 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-daily-report-fuel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDailyReportFuelExcel(@Valid IotDailyReportFuelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDailyReportFuelDO> list = iotDailyReportFuelService.getIotDailyReportFuelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "日报-油耗.xls", "数据", IotDailyReportFuelRespVO.class,
                        BeanUtils.toBean(list, IotDailyReportFuelRespVO.class));
    }

}