package com.cf.imes.module.report.controller.admin.dataset;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetReqVO;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetRespVO;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetSaveReqVO;
import com.cf.imes.module.report.dal.dataobject.dataset.ReportDatasetDO;
import com.cf.imes.module.report.service.dataset.ReportDatasetService;
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 jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.CREATE;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.UPDATE;

/**
 * 报表数据集控制器
 *
 * @author Gqr
 * @since 2024/7/8 15:22
 */
@Tag(name = "管理后台 - 报表数据集")
@RestController
@RequestMapping("/report")
@Validated
public class ReportDatasetController {
    @Resource
    private ReportDatasetService datasetService;

    @PutMapping("/dataset")
    @Operation(summary = "创建报表数据集")
    @OperateLog(type = CREATE)
    @PreAuthorize("@ss.hasAnyPermissions('system:report:datasource-connect','base:report:datasource-connect')")
    public CommonResult<Long> createDataset(@Valid @RequestBody ReportDatasetSaveReqVO createReqVO) {
        return success(datasetService.createDataset(createReqVO));
    }

    @PostMapping("/dataset")
    @Operation(summary = "更新报表数据集")
    @OperateLog(type = UPDATE)
    @PreAuthorize("@ss.hasAnyPermissions('system:report:datasource-connect','base:report:datasource-connect')")
    public CommonResult<Boolean> updateDataset(@Valid @RequestBody ReportDatasetSaveReqVO updateReqVO) {
        datasetService.updateDataset(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/dataset/{id}")
    @Operation(summary = "删除报表数据集")
    @Parameter(name = "id", description = "数据集id", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('system:report:datasource-connect','base:report:datasource-connect')")
    public CommonResult<Boolean> deleteDataset(@PathVariable("id") Long id) {
        datasetService.deleteDataset(id);
        return success(true);
    }

    @GetMapping("/dataset/{id}")
    @Operation(summary = "获取报表数据集")
    @Parameter(name = "id", description = "数据集id", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('report:design')")
    public CommonResult<ReportDatasetRespVO> getDataset(@PathVariable("id") Long id) {
        ReportDatasetDO dataset = datasetService.getDataset(id);
        return success(BeanUtils.toBean(dataset, ReportDatasetRespVO.class));
    }

    @GetMapping("/datasets")
    @Operation(summary = "获取数据源下的报表数据集列表")
    @PreAuthorize("@ss.hasPermission('report:design')")
    public CommonResult<List<ReportDatasetRespVO>> getDatasetPage(@RequestParam(value = "datasourceId") Long datasourceId,
                                                                  @RequestParam(value = "name", required = false) String name) {
        ReportDatasetReqVO reqVO = new ReportDatasetReqVO(name, datasourceId);
        return success(BeanUtils.toBean(datasetService.getDatasetList(reqVO), ReportDatasetRespVO.class));
    }
}
