
package com.tmerclub.cloud.platform.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.platform.model.SysConfig;
import com.tmerclub.cloud.platform.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 网站配置信息表
 *
 * @author Orange
 */
@RestController("platformWebConfigController")
@RequestMapping("/p/web_config")
@Tag(name = "网站配置信息表")
public class WebConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @Value("${tmerclub.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/info/{key}")
    @Operation(summary = "根据key获取对应的网站配置", description = "根据key获取对应的网站配置")
    public ServerResponseEntity<String> info(@PathVariable("key") String key) {
        return ServerResponseEntity.success(sysConfigService.getValue(key));
    }

    @PostMapping("/save")
    @Operation(summary = "更新网站配置", description = "更新网站配置")
    public ServerResponseEntity<Void> save(@RequestBody @Valid SysConfig sysConfig) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("对不起，您没有修改网站配置的权限");
        }
        if (Objects.isNull(sysConfig.getParamKey()) || Objects.isNull(sysConfig.getParamValue())) {
            throw new LuckException("参数不完整，请准确填写后重试");
        }
        sysConfigService.saveOrUpdateSysConfig(sysConfig);
        return ServerResponseEntity.success();
    }

}
