package com.schisandra.oss.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.oss.application.convert.SchisandraOssUcloudDTOConverter;
import com.schisandra.oss.application.dto.SchisandraOssUcloudDTO;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.domain.bo.SchisandraOssUcloudBO;
import com.schisandra.oss.domain.service.SchisandraOssUcloudDomainService;
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;

/**
 * ucloud对象存储配置表 controller
 *
 * @author landaiqing
 * @since 2024-06-02 22:27:45
 */
@RestController
@RequestMapping("/oss/ucloud/")
@Slf4j
public class SchisandraOssUcloudController {

    @Resource
    private SchisandraOssUcloudDomainService schisandraOssUcloudDomainService;

    /**
     * 新增ucloud对象存储配置表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraOssUcloudDTO schisandraOssUcloudDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssUcloudController.add.dto:{}", JSON.toJSONString(schisandraOssUcloudDTO));
            }
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPublicKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPrivateKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCustomHost(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutConnect(), "连接超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutRead(), "读超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutWrite(), "写超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getMaxIdleConnections(), "okhttp最大空闲连接数（5）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getKeepAliveDuration(), "okhttp活动链接存货时间（5分钟）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getKeepAliveTimeUnit(), "okhttp活动链接存货时间单位, （分钟）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssUcloudBO SchisandraOssUcloudBO = SchisandraOssUcloudDTOConverter.INSTANCE.convertDTOToBO(schisandraOssUcloudDTO);
            return Result.ok(schisandraOssUcloudDomainService.add(SchisandraOssUcloudBO));
        } catch (Exception e) {
            log.error("SchisandraOssUcloudController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增ucloud对象存储配置表失败");
        }

    }

    /**
     * 修改ucloud对象存储配置表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraOssUcloudDTO schisandraOssUcloudDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssUcloudController.update.dto:{}", JSON.toJSONString(schisandraOssUcloudDTO));
            }
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPublicKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPrivateKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCustomHost(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutConnect(), "连接超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutRead(), "读超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutWrite(), "写超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getMaxIdleConnections(), "okhttp最大空闲连接数（5）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getKeepAliveDuration(), "okhttp活动链接存货时间（5分钟）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getKeepAliveTimeUnit(), "okhttp活动链接存货时间单位, （分钟）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssUcloudBO schisandraOssUcloudBO = SchisandraOssUcloudDTOConverter.INSTANCE.convertDTOToBO(schisandraOssUcloudDTO);
            return Result.ok(schisandraOssUcloudDomainService.update(schisandraOssUcloudBO));
        } catch (Exception e) {
            log.error("SchisandraOssUcloudController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新ucloud对象存储配置表信息失败");
        }

    }

    /**
     * 删除ucloud对象存储配置表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraOssUcloudDTO schisandraOssUcloudDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraOssUcloudController.delete.dto:{}", JSON.toJSONString(schisandraOssUcloudDTO));
            }
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUserId(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getBasePath(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getBucketName(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPublicKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPrivateKey(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCustomHost(), "不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutConnect(), "连接超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutRead(), "读超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTimeoutWrite(), "写超时时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getMaxIdleConnections(), "okhttp最大空闲连接数（5）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getKeepAliveDuration(), "okhttp活动链接存货时间（5分钟）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getKeepAliveTimeUnit(), "okhttp活动链接存货时间单位, （分钟）不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getPartSize(), "分片大小,默认5MB不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getTaskNum(), "并发线程数,默认等于CPU的核数不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getStatus(), "状态不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getOpenAdvancedSetup(), "是否开启高级设置不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUpdateTime(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getUpdateBy(), "更新人不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getIsDeleted(), "是否删除 0 未删除 1已删除不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getExtraJson(), "额外字段不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCreateBucket(), "当桶不存在，是否创建不能为空");
            Preconditions.checkNotNull(schisandraOssUcloudDTO.getCheckBucket(), "启动检测桶，是否存在不能为空");
            SchisandraOssUcloudBO schisandraOssUcloudBO = SchisandraOssUcloudDTOConverter.INSTANCE.convertDTOToBO(schisandraOssUcloudDTO);
            return Result.ok(schisandraOssUcloudDomainService.delete(schisandraOssUcloudBO));
        } catch (Exception e) {
            log.error("SchisandraOssUcloudController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除ucloud对象存储配置表信息失败");
        }

    }

}
