package com.activity.chaoran;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.chaoran.entiry.DataGrid;
import com.sys.SysData;
import com.util.IoUtil;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.HashMap;
import java.util.Map;

/*运行检索方案线程*/
public class RunSearchThread extends Thread {
	private String sql;
	private String param;
	private Handler handler;
	private String return_one;// 是否单行返回
	private String audioFld;// 声音播放字段

	public RunSearchThread(String sql, String param, Handler handler,
			String return_one, String audioFld) {
		this.sql = sql;
		this.param = param;
		this.handler = handler;
		this.return_one = return_one;
		this.audioFld = audioFld;
	}

	public RunSearchThread(String sql, String param, Handler handler) {
		this.sql = sql;
		this.param = param;
		this.handler = handler;
	}

	public void run() {
		super.run();
		Log.v("RunSearchThread", "run执行");
		try {
			String url = SysData.url + "/webservice/ServiceInterface?wsdl";
			String method = "runSearch";
			SoapObject rpc = new SoapObject("", method);
			rpc.addProperty("sql", sql);
			rpc.addProperty("base64Param", param);
			HttpTransportSE ht = new HttpTransportSE(url,SysData.timeout);
			ht.debug = true;
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			ht.call("", envelope);
			byte[] bb = org.kobjects.base64.Base64.decode(envelope
					.getResponse().toString());
			Object ob = IoUtil.byte_obj(bb);
			if (ob instanceof DataGrid) {// 判断是否返回结果，因为我这个是一个查询操作，是带有返回值的。
				DataGrid dg = (DataGrid) ob;
				Map map = new HashMap();
				if (return_one != null) {
					map.put("return_one", return_one);
				}
				if (audioFld != null) {
					map.put("audioFld", audioFld);
				}
				map.put("dg", dg);
				Message msg = new Message();
				msg.obj = map;
				msg.arg1 = 1;
				handler.sendMessage(msg);
			} else {
				Message msg = new Message();
				msg.obj = ob.toString();
				msg.what = -1;
				handler.sendMessage(msg);
			}
		} catch (Exception e) {
			Message msg = new Message();
			msg.what = -1;
			msg.obj = e.toString();
			handler.sendMessage(msg);
		}
	}
}
