package com.jqyd.newprocess;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.jqyd.adapter.MyAdapter;
import com.jqyd.app.MyApp;
import com.jqyd.manager.R;
import com.jqyd.model.EmpsModule;
import com.jqyd.model.GroupsModule;
import com.jqyd.model.PositionInfo;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.UpdataToServer;
public class EmpGroupList extends Activity implements OnClickListener{//分组查询
private RelativeLayout relayout;
private Button leftOpt;
private Button righOpt;
private TextView title;
private Button sure;
private Button textDesc;
private ListView fzLv;
private MyAdapter adapter;
private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private ArrayList<EmpsModule> empList = new ArrayList<EmpsModule>();
private ArrayList<EmpsModule> isSelected = new ArrayList<EmpsModule>();
private MyApp myApp;
private Optdb_interfce db = null;
private String groupNums = "";
private ArrayList<String> groupNumList = new ArrayList<String>();
private boolean flag_cust = false;//用来判断当前显示内容是员工、客户还是分组
private int num =12;
private ArrayList<PositionInfo> locInfo = new ArrayList<PositionInfo>();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.emplist);
myApp = (MyApp) this.getApplication();
fzLv=(ListView) findViewById(R.id.list);
title = (TextView) this.findViewById(R.id.title);
title.setText("人员列表");
sure = (Button) this.findViewById(R.id.sure);
textDesc = (Button) this.findViewById(R.id.textDesc);
relayout = (RelativeLayout) this.findViewById(R.id.opt);
sure.setOnClickListener(this);
textDesc.setOnClickListener(this);
leftOpt = (Button) this.findViewById(R.id.leftOpt);
leftOpt.setVisibility(Button.INVISIBLE);
righOpt = (Button) this.findViewById(R.id.rightOpt);
righOpt.setVisibility(Button.INVISIBLE);
db = new Optdb_interfce(this);
groupNums = myApp.getGroupNums();
Optsharepre_interface share_obj = new Optsharepre_interface(this);
String glfw = share_obj.getDataFromPres("GLFW");
String searchResult = db.RecursionGroup(glfw, 2);
// String[] resArry =searchResult.split(",");
// Log.e("resArry", resArry.toString());
Log.i("EmpGroupList","管理范围:"+share_obj.getDataFromPres("GLFW"));
if(groupNums.equals("")){
ArrayList<Object> li = db.searchFGroups("0",2);//取出一级分组编号
for(int i=0;i<li.size();i++){
HashMap<String,String> map = new HashMap<String,String>();
GroupsModule group = (GroupsModule) li.get(i);
String p_id = String.valueOf(group.getG_pid());
String g_id = String.valueOf(group.getGid());
System.out.println("---------"+glfw);
System.out.println("---------"+g_id);
if(searchResult.contains(g_id)){
map.put("textView1", group.getGname());
list.add(map);
groupNumList.add(group.getGid()+"");
}
}
}else{
System.out.println("分组编号:"+groupNums);
ArrayList<Object> li = db.searchFGroups(groupNums,2);//逐级取出分组编号及分组名称
//判断分组下是否有员工存在,如果存在,则直接在当前界面显示出来,否则不显示
ArrayList<Object> emps = db.searchEmpsByMoreGps(groupNums);
groupNums = "";
//分组循环
for(int i=0;i<li.size();i++){
HashMap<String,String> map = new HashMap<String,String>();
GroupsModule group = (GroupsModule) li.get(i);
String p_id = String.valueOf(group.getG_pid());
String g_id = String.valueOf(group.getGid());
if(searchResult.contains(g_id)){
map.put("textView1", group.getGname());
list.add(map);
groupNumList.add(group.getGid()+"");
}
}
if(list.size()==0){
//说明没有分组,查询员工
System.out.println("员工数:"+emps.size());
//员工循环
for(int j=0;j<emps.size();j++){
HashMap<String,String> map = new HashMap<String,String>();
EmpsModule emp = (EmpsModule) emps.get(j);
System.out.println("员工姓名:"+emp.getXm());
map.put("textView1", emp.getXm());
list.add(map);
empList.add(emp);
flag_cust = true;
}
System.out.println("是否有员工:"+flag_cust);
}
if(flag_cust && myApp.getModuleName().equals("wzcx")){
relayout.setVisibility(RelativeLayout.VISIBLE);
num = 13;
}
}
db.close_SqlDb();
initData(num);
}
private void initData(int num) {
System.out.println("人:"+list.size()+","+list.toString());
adapter=new MyAdapter(list, this,num);
fzLv.setAdapter(adapter);
fzLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//startActivity(new Intent().setClass(Fzcx.this, SonList.class));
if(flag_cust){
if(myApp.getModuleName().equals("wzcx")){
isSelected.clear();
System.out.println("编号:"+position);
for(int i=0;i<fzLv.getCount();i++){
adapter.getIsSelected().put(i, false);
}
adapter.getIsSelected().put(position, true);
isSelected.add(empList.get(position));
adapter.notifyDataSetChanged();
}else{//轨迹回放
Intent intent =new Intent();
intent.putExtra("emp", empList.get(position));
intent.setClass(EmpGroupList.this, TakePosition.class);
startActivity(intent);
finish();
}
}else{
String groupNum = groupNumList.get(position);
myApp.setGroupNums(groupNum);
startActivity(new Intent().setClass(EmpGroupList.this, EmpGroupList.class));
}
}
});
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(view.equals(sure)){
if(isSelected != null && isSelected.size()>0){
new NowSearchThread().start();
}else{
Toast.makeText(EmpGroupList.this, "请选择人员", Toast.LENGTH_SHORT).show();
}
}else{
if(isSelected != null && isSelected.size()>0){
new SearchHistoryThread().start();
}else{
Toast.makeText(EmpGroupList.this, "请选择人员", Toast.LENGTH_SHORT).show();
}
}
}
Handler myHander = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what){
case 1:
showDialog(1);
break;
case 2:
removeDialog(1);
showResult();
break;
case 3:
removeDialog(1);
Toast.makeText(EmpGroupList.this, msg.getData().getString("msg"), Toast.LENGTH_SHORT).show();
break;
case 4:
showDialog(2);
break;
case 5:
removeDialog(2);
showDesc();
break;
case 6:
removeDialog(2);
Toast.makeText(EmpGroupList.this, msg.getData().getString("msg"), Toast.LENGTH_SHORT).show();
break;
}
}
};
/**
* 位置查询线程
* @author Administrator
*
*/
class NowSearchThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
Message msg =new Message();
msg.what = 1;
myHander.sendMessage(msg);
Message msg2 =new Message();
String result = "";
Optsharepre_interface shareFile = new Optsharepre_interface(EmpGroupList.this);
JSONObject obj = new JSONObject();
EmpsModule empModle = isSelected.get(0);
try {
obj.put("xm", empModle.getXm());
obj.put("dwhm", empModle.getDwhm());
obj.put("zguid", empModle.getGuid());
obj.put("dwfs", empModle.getDwfs());
obj.put("gguid", shareFile.getDataFromPres("GGUID"));
obj.put("cosim", shareFile.getDataFromPres("COSIM"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bundle bunlde = new Bundle();
String errMsg = "";
locInfo.clear();
result = new UpdataToServer(EmpGroupList.this).dataToServer("WZCX", obj);
if (result != null && !result.equals("-1") && !result.equals("500")) {
try {
JSONObject return_obj = new JSONObject(result);
String res = return_obj.getString("result");
if (res.equals("0")) {
msg2.what = 2;
JSONObject pObject = return_obj.getJSONObject("locinfo");
PositionInfo position = new PositionInfo();
position.setAddTime(pObject.getString("addTime"));
position.setContent(pObject.getString("content"));
position.setLon(pObject.getDouble("lon"));
position.setLat(pObject.getDouble("lat"));
position.setZdmc(pObject.getString("zdmc"));
position.setSjhm(pObject.getString("sjhm"));
locInfo.add(position);
}else{
msg2.what = 3;
errMsg = return_obj.getString("detail");
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
msg2.what = 3;
errMsg = "定位失败!";
}
bunlde.putString("msg", errMsg);
msg2.setData(bunlde);
myHander.sendMessage(msg2);
Looper.loop();
}
}
/**
* 地图显示位置
*/
public void showResult(){
Intent intent = new Intent();
intent.putExtra("locList", locInfo);
intent.setClass(EmpGroupList.this, Wzcx_new.class);
startActivity(intent);
}
/**
* 位置查询线程
* @author Administrator
*
*/
class SearchHistoryThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
Message msg =new Message();
msg.what = 4;
myHander.sendMessage(msg);
Message msg2 =new Message();
String result = "";
JSONObject obj = new JSONObject();
EmpsModule empModle = isSelected.get(0);
try {
obj.put("zguid", empModle.getGuid());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bundle bunlde = new Bundle();
String errMsg = "";
locInfo.clear();
result = new UpdataToServer(EmpGroupList.this).dataToServer("LJDWDETAIL", obj);
if (result != null && !result.equals("-1") && !result.equals("500")) {
try {
JSONObject return_obj = new JSONObject(result);
String res = return_obj.getString("result");
if (res.equals("0")) {
msg2.what = 5;
JSONArray arr = return_obj.getJSONArray("locinfo");
for(int i=0;i<arr.length();i++){
JSONObject pObject = arr.getJSONObject(i);
PositionInfo position = new PositionInfo();
position.setAddTime(pObject.getString("addTime"));
position.setContent(pObject.getString("content"));
position.setLon(pObject.getDouble("lon"));
position.setLat(pObject.getDouble("lat"));
position.setZdmc(pObject.getString("zdmc"));
position.setSjhm(pObject.getString("sjhm"));
position.setSucess(pObject.getInt("sucess"));
position.setProvince(pObject.getString("province"));
position.setCity(pObject.getString("city"));
position.setCountry(pObject.getString("country"));
locInfo.add(position);
System.out.println("---------:"+position.getLon()+","+position.getLat());
}
}else{
msg2.what = 6;
errMsg = return_obj.getString("detail");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
msg2.what = 6;
errMsg = "获取文字详情失败!";
}
bunlde.putString("msg", errMsg);
msg2.setData(bunlde);
myHander.sendMessage(msg2);
Looper.loop();
}
}
/**
* 文字详情显示
*/
public void showDesc(){
Intent intent = new Intent();
intent.putExtra("moduleName", "wzxq");
intent.putExtra("locList", locInfo);
intent.setClass(EmpGroupList.this, Information.class);
startActivity(intent);
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch(id){
case 1:
dialog.setMessage("定位中,请稍候……");
break;
case 2:
dialog.setMessage("正在查询,请稍候……");
break;
case 3:
break;
}
return dialog;
}
}