package com.anji.xwl.pojo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.anji.xwl.pojo.Conf;
import com.anji.xwl.pojo.DataBase;
import com.anji.xwl.pojo.Table;
import com.anji.xwl.pojo.TableColumn;


import freemarker.template.Configuration;
import freemarker.template.Template;



public class MackMainPage {



	private DBTool db=null;

	public void initDB(Connection dataBase)
	{
		db = new DBTool(dataBase);
	}
	
	public void getModel(Conf conf)  throws Exception
	{
		String tableName = conf.getTableName();
		Table table = db.getTable(tableName,null);//获得表信息 1表明2 查询条件 暂时没用
		Map<String,Object> mainRoot = new HashMap<>();
		String[] sqls = DBTool.buildSQLs(conf.getJndi(), table.getTableName(), false, 1, null, null, null, null);
		String tableName_en = this.getNameEn(table.getTableName());//获得表英文信息
		String tableName_cn = table.getTableName_cn()==null?table.getTableName():table.getTableName_cn(); //获得中文
		String tableName_firstup = this.getFirstUp(tableName_en);//获得表的备注信息
		//String baseUrl = "main?action=webbuilder/application/shipping/cntrmanager";
		mainRoot.put("tableName_en", tableName_en.toUpperCase());
		mainRoot.put("tableName_cn", tableName_cn);
		mainRoot.put("tableName_lower", tableName_en.toLowerCase());
		mainRoot.put("tableName_firstup", tableName_firstup);
		mainRoot.put("baseUrl", conf.getBaseurl());
		mainRoot.put("column", table.getColumnList());
		mainRoot.put("form", XmlUtil.getListString(table.getColumnList()));
		mainRoot.put("columnString", XmlUtil.getColumnList(table.getColumnList()));
		mainRoot.put("selectUrl", "m?xwl="+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/select");
		mainRoot.put("insertUrl", "m?xwl="+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/insert");
		mainRoot.put("updateUrl", "m?xwl="+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/update");
		mainRoot.put("deleteUrl", "m?xwl="+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/delete");
		mainRoot.put("key", conf.getKey());
		List<TableColumn>  list=table.getColumnList();
		for(int i=0;i<list.size();i++)
		{
			
			System.out.println(i+"========"+list.get(i));
		}
	
		Configuration cfg = new Configuration(); 
		cfg.setDefaultEncoding("UTF-8");
		cfg.setDirectoryForTemplateLoading(new File(conf.getModelpath()));  
		Template mainTemplate = cfg.getTemplate("windowForm.ftl");
		 
	
		String mainSavePath=conf.getTopath()+"//"+tableName_en.toLowerCase()+"//";
		String mainFileName = tableName_en+".xwl";
		
		 
		this.buildTemplate(mainRoot, mainSavePath, mainFileName, mainTemplate);
		
		//	 查询模板设置
		
		Map<String, Object> selectRoot = new HashMap<>();
		
        selectRoot.put("selectSql", sqls[3]);
		selectRoot.put("packageName", conf.getPackagename());
		selectRoot.put("tableName_firstup",tableName_firstup);
		Template selectTemplate = cfg.getTemplate("testSelect.ftl");  
		String selectSavePath =conf.getTopath()+"/"+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/";
		String selectFileName = "select.xwl";
		 
		this.buildTemplate(selectRoot, selectSavePath, selectFileName, selectTemplate);
		
	/*	
	 *  insert模板插入
	 */
		Map<String, Object> insertRoot = new HashMap<>();
		insertRoot.put("insertSql", sqls[0]);
		Template insertTemplate = cfg.getTemplate("testInsert.ftl");  
		String insertSavePath = conf.getTopath()+"/"+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/";
		String insertFileName = "insert.xwl";
		  
		this.buildTemplate(insertRoot, insertSavePath, insertFileName, insertTemplate);
		/*	
		 * *update模板设置
		*/
	    Map<String, Object> updateRoot = new HashMap<>();
		updateRoot.put("updateSql", sqls[1]);
		updateRoot.put( "tableName_en", table.getTableName());
		Template udpateTemplate = cfg.getTemplate("testUpdate.ftl");  
		String updateSavePath = conf.getTopath()+"/"+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/";
		String updateFileName = "update.xwl";
		this.buildTemplate(updateRoot, updateSavePath, updateFileName, udpateTemplate); 
		
		
		Map<String, Object> deleteRoot = new HashMap<>();
		deleteRoot.put("deleteSql", sqls[2]);
		Template deleteTemplate = cfg.getTemplate("testDelete.ftl");  
		String deleteSavePath =conf.getTopath()+"/"+tableName_en.toLowerCase()+"/"+tableName_en.toLowerCase()+"edit"+"/";
		String deleteFileName = "delete.xwl";
		this.buildTemplate(deleteRoot, deleteSavePath, deleteFileName, deleteTemplate);
	

		

		
	}
	private String getNameEn(String Name)
	{
		
		char[] cAry = Name.toLowerCase().toCharArray();
		
		String result = "";
		int length = cAry.length;
		int i=0;
		while(i<length)
		{
			char temp = cAry[i];
			
			if('_'==temp&&(i+1)<length)
			{
				cAry[i+1] = (cAry[i+1]+"").toUpperCase().toCharArray()[0];
			}
			i++;
			result = result+temp;
		}
		
		return result.replace("_", "");
	}
	private String getFirstUp(String name)
	{
		StringBuilder sb = new StringBuilder(name);
		sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		return sb.toString();
	}
	public void buildTemplate(Map root, String savePath,  
            String fileName, Template template) {  
        String realFileName = savePath + fileName;

        String realSavePath = savePath;

        File newsDir = new File(realSavePath);
        if (!newsDir.exists()) {  
            newsDir.mkdirs();  
        }  
  
        try {  
            // SYSTEM_ENCODING = "UTF-8";  
            Writer out = new OutputStreamWriter(new FileOutputStream(  
                    realFileName), "UTF-8");  
            template.setEncoding("UTF-8");
            template.process(root, out);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}
	public static void main(String[] args)
	{
	}
}
