package com.mortals.xhx.base.system.gentable.model;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.github.javafaker.Faker;
import com.mortals.framework.model.BaseEntityLong;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.common.utils.StringUtils;
import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.mortals.xhx.common.key.GenConstants.KEYWORDS_SET;

/**
 * Description:GentableColumn
 * date: 2021-9-28 10:10:15
 */
@Data
public class GentableColumnEntityExt extends BaseEntityLong {

    private List<Map<String, String>> dict;

    private List<Long> idList;


    public static String subComment(String comment) {

        if (comment.indexOf("(") != -1) {
            return comment.substring(0, comment.indexOf("("));
        }

        if (comment.indexOf("（") != -1) {
            return comment.substring(0, comment.indexOf("（"));
        }

        return comment;
    }

    public static String randomStr(GentableColumnEntity column) {

        String returnStr = "";

        if (!ObjectUtils.isEmpty(column.getDefaultValue())) {
            returnStr = column.getDefaultValue();
        } else {
            if ("String".equalsIgnoreCase(column.getJavaType())) {
                returnStr = RandomUtil.randomString(6);
            } else if ("Integer".equalsIgnoreCase(column.getJavaType()) || "Long".equalsIgnoreCase(column.getJavaType())) {
                returnStr = RandomUtil.randomInt(0, 1000) + "";
            } else if ("BigDecimal".equalsIgnoreCase(column.getJavaType())) {
                returnStr = RandomUtil.randomBigDecimal().setScale(2, 1).doubleValue() + "";
            } else if ("date".equalsIgnoreCase(column.getJavaType())) {
                returnStr = DateUtils.getCurrDate().getTime() + "";
            }
        }


        return returnStr;
    }


    public static String randomStr(String javaType) {

        String returnStr = "";
        if ("String".equalsIgnoreCase(javaType)) {
            returnStr = RandomUtil.randomString(6);
        } else if ("Integer".equalsIgnoreCase(javaType) || "Long".equalsIgnoreCase(javaType)) {
            returnStr = RandomUtil.randomInt(0, 10000) + "";
        } else if ("BigDecimal".equalsIgnoreCase(javaType)) {
            returnStr = RandomUtil.randomBigDecimal().setScale(2, 1).doubleValue() + "";
        } else if ("date".equalsIgnoreCase(javaType)) {
            returnStr = DateUtils.getCurrStrDate();
        }
        return returnStr;
    }

    public static String isKeyWord(String columnName) {
        if (KEYWORDS_SET.contains(columnName)) {
            return "`" + columnName + '`';
        }
        return columnName;
    }


    public static String defaultFormatValue(String javaType, String columnName, String defaultValue) {
        if ("Long".equals(javaType)) {
            if (ObjectUtils.isEmpty(defaultValue)) {
                defaultValue = "null";
            }
        } else if ("Integer".equals(javaType)) {
            if ("year".equals(columnName)) {
                defaultValue = "DateUtil.year(new Date())";
            } else if ("month".equals(columnName)) {
                defaultValue = "DateUtil.month(new Date())+1";
            } else if ("day".equals(columnName)) {
                defaultValue = "DateUtil.dayOfMonth(new Date())";
            }
            if (ObjectUtils.isEmpty(defaultValue)) {
                defaultValue = "0";
            }
        } else if ("String".equals(javaType)) {
            if (ObjectUtils.isEmpty(defaultValue)) {
                defaultValue = "\"\"";
            }
        } else if ("BigDecimal".equals(javaType)) {
            defaultValue = "BigDecimal.ZERO";
        } else if ("Date".equals(javaType)) {
            defaultValue = "new Date()";
        } else {
            defaultValue = "null";
        }

        return defaultValue;
    }


    public static String randomNumber() {

        Faker faker = new Faker(Locale.CHINESE);
        return faker.number().numberBetween(0, 10000) + "";
    }

    public static String subBetween(String comment) {

        comment = comment.replaceAll("（", "(");
        comment = comment.replaceAll("）", ")");
        String max = StrUtil.subBetween(comment, "(", ")");

        return max == null ? "9999" : max;
    }


    public String test() {
        return "aaa";
    }


    public static void main(String[] args) {
        GentableColumnEntity gentableColumnEntity = new GentableColumnEntity();
        gentableColumnEntity.setCreateUser("bbbbbb");
        System.out.println(JSON.toJSONString(gentableColumnEntity));
        // System.out.println(gentableColumnEntity.test());


        System.out.println(DateUtils.getCurrDatetime().getTime());

    }

}