package com.jqyd.son;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.jqyd.adapter.MyAdapter;
import com.jqyd.app.MyApp;
import com.jqyd.manager.R;
import com.jqyd.model.CustomerModule;
import com.jqyd.model.OrderModule;
import com.jqyd.model.OrderMxDetailModel;
import com.jqyd.model.ProTypeModule;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.UpdataToServer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class Ddsb extends Activity implements OnClickListener {
private EditText khmc;
private EditText lxr;
private EditText lxdh;
private EditText shr;
private EditText jhrq;
private EditText jhdz;
private EditText bz;
private EditText sjsj;
private EditText dgsl = null;
private EditText spmc;
private ListView listView;
private Button add_cust;
private Button add_good;
private Button up;
private Button back;
private Calendar c;
private MyApp myApp;
private Optdb_interfce db;
private MyAdapter adapter;
private ArrayList<String> custIds = new ArrayList<String>();
private ArrayList<String> custNames = new ArrayList<String>();
private ArrayList<String> goodIds = new ArrayList<String>();
private ArrayList<String> goodNames = new ArrayList<String>();
private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private String cid = "";
private String pid = "";
private String ckj = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ddsb);
khmc = (EditText) this.findViewById(R.id.khmc);
spmc = (EditText) this.findViewById(R.id.spmc);
lxr = (EditText) this.findViewById(R.id.lxr);
lxdh = (EditText) this.findViewById(R.id.lxdh);
shr = (EditText) this.findViewById(R.id.shr);
jhrq = (EditText) this.findViewById(R.id.jhrq);
jhdz = (EditText) this.findViewById(R.id.jhdz);
bz = (EditText) this.findViewById(R.id.bz);
sjsj = (EditText) this.findViewById(R.id.sjsj);
dgsl = (EditText) this.findViewById(R.id.dgsl);
listView = (ListView) this.findViewById(R.id.list);
add_cust = (Button) this.findViewById(R.id.add_cust);
add_good = (Button) this.findViewById(R.id.add_good);
up = (Button) this.findViewById(R.id.up);
back = (Button) this.findViewById(R.id.back);
add_cust.setOnClickListener(this);
add_good.setOnClickListener(this);
up.setOnClickListener(this);
back.setOnClickListener(this);
jhrq.setOnClickListener(this);
sjsj.setEnabled(false);
dgsl.setEnabled(false);
myApp = (MyApp) this.getApplication();
init();
initEnter();
}
public void init(){
c = Calendar.getInstance();
//业务时间默认当天
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String mDateTime1 = formatter.format(date);
jhrq.setText(mDateTime1);
sjsj.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(list !=null && list.size()>0 && !sjsj.getText().toString().equals("")){
//更新界面列表显示
HashMap<String,String> map = list.get(list.size()-1);
map.put("saleprice", sjsj.getText().toString());
list.remove(list.size()-1);
list.add(map);
setShow();
//更新订单详情
ArrayList<HashMap<String,String>> ls = myApp.getOrderList();
HashMap<String,String> map2 =ls.get(ls.size()-1);
map2.put("saleprice", sjsj.getText().toString());
ls.remove(ls.size()-1);
ls.add(map2);
myApp.setOrderList(ls);
}
}
@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
}
});
dgsl.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(list !=null && list.size()>0 && !dgsl.getText().toString().equals("")){
//更新界面列表显示
HashMap<String,String> map = list.get(list.size()-1);
map.put("spnum", dgsl.getText().toString());
list.remove(list.size()-1);
list.add(map);
setShow();
//更新订单详情
ArrayList<HashMap<String,String>> ls = myApp.getOrderList();
HashMap<String,String> map2 =ls.get(ls.size()-1);
map2.put("spnum", dgsl.getText().toString());
ls.remove(ls.size()-1);
ls.add(map2);
myApp.setOrderList(ls);
}
}
@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 initEnter(){
//System.out.println("二次进宫-----------------"+myApp.getModuleName());
//System.out.println("二次进宫-----------------"+myApp.getLxrTel().size()+"---------"+myApp.getLxrTelAndName().size());
list.clear();
String pName = "";
String cName = "";
//为客户赋值--单选客户
custIds = myApp.getCustId();
custNames = myApp.getCustIdAndName();
if(custIds.size()>0){
cid = custIds.get(0);
System.out.println("客户id---------------------"+cid);
cName = custNames.get(0);
//根据客户id,查询客户相关信息
db = new Optdb_interfce(Ddsb.this);
CustomerModule cust = db.searchCustById(cid);
db.close_SqlDb();
khmc.setText(cName);
lxr.setText(cust.getLinkman());
lxdh.setText(cust.getLink_sim());
shr.setText(cust.getLinkman());
jhdz.setText(cust.getAddress());
}
//为商品赋值--单选商品
goodIds = myApp.getLxrTel();
goodNames = myApp.getLxrTelAndName();
if(goodIds.size()>0){
sjsj.setEnabled(true);
dgsl.setEnabled(true);
pid = goodIds.get(0);
pName = goodNames.get(0);
//根据商品id,查询商品相关信息
db = new Optdb_interfce(Ddsb.this);
ProTypeModule product = db.searchProductById(pid);
db.close_SqlDb();
//保存数据
ArrayList<HashMap<String,String>> ls = myApp.getOrderList();
HashMap<String,String> order_map = myApp.getOrder_map();
System.out.println("订单保存的数据-----------------"+order_map.get("saleprice")+","+order_map.get("spnum")
+","+order_map.get("prises")+","+order_map.get("bz")+","+order_map.get("jhrq"));
HashMap<String,String> map = new HashMap<String,String>();
map.put("spid", product.getId());
map.put("spname", product.getName());
map.put("ggxh", product.getModel());
map.put("unit", product.getUnit());
map.put("ckprice", product.getLsPrise());
map.put("saleprice", product.getLsPrise());
map.put("spnum", order_map.get("spnum"));
map.put("prises", order_map.get("prises"));
ls.add(map);
myApp.setOrderList(ls);
//******************
//只能选择同一客户
add_cust.setClickable(false);
bz.setText(order_map.get("bz"));
jhrq.setText(order_map.get("jhrq"));
sjsj.setText(product.getLsPrise());
dgsl.setText("0");
spmc.setText(product.getName());
ckj = product.getLsPrise();
}
//每次回到当前界面,判断是否已经添加过数据至共享池了,如果有显示,否则不显示
ArrayList<HashMap<String,String>> ls = myApp.getOrderList();
System.out.println("共享池中保存数据--------"+ls.size());
if(ls.size()>0){
for(int i=0;i<ls.size();i++){
HashMap<String,String> map = ls.get(i);
map.put("spid", map.get("spid"));
map.put("spname", map.get("spname"));
map.put("ggxh", map.get("ggxh"));
map.put("unit", map.get("unit"));
map.put("ckprice", map.get("ckprice"));
map.put("saleprice", map.get("saleprice"));
map.put("spnum", map.get("spnum"));
map.put("prises", map.get("prises"));
list.add(map);
}
setShow();
}
}
/**
* 清空历史数据
*/
public void cheanHisdatas(){
sjsj.setText("");
dgsl.setText("");
bz.setText("");
init();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == add_cust){
myApp.setModuleName("ddsb_cust");
startActivity(new Intent().setClass(Ddsb.this, MyOneData.class).putExtra("moduleName", "ddsb_cust"));
finish();
}else if(v == add_good){
myApp.setModuleName("ddsb_good");
//保存当前界面的数据至共享池中
HashMap<String,String> map = new HashMap<String,String>();
//计算总金额
double prises = 0.0;
System.out.println("----------订单测试------------"+dgsl.getText().toString());
if(!dgsl.getText().toString().equals("") && !sjsj.getText().toString().equals("")){
prises = Double.parseDouble(dgsl.getText().toString()) * Double.parseDouble(sjsj.getText().toString());
}
map.put("prises", prises+"");
map.put("spnum", "0");
map.put("saleprice", ckj);
map.put("jhrq", jhrq.getText().toString());
map.put("bz", bz.getText().toString());
myApp.setOrder_map(map);
startActivity(new Intent().setClass(Ddsb.this, MyOneData.class).putExtra("moduleName", "ddsb_good"));
finish();
}else if(v == up){
if(myApp.getOrderList().size()>0){
new AlertDialog.Builder(Ddsb.this).setTitle("确认提示").setMessage("确认要上报订单吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new OrderThread().start();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}else{
warnTell("请先添加商品!");
}
}else if(v == jhrq){
getDate();
}else if(v == back){
finish();
}
}
//设置界面显示
public void setShow(){
adapter=new MyAdapter(list, this,11);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
}
//弹出日期框
public void getDate() {
new DatePickerDialog(Ddsb.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker arg0, int year, int month,
int date) {
// TODO Auto-generated method stub
String m = "";
String d = "";
if ((month + 1) < 10) {
m = "0" + (month + 1);
} else {
m = (month + 1) + "";
}
if (date < 10) {
d = "0" + date;
} else {
d = date + "";
}
String time = year + "-" + m + "-" + d;
jhrq.setText(time);
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)).show();
}
/**
* 订单上报线程
* @author Administrator
*
*/
class OrderThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
Message message = new Message();
message.what = 1;
myHander.sendMessage(message);
Message message2 = new Message();
message2.what = 2;
Bundle bundle = new Bundle();
JSONObject object = new JSONObject();
Optsharepre_interface share_obj = new Optsharepre_interface(Ddsb.this);
db = new Optdb_interfce(Ddsb.this);
//获得登录者姓名
String userName = db.searchEmpName(share_obj.getDataFromPres("GUID"));
db.close_SqlDb();
//订单总金额
double ddCounts = 0.0;
ArrayList<HashMap<String,String>> ls = myApp.getOrderList();
ArrayList<OrderMxDetailModel> mxlist = new ArrayList<OrderMxDetailModel>();
JSONArray mxlist_arr = new JSONArray();
OrderModule order = new OrderModule();
for(int i=0;i<ls.size();i++){
HashMap<String,String> map = ls.get(i);
JSONObject obj = new JSONObject();
try {
obj.put("ckprice", map.get("ckprice"));
obj.put("ggxh", map.get("ggxh"));
obj.put("prises", map.get("prises"));
obj.put("saleprice", map.get("saleprice"));
obj.put("spid", map.get("spid"));
obj.put("spname", map.get("spname"));
obj.put("spnum", map.get("spnum"));
obj.put("unit", map.get("unit"));
obj.put("ddbh","-1");
mxlist_arr.put(obj);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ddCounts+=Double.parseDouble(map.get("prises"));
}
order.setBz(bz.getText().toString());
order.setCid(cid);
order.setCname(khmc.getText().toString());
order.setCounts(ddCounts+"");
order.setFh_date(jhrq.getText().toString());
order.setGguid(share_obj.getDataFromPres("GGUID"));
order.setGuid(share_obj.getDataFromPres("GUID"));
order.setSjzg(share_obj.getDataFromPres("SJZG"));
order.setLxr(lxr.getText().toString());
order.setLxrtel(lxdh.getText().toString());
order.setMxlist(mxlist);
order.setShaddr(jhdz.getText().toString());
order.setShr(shr.getText().toString());
order.setXd_person(userName);
try {
object.put("cid", order.getCid());
object.put("cname", order.getCname());
object.put("counts",order.getCounts());
object.put("fh_date", order.getFh_date());
object.put("gguid", order.getGguid());
object.put("guid", order.getGuid());
object.put("sjzg", order.getSjzg());
object.put("lxr", order.getLxr());
object.put("lxrtel", order.getLxrtel());
object.put("shaddr", order.getShaddr());
object.put("shr", order.getShr());
object.put("xd_person", order.getXd_person());
object.put("mxlist", mxlist_arr);
object.put("bz", bz.getText().toString());
object.put("shaddr", jhdz.getText().toString());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("订单上报----------------"+object.toString());
// 请求服务器
String result = new UpdataToServer(Ddsb.this).dataToServer("DDSB", object);
if (result != null && !result.equals("-1") && !result.equals("500")) {
try {
JSONObject obj = new JSONObject(result);
String res = obj.getString("result");
if (res.equals("0")) {
bundle.putString("msg","上传成功");
} else {
bundle.putString("msg","上传失败!");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(result.equals("-1")){
bundle.putString("msg", "抱歉,网络连接失败!");
} else if(result.equals("500")){
bundle.putString("msg", "上报失败,请联系你的管理员!");
}
message2.setData(bundle);
myHander.sendMessage(message2);
Looper.loop();
}
}
/**
* 非空警告提示
*/
public void warnTell(String message){
new AlertDialog.Builder(Ddsb.this).setTitle("警告").setMessage(message)
.setPositiveButton("确定", null).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);
Bundle data = msg.getData();
String message = data.getString("msg");
showToast(message);
break;
}
}
};
/**
* 结果显示
*
* @param message
*/
public void showToast(final String message) {
new AlertDialog.Builder(Ddsb.this).setTitle("提示").setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(message.contains("成功")){
//清空数据
myApp.setOrderList(new ArrayList<HashMap<String,String>>());
myApp.setOrder_map(new HashMap<String,String>());
khmc.setText("");
cid = "";
lxr.setText("");
lxdh.setText("");
shr.setText("");
bz.setText("");
jhdz.setText("");
sjsj.setText("");
dgsl.setText("");
spmc.setText("");
init();
list.clear();
setShow();
add_cust.setClickable(true);
}
}
}).show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(Ddsb.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch (id) {
case 1:
dialog.setMessage("正在上传,请稍候……");
break;
}
return dialog;
}
}