package com.th.demo.tools;


import com.th.demo.model.develop.DevCodeMechine;
import com.th.demo.model.system.Type;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.internal.DefaultShellCallback;


import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by dell on 2018-12-05.
 */
public class CodeMachineTools {
    //packagePath com.th.demo
    //filePath src/main/test




    public static String tableName2ModelName(String tableName) {
        String[] tableArr = tableName.split("_");
        String b = "", c = "";
        String temp = "";
        String result = "";

        try {
            for (int i = 0; i < tableArr.length; i++) {
                b = "";
                c = "";
                temp = tableArr[i];
                if (temp.equals("T")) {

                } else {
                    b = temp.substring(0, 1);
                    c = temp.substring(1, temp.length());
                    c = c.toLowerCase();
                    result += b + c;
                }
            }
        } catch (Exception ex) {
            result = Type.FAIL + ex.getMessage();
            ex.printStackTrace();
        } finally {
            return result;
        }

    }


    public static List<DevCodeMechine> feild2Property(List<DevCodeMechine> feildList) {

        List<DevCodeMechine> resultList = new ArrayList<DevCodeMechine>();
        String b = "", c = "";
        String temp = "";
        String result = "";
        for (DevCodeMechine dcm :feildList) {
            if (dcm.getColumnName().equals("ADD_ID") ||
                    dcm.getColumnName().equals("ADD_TIME") ||
                    dcm.getColumnName().equals("MODIFY_ID") ||
                    dcm.getColumnName().equals("MODIFY_TIME") ||
                    dcm.getColumnName().equals("CANCEL_ID") ||
                    dcm.getColumnName().equals("CANCEL_TIME") ||
                    dcm.getColumnName().equals("CANCEL_FLAG") ||
                    dcm.getColumnName().equals("ATTR1") ||
                    dcm.getColumnName().equals("ATTR2") ||
                    dcm.getColumnName().equals("ATTR3") ||
                    dcm.getColumnName().equals("ATTR4") ||
                    dcm.getColumnName().equals("ATTR5") ||
                    dcm.getColumnName().equals("ATTR6")) {

                continue;
            }


            if (dcm.getColumnName().contains("_")) {
                result = "";
                String[] tableArr = dcm.getColumnName().split("_");
                for (int i = 0; i < tableArr.length; i++) {
                    b = "";
                    c = "";
                    temp = tableArr[i];
                    if (i == 0) {
                        result += temp.toLowerCase();
                    } else {
                        b = temp.substring(0, 1);
                        c = temp.substring(1, temp.length());
                        c = c.toLowerCase();
                        result += b + c;
                    }
                }
                dcm.setPropertyName(result); ;
                dcm.setSetFunction("set" + result.substring(0, 1).toUpperCase() + result.substring(1, result.length()));
            } else {

                result = dcm.getColumnName().toLowerCase();
                dcm.setPropertyName(result) ;
                dcm.setSetFunction("set" + result.substring(0, 1).toUpperCase() + result.substring(1, result.length()));
            }

            if(dcm.getDataType().equals("VARCHAR2")){
                dcm.setDataType("String");
            }else if(dcm.getDataType().equals("NUMBER")){
                dcm.setDataType("BIGDECIMAL");
            }else if(dcm.getDataType().equals("DATE")){
                dcm.setDataType("DATE");
            }

            resultList.add(dcm);
        }

        return resultList;
    }


}

