package com.ruoyi.xq.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xq.dto.common.sms.SystemConfigResponse;
import com.ruoyi.xq.enums.common.SystemConfigEnum;
import com.ruoyi.xq.enums.common.SystemConfigGroupEnum;
import com.ruoyi.xq.manager.SystemConfigManager;
import com.ruoyi.xq.manager.systemconfig.SystemCheckResp;
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("/xq/systemConfig")
public class SystemConfigController {
    @Autowired
    private SystemConfigManager systemConfigManager;
    @GetMapping("/business/all")
    @SaCheckPermission("xq:businessConfig:query")
    public R<List<SystemConfigResponse>> businessAll(){
        List<SystemConfigResponse> responses = new ArrayList<>();
        Map<String, String> allSystemConfig = systemConfigManager.getAllSystemConfig();
        SystemConfigEnum[] values = SystemConfigEnum.values();
        for (SystemConfigEnum value : values) {
            if(value.getGroup() == SystemConfigGroupEnum.BUSINESS){
                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("/business/update")
    @SaCheckPermission("xq:businessConfig:edit")
    @Log(title = "修改业务设置", businessType = BusinessType.UPDATE)
    public R<Void> businessUpdate(String key,String value){
        SystemConfigEnum[] values = SystemConfigEnum.values();
        for (SystemConfigEnum config : values) {
            if (config.getGroup() == SystemConfigGroupEnum.BUSINESS && config.getKey().equals(key)) {
                SystemCheckResp checkResp = systemConfigManager.setSystemConfig(config, value);
                if(checkResp.isSuccess()){
                    return R.ok();
                }
                return R.fail(checkResp.getMessage());
            }
        }
        return R.ok();
    }

    @GetMapping("/system/all")
    @SaCheckPermission("xq:systemConfig:query")
    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")
    @SaCheckPermission("xq:systemConfig:edit")
    @Log(title = "修改系统设置", businessType = BusinessType.UPDATE)
    public R<Boolean> systemUpdate(String key,String value){
        systemConfigManager.set(key,value);
        return R.ok(true);
    }
}
