package com.steering.comm.tools;

import java.util.List;
import java.util.Map;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA06.CoreReturnSortObject;

import com.steering.comm.db.DBCommon;

public class CodeTool extends DBCommon {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CodeTool ct = new CodeTool();
        String codeStr = ct.printGridColumnCode("PLN_JGZY_M", "dtCoupPlan",31);
        System.out.println(codeStr);
    }
    
    /**
     * 根据表名和dataTable名自动生成DataTable字段源代码(C#)
     * @param tableName 表名
     * @param dtName dataTalbe名称
     * @param colBeginIndex dataColumn起始列
     */
    public String printGridColumnCode(String tableName,String dtName,int colBeginIndex){
        StringBuffer buffer = new StringBuffer();
        String sql = "select t.table_name,t.comments tab_comments,c.column_name" +
                ",c.comments col_comments from user_col_comments c, user_tab_comments t " +
                "where c.table_name = t.table_name and t.table_name = upper(?)";
        CoreReturnObject cro = this.getDao("TgDao").ExcuteSortResultByQuery(sql, new Object[]{tableName});
        CoreReturnSortObject obj = (CoreReturnSortObject)(cro.getResult());
        List list = obj.getResult();
        
        buffer.append("//table声明");
        buffer.append("\r\n");
        buffer.append("private System.Data.DataTable "+dtName+";");
        buffer.append("\r\n");
        String[] columns = new String[list.size()];
        for(int i = 0; i < list.size();i++){
            Map map  = (Map)list.get(i);
            buffer.append("private System.Data.DataColumn dataColumn"+(i+colBeginIndex)+";");
            buffer.append("\r\n");
        }
        
        buffer.append("\r\n//table初始化");
        buffer.append("\r\n");
        buffer.append("this."+dtName+" = new System.Data.DataTable();");
        buffer.append("\r\n");
        buffer.append("this."+dtName+".TableName = \""+dtName+"\";");
        buffer.append("\r\n");
        for(int i = 0; i < list.size();i++){
            Map map  = (Map)list.get(i);
            buffer.append("this.dataColumn"+(i+colBeginIndex)+" = new System.Data.DataColumn();");
            buffer.append("\r\n");
            buffer.append("this.dataColumn"+(i+colBeginIndex)+".Caption = \""+map.get("COL_COMMENTS")+"\";");
            buffer.append("\r\n");
            buffer.append("this.dataColumn"+(i+colBeginIndex)+".ColumnName = \""+map.get("COLUMN_NAME")+"\";");
            buffer.append("\r\n");
            columns[i] = "this.dataColumn"+(i+colBeginIndex);
            buffer.append("\r\n");
        }
        buffer.append("this."+dtName+".Columns.AddRange(new System.Data.DataColumn[] {");
        buffer.append("\r\n");
        for(int i = 0;i< columns.length;i++){
            buffer.append(columns[i] + (i < columns.length-1 ? "," : ""));
            buffer.append("\r\n");
        }
        buffer.append("});");
        buffer.append("\r\n");
        
        buffer.append("\r\n//添加到dataset");
        buffer.append("\r\n");
        buffer.append("this.dataSet1.Tables.AddRange(new System.Data.DataTable[]{this."+dtName+"});");
        buffer.append("\r\n");
        return buffer.toString();
    }

}
