package cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency;

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.qhse.emergency.vo.QhseEmergencyBookPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency.vo.QhseEmergencyBookRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency.vo.QhseEmergencyBookSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.emergency.QhseEmergencyBookDO;
import cn.iocoder.yudao.module.pms.service.qhse.emergency.QhseEmergencyBookService;
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 = "管理后台 - QHSE应检物资台账")
@RestController
@RequestMapping("/rq/qhse-emergency-book")
@Validated
public class QhseEmergencyBookController {

    @Resource
    private QhseEmergencyBookService qhseEmergencyBookService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE应检物资台账")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-book:create')")
    public CommonResult<Long> createQhseEmergencyBook(@Valid @RequestBody QhseEmergencyBookSaveReqVO createReqVO) {
        return success(qhseEmergencyBookService.createQhseEmergencyBook(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE应检物资台账")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-book:update')")
    public CommonResult<Boolean> updateQhseEmergencyBook(@Valid @RequestBody QhseEmergencyBookSaveReqVO updateReqVO) {
        qhseEmergencyBookService.updateQhseEmergencyBook(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE应检物资台账")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-book:delete')")
    public CommonResult<Boolean> deleteQhseEmergencyBook(@RequestParam("id") Long id) {
        qhseEmergencyBookService.deleteQhseEmergencyBook(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE应检物资台账")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-book:query')")
    public CommonResult<QhseEmergencyBookRespVO> getQhseEmergencyBook(@RequestParam("id") Long id) {
        QhseEmergencyBookDO qhseEmergencyBook = qhseEmergencyBookService.getQhseEmergencyBook(id);
        return success(BeanUtils.toBean(qhseEmergencyBook, QhseEmergencyBookRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE应检物资台账分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-book:query')")
    public CommonResult<PageResult<QhseEmergencyBookRespVO>> getQhseEmergencyBookPage(@Valid QhseEmergencyBookPageReqVO pageReqVO) {
        PageResult<QhseEmergencyBookDO> pageResult = qhseEmergencyBookService.getQhseEmergencyBookPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhseEmergencyBookRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE应检物资台账 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-book:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseEmergencyBookExcel(@Valid QhseEmergencyBookPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseEmergencyBookDO> list = qhseEmergencyBookService.getQhseEmergencyBookPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE应检物资台账.xls", "数据", QhseEmergencyBookRespVO.class,
                        BeanUtils.toBean(list, QhseEmergencyBookRespVO.class));
    }

}