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 com.jqyd.adapter.MyAdapter;
import com.jqyd.app.MyApp;
import com.jqyd.manager.MainActivity;
import com.jqyd.manager.R;
import com.jqyd.model.OrderModule;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.UpdataToServer;
import com.jqyd.son.InfoAndAct;
import com.jqyd.son.Rzsb;
import com.jqyd.son.Scjc;
import android.app.Activity;
import android.app.AlertDialog;
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.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class InfoSure extends Activity implements OnClickListener {
private TextView custDetail;
private ListView listView;
private Button up;
private Button back;
private MyApp myApp;
private MyAdapter adapter;
private ArrayList<HashMap<String,String>> productList = null;
private Bundle custBundle = null;
private ArrayList<Bundle> goodList = null;
private double allprises = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.infosure);
custDetail = (TextView) this.findViewById(R.id.custDetail);
listView = (ListView) this.findViewById(R.id.listView);
up = (Button) this.findViewById(R.id.up);
back = (Button) this.findViewById(R.id.back);
up.setOnClickListener(this);
back.setOnClickListener(this);
myApp = (MyApp) this.getApplication();
System.out.println("模块名称:"+myApp.getModuleName());
if(myApp.getModuleName().equals("xssb")){
this.setTitle("销售上报信息确认");
up.setText("销售上报");
}else if(myApp.getModuleName().equals("kcsb")){
this.setTitle("库存上报信息确认");
up.setText("库存上报");
}else if(myApp.getModuleName().equals("jpsb")){
this.setTitle("竞品上报信息确认");
up.setText("竞品上报");
back.setText("选择竞品");
}
if(myApp.getModuleName().equals("ddsb")){
loadOrderData();
}else{
loadSjcjData();
}
}
/**
* 加载订单数据
*/
public void loadOrderData(){
goodList = myApp.getGoodList();
//加载客户信息
custBundle = myApp.getCustBundle();
String custInfo = "";
/* custInfo = "下单人:"+custBundle.getString("xdr")
+"<br>客户名称:"+custBundle.getString("khmc")+"</br>"
+"<br>收货人:"+custBundle.getString("shr")+"</br>"
+"<br>联系电话:"+custBundle.getString("lxdh")+"</br>"
+"<br>交货日期:"+custBundle.getString("jhrq")+"</br>"
+"<br>交货地址:"+custBundle.getString("jhdz")+"</br>";*/
//计算总金额
for(int i=0;i<goodList.size();i++){
Bundle bundle = goodList.get(i);
System.out.println("**********"+bundle.toString());
if(bundle.getString("prises") != null && !bundle.getString("prises").equals("")){
allprises+=Double.parseDouble(bundle.getString("prises"));
}
}
custInfo+="订单总金额:"+allprises+"元"
+"<p>备注:"+custBundle.getString("bz")+"</p>"
+"<font color=red>温馨提示:单击可删除已选择商品</font>";
custDetail.setText(Html.fromHtml(custInfo));
//加载商品信息
HashMap<String,String> map = null;
productList = new ArrayList<HashMap<String, String>>();
String goodInfo = "";
for(int i=0;i<goodList.size();i++){
Bundle bundle = goodList.get(i);
map = new HashMap<String, String>();
goodInfo = bundle.getString("spmc")
+"<p>"+bundle.getString("ggxh")+"</p>"
+"<p>参考单价:"+bundle.getString("ckdj")+"元 实际售价:"+bundle.getString("sjsj")+"元</p>"
+"订购数量:"+bundle.getString("sl")+bundle.getString("dw");
map.put("textView1", goodInfo);
productList.add(map);
}
showData();
}
/**
* 加载数据采集数据
*/
public void loadSjcjData(){
goodList = myApp.getGoodList();
//加载客户信息
custBundle = myApp.getCustBundle();
String fzid = myApp.getFzid();
if(fzid != null && !fzid.equals("")){
System.out.println("选择了分组上报数据!");
custDetail.setVisibility(TextView.GONE);
}else{
String custInfo = "客户名称:"+custBundle.getString("cname");
custDetail.setText(Html.fromHtml(custInfo));
}
//加载商品信息
HashMap<String,String> map = null;
productList = new ArrayList<HashMap<String, String>>();
String goodInfo = "";
for(int i=0;i<goodList.size();i++){
Bundle bundle = goodList.get(i);
map = new HashMap<String, String>();
if(myApp.getModuleName().equals("xssb")){
goodInfo = bundle.getString("spmc")
+"<p>"+bundle.getString("ggxh")+"</p>"
+"<p>销售售价:"+bundle.getString("sjsj")+"元</p>"
+"销售数量:"+bundle.getString("sl")+bundle.getString("dw");
}else if(myApp.getModuleName().equals("jpsb")){
goodInfo = bundle.getString("spmc")
+"<p>"+bundle.getString("ggxh")+"</p>"
+"销售数量:"+bundle.getString("sl")+bundle.getString("dw");
}else if(myApp.getModuleName().equals("kcsb")){
goodInfo = bundle.getString("spmc")
+"<p>"+bundle.getString("ggxh")+"</p>"
+"库存量:"+bundle.getString("kcl")+bundle.getString("dw");
}
map.put("textView1", goodInfo);
productList.add(map);
}
showData();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == up){
if(myApp.getModuleName().equals("ddsb")){
if(goodList.size()>0){
new OrderThread().start();
}else{
warnTell("商品为空不能上报订单!");
}
}else{
if(goodList.size()>0){
if(myApp.getModuleName().equals("xssb")){
new SjcjThread(1).start();
}else if(myApp.getModuleName().equals("kcsb")){
new SjcjThread(2).start();
}else{
new SjcjThread(3).start();
}
}else{
if(myApp.getModuleName().equals("xssb") || myApp.getModuleName().equals("kcsb")){
warnTell("商品为空不能上报!");
}else{
warnTell("竞品为空不能上报!");
}
}
}
}else{
myApp.setGroupNums("");
myApp.setGoodGroupNum("");
startActivity(new Intent().setClass(InfoSure.this, GoodGroupList.class));
}
}
/**
* 警告提示
*/
public void warnTell(String message){
new AlertDialog.Builder(InfoSure.this).setTitle("警告").setMessage(message)
.setPositiveButton("确定", null).show();
}
//设置界面显示
public void showData(){
adapter=new MyAdapter(productList, this,1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
final int pos = position;
new AlertDialog.Builder(InfoSure.this).setTitle("提示").setMessage("确认删除吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
productList.remove(pos);
goodList.remove(pos);
showData();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).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(InfoSure.this);
JSONArray mxlist_arr = new JSONArray();
for(int i=0;i<goodList.size();i++){
Bundle bundle2 = goodList.get(i);
JSONObject obj = new JSONObject();
try {
obj.put("ckprice", bundle2.getString("ckdj"));
obj.put("ggxh", bundle2.getString("ggxh"));
obj.put("prises", bundle2.getString("prises"));
obj.put("saleprice", bundle2.getString("sjsj"));
obj.put("spid", bundle2.getString("spbh"));
obj.put("spname", bundle2.getString("spmc"));
obj.put("spnum", bundle2.getString("sl"));
obj.put("unit", bundle2.getString("dw"));
obj.put("ddbh","-1");
mxlist_arr.put(obj);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
object.put("cid", custBundle.getString("cid"));
object.put("cname", custBundle.getString("khmc"));
object.put("counts",allprises+"");
object.put("fh_date", custBundle.getString("jhrq"));
object.put("gguid", share_obj.getDataFromPres("GGUID"));
object.put("guid", share_obj.getDataFromPres("GUID"));
object.put("sjzg", share_obj.getDataFromPres("SJZG"));
object.put("lxr", custBundle.getString("lxr"));
object.put("lxrtel", custBundle.getString("lxdh"));
object.put("shaddr", custBundle.getString("jhdz"));
object.put("shr", custBundle.getString("shr"));
object.put("xd_person", custBundle.getString("xdr"));
object.put("mxlist", mxlist_arr);
object.put("bz", custBundle.getString("bz"));
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("订单上报----------------"+object.toString());
// 请求服务器
String result = new UpdataToServer(InfoSure.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();
}
}
/**
* 数据采集线程
* @author Administrator
*
*/
class SjcjThread extends Thread{
private int type = 0;
public SjcjThread(int type){
this.type = type;
}
@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(InfoSure.this);
JSONArray mxlist_arr = new JSONArray();
for(int i=0;i<goodList.size();i++){
Bundle bundle2 = goodList.get(i);
JSONObject obj = new JSONObject();
try {
obj.put("spid", bundle2.getString("spbh"));
obj.put("spname", bundle2.getString("spmc"));
obj.put("xssl", bundle2.getString("sl"));
obj.put("kcl", bundle2.getString("kcl"));
obj.put("unit", bundle2.getString("dw"));
obj.put("xsdj", bundle2.getString("sjsj"));
mxlist_arr.put(obj);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Optdb_interfce db = new Optdb_interfce(InfoSure.this);
//获得登录者姓名
String userName = db.searchEmpName(share_obj.getDataFromPres("GUID"));
db.close_SqlDb();
try {
object.put("cuid", custBundle.getString("cid"));
object.put("cname", custBundle.getString("cname"));
object.put("fzid", myApp.getFzid());//分组id
object.put("fzmc", myApp.getFzmc());//分组名称
object.put("type", type+"");
object.put("zdmc", userName);
object.put("coSim", share_obj.getDataFromPres("COSIM"));
object.put("regSim", share_obj.getDataFromPres("REGSIM"));
object.put("gguid", share_obj.getDataFromPres("GGUID"));
object.put("zguid", share_obj.getDataFromPres("GUID"));
object.put("dataList", mxlist_arr);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("数据采集----------------"+object.toString());
// 请求服务器
String result = new UpdataToServer(InfoSure.this).dataToServer("SJCJ", 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();
}
}
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(InfoSure.this).setTitle("提示").setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(message.contains("成功")){
startActivity(new Intent().setClass(InfoSure.this, MainActivity.class));
finish();
}
}
}).show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(InfoSure.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch (id) {
case 1:
dialog.setMessage("正在上传,请稍候……");
break;
}
return dialog;
}
}