package com.jeeplus.modules.form.utils;


import com.jeeplus.modules.form.entity.Column;
import com.jeeplus.modules.form.service.FormService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCountCallbackHandler;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * 表单数据表生成
 */
public class FormTableBuilder {
    private static String tablePrefix = "jp_form_";
    private String tableName = "";
    private JdbcTemplate jdbcTemplate;

    public FormTableBuilder(JdbcTemplate jdbcTemplate, String tableName) {
        this.jdbcTemplate = jdbcTemplate;
        this.tableName = tableName;
    }

    public static String getTablePrefix() {
        return tablePrefix;
    }

    /**
     * 删除表格
     *
     * @return
     */
    public boolean dropTable() {
        jdbcTemplate.execute(String.format("drop table if exists %s", tableName));
        return true;
    }



    public boolean createTable() {
        jdbcTemplate.execute(String.format("CREATE TABLE IF NOT EXISTS %s(`id` varchar(32) primary key)", tableName));
        return true;
    }

    public HashSet getColumns () {
        HashSet columns = new HashSet();
        String sql = String.format("select * from   %s", tableName);
        String[] str= jdbcTemplate.queryForRowSet(sql).getMetaData().getColumnNames();
        for(int i = 0;i<str.length;i++){
            columns.add(str[i]);
        }
        return columns;
    }

    public  void modifyColumn(List<Column> columnList){
        HashSet<String> newColumns = new HashSet();
        HashMap<String, String> map = new HashMap();
        HashSet<String> oldColumns = getColumns();
        for(Column column:columnList){
            newColumns.add(column.getModel());
            map.put(column.getModel(),column.getType());
        }
        for(String oldColumn: oldColumns){
            if(!newColumns.contains(oldColumn) && !oldColumn.equals("id")){
                jdbcTemplate.execute("alter table `"+tableName+"` drop column `"+oldColumn+"`" );
            }
        }
        for(String newColumn : newColumns){
           if(!oldColumns.contains(newColumn)){
               String type = map.get(newColumn);
               String sql = "alter table `"+tableName+"` add column `"+newColumn+"`";
               if (type.equals("textarea")) {
                   sql +=  " text";
               }else if(type.equals("slider") || type.equals("number")  || type.equals("rate") ){
                   sql +=  " integer";
               }else if(type.equals("switch")){
                   sql +=  " boolean";
               }else if(type.equals("table") || type.equals("imgupload") || type.equals("fileupload") || type.equals("editor")){
                   sql +=  " text";
               }else{
                   sql +=  " varchar(512)";
               }
               jdbcTemplate.execute(sql);
           }
        }
    }



    /**
     * 创建表格
     *
     * @return
     */
    public boolean createTable(List<Column> columnList) {
        if (this.createTable()) {
            this.modifyColumn(columnList);
        }
        return true;
    }

    public String getTableName() {
        return tableName;
    }
}
