package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssAwsDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssAwsDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssAwsBO;
import com.schisandra.oss.domain.service.SchisandraOssAwsDomainService;
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 21:48:16
 */
@RestController
@RequestMapping("/oss/aws/")
@Slf4j
public class SchisandraOssAwsController {

    @Resource
    private SchisandraOssAwsDomainService schisandraOssAwsDomainService;

    /**
     * 新增亚马逊对象存储配置表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssAwsDTO schisandraOssAwsDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssAwsController.add.dto:{}", JSON.toJSONString(schisandraOssAwsDTO));
            }
            Preconditions.checkNotNull(schisandraOssAwsDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getBasePath(), "数据存储路径不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getBucketName(), "Bucket名称不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getSecretAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getRegion(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getMode(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getAccelerateModeEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getChecksumValidationEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getMultiRegionEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getChunkedEncodingEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getPathStyleAccessEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUseArnRegionEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getFipsEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getDualstackEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssAwsBO SchisandraOssAwsBO = SchisandraOssAwsDTOConverter.INSTANCE.convertDTOToBO(schisandraOssAwsDTO);
            return Result.ok(schisandraOssAwsDomainService.add(SchisandraOssAwsBO));
        } catch (Exception e) {
            log.error("SchisandraOssAwsController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增亚马逊对象存储配置表失败");
        }

    }

    /**
     * 修改亚马逊对象存储配置表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssAwsDTO schisandraOssAwsDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssAwsController.update.dto:{}", JSON.toJSONString(schisandraOssAwsDTO));
            }
            Preconditions.checkNotNull(schisandraOssAwsDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getBasePath(), "数据存储路径不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getBucketName(), "Bucket名称不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getSecretAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getRegion(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getMode(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getAccelerateModeEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getChecksumValidationEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getMultiRegionEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getChunkedEncodingEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getPathStyleAccessEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUseArnRegionEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getFipsEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getDualstackEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssAwsBO schisandraOssAwsBO = SchisandraOssAwsDTOConverter.INSTANCE.convertDTOToBO(schisandraOssAwsDTO);
            return Result.ok(schisandraOssAwsDomainService.update(schisandraOssAwsBO));
        } catch (Exception e) {
            log.error("SchisandraOssAwsController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新亚马逊对象存储配置表信息失败");
        }

    }

    /**
     * 删除亚马逊对象存储配置表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssAwsDTO schisandraOssAwsDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssAwsController.delete.dto:{}", JSON.toJSONString(schisandraOssAwsDTO));
            }
            Preconditions.checkNotNull(schisandraOssAwsDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getBasePath(), "数据存储路径不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getBucketName(), "Bucket名称不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getSecretAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getRegion(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getMode(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getAccelerateModeEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getChecksumValidationEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getMultiRegionEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getChunkedEncodingEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getPathStyleAccessEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUseArnRegionEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getFipsEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getDualstackEnabled(), "不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssAwsDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssAwsBO schisandraOssAwsBO = SchisandraOssAwsDTOConverter.INSTANCE.convertDTOToBO(schisandraOssAwsDTO);
            return Result.ok(schisandraOssAwsDomainService.delete(schisandraOssAwsBO));
        } catch (Exception e) {
            log.error("SchisandraOssAwsController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除亚马逊对象存储配置表信息失败");
        }

    }

}
