package com.chushang.canal.sync;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.chushang.common.canal.event.CanalEventListener;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author by zhaowenyuan create 2022/Ca6 15:45
 * 项目 公共配置修改
 */
@Slf4j
@Component
public class SystemProjectConfigListen implements CanalEventListener {

    @Autowired
    RedissonClient redissonClient;

    /**
     * 项目 公共配置 key 前缀 值
     */
    private static final String PROJECT_CONFIG_KEY = "PROJECT-ID:";

    /**
     * 新增会有
     * @param rowData rowData
     */
    @Override
    public void onInsert(CanalEntry.RowData rowData) {
        //
        log.info("insert");
        convert(rowData.getAfterColumnsList());
    }

    @Override
    public void onUpdate(CanalEntry.RowData rowData) {
        //
        log.info("update");
        convert(rowData.getAfterColumnsList());
    }


    /**
     * 理论上 不会有删除
     * @param rowData rowData
     */
    @Override
    public void onDelete(CanalEntry.RowData rowData) {
        //
        log.info("delete");
        convert(rowData.getAfterColumnsList());
    }

    /**
     * 当前 实现对应的表名
     */
    @Override
    public String tableName() {
        return "sanyi_system_config";
    }

    @Override
    public String schemaName() {
        return "sanyi_manager";
    }

    private void convert(List<CanalEntry.Column> afterColumnsList){
        if (CollectionUtil.isNotEmpty(afterColumnsList)){
            Map<String, String> columnMap =
                    afterColumnsList.stream().collect(
                            Collectors.toMap(CanalEntry.Column::getName,
                                    CanalEntry.Column::getValue,
                                    (k1, k2) -> k2
                            )
                    );

            log.info("{}", columnMap);
            // 如果
            if (CollectionUtil.isNotEmpty(columnMap)){
                String config_key = columnMap.get("config_key");
                // value 可能为空
                String config_value = columnMap.get("config_value");
                String del_state = columnMap.get("del_state");
                // 任意一个为空, 则不进行继续
                if (StrUtil.hasEmpty(config_key, del_state)){
                    return;
                }
                String[] split = config_key.split(":");
                // 长度不为2时, 代表必定不是所需要的数据
                if (split.length != 2){
                    return;
                }
                String projectId = split[0];
                String key = split[1];
                // 0 时, 代表未删除 true 代表未删除
                boolean flag = "0".equals(del_state);
                updateConfig(projectId, key, flag ? config_value : null);
            }
        }
    }

    private void updateConfig(String projectId, String key, Object value){
        RMap<String, Object> map = redissonClient.getMap(PROJECT_CONFIG_KEY + projectId);
        if (map.containsKey(key)){
            if (null == value){
                map.remove(key);
                return;
            }
        }
        map.put(key, value);
    }

}
