package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssJdbcStoreDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssJdbcStoreDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssJdbcStoreBO;
import com.schisandra.oss.domain.service.SchisandraOssJdbcStoreDomainService;
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:11:22
 */
@RestController
@RequestMapping("/oss/jdbc/store/")
@Slf4j
public class SchisandraOssJdbcStoreController {

    @Resource
    private SchisandraOssJdbcStoreDomainService schisandraOssJdbcStoreDomainService;

    /**
     * 新增jdbc存储信息表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssJdbcStoreDTO schisandraOssJdbcStoreDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcStoreController.add.dto:{}", JSON.toJSONString(schisandraOssJdbcStoreDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getId(), "主键不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getName(), "文件/目录名不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getPath(), "路径不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getSize(), "文件大小不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getParentId(), "父级目录ID不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getType(), "类型(D:目录;F:文件)不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getDataId(), "数据ID不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcStoreBO SchisandraOssJdbcStoreBO = SchisandraOssJdbcStoreDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcStoreDTO);
            return Result.ok(schisandraOssJdbcStoreDomainService.add(SchisandraOssJdbcStoreBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcStoreController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增jdbc存储信息表失败");
        }

    }

    /**
     * 修改jdbc存储信息表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssJdbcStoreDTO schisandraOssJdbcStoreDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcStoreController.update.dto:{}", JSON.toJSONString(schisandraOssJdbcStoreDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getId(), "主键不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getName(), "文件/目录名不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getPath(), "路径不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getSize(), "文件大小不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getParentId(), "父级目录ID不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getType(), "类型(D:目录;F:文件)不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getDataId(), "数据ID不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcStoreBO schisandraOssJdbcStoreBO = SchisandraOssJdbcStoreDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcStoreDTO);
            return Result.ok(schisandraOssJdbcStoreDomainService.update(schisandraOssJdbcStoreBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcStoreController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新jdbc存储信息表信息失败");
        }

    }

    /**
     * 删除jdbc存储信息表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssJdbcStoreDTO schisandraOssJdbcStoreDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJdbcStoreController.delete.dto:{}", JSON.toJSONString(schisandraOssJdbcStoreDTO));
            }
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getId(), "主键不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getName(), "文件/目录名不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getPath(), "路径不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getSize(), "文件大小不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getParentId(), "父级目录ID不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getType(), "类型(D:目录;F:文件)不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getDataId(), "数据ID不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJdbcStoreDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssJdbcStoreBO schisandraOssJdbcStoreBO = SchisandraOssJdbcStoreDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJdbcStoreDTO);
            return Result.ok(schisandraOssJdbcStoreDomainService.delete(schisandraOssJdbcStoreBO));
        } catch (Exception e) {
            log.error("SchisandraOssJdbcStoreController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除jdbc存储信息表信息失败");
        }

    }

}
