
package com.tmerclub.cloud.platform.controller;

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 io.swagger.v3.oas.annotations.Operation;
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.List;

/**
 * @author chenlin
 * @DateTime: 2020/3/19 8:29
 * @description: 支付配置、文件上传配置、短信配置、快递配置、小程序配置、公众号配置
 */
@RestController
@RequestMapping("/p/sys_config")
public class SysConfigController {


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

    /**
     * 获取保存配置信息
     */
    @GetMapping("/info/{key}")
    public ServerResponseEntity<String> info(@PathVariable("key") String key) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.success(sysConfigService.getValue(key));
    }

    /**
     * 保存配置
     */
    @PostMapping("/save")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid SysConfig sysConfig) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限进行操作");
        }
        sysConfigService.saveOrUpdateSysConfig(sysConfig);
        return ServerResponseEntity.success(true);
    }

    @PostMapping("/saveDeliveryConfig")
    @Operation(summary = "保存物流查询配置信息", description = "保存物流查询配置信息")
    public ServerResponseEntity<Boolean> saveDeliveryConfig(@RequestBody @Valid SysConfig sysConfig) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限进行操作");
        }
        List<String> invalidKeys = sysConfigService.saveDeliveryConfig(sysConfig);
        invalidKeys.forEach(key -> sysConfigService.removeSysConfig(key));
        return ServerResponseEntity.success(true);
    }


}
