package com.jqyd.son;
import java.util.ArrayList;
import java.util.HashMap;
import com.jqyd.adapter.MyAdapter;
import com.jqyd.app.MyApp;
import com.jqyd.manager.R;
import com.jqyd.manager.TabTitle;
import com.jqyd.model.CustomerModule;
import com.jqyd.model.EmpsModule;
import com.jqyd.model.GroupsModule;
import com.jqyd.model.ProTypeModule;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class MyLxr extends Activity implements OnClickListener{
private ListView listView;
private Button sure;
private Button cancel;
private EditText find;
private MyApp myApp;
private MyAdapter adapter;
private ArrayList<HashMap<String, String>> listInfo = new ArrayList<HashMap<String, String>>();
private ArrayList<String> lxrTels = new ArrayList<String>();
private ArrayList<String> lxrTelAndNames = new ArrayList<String>();
private ArrayList<String> custId = new ArrayList<String>();//临时创建
private ArrayList<String> custIdAndName = new ArrayList<String>(); //临时创建
private ArrayList<String> listGroups = new ArrayList<String>();
private String moduleName;
private Boolean isSearch = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylxr);
listView = (ListView) this.findViewById(R.id.list);
sure = (Button) this.findViewById(R.id.sure);
cancel = (Button) this.findViewById(R.id.cancel);
find = (EditText) this.findViewById(R.id.find);
find.setVisibility(EditText.GONE);
sure.setOnClickListener(this);
cancel.setOnClickListener(this);
myApp = (MyApp) this.getApplication();
Intent intent = this.getIntent();
moduleName = intent.getStringExtra("moduleName");
loadInfo();
setSearch();
if(moduleName.equals("cust_list")){
find.setVisibility(EditText.VISIBLE);
}
}
public void loadVirtualDatas(){
Optdb_interfce db = new Optdb_interfce(MyLxr.this);
ArrayList<Object> list_obj = new ArrayList<Object>();
Optsharepre_interface share_obj = new Optsharepre_interface(MyLxr.this);
HashMap<String,String> map = null;
if(moduleName.equals("ywtz_lxr_list") || moduleName.equals("sms_lxr_list") || moduleName.equals("jhgl_emp_list")){
listInfo = new ArrayList<HashMap<String, String>>();
String groupStr = db.RecursionGroup(share_obj.getDataFromPres("GLFW"), 1, 2,0);
Log.e("share_obj.getDataFromPres(GLFW)", share_obj.getDataFromPres("GLFW"));
String groups[] = groupStr.split("#");
for(int i=0;i<groups.length;i++){
String gps[] = groups[i].split(",");
ArrayList<Object> ls = db.searchEmpsByGps(gps[0],"");
for(int k=0;k<ls.size();k++){
list_obj.add(ls.get(k));
}
}
for(int i=0;i<list_obj.size();i++){
map = new HashMap<String, String>();
EmpsModule emp = (EmpsModule) list_obj.get(i);
map.put("textView1", emp.getXm());
map.put("textView2", emp.getDwhm());
listInfo.add(map);
}
}else if(moduleName.equals("ywtz_lxr_group") || moduleName.equals("sms_lxr_group") || moduleName.equals("jhgl_emp_group")){
//listInfo = VirtualData.getCustGroupInfo2();
//list_obj = db.searchGroups("GROUPS", share_obj.getDataFromPres("GID"),2);
listInfo = new ArrayList<HashMap<String, String>>();
list_obj = db.searchGroups(share_obj.getDataFromPres("GLFW"), 2);
for(int i=0;i<list_obj.size();i++){
map = new HashMap<String, String>();
GroupsModule group = (GroupsModule) list_obj.get(i);
map.put("textView1", group.getGname());
map.put("textView2", "");
map.put("gid", group.getGid()+"");
listInfo.add(map);
}
}else if(moduleName.equals("cust_list") || moduleName.equals("jhgl_cust_list") || moduleName.equals("ddsb_cust_list")){
listInfo = new ArrayList<HashMap<String, String>>();
ArrayList<CustomerModule> customers = db.searchCustomers(1,1,"");
for(int i=0;i<customers.size();i++){
CustomerModule cust = (CustomerModule) customers.get(i);
map = new HashMap<String, String>();
map.put("textView1", cust.getShort_name());
map.put("textView2", "");
map.put("cid", cust.getCid());
listInfo.add(map);
}
}else if(moduleName.equals("cust_group") || moduleName.equals("jhgl_cust_group") || moduleName.equals("ddsb_cust_group")){
listInfo = new ArrayList<HashMap<String, String>>();
list_obj = db.searchFGroups("0",1);
for(int i=0;i<list_obj.size();i++){
map = new HashMap<String, String>();
GroupsModule group = (GroupsModule) list_obj.get(i);
map.put("textView1", group.getGname());
map.put("textView2", "");
map.put("gid", group.getGid()+"");
listInfo.add(map);
}
}else if(moduleName.equals("ddsb_good_list")){
listInfo = new ArrayList<HashMap<String, String>>();
ArrayList<ProTypeModule> goods = db.searchProducts(1,1,"");
for(int i=0;i<goods.size();i++){
ProTypeModule good = (ProTypeModule) goods.get(i);
map = new HashMap<String, String>();
map.put("textView1", good.getName());
map.put("textView2", "");
map.put("pid", good.getId());
listInfo.add(map);
}
}else if(moduleName.equals("ddsb_good_group")){
listInfo = new ArrayList<HashMap<String, String>>();
list_obj = db.searchFGroups("0",3);
for(int i=0;i<list_obj.size();i++){
map = new HashMap<String, String>();
GroupsModule group = (GroupsModule) list_obj.get(i);
map.put("textView1", group.getGname());
map.put("textView2", "");
map.put("gid", group.getGid()+"");
listInfo.add(map);
}
}
db.close_SqlDb();
}
private void setSearch(){
find.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
isSearch=true;
Optdb_interfce db = new Optdb_interfce(MyLxr.this);
HashMap<String,String> map = null;
if(moduleName.equals("cust_list")){
listInfo = new ArrayList<HashMap<String, String>>();
ArrayList<CustomerModule> customers = db.searchCustomers(1,1,find.getText().toString());
Log.e("customers.size()", customers.size()+"");
listInfo.clear();
for(int i=0;i<customers.size();i++){
CustomerModule cust = (CustomerModule) customers.get(i);
map = new HashMap<String, String>();
map.put("textView1", cust.getShort_name());
map.put("textView2", "");
map.put("cid", cust.getCid());
listInfo.add(map);
}
db.close_SqlDb();
loadInfo();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
}
/**
* 初始化,加载信息
*/
public void loadInfo() {
if(!isSearch){
loadVirtualDatas();
}
Log.e("listInfo", listInfo.toString());
adapter = new MyAdapter(listInfo,MyLxr.this,5);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
LinearLayout ll = (LinearLayout)arg1;// 获得子级
CheckBox chkone = (CheckBox) ll.findViewById(R.id.select);// 从子级中获得控件
if(moduleName.equals("ywtz_lxr_group") || moduleName.equals("sms_lxr_group") || moduleName.equals("cust_group") ||
moduleName.equals("jhgl_cust_group") || moduleName.equals("jhgl_emp_group") || moduleName.equals("ddsb_good_group")){//存放的是分组的id
String gid= listInfo.get(position).get("gid");
if(chkone.isChecked()){
chkone.setChecked(false);
listGroups.remove(gid);
}else{
chkone.setChecked(true);
listGroups.add(gid);
}
}else if(moduleName.equals("ywtz_lxr_list") || moduleName.equals("sms_lxr_list") || moduleName.equals("jhgl_emp_list")){//存放的是员工的手机号和名称
String tel = listInfo.get(position).get("textView2");
String username = listInfo.get(position).get("textView1");
String telName = username+"<"+tel+">";
if(chkone.isChecked()){
//System.out.println("取消选择:"+position+"----------"+lxrTels.size());
chkone.setChecked(false);
lxrTels.remove(tel);
lxrTelAndNames.remove(telName);
}else{
//System.out.println("选择:"+position+"----------"+lxrTels.size());
chkone.setChecked(true);
lxrTels.add(tel);
lxrTelAndNames.add(telName);
}
}else if(moduleName.equals("cust_list") || moduleName.equals("ddsb_good_list")){//存放的是客户的id和名称或商品的id和名称
String ids = listInfo.get(position).get("cid");//客户id
String custNames = listInfo.get(position).get("textView1");//客户名称
lxrTels=myApp.getLxrTel();
lxrTelAndNames=myApp.getLxrTelAndName();
if(chkone.isChecked()){
chkone.setChecked(false);
lxrTels.remove(ids);
lxrTelAndNames.remove(custNames);
}else{
Log.e("custNames", custNames);
chkone.setChecked(true);
lxrTels.add(ids);
lxrTelAndNames.add(custNames);
}
}else if(moduleName.equals("jhgl_cust_list") || moduleName.equals("ddsb_cust_list")){//存放的是客户的id和名称
String ids = listInfo.get(position).get("cid");//客户id
String custNames = listInfo.get(position).get("textView1");//客户名称
if(chkone.isChecked()){
chkone.setChecked(false);
custId.remove(ids);
custIdAndName.remove(custNames);
}else{
chkone.setChecked(true);
custId.add(ids);
custIdAndName.add(custNames);
}
}
}
});
}
/**
* 捕获返回键
*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
if(moduleName.equals("ywtz_lxr_group") || moduleName.equals("ywtz_lxr_list")){
myApp.setModuleName("ywtz");
}else if(moduleName.equals("sms_lxr_group") || moduleName.equals("sms_lxr_list")){
myApp.setModuleName("dxqf");
}else if(moduleName.equals("cust_group") || moduleName.equals("cust_list")){
myApp.setModuleName("wdjh");
}else if(moduleName.equals("jhgl_emp_group") || moduleName.equals("jhgl_emp_list")){
myApp.setModuleName("jh_emp");
}else if(moduleName.equals("jhgl_cust_group") || moduleName.equals("jhgl_cust_list")){
myApp.setModuleName("jh_cust");
}else if(moduleName.equals("ddgl_cust_group") || moduleName.equals("ddgl_cust_list")){
myApp.setModuleName("dd_cust");
}else if(moduleName.equals("ddgl_good_group") || moduleName.equals("ddgl_good_list")){
myApp.setModuleName("dd_good");
}
startActivity(new Intent().setClass(MyLxr.this, TabTitle.class));
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(moduleName.equals("ywtz_lxr_group") || moduleName.equals("ywtz_lxr_list")){
myApp.setModuleName("ywtz");
}else if(moduleName.equals("sms_lxr_group") || moduleName.equals("sms_lxr_list")){
myApp.setModuleName("dxqf");
}else if(moduleName.equals("cust_group") || moduleName.equals("cust_list")){
myApp.setModuleName("wdjh");
}else if(moduleName.equals("jhgl_emp_group") || moduleName.equals("jhgl_emp_list")){
myApp.setModuleName("jh_emp");
}else if(moduleName.equals("jhgl_cust_group") || moduleName.equals("jhgl_cust_list")){
myApp.setModuleName("jh_cust");
}else if(moduleName.equals("ddgl_cust_group") || moduleName.equals("ddgl_cust_list")){
myApp.setModuleName("dd_cust");
}else if(moduleName.equals("ddgl_good_group") || moduleName.equals("ddgl_good_list")){
myApp.setModuleName("dd_good");
}
if(v == sure){
//判断是否选择了某项
if((lxrTels.size()<=0 || lxrTelAndNames.size()<=0) &&
(custId.size()<=0 || custIdAndName.size()<=0) &&
listGroups.size()<=0){
System.out.println("没有做任何选择");
}else{
//再次进入,首先取出上次选择的联系人对象,然后添加至数据池中,再次将数据池中的数据返回值信息发布界面
if(myApp.getLxrTel().size()>0){
Log.e("myApp.getLxrTel().size()", myApp.getLxrTel().size()+"");
for(int i=0;i<myApp.getLxrTel().size();i++){
if(!lxrTelAndNames.contains(myApp.getLxrTelAndName().get(i)) &&!lxrTels.contains(myApp.getLxrTel().get(i))){
lxrTelAndNames.add(myApp.getLxrTelAndName().get(i));
lxrTels.add(myApp.getLxrTel().get(i));
}
}
}
if(myApp.getCustId().size()>0){
for(int i=0;i<myApp.getCustId().size();i++){
if(!custId.contains(myApp.getCustId().get(i)) &&!custIdAndName.contains(myApp.getCustIdAndName().get(i))){
custId.add(myApp.getCustId().get(i));
custIdAndName.add(myApp.getCustIdAndName().get(i));
}
}
}
if(moduleName.equals("ywtz_lxr_group") || moduleName.equals("sms_lxr_group") || moduleName.equals("jhgl_emp_group")){
Optdb_interfce db = new Optdb_interfce(MyLxr.this);
ArrayList<Object> list_obj = new ArrayList<Object>();
for(int i=0;i<listGroups.size();i++){
String groupStr = db.RecursionGroup(listGroups.get(i), 1, 2,0);
String groups[] = groupStr.split("#");
for(int k=0;k<groups.length;k++){
String gps[] = groups[k].split(",");
ArrayList<Object> ls = db.searchEmpsByGps(gps[0],"");
for(int m=0;m<ls.size();m++){
list_obj.add(ls.get(m));
}
}
for(int n=0;n<list_obj.size();n++){
EmpsModule emp = (EmpsModule) list_obj.get(n);
String username = emp.getXm();
String tel = emp.getDwhm();
lxrTels.add(tel);
lxrTelAndNames.add(username+"<"+tel+">");
}
}
db.close_SqlDb();
myApp.setLxrTel(lxrTels);
myApp.setLxrTelAndName(lxrTelAndNames);
}else if(moduleName.equals("cust_group")){
Optdb_interfce db = new Optdb_interfce(MyLxr.this);
ArrayList<Object> list_obj = new ArrayList<Object>();
for(int i=0;i<listGroups.size();i++){
String groupStr = db.RecursionGroup(listGroups.get(i), 1, 1,0);
String groups[] = groupStr.split("#");
for(int k=0;k<groups.length;k++){
String gps[] = groups[k].split(",");
ArrayList<Object> ls = db.searchCustsByGps(gps[0],"");
for(int m=0;m<ls.size();m++){
list_obj.add(ls.get(m));
}
}
for(int n=0;n<list_obj.size();n++){
CustomerModule cust = (CustomerModule) list_obj.get(n);
String custName = cust.getShort_name();
String custId = cust.getCid();
if(!myApp.getLxrTelAndName().contains(custName) ||!myApp.getLxrTel().contains(custId)){
lxrTels.add(custId);
lxrTelAndNames.add(custName);
}
}
}
db.close_SqlDb();
myApp.setLxrTel(lxrTels);
myApp.setLxrTelAndName(lxrTelAndNames);
}else if(moduleName.equals("ddsb_good_group")){
Optdb_interfce db = new Optdb_interfce(MyLxr.this);
ArrayList<Object> list_obj = new ArrayList<Object>();
for(int i=0;i<listGroups.size();i++){
String groupStr = db.RecursionGroup(listGroups.get(i), 1, 3,0);
String groups[] = groupStr.split("#");
for(int k=0;k<groups.length;k++){
String gps[] = groups[k].split(",");
ArrayList<Object> ls = db.searchCustsByGps(gps[0],"");
for(int m=0;m<ls.size();m++){
list_obj.add(ls.get(m));
}
}
for(int n=0;n<list_obj.size();n++){
ProTypeModule product = (ProTypeModule) list_obj.get(n);
String goodName = product.getName();
String goodId = product.getId();
lxrTels.add(goodId);
lxrTelAndNames.add(goodName);
}
}
db.close_SqlDb();
myApp.setLxrTel(lxrTels);
myApp.setLxrTelAndName(lxrTelAndNames);
}else if(moduleName.equals("jhgl_cust_group") || moduleName.equals("ddsb_cust_group")){
Optdb_interfce db = new Optdb_interfce(MyLxr.this);
ArrayList<Object> list_obj = new ArrayList<Object>();
for(int i=0;i<listGroups.size();i++){
String groupStr = db.RecursionGroup(listGroups.get(i), 1, 1,0);
String groups[] = groupStr.split("#");
for(int k=0;k<groups.length;k++){
String gps[] = groups[k].split(",");
ArrayList<Object> ls = db.searchCustsByGps(gps[0],"");
for(int m=0;m<ls.size();m++){
list_obj.add(ls.get(m));
}
}
for(int n=0;n<list_obj.size();n++){
CustomerModule cust = (CustomerModule) list_obj.get(n);
String custName = cust.getShort_name();
String id = cust.getCid();
custId.add(id);
custIdAndName.add(custName);
}
}
db.close_SqlDb();
myApp.setCustId(custId);
myApp.setCustIdAndName(custIdAndName);
}else if(moduleName.equals("ywtz_lxr_list") || moduleName.equals("sms_lxr_list") || moduleName.equals("cust_list")
|| moduleName.equals("jhgl_emp_list") || moduleName.equals("ddsb_good_list")){
myApp.setLxrTel(lxrTels);
myApp.setLxrTelAndName(lxrTelAndNames);
}else if(moduleName.equals("jhgl_cust_list") || moduleName.equals("ddsb_cust_list")){
myApp.setCustId(custId);
myApp.setCustIdAndName(custIdAndName);
}
myApp.setIsInit(1);
}
startActivity(new Intent().setClass(MyLxr.this, TabTitle.class));
finish();
}else if(v == cancel){
startActivity(new Intent().setClass(MyLxr.this, TabTitle.class));
finish();
}
}
}