package com.ruoyi.dk.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.ruoyi.component.core.constant.TenantConstants;
import com.ruoyi.component.core.domain.R;
import com.ruoyi.component.log.annotation.Log;
import com.ruoyi.component.log.enums.BusinessType;
import com.ruoyi.dk.systemconfig.SystemConfigManager;
import com.ruoyi.dk.systemconfig.dto.SystemConfigResponse;
import com.ruoyi.dk.systemconfig.enums.SystemConfigEnum;
import com.ruoyi.dk.systemconfig.enums.SystemConfigGroupEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dk/systemConfig")
public class SystemConfigController {
    @Autowired
    private SystemConfigManager systemConfigManager;

    @GetMapping("/system/all")
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    public R<List<SystemConfigResponse>> systemAll(){
        List<SystemConfigResponse> responses = new ArrayList<>();
        Map<String, String> allSystemConfig = systemConfigManager.getAllSystemConfig();
        SystemConfigEnum[] values = SystemConfigEnum.values();
        for (SystemConfigEnum value : values) {
            if(value.getGroup() == SystemConfigGroupEnum.SYSTEM){
                SystemConfigResponse sys = new SystemConfigResponse();
                sys.setKey(value.getKey());
                sys.setValue(allSystemConfig.getOrDefault(value.getKey(),value.getDefaultValue()));
                sys.setDesc(value.getDesc());
                responses.add(sys);
            }
        }
        return R.ok(responses);
    }

    @GetMapping("/system/update")
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @Log(title = "修改系统设置", businessType = BusinessType.UPDATE)
    public R<Boolean> systemUpdate(String key,String value){
        systemConfigManager.set(key,value);
        return R.ok(true);
    }
}
