package com.api.doc.search.util;

import org.apache.commons.collections.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * @Description TODO
 * @Author b-r
 * @Date 2022/9/9 14:40
 * @Version 1.0
 **/
public class DocLabelUtil extends BaseBean {

    private static RecordSet rs =  new RecordSet();

    public static String getSql(String field,String ids){
        String returnSql = "";
        String sql = "";
        String dbType = rs.getDBType();

        List list = Util.TokenizerString(ids, ",");

        if("mysql".equals(dbType)){
            for (Object o : list) {
                if(!"".equals(sql)){
                    sql += " or ";
                }
                String id = Util.null2String(o);
                sql += " (concat(',',"+field+",',') like '%,"+id+",%') ";
            }
        }else if("sqlserver".equals(dbType)){
            for (Object o : list) {
                if(!"".equals(sql)){
                    sql += " or ";
                }
                String id = Util.null2String(o);
                sql += " (','+"+field+"+',' like '%,"+id+",%') ";
            }

        }else{
            for (Object o : list) {
                if(!"".equals(sql)){
                    sql += " or ";
                }
                String id = Util.null2String(o);
                sql += " (','||"+field+"||',' like '%,"+id+",%') ";
            }
        }

        if(!"".equals(sql)){
            returnSql = "(" + sql + ")";
        }
        return returnSql;
    }

    //根据标签id获取标签的合并标签
    public static String getMergeLabelIds(String labelType,String labelId){
        String mergerIds = "";
        List<String> labelIdList = Util.splitString2List(labelId, ",");
        HashSet<String> mergeSet = new HashSet<>();

        if("public".equals(labelType)){
            rs.executeQuery("select mergelabel from uf_publiclabel where id in ("+labelId+")");
            while (rs.next()){
                String mergelabel = Util.null2String(rs.getString("mergelabel"));
                if(!"".equals(mergelabel)){
                    mergelabel = mergelabel.contains("_") ? mergelabel.substring(mergelabel.lastIndexOf("_") + 1) : mergelabel;
                    mergeSet.add(mergelabel);
                }
            }

        }else if("private".equals(labelType)){
            rs.executeQuery("select mergelabel from uf_privatelabel where id in ("+labelId+")");
            while (rs.next()){
                String mergelabel = Util.null2String(rs.getString("mergelabel"));
                if(!"".equals(mergelabel)) {
                    mergelabel = mergelabel.contains("_") ? mergelabel.substring(mergelabel.lastIndexOf("_") + 1) : mergelabel;
                    mergeSet.add(mergelabel);
                }
            }
        }
        List<String> list = (List<String>)CollectionUtils.union(labelIdList, mergeSet);

        return String.join(",",list);
    }
}
