

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


import com.tmerclub.cloud.common.constant.ConfigNameConstant;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.platform.dto.GrowthConfigDTO;
import com.tmerclub.cloud.platform.model.SysConfig;
import com.tmerclub.cloud.platform.service.SysConfigService;
import com.tmerclub.cloud.platform.vo.GrowthConfigVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * 成长值基本配置信息
 * @author lhd
 */
@RestController
@RequestMapping("/p/growth_config")
public class GrowthConfigController {

    @Autowired
    private SysConfigService sysConfigService;


    /**
     * 配置信息
     */
    @GetMapping("/info/{key}")
    public ServerResponseEntity<GrowthConfigVO> info(@PathVariable("key") String key) {
        GrowthConfigVO growthConfig = sysConfigService.getSysConfigObject(key, GrowthConfigVO.class);
        if (Objects.isNull(growthConfig)) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.success(growthConfig);
    }

    /**
     * 保存配置
     */
    @PostMapping
    public ServerResponseEntity<Void> save(@RequestBody @Valid GrowthConfigDTO growthConfig) {
        SysConfig config = new SysConfig();
        String paramValue = Json.toJsonString(growthConfig);
        config.setParamKey(ConfigNameConstant.GROWTH_CONFIG);
        config.setParamValue(paramValue);
        config.setRemark(ConfigNameConstant.GROWTH_REMARKS);
        sysConfigService.saveOrUpdateSysConfig(config);
        return ServerResponseEntity.success();
    }

}
