package com.chaoran.lx.thread;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import com.chaoran.db.DBManager;
import com.sys.SysData;
import com.util.IoUtil;
import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LxDataDownThread implements Runnable {
	private Handler handler;
	private int currentPage;
	private int pagingNum;// 每页条数
	private DBManager dbManager;
	private String sql;
	private HashMap param;
	private String tablename;
	private String createtablesql;
	private ProgressDialog pd;
	private TextView tv;
	private String showTvStr;

	public LxDataDownThread(Handler handler, int currentPage, int pagingNum,
			DBManager dbManager, String sql, HashMap param, String tablename,
			String createtablesql, ProgressDialog pd, TextView tv,String showTvStr) {
		this.handler = handler;
		this.currentPage = currentPage;
		this.pagingNum = pagingNum;
		this.dbManager = dbManager;
		this.sql = sql;
		this.param = param;
		this.tablename = tablename;
		this.createtablesql = createtablesql;
		this.pd = pd;
		this.tv = tv;
		this.showTvStr=showTvStr;
	}

	@Override
	public void run() {
		try {
			luoji();
			Message msg = new Message();
			msg.what = 1;
			ArrayList tempList = new ArrayList();
			tempList.add(tablename);
			tempList.add(tv);
			tempList.add(showTvStr);
			msg.obj = tempList;
			handler.sendMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
			Message msg = new Message();
			msg.what = -1;
			msg.obj = e.toString();
			handler.sendMessage(msg);
		} finally {
			dbManager = null;
		}
	}

	private void luoji() throws Exception {
		System.gc();
		String url = SysData.url + "/webservice/offLineInventoryInterface?wsdl";
		String method = "lxDataDown";
		SoapObject rpc = new SoapObject("", method);
		int start = (currentPage - 1) * pagingNum + 1;
		String tempSql = "select * from (".concat(sql).concat(
				") temptable where rowNumber between '" + start + "' and '"
						+ currentPage * pagingNum + "'");
		HashMap tempParamMap = new HashMap();
		tempParamMap.put("sql", tempSql);
		if (currentPage == 1) {
			tempParamMap.put("currentPage", currentPage);
			tempParamMap.put("mac", SysData.clientid);
		}
		if (param != null) {
			tempParamMap.put("param", param);
		}
		byte[] b = IoUtil.getbyte(tempParamMap);
		String paramString = Base64.encode(b);
		rpc.addProperty("param", paramString);
		HttpTransportSE ht = new HttpTransportSE(url);
		ht.debug = true;
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.bodyOut = rpc;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(rpc);
		ht.call("", envelope);
		byte[] bb = Base64.decode(envelope.getResponse()
				.toString());
		Object ob = IoUtil.byte_obj(bb);
		if (!(ob instanceof List)) {
			throw new Exception(ob.toString());
		}

		List list = (List) ob;
		if (currentPage == 1) {
			System.out.println(tablename+"--------------------"+dbManager.existsTable(tablename));
			if (dbManager.existsTable(tablename) > 0) {
				dbManager.exeSql("drop table  ".concat(tablename));
			}
			dbManager.exeSql(createtablesql);
		}
		if (list != null && list.size() > 0) {
			dbManager.batchSave(list, tablename);
		}
		if (list.size() == pagingNum) {
			list.clear();
			list = null;
			currentPage++;
			Message msg = new Message();
			msg.what = 10;
			msg.obj = "正在下载" + tablename + "表数据,第" + currentPage + "页";
			handler.sendMessage(msg);
			// pd.setMessage("正在下载"+tablename+"表数据,第"+currentPage+"页");
			luoji();
		}
		// HashMap map=new HashMap();
		// map.put("obj", IoUtil.byte_obj(bb));
		// map.put("tableName",tablename);
		// map.put("currentPage", currentPage);
		// msg.obj = map;
		// handler.sendMessage(msg);
	}

}
