package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssJdbcDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssJdbcDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssJdbcBO;
import com.schisandra.oss.domain.service.SchisandraOssJdbcDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * jdbc存储配置表 controller
 *
 * @author landaiqing
 * @since 2024-06-02 22:08:16
 */
@RestController
@RequestMapping("/oss/jdbc/")
@Slf4j
public class SchisandraOssJdbcController {

    @Resource
    private SchisandraOssJdbcDomainService schisandraOssJdbcDomainService;

    /**
     * 新增jdbc存储配置表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssJdbcDTO schisandraOssJdbcDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcController.add.dto:{}", JSON.toJSONString(schisandraOssJdbcDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getDataSourceName(), "系统数据源Bean名称(适用于系统多数据源配置)不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUrl(), "对象存储数据源不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getType(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getDriver(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUsername(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getPassword(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcBO SchisandraOssJdbcBO = SchisandraOssJdbcDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcDTO);
            return Result.ok(schisandraOssJdbcDomainService.add(SchisandraOssJdbcBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增jdbc存储配置表失败");
        }

    }

    /**
     * 修改jdbc存储配置表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssJdbcDTO schisandraOssJdbcDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcController.update.dto:{}", JSON.toJSONString(schisandraOssJdbcDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getDataSourceName(), "系统数据源Bean名称(适用于系统多数据源配置)不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUrl(), "对象存储数据源不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getType(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getDriver(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUsername(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getPassword(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcBO schisandraOssJdbcBO = SchisandraOssJdbcDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcDTO);
            return Result.ok(schisandraOssJdbcDomainService.update(schisandraOssJdbcBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新jdbc存储配置表信息失败");
        }

    }

    /**
     * 删除jdbc存储配置表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssJdbcDTO schisandraOssJdbcDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcController.delete.dto:{}", JSON.toJSONString(schisandraOssJdbcDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getDataSourceName(), "系统数据源Bean名称(适用于系统多数据源配置)不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUrl(), "对象存储数据源不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getType(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getDriver(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUsername(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getPassword(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcBO schisandraOssJdbcBO = SchisandraOssJdbcDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcDTO);
            return Result.ok(schisandraOssJdbcDomainService.delete(schisandraOssJdbcBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除jdbc存储配置表信息失败");
        }

    }

}
