package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssBaiduDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssBaiduDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssBaiduBO;
import com.schisandra.oss.domain.service.SchisandraOssBaiduDomainService;
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:02:15
 */
@RestController
@RequestMapping("/oss/baidu/")
@Slf4j
public class SchisandraOssBaiduController {

    @Resource
    private SchisandraOssBaiduDomainService schisandraOssBaiduDomainService;

    /**
     * 新增百度云对象存储配置表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssBaiduDTO schisandraOssBaiduDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssBaiduController.add.dto:{}", JSON.toJSONString(schisandraOssBaiduDTO));
            }
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSecretAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCnameEnabled(), "使用cname访问BOS资源不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getEnableHttpAsyncPut(), "异步put不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getConnectionTimeoutInMillis(), "建立连接的超时时间（单位：毫秒）不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxConnections(), "允许打开的最大HTTP连接数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProtocol(), "连接协议类型不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyDomain(), "访问NTLM验证的代理服务器的Windows域名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyHost(), "代理服务器主机地址不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPassword(), "代理服务器验证的密码不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPort(), "代理服务器端口不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyUsername(), "代理服务器验证的用户名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyWorkstation(), "NTLM代理服务器的Windows工作站名称不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPreemptiveAuthenticationEnable(), "是否设置用户代理认证不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSocketTimeoutInMillis(), "通过打开的连接传输数据的超时时间（单位：毫秒）不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSocketBufferSizeInBytes(), "Socket缓冲区大小不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getEndpoint(), "访问域名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getRegion(), "地域不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getRedirectsEnabled(), "是否开启HTTP重定向。默认开启不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getLocalAddress(), "本地地址不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxErrorRetry(), "请求失败最大重试次数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxDelayInMillis(), "最大延迟时间，单位：毫秒不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getStreamBufferSize(), "流文件缓冲区大小不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUserAgent(), "用户代理，指HTTP的User-Agent头不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssBaiduBO SchisandraOssBaiduBO = SchisandraOssBaiduDTOConverter.INSTANCE.convertDTOToBO(schisandraOssBaiduDTO);
            return Result.ok(schisandraOssBaiduDomainService.add(SchisandraOssBaiduBO));
        } catch (Exception e) {
            log.error("SchisandraOssBaiduController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增百度云对象存储配置表失败");
        }

    }

    /**
     * 修改百度云对象存储配置表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssBaiduDTO schisandraOssBaiduDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssBaiduController.update.dto:{}", JSON.toJSONString(schisandraOssBaiduDTO));
            }
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSecretAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCnameEnabled(), "使用cname访问BOS资源不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getEnableHttpAsyncPut(), "异步put不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getConnectionTimeoutInMillis(), "建立连接的超时时间（单位：毫秒）不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxConnections(), "允许打开的最大HTTP连接数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProtocol(), "连接协议类型不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyDomain(), "访问NTLM验证的代理服务器的Windows域名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyHost(), "代理服务器主机地址不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPassword(), "代理服务器验证的密码不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPort(), "代理服务器端口不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyUsername(), "代理服务器验证的用户名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyWorkstation(), "NTLM代理服务器的Windows工作站名称不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPreemptiveAuthenticationEnable(), "是否设置用户代理认证不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSocketTimeoutInMillis(), "通过打开的连接传输数据的超时时间（单位：毫秒）不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSocketBufferSizeInBytes(), "Socket缓冲区大小不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getEndpoint(), "访问域名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getRegion(), "地域不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getRedirectsEnabled(), "是否开启HTTP重定向。默认开启不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getLocalAddress(), "本地地址不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxErrorRetry(), "请求失败最大重试次数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxDelayInMillis(), "最大延迟时间，单位：毫秒不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getStreamBufferSize(), "流文件缓冲区大小不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUserAgent(), "用户代理，指HTTP的User-Agent头不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssBaiduBO schisandraOssBaiduBO = SchisandraOssBaiduDTOConverter.INSTANCE.convertDTOToBO(schisandraOssBaiduDTO);
            return Result.ok(schisandraOssBaiduDomainService.update(schisandraOssBaiduBO));
        } catch (Exception e) {
            log.error("SchisandraOssBaiduController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新百度云对象存储配置表信息失败");
        }

    }

    /**
     * 删除百度云对象存储配置表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssBaiduDTO schisandraOssBaiduDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssBaiduController.delete.dto:{}", JSON.toJSONString(schisandraOssBaiduDTO));
            }
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSecretAccessKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCnameEnabled(), "使用cname访问BOS资源不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getEnableHttpAsyncPut(), "异步put不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getConnectionTimeoutInMillis(), "建立连接的超时时间（单位：毫秒）不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxConnections(), "允许打开的最大HTTP连接数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProtocol(), "连接协议类型不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyDomain(), "访问NTLM验证的代理服务器的Windows域名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyHost(), "代理服务器主机地址不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPassword(), "代理服务器验证的密码不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPort(), "代理服务器端口不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyUsername(), "代理服务器验证的用户名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyWorkstation(), "NTLM代理服务器的Windows工作站名称不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getProxyPreemptiveAuthenticationEnable(), "是否设置用户代理认证不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSocketTimeoutInMillis(), "通过打开的连接传输数据的超时时间（单位：毫秒）不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getSocketBufferSizeInBytes(), "Socket缓冲区大小不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getEndpoint(), "访问域名不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getRegion(), "地域不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getRedirectsEnabled(), "是否开启HTTP重定向。默认开启不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getLocalAddress(), "本地地址不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxErrorRetry(), "请求失败最大重试次数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getMaxDelayInMillis(), "最大延迟时间，单位：毫秒不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getStreamBufferSize(), "流文件缓冲区大小不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUserAgent(), "用户代理，指HTTP的User-Agent头不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssBaiduDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssBaiduBO schisandraOssBaiduBO = SchisandraOssBaiduDTOConverter.INSTANCE.convertDTOToBO(schisandraOssBaiduDTO);
            return Result.ok(schisandraOssBaiduDomainService.delete(schisandraOssBaiduBO));
        } catch (Exception e) {
            log.error("SchisandraOssBaiduController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除百度云对象存储配置表信息失败");
        }

    }

}
