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

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.laswelldata.vo.LasWellDataPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.laswelldata.vo.LasWellDataRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.laswelldata.vo.LasWellDataSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.laswelldata.vo.LasWellDataSetRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.laswelldata.LasWellDataDO;
import cn.iocoder.yudao.module.pms.service.laswelldata.LasWellDataService;
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-data")
@Validated
public class LasWellDataController {

    @Resource
    private LasWellDataService lasWellDataService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得数字油藏LAS数据文件-测井数据分页")
    @PreAuthorize("@ss.hasPermission('pms:las-well-data:query')")
    public CommonResult<PageResult<LasWellDataRespVO>> getLasWellDataPage(@Valid LasWellDataPageReqVO pageReqVO) {
        PageResult<LasWellDataDO> pageResult = lasWellDataService.getLasWellDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LasWellDataRespVO.class));
    }

    @GetMapping("/wellLogDataset")
    @Operation(summary = "获得数字油藏LAS数据文件-各项测井数据集合")
    @PreAuthorize("@ss.hasPermission('pms:las-well-data:query')")
    public CommonResult<LasWellDataSetRespVO> wellLogDataset(@Valid LasWellDataPageReqVO pageReqVO) {
        LasWellDataSetRespVO dataSet = lasWellDataService.wellLogDataset(pageReqVO);
        return success(dataSet);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数字油藏LAS数据文件-测井数据 Excel")
    @PreAuthorize("@ss.hasPermission('pms:las-well-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLasWellDataExcel(@Valid LasWellDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LasWellDataDO> list = lasWellDataService.getLasWellDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数字油藏LAS数据文件-测井数据.xls", "数据", LasWellDataRespVO.class,
                        BeanUtils.toBean(list, LasWellDataRespVO.class));
    }

}