package com.fs.his.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.his.config.FsSysConfig;
import com.fs.hisStore.config.FsErpConfig;
import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ConfigUtil {
    FsSysConfig fsSysConfig;

    FsErpConfig fsErpConfig;

    @Autowired
    SysConfigMapper sysConfigMapper;
    public FsSysConfig getSysConfig(){
        if (this.fsSysConfig==null){
            SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.config");
            String configValue = sysConfig.getConfigValue();
            FsSysConfig config = JSON.parseObject(configValue, FsSysConfig.class);
            this.fsSysConfig=config;
            return this.fsSysConfig;
        }else {
            return this.fsSysConfig ;
        }
    }

    public FsErpConfig getErpConfig(){
        if (this.fsErpConfig==null){
            SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.config");
            String configValue = sysConfig.getConfigValue();
            FsErpConfig config = JSON.parseObject(configValue, FsErpConfig.class);
            this.fsErpConfig=config;
            return this.fsErpConfig;
        }else {
            return this.fsErpConfig ;
        }
    }

    public <T> T generateStructConfigByKey(String key, Class<T> clazz){
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey(key);
        String configValue = sysConfig.getConfigValue();
        if(configValue != null && configValue.trim().startsWith("[")){
            JSONArray array = JSON.parseArray(configValue);
            if (array.size() > 0) {
                return array.getObject(0, clazz);
            }
            return null;
        }
        return JSON.parseObject(configValue, clazz);
    }

    public JSONObject generateConfigByKey(String key){
        int index = 0;
        return generateConfigByKeyIndexed(key,index);
    }

    public JSONObject generateConfigByKeyIndexed(String key,int index){
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey(key);
        if (sysConfig == null) {
            return new JSONObject();
        }
        String configValue = sysConfig.getConfigValue();
        if(configValue != null && configValue.trim().startsWith("[")){
            JSONArray array = JSON.parseArray(configValue);
            if (array.size() >= index+1) {
                return array.getJSONObject(index);
            }
            return new JSONObject();
        }
        return JSONObject.parseObject(configValue);
    }


    public JSONObject generateConfigMiniByKey(String key, String appId) {

        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey(key);
        if (sysConfig == null) {
            throw new RuntimeException("未找到key为[" + key + "]的配置");
        }

        String configValue = sysConfig.getConfigValue();
        if (configValue == null || configValue.trim().isEmpty()) {
            throw new RuntimeException("key为[" + key + "]的配置值为空");
        }

        if (configValue.trim().startsWith("[")) {
            JSONArray array = JSON.parseArray(configValue);

            return array.stream()
                    .map(obj -> (JSONObject) obj)
                    .filter(json -> appId.equals(json.getString("appid")))
                    .findFirst()
                    .orElseThrow(() -> new RuntimeException("未找到与appId[" + appId + "]匹配的配置"));
        }

        JSONObject jsonObject = JSONObject.parseObject(configValue);
        if (!appId.equals(jsonObject.getString("appId"))) {
            throw new RuntimeException("配置的appId与当前appId[" + appId + "]不匹配");
        }
        return jsonObject;
    }

}
