package com.lideeyunji.service.infra.controller;

import com.lideeyunji.tool.framework.common.pojo.CommonResult;
import com.lideeyunji.tool.framework.common.util.object.BeanUtils;
import com.lideeyunji.service.infra.controller.vo.db.DataSourceConfigRespVO;
import com.lideeyunji.service.infra.controller.vo.db.DataSourceConfigSaveReqVO;
import com.lideeyunji.service.infra.entity.DataSourceConfigDO;
import com.lideeyunji.service.infra.enums.ErrorCodeConstants;
import com.lideeyunji.service.infra.service.IDataSourceConfigService;
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.validation.Valid;
import java.util.List;

import static com.lideeyunji.tool.framework.common.pojo.CommonResult.error;
import static com.lideeyunji.tool.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 数据源配置")
@RestController
@RequestMapping("/infra/data-source-config")
@Validated
public class DataSourceConfigController {

    @Resource
    private IDataSourceConfigService dataSourceConfigService;

    @PostMapping("/create")
    @Operation(tags = "数据库文档",summary = "创建数据源配置")
    @PreAuthorize("@ss.hasPermission('infra:data-source-config:create')")
    public CommonResult<Long> createDataSourceConfig(@Valid @RequestBody DataSourceConfigSaveReqVO createReqVO) {
        String dbCode = createReqVO.getDbCode();//校验编码是否存在
        if(dataSourceConfigService.codeIsExist(dbCode)){
            return error(ErrorCodeConstants.DATA_SOURCE_CONFIG_CODE_ISEXIT);
        }
        return success(dataSourceConfigService.createDataSourceConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(tags = "数据库文档",summary = "更新数据源配置")
    @PreAuthorize("@ss.hasPermission('infra:data-source-config:update')")
    public CommonResult<Boolean> updateDataSourceConfig(@Valid @RequestBody DataSourceConfigSaveReqVO updateReqVO) {
        dataSourceConfigService.updateDataSourceConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(tags = "数据库文档",summary = "删除数据源配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:data-source-config:delete')")
    public CommonResult<Boolean> deleteDataSourceConfig(@RequestParam("id") Long id) {
        dataSourceConfigService.deleteDataSourceConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(tags = "数据库文档",summary = "获得数据源配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:data-source-config:query')")
    public CommonResult<DataSourceConfigRespVO> getDataSourceConfig(@RequestParam("id") Long id) {
        DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(id);
        return success(BeanUtils.toBean(config, DataSourceConfigRespVO.class));
    }

    @GetMapping("/list")
    @Operation(tags = "数据库文档",summary = "获得数据源配置列表")
    @PreAuthorize("@ss.hasPermission('infra:data-source-config:query')")
    public CommonResult<List<DataSourceConfigRespVO>> getDataSourceConfigList() {
        List<DataSourceConfigDO> list = dataSourceConfigService.getDataSourceConfigList();
        List<DataSourceConfigRespVO> voList = BeanUtils.toBean(list, DataSourceConfigRespVO.class);
        for(DataSourceConfigRespVO vo:voList){
            String dbCode = vo.getDbCode();
            boolean connect = dataSourceConfigService.isConnect(dbCode);
            vo.setIsConnect(connect?"Y":"N");
        }
        return success(voList);
    }

}
