

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


import cn.hutool.core.util.BooleanUtil;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.platform.model.SysConfig;
import com.tmerclub.cloud.platform.service.SysConfigService;
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 lhd
 */
@RestController
@RequestMapping("/p/score/other_related")
public class ScoreOtherRelatedConfigController {

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

    /**
     * 配置信息
     */
    @GetMapping("/info/{id}")
    public ServerResponseEntity<SysConfig> info(@PathVariable("id") String key) {
        SysConfig config = sysConfigService.getByKey(key);
        if (Objects.isNull(config)) {
            return ServerResponseEntity.success(new SysConfig());
        }
        return ServerResponseEntity.success(config);
    }

    /**
     * 保存or修改配置
     */
    @PostMapping
    public ServerResponseEntity<Void> saveOrUpdate(@RequestBody SysConfig config) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("抱歉，当前无操作权限");
        }
        sysConfigService.saveOrUpdateSysConfig(config);
        return ServerResponseEntity.success();
    }
}
