package com.cf.imes.module.system.controller.admin.datasource;

import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import static com.cf.imes.framework.common.pojo.CommonResult.success;

import com.cf.imes.framework.excel.core.util.ExcelUtils;

import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cf.imes.module.system.controller.admin.datasource.vo.*;
import com.cf.imes.module.system.dal.dataobject.datasource.DataSourceDO;
import com.cf.imes.module.system.service.datasource.DataSourceService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 系统数据源")
@RestController
@RequestMapping("/system/data-source")
@Validated
public class DataSourceController {

    @Resource
    private DataSourceService dataSourceService;

   /* @PostMapping("/create")
    @Operation(summary = "创建系统数据源")
    @PreAuthorize("@ss.hasPermission('system:data-source:create')")
    public CommonResult<Long> createDataSource(@Valid @RequestBody DataSourceSaveReqVO createReqVO) {
        return success(dataSourceService.createDataSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统数据源")
    @PreAuthorize("@ss.hasPermission('system:data-source:update')")
    public CommonResult<Boolean> updateDataSource(@Valid @RequestBody DataSourceSaveReqVO updateReqVO) {
        dataSourceService.updateDataSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统数据源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:data-source:delete')")
    public CommonResult<Boolean> deleteDataSource(@RequestParam("id") Long id) {
        dataSourceService.deleteDataSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统数据源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:data-source:query')")
    public CommonResult<DataSourceRespVO> getDataSource(@RequestParam("id") Long id) {
        DataSourceDO dataSource = dataSourceService.getDataSource(id);
        return success(BeanUtils.toBean(dataSource, DataSourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统数据源分页")
    @PreAuthorize("@ss.hasPermission('system:data-source:query')")
    public CommonResult<PageResult<DataSourceRespVO>> getDataSourcePage(@Valid DataSourcePageReqVO pageReqVO) {
        PageResult<DataSourceDO> pageResult = dataSourceService.getDataSourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataSourceRespVO.class));
    }*/

    @GetMapping("/list")
    @Operation(summary = "获得系统数据源列表")
    //@PreAuthorize("@ss.hasPermission('system:data-source:query')")
    public CommonResult<List<DataSourceRespVO>> list( ) {
        return success(dataSourceService.list());

        /*List<DataSourceDO> list =
        return success(BeanUtils.toBean(list, DataSourceRespVO.class));*/
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统数据源 Excel")
    @PreAuthorize("@ss.hasPermission('system:data-source:export')")
    @OperateLog(type = EXPORT)
    public void exportDataSourceExcel(@Valid DataSourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataSourceDO> list = dataSourceService.getDataSourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "系统数据源.xls", "数据", DataSourceRespVO.class,
                        BeanUtils.toBean(list, DataSourceRespVO.class));
    }

}