package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssJinshanDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssJinshanDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssJinshanBO;
import com.schisandra.oss.domain.service.SchisandraOssJinshanDomainService;
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:13:26
 */
@RestController
@RequestMapping("/oss/jinshan/")
@Slf4j
public class SchisandraOssJinshanController {

    @Resource
    private SchisandraOssJinshanDomainService schisandraOssJinshanDomainService;

    /**
     * 新增金山云对象存储配置表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssJinshanDTO schisandraOssJinshanDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJinshanController.add.dto:{}", JSON.toJSONString(schisandraOssJinshanDTO));
            }
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAccessKeySecret(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getEndpoint(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getRegion(), "服务地址,参考{@linkplain http://ks3.ksyun.com/doc/api/index.html Doc}");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getSecurityToken(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getProtocol(), "http或者https不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getVersion(), "签名版本不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getPathStyleAccess(), "是否使用path style access方式访问不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAllowAnonymous(), "允许客户端发送匿名请求不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getFlowRedirect(), "当服务端返回307时是否自动跳转");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getDomainMode(), "是否使用绑定的域名作为endpoint不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getSignerClass(), "签名类不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUseGzip(), "是否压缩不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssJinshanBO SchisandraOssJinshanBO = SchisandraOssJinshanDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJinshanDTO);
            return Result.ok(schisandraOssJinshanDomainService.add(SchisandraOssJinshanBO));
        } catch (Exception e) {
            log.error("SchisandraOssJinshanController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增金山云对象存储配置表失败");
        }

    }

    /**
     * 修改金山云对象存储配置表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssJinshanDTO schisandraOssJinshanDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJinshanController.update.dto:{}", JSON.toJSONString(schisandraOssJinshanDTO));
            }
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAccessKeySecret(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getEndpoint(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getRegion(), "服务地址,参考{@linkplain http://ks3.ksyun.com/doc/api/index.html Doc}");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getSecurityToken(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getProtocol(), "http或者https不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getVersion(), "签名版本不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getPathStyleAccess(), "是否使用path style access方式访问不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAllowAnonymous(), "允许客户端发送匿名请求不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getFlowRedirect(), "当服务端返回307时是否自动跳");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getDomainMode(), "是否使用绑定的域名作为endpoint不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getSignerClass(), "签名类不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUseGzip(), "是否压缩不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssJinshanBO schisandraOssJinshanBO = SchisandraOssJinshanDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJinshanDTO);
            return Result.ok(schisandraOssJinshanDomainService.update(schisandraOssJinshanBO));
        } catch (Exception e) {
            log.error("SchisandraOssJinshanController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新金山云对象存储配置表信息失败");
        }

    }

    /**
     * 删除金山云对象存储配置表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssJinshanDTO schisandraOssJinshanDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssJinshanController.delete.dto:{}", JSON.toJSONString(schisandraOssJinshanDTO));
            }
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAccessKeyId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAccessKeySecret(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getEndpoint(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getRegion(), "服务地址,参考{@linkplain http://ks3.ksyun.com/doc/api/index.html Doc}");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getSecurityToken(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getProtocol(), "http或者https不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getVersion(), "签名版本不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getPathStyleAccess(), "是否使用path style access方式访问不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getAllowAnonymous(), "允许客户端发送匿名请求不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getFlowRedirect(), "当服务端返回307时是否自动跳转");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getDomainMode(), "是否使用绑定的域名作为endpoint不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getSignerClass(), "签名类不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUseGzip(), "是否压缩不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssJinshanDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssJinshanBO schisandraOssJinshanBO = SchisandraOssJinshanDTOConverter.INSTANCE.convertDTOToBO(schisandraOssJinshanDTO);
            return Result.ok(schisandraOssJinshanDomainService.delete(schisandraOssJinshanBO));
        } catch (Exception e) {
            log.error("SchisandraOssJinshanController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除金山云对象存储配置表信息失败");
        }

    }

}
