package com.chaoran.lx.thread;

import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import com.chaoran.db.DBManager;
import com.chaoran.entiry.Sys_DanJuFormsOptions;
import com.util.DateUtil;

import java.util.HashMap;

public class LxDJNrThread implements Runnable {
	private DBManager dbManager;
	private Handler menuHandler;
	private String formlx;

	public LxDJNrThread(DBManager dbManager, Handler menuHandler, String formlx) {
		this.dbManager = dbManager;
		this.menuHandler = menuHandler;
		this.formlx = formlx;
	}

	@Override
	public void run() {
		String sql = "select FORMbs,mxactive,candelmx,cannewmx,defaultFouseField,dialog_hei,dialog_wid,FORMsm,FORM from Sys_DanJuFormsOptions where rtrim(formlx)='"
				+ formlx + "'";
		Cursor cur = dbManager.query(sql);
		Sys_DanJuFormsOptions danju = null;
		if (cur.moveToFirst()) {
			danju = new Sys_DanJuFormsOptions();
			danju.setFORMbs(cur.getString(0));
			danju.setMxactive(cur.getString(1));
			danju.setCandelmx(cur.getString(2));
			danju.setCannewmx(cur.getString(3));
			danju.setDefaultFouseField(cur.getString(4));
			danju.setDialog_hei(Integer.parseInt(cur.getString(5)));
			danju.setDialog_wid(Integer.parseInt(cur.getString(6)));
			danju.setFORMsm(cur.getString(7));
			System.out.println(cur.getColumnIndex("FORM"));
			byte[] b = cur.getBlob(8);
			// System.out.println(b);
			danju.setFORM(b);
		}
		cur.close();
		HashMap fangAMap = new HashMap();
		if (dbManager.existsTable("sys_SearchOptions".toUpperCase()) > 0) {
			sql = "select functitle,upper('SEARCH_'+rtrim(functionname)) as   funName,fangalx,dialog_wid,dialog_hei,dialog_cap,zdysql,(CASE   return_one WHEN '是' THEN'Y' ELSE 'N' END )AS return_one   ,audioFld from sys_SearchOptions where rtrim(fangalx)='"
					+ formlx + "'";
			cur = dbManager.query(sql);
			HashMap tempMap = null;
			int len = cur.getColumnCount();
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				tempMap = new HashMap();
				for (int i = 0; i < len; i++) {
					tempMap.put(cur.getColumnName(i), cur.getString(i));
				}
				fangAMap.put(tempMap.get("funName"), tempMap);
			}
			cur.close();
		}
		System.out.println(dbManager.existsTable("sys_YmupOptions"
				.toUpperCase()) + "-----------------------");
		if (dbManager.existsTable("sys_YmupOptions".toUpperCase()) > 0) {
			sql = "select upper('YMUP_'||rtrim(fangabh)) as funName,zdysql as zdysql from Sys_YmupOptions where   rtrim(fangalx)='"
					+ formlx + "'";
			cur = dbManager.query(sql);
			HashMap tempMap = null;
			int len = cur.getColumnCount();
			System.out.println(len + "--------len---------------"
					+ cur.getCount());
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				System.out.println("-------------=========-------------");
				tempMap = new HashMap();
				for (int i = 0; i < len; i++) {
					tempMap.put(cur.getColumnName(i), cur.getString(i));
					System.out.println(cur.getColumnName(i) + "------"
							+ cur.getString(i));
				}
				fangAMap.put(tempMap.get("funName"), tempMap);
			}
			cur.close();
		}
		if (dbManager.existsTable("Djselefa".toUpperCase()) > 0) {
			sql = "select upper('DJTQ_'+rtrim(fangabh)) as funName,fangalx,fangamch, emptylyb, selectmx, t_sql, hz_sql,   mx_sql from Djselefa where rtrim(fangalx)='"
					+ formlx + "'";
			cur = dbManager.query(sql);
			HashMap tempMap = null;
			int len = cur.getColumnCount();
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				tempMap = new HashMap();
				for (int i = 0; i < len; i++) {
					tempMap.put(cur.getColumnName(i), cur.getString(i));
				}
				fangAMap.put(tempMap.get("funName"), tempMap);
			}
			cur.close();
		}
		HashMap djInitMap = new HashMap();
		djInitMap.put("danju", danju);
		djInitMap.put("GZID", "");
		djInitMap.put("RQ", DateUtil.getDateAndTime());
		djInitMap.put("ONTIME", DateUtil.getTime());
		// djInitMap.put("searchList", new ArrayList());
		// djInitMap.put("ymupList", new ArrayList());
		// djInitMap.put("djtqList", new ArrayList());
		Message message = new Message();
//		ArrayList tempList = new ArrayList();
//		tempList.add(djInitMap);
//		tempList.add(fangAMap);
		djInitMap.put("fangAMap",fangAMap);
		message.obj = fangAMap;
		menuHandler.sendMessage(message);
		// if (danju != null) {
		// System.out.println(danju.getFORM().length+"---------------------------------");
		// ByteArrayInputStream bin = new ByteArrayInputStream(danju.getFORM());
		// try {
		// ObjectInputStream oin = new ObjectInputStream(bin);
		// DanJuEntity danJuEntity=(DanJuEntity) oin.readObject();
		// System.out.println(danJuEntity.pageNr + "--------");
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
	}
}
