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

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.laswell.vo.LasWellPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.laswell.vo.LasWellRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.laswell.vo.LasWellSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.laswell.LasWellDO;
import cn.iocoder.yudao.module.pms.service.laswell.LasWellService;
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 = "管理后台 - 数字油藏LAS数据文件-井信息")
@RestController
@RequestMapping("/pms/las-well")
@Validated
public class LasWellController {

    @Resource
    private LasWellService lasWellService;

    @PostMapping("/create")
    @Operation(summary = "创建数字油藏LAS数据文件-井信息")
    @PreAuthorize("@ss.hasPermission('pms:las-well:create')")
    public CommonResult<Long> createLasWell(@Valid @RequestBody LasWellSaveReqVO createReqVO) {
        return success(lasWellService.createLasWell(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数字油藏LAS数据文件-井信息")
    @PreAuthorize("@ss.hasPermission('pms:las-well:update')")
    public CommonResult<Boolean> updateLasWell(@Valid @RequestBody LasWellSaveReqVO updateReqVO) {
        lasWellService.updateLasWell(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数字油藏LAS数据文件-井信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:las-well:delete')")
    public CommonResult<Boolean> deleteLasWell(@RequestParam("id") Long id) {
        lasWellService.deleteLasWell(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数字油藏LAS数据文件-井信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:las-well:query')")
    public CommonResult<LasWellRespVO> getLasWell(@RequestParam("id") Long id) {
        LasWellDO lasWell = lasWellService.getLasWell(id);
        return success(BeanUtils.toBean(lasWell, LasWellRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数字油藏LAS数据文件-井信息分页")
    @PreAuthorize("@ss.hasPermission('pms:las-well:query')")
    public CommonResult<PageResult<LasWellRespVO>> getLasWellPage(@Valid LasWellPageReqVO pageReqVO) {
        PageResult<LasWellDO> pageResult = lasWellService.getLasWellPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LasWellRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数字油藏LAS数据文件-井信息 Excel")
    @PreAuthorize("@ss.hasPermission('pms:las-well:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLasWellExcel(@Valid LasWellPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LasWellDO> list = lasWellService.getLasWellPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数字油藏LAS数据文件-井信息.xls", "数据", LasWellRespVO.class,
                        BeanUtils.toBean(list, LasWellRespVO.class));
    }

}