package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssWangyiDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssWangyiDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssWangyiBO;
import com.schisandra.oss.domain.service.SchisandraOssWangyiDomainService;
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:31:37
 */
@RestController
@RequestMapping("/oss/wangyi/")
@Slf4j
public class SchisandraOssWangyiController {

    @Resource
    private SchisandraOssWangyiDomainService schisandraOssWangyiDomainService;

    /**
     * 新增网易数帆对象存储配置表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssWangyiDTO schisandraOssWangyiDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssWangyiController.add.dto:{}", JSON.toJSONString(schisandraOssWangyiDTO));
            }
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSecretKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getEndpoint(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getConnectionTimeout(), "连接超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getMaxConnections(), "最大连接池大小。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getMaxErrorRetry(), "最大失败重试次数不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getIsSubDomain(), "是否使用子域名不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProtocol(), "连接OSS所采用的协议（HTTP或HTTPS），默认为HTTP。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyDomain(), "代理服务器的域，该域可以执行NTLM认证不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyHost(), "代理服务器主机地址不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyPassword(), "代理服务器验证的密码不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyPort(), "代理服务器端口不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyUsername(), "代理服务器验证的用户名不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyWorkstation(), "代理主机的NTLM身份验证服务器不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketTimeout(), "Socket层传输数据的超时时间（单位：毫秒）。默认为50000毫秒。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUserAgent(), "用户代理，指HTTP的User-Agent头不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketReceiveBufferSizeHint(), "Socket接收缓冲区的大小提示(以字节为单位)。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketSendBufferSizeHint(), "Socket发送缓冲区的大小提示(以字节为单位)。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssWangyiBO SchisandraOssWangyiBO = SchisandraOssWangyiDTOConverter.INSTANCE.convertDTOToBO(schisandraOssWangyiDTO);
            return Result.ok(schisandraOssWangyiDomainService.add(SchisandraOssWangyiBO));
        } catch (Exception e) {
            log.error("SchisandraOssWangyiController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增网易数帆对象存储配置表失败");
        }

    }

    /**
     * 修改网易数帆对象存储配置表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssWangyiDTO schisandraOssWangyiDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssWangyiController.update.dto:{}", JSON.toJSONString(schisandraOssWangyiDTO));
            }
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSecretKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getEndpoint(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getConnectionTimeout(), "连接超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getMaxConnections(), "最大连接池大小。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getMaxErrorRetry(), "最大失败重试次数不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getIsSubDomain(), "是否使用子域名不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProtocol(), "连接OSS所采用的协议（HTTP或HTTPS），默认为HTTP。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyDomain(), "代理服务器的域，该域可以执行NTLM认证不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyHost(), "代理服务器主机地址不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyPassword(), "代理服务器验证的密码不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyPort(), "代理服务器端口不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyUsername(), "代理服务器验证的用户名不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyWorkstation(), "代理主机的NTLM身份验证服务器不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketTimeout(), "Socket层传输数据的超时时间（单位：毫秒）。默认为50000毫秒。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUserAgent(), "用户代理，指HTTP的User-Agent头不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketReceiveBufferSizeHint(), "Socket接收缓冲区的大小提示(以字节为单位)。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketSendBufferSizeHint(), "Socket发送缓冲区的大小提示(以字节为单位)。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssWangyiBO schisandraOssWangyiBO = SchisandraOssWangyiDTOConverter.INSTANCE.convertDTOToBO(schisandraOssWangyiDTO);
            return Result.ok(schisandraOssWangyiDomainService.update(schisandraOssWangyiBO));
        } catch (Exception e) {
            log.error("SchisandraOssWangyiController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新网易数帆对象存储配置表信息失败");
        }

    }

    /**
     * 删除网易数帆对象存储配置表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssWangyiDTO schisandraOssWangyiDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssWangyiController.delete.dto:{}", JSON.toJSONString(schisandraOssWangyiDTO));
            }
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSecretKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getEndpoint(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getConnectionTimeout(), "连接超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getMaxConnections(), "最大连接池大小。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getMaxErrorRetry(), "最大失败重试次数不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getIsSubDomain(), "是否使用子域名不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProtocol(), "连接OSS所采用的协议（HTTP或HTTPS），默认为HTTP。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyDomain(), "代理服务器的域，该域可以执行NTLM认证不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyHost(), "代理服务器主机地址不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyPassword(), "代理服务器验证的密码不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyPort(), "代理服务器端口不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyUsername(), "代理服务器验证的用户名不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getProxyWorkstation(), "代理主机的NTLM身份验证服务器不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketTimeout(), "Socket层传输数据的超时时间（单位：毫秒）。默认为50000毫秒。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUserAgent(), "用户代理，指HTTP的User-Agent头不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketReceiveBufferSizeHint(), "Socket接收缓冲区的大小提示(以字节为单位)。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getSocketSendBufferSizeHint(), "Socket发送缓冲区的大小提示(以字节为单位)。不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssWangyiDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssWangyiBO schisandraOssWangyiBO = SchisandraOssWangyiDTOConverter.INSTANCE.convertDTOToBO(schisandraOssWangyiDTO);
            return Result.ok(schisandraOssWangyiDomainService.delete(schisandraOssWangyiBO));
        } catch (Exception e) {
            log.error("SchisandraOssWangyiController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除网易数帆对象存储配置表信息失败");
        }

    }

}
