package com.hchyun.generator.util;

import com.hchyun.common.constant.GenConstants;
import com.hchyun.common.utils.StringUtils;
import com.hchyun.generator.entity.AssColumn;
import org.apache.commons.lang3.RegExUtils;

/**
 * @Author 18209
 * @Date 2021/2/6 14:48
 * @Version 1.0
 */
public class AssociatedUtils {


    public static void initAssColumn(AssColumn assColumn, Long tableId) {
        //获取数据库数据类型
        String dataType = GenUtils.getDbType(assColumn.getColumnType());
        //获取字段名称
        String columnName = assColumn.getColumnName();
        //设置tableid
        assColumn.setTableId(tableId);
        //设置创建者id
        assColumn.setCreateBy(1);
        //转为设置java字段名称
        assColumn.setJavaField(StringUtils.toCamelCase(columnName));
        //默认类型
        assColumn.setJavaType(GenConstants.TYPE_STRING);
        //默认查询条件 精确查询
        assColumn.setQueryType(GenConstants.QUERY_EQ);

        if (GenUtils.arraysContains(GenConstants.COLUMNTYPE_STR, dataType) || GenUtils.arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType)) {
            //字符串长度超500设置htmlType为文本域
            Integer columnLength = GenUtils.getColumnLength(assColumn.getColumnType());
            String htmlType = columnLength >= 500 || GenUtils.arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType) ? GenConstants.HTML_TEXTAREA : GenConstants.HTML_INPUT;
            assColumn.setHtmlType(htmlType);
        } else if (GenUtils.arraysContains(GenConstants.COLUMNTYPE_TIME, dataType)) {
            assColumn.setJavaType(GenConstants.TYPE_DATE);
            assColumn.setHtmlType(GenConstants.HTML_DATETIME);
        } else if (GenUtils.arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType)) {
            assColumn.setHtmlType(GenConstants.HTML_INPUT);

            // 如果是浮点型 统一用BigDecimal
            String[] str = StringUtils.split(StringUtils.substringBetween(assColumn.getColumnType(), "(", ")"), ",");
            if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) {
                assColumn.setJavaType(GenConstants.TYPE_BIGDECIMAL);
            }
            // 如果是整形
            else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) {
                assColumn.setJavaType(GenConstants.TYPE_INTEGER);
            }
            // 长整形
            else {
                assColumn.setJavaType(GenConstants.TYPE_LONG);
            }
        }
        // 列表字段
        assColumn.setIsList(GenConstants.REQUIRE);
        // 查询字段
        assColumn.setIsQuery(GenConstants.NO_REQUIRE);
        // 查询字段类型
        if (StringUtils.endsWithIgnoreCase(columnName, "name")) {
            assColumn.setQueryType(GenConstants.QUERY_LIKE);
        }
        // 状态字段设置单选框
        if (StringUtils.endsWithIgnoreCase(columnName, "status")) {
            assColumn.setHtmlType(GenConstants.HTML_RADIO);
        }
        // 类型&性别字段设置下拉框
        else if (StringUtils.endsWithIgnoreCase(columnName, "type")
                || StringUtils.endsWithIgnoreCase(columnName, "sex")) {
            assColumn.setHtmlType(GenConstants.HTML_SELECT);
        }
    }


}
