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

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.ly.vo.LyDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.ly.vo.LyDeviceRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.ly.vo.LyDeviceSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.ly.LyDeviceDO;
import cn.iocoder.yudao.module.pms.service.ly.LyDeviceService;
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("/rq/ly-device")
@Validated
public class LyDeviceController {

    @Resource
    private LyDeviceService lyDeviceService;


    @PostMapping("/create")
    @Operation(summary = "创建连油设备信息")
    @PreAuthorize("@ss.hasPermission('rq:ly-device:create')")
    public CommonResult<Long> createLyDevice(@Valid @RequestBody LyDeviceSaveReqVO createReqVO) {
        return success(lyDeviceService.createLyDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新连油设备信息")
    @PreAuthorize("@ss.hasPermission('rq:ly-device:update')")
    public CommonResult<Boolean> updateLyDevice(@Valid @RequestBody LyDeviceSaveReqVO updateReqVO) {
        lyDeviceService.updateLyDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除连油设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:ly-device:delete')")
    public CommonResult<Boolean> deleteLyDevice(@RequestParam("id") Long id) {
        lyDeviceService.deleteLyDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得连油设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:ly-device:query')")
    public CommonResult<LyDeviceRespVO> getLyDevice(@RequestParam("id") Long id) {
        LyDeviceDO lyDevice = lyDeviceService.getLyDevice(id);
        return success(BeanUtils.toBean(lyDevice, LyDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得连油设备信息分页")
    @PreAuthorize("@ss.hasPermission('rq:ly-device:query')")
    public CommonResult<PageResult<LyDeviceRespVO>> getLyDevicePage(@Valid LyDevicePageReqVO pageReqVO) {
        PageResult<LyDeviceDO> pageResult = lyDeviceService.getLyDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LyDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出连油设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('rq:ly-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLyDeviceExcel(@Valid LyDevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LyDeviceDO> list = lyDeviceService.getLyDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "连油设备信息.xls", "数据", LyDeviceRespVO.class,
                        BeanUtils.toBean(list, LyDeviceRespVO.class));
    }

}