package com.dcs.hnyz.cache.builder;

import com.dcs.common.enums.GeneralStatus;
import com.dcs.hnyz.cache.CacheCenter;
import com.dcs.hnyz.domain.bo.CachedAction;
import com.dcs.hnyz.domain.condition.Condition;
import com.dcs.hnyz.domain.ActionConfig;
import com.dcs.hnyz.mapper.ActionConfigMapper;
import com.dcs.hnyz.utils.ConditionParser;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dcs.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * ActionCacheBuilder
 * 读取数据库并构造缓存动作配置
 * @author: hmy
 * @date: 2025/5/27 16:58
 */
@Component
public class ActionCacheBuilder {

    @Resource
    private ActionConfigMapper actionConfigMapper;

    // 缓存动作配置 动作id-->CachedAction（actionConfig动作配置+condition条件树）
    private final Map<Long, CachedAction> cachedActionMap = new HashMap<>();

    public Map<Long, CachedAction> build() {
        List<ActionConfig> configList = actionConfigMapper.selectList(null);

        for (ActionConfig config : configList) {
            try {
                Condition condition = ConditionParser.fromJsonString(config.getTriggerCondition());
                CachedAction cachedAction = new CachedAction();
                cachedAction.setActionConfig(config);
                cachedAction.setCondition(condition);
                cachedAction.setTrigger(false);
                cachedActionMap.put(config.getActionId(), cachedAction);
            } catch (Exception e) {
                log.error("解析动作配置失败，ID={}，内容={}，原因={}",
                        config.getActionId(), config.getTriggerCondition(), e.getMessage());
                continue;
            }
        }
        return cachedActionMap;
    }

    // 提供缓存访问方法
    public Map<Long, CachedAction> getCache() {
        return cachedActionMap;
    }

    // 修改缓存内是否触发的状态
    public void setTrigger(long actionId, boolean trigger) {
        if (cachedActionMap.containsKey(actionId) && cachedActionMap.get(actionId).isTrigger()!=trigger) {//有变化才进行修改
            cachedActionMap.get(actionId).setTrigger(trigger);
        }
    }

    //缓存更新方法
    public void updateCache(ActionConfig config) {
        if(config.getTriggerCondition()==null){//仅修改动作配置的启用状态
            if(GeneralStatus.DISABLE.getCode().equals(config.getStatus())){
                cachedActionMap.remove(config.getActionId());
                return;
            }else {
                config=actionConfigMapper.selectById(config.getActionId());
            }
        }

        Condition condition = null;
        try {
            condition = ConditionParser.fromJsonString(config.getTriggerCondition());
        } catch (Exception e) {
            e.printStackTrace();
        }
        CachedAction cachedAction = new CachedAction();
        cachedAction.setActionConfig(config);
        cachedAction.setCondition(condition);
        cachedAction.setTrigger(false);
        cachedActionMap.put(config.getActionId(), cachedAction);
    }
    //缓存删除方法
    public void deleteCache(long actionId) {
        cachedActionMap.remove(actionId);
    }
    //缓存新增方法
    public void addCache(ActionConfig config) throws Exception {
        Condition condition = ConditionParser.fromJsonString(config.getTriggerCondition());
        CachedAction cachedAction = new CachedAction();
        cachedAction.setActionConfig(config);
        cachedAction.setCondition(condition);
        cachedAction.setTrigger(false);
        cachedActionMap.put(config.getActionId(), cachedAction);
    }

}
