package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssLocalDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssLocalDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssLocalBO;
import com.schisandra.oss.domain.service.SchisandraOssLocalDomainService;
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;

/**
 * 本地存储配置表 controller
 *
 * @author landaiqing
 * @since 2024-06-02 22:18:08
 */
@RestController
@RequestMapping("/oss/local/")
@Slf4j
public class SchisandraOssLocalController {

    @Resource
    private SchisandraOssLocalDomainService schisandraOssLocalDomainService;

    /**
     * 新增本地存储配置表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssLocalDTO schisandraOssLocalDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssLocalController.add.dto:{}", JSON.toJSONString(schisandraOssLocalDTO));
            }
            Preconditions.checkNotNull(schisandraOssLocalDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssLocalBO SchisandraOssLocalBO = SchisandraOssLocalDTOConverter.INSTANCE.convertDTOToBO(schisandraOssLocalDTO);
            return Result.ok(schisandraOssLocalDomainService.add(SchisandraOssLocalBO));
        } catch (Exception e) {
            log.error("SchisandraOssLocalController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增本地存储配置表失败");
        }

    }

    /**
     * 修改本地存储配置表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssLocalDTO schisandraOssLocalDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssLocalController.update.dto:{}", JSON.toJSONString(schisandraOssLocalDTO));
            }
            Preconditions.checkNotNull(schisandraOssLocalDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssLocalBO schisandraOssLocalBO = SchisandraOssLocalDTOConverter.INSTANCE.convertDTOToBO(schisandraOssLocalDTO);
            return Result.ok(schisandraOssLocalDomainService.update(schisandraOssLocalBO));
        } catch (Exception e) {
            log.error("SchisandraOssLocalController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新本地存储配置表信息失败");
        }

    }

    /**
     * 删除本地存储配置表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssLocalDTO schisandraOssLocalDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssLocalController.delete.dto:{}", JSON.toJSONString(schisandraOssLocalDTO));
            }
            Preconditions.checkNotNull(schisandraOssLocalDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssLocalDTO.getExtraJson(), "额外字段不能为空");
            SchisandraOssLocalBO schisandraOssLocalBO = SchisandraOssLocalDTOConverter.INSTANCE.convertDTOToBO(schisandraOssLocalDTO);
            return Result.ok(schisandraOssLocalDomainService.delete(schisandraOssLocalBO));
        } catch (Exception e) {
            log.error("SchisandraOssLocalController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除本地存储配置表信息失败");
        }

    }

}
