package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssJdbcDataDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssJdbcDataDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssJdbcDataBO;
import com.schisandra.oss.domain.service.SchisandraOssJdbcDataDomainService;
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:55
 */
@RestController
@RequestMapping("/oss/jdbc/data/")
@Slf4j
public class SchisandraOssJdbcDataController {

    @Resource
    private SchisandraOssJdbcDataDomainService schisandraOssJdbcDataDomainService;

    /**
     * 新增jdbc存储数据表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssJdbcDataDTO schisandraOssJdbcDataDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcDataController.add.dto:{}", JSON.toJSONString(schisandraOssJdbcDataDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getData(), "数据不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcDataBO SchisandraOssJdbcDataBO = SchisandraOssJdbcDataDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcDataDTO);
            return Result.ok(schisandraOssJdbcDataDomainService.add(SchisandraOssJdbcDataBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcDataController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增jdbc存储数据表失败");
        }

    }

    /**
     * 修改jdbc存储数据表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssJdbcDataDTO schisandraOssJdbcDataDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcDataController.update.dto:{}", JSON.toJSONString(schisandraOssJdbcDataDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getData(), "数据不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcDataBO schisandraOssJdbcDataBO = SchisandraOssJdbcDataDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcDataDTO);
            return Result.ok(schisandraOssJdbcDataDomainService.update(schisandraOssJdbcDataBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcDataController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新jdbc存储数据表信息失败");
        }

    }

    /**
     * 删除jdbc存储数据表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssJdbcDataDTO schisandraOssJdbcDataDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcDataController.delete.dto:{}", JSON.toJSONString(schisandraOssJdbcDataDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getData(), "数据不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcDataDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcDataBO schisandraOssJdbcDataBO = SchisandraOssJdbcDataDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcDataDTO);
            return Result.ok(schisandraOssJdbcDataDomainService.delete(schisandraOssJdbcDataBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcDataController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除jdbc存储数据表信息失败");
        }

    }

}
