package com.taais.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.annotation.RepeatSubmit;
import com.taais.common.web.core.BaseController;
import com.taais.system.domain.bo.SysOssConfigBo;
import com.taais.system.domain.vo.SysOssConfigVo;
import com.taais.system.service.ISysOssConfigService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 对象存储配置
 *
 * @author wgk
 * @author 孤舟烟雨
 * @author wgk
 * @date 2023-11-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/resource/oss/config")
public class SysOssConfigController extends BaseController {

    @Resource
    private final ISysOssConfigService ossConfigService;

    /**
     * 查询对象存储配置列表
     */
    @SaCheckPermission("system:ossConfig:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysOssConfigVo>> list(SysOssConfigBo bo) {
        return CommonResult.success(ossConfigService.queryPageList(bo));
    }

    /**
     * 获取对象存储配置详细信息
     *
     * @param ossConfigId OSS配置ID
     */
    @SaCheckPermission("system:ossConfig:list")
    @GetMapping("/{ossConfigId}")
    public CommonResult<SysOssConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                                @PathVariable Long ossConfigId) {
        return CommonResult.success(ossConfigService.queryById(ossConfigId));
    }

    /**
     * 新增对象存储配置
     */
    @SaCheckPermission("system:ossConfig:add")
    @Log(title = "对象存储配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public CommonResult<Void> add(@Validated @RequestBody SysOssConfigBo bo) {
        boolean inserted = ossConfigService.insertByBo(bo);
        if (!inserted) {
            return CommonResult.fail("新增对象存储配置记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改对象存储配置
     */
    @SaCheckPermission("system:ossConfig:edit")
    @Log(title = "对象存储配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public CommonResult<Void> edit(@Validated @RequestBody SysOssConfigBo bo) {
        boolean updated = ossConfigService.updateByBo(bo);
        if (!updated) {
            return CommonResult.fail("修改对象存储配置记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除对象存储配置
     *
     * @param ossConfigIds OSS配置ID串
     */
    @SaCheckPermission("system:ossConfig:remove")
    @Log(title = "对象存储配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ossConfigIds}")
    public CommonResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                     @PathVariable Long[] ossConfigIds) {
        boolean deleted = ossConfigService.deleteWithValidByIds(List.of(ossConfigIds), true);
        if (!deleted) {
            return CommonResult.fail("删除对象存储配置记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("system:ossConfig:edit")
    @Log(title = "对象存储状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public CommonResult<Void> changeStatus(@RequestBody SysOssConfigBo bo) {
        boolean updated = ossConfigService.updateOssConfigStatus(bo);
        if (!updated) {
            return CommonResult.fail("状态修改失败!");
        }
        return CommonResult.success();
    }
}
