/*
 * Copyright 2019-2029 FISOK(www.fisok.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.goer.common.command.kit;

import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author : 杨松<yangsong158@qq.com>
 * @date : 2021/6/14
 * @desc : 连接以及解析处理工具类
 */
public abstract class ConnParseKit {
    private static Logger logger = LoggerFactory.getLogger(ConnParseKit.class);

    /**
     * 从注释文字中解析表的业务名（中文名）
     *
     * @param remarks
     * @return Left为字段名，Right为字段注释
     */
    public static Pair parseNameAndComment(String remarks) {
        String[] delimters = new String[]{" ", ",", "，", ";", "\t", "\n"};

        //有括号的字段，不解析其中的注释了
        int lIndex = remarks.indexOf("(");
        if(lIndex < 0) {
            lIndex = remarks.indexOf("（");
        }

        int rIndex = remarks.indexOf(")");
        if(rIndex < 0) {
            rIndex = remarks.indexOf("）");
        }
        if (lIndex > 0
                && rIndex > 0
                && lIndex < rIndex
        ) {
            return Pair.of(remarks, "");
        }


        Pair<String, String> pair = null;
        for (int i = 0; i < delimters.length; i++) {
            String delimter = delimters[i];
            int idxDelimter = remarks.indexOf(delimter);
            if (idxDelimter > 0) {
                String left = remarks.substring(0, idxDelimter);
                String right = remarks.substring(idxDelimter + 1);
                pair = Pair.of(left, right);
            }
            if (pair != null) {
                break;
            }
        }
        if (pair == null) {
            pair = Pair.of(remarks, "");
        }
        return pair;
    }
}
