一、Weiyima119.java
package com.bfdl.weiyima119;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class WeiYiMa119 extends AppCompatActivity {
//5个字符串,5个数值,2个布尔值
String strYundanhao = "", strKeyForQqjk = "******"; //l=物流单号 ,t=密码,snStr=唯一吗字符串,jsonStr=扫描完成后需要上传的字符串
String strWeiyima, strXiugaihousn, strJson;
int intDangqianSku = 0, intDangqianSn = 0, intYisaoSnZongshu = 0;/*danqSku = 当前第几个SKU, dangqSno =当前SKU的第几个商品, dangqianzjdjgsp = 总计第几个商品,初始值都为0*/
int intXghDanqSku = 0, intXghDangqianSn = 0;
Boolean isNoXiugai = true;
Boolean isYundanhao = true;
Boolean isNoChongfu = true;
Firstsn f;
List<Sn> sns; //Sn的清单列表
Sninfo sninfo = new Sninfo();
List<Snlist> snlists;
List<Snlist> xgSnLists;
Sninfo xiugaihousninfo = new Sninfo();//每次扫描需要上传时的信息
Call<Firstsn> callFirstsn; //通过接口得到数据库的信息
Call<Fanhui> callFanhui; //通过接口发送到数据库的信息
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://117.50.38.18/jitiao/index.php/index/yzapi/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Qqjk qqjk = retrofit.create(Qqjk.class);
//8个文本框,1个输入框,2个按钮
EditText etTiaomaShuru;
TextView tvWuliudanhao, tvShangpinMingcheng, tvDangqianSkuYisao, tvDangqianSkuYingsao, tvYisaoSkupinzhongnum,
tvYingsaoskupinzhongshu, tvYisaoSkuZongshu, tvYingsaoskuZongshu;
Button btXiugai, btTijiao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wei_yi_ma119);
etTiaomaShuru = findViewById(R.id.et1);
btXiugai = findViewById(R.id.bt1);
btTijiao = findViewById(R.id.bt2);
tvDangqianSkuYisao = findViewById(R.id.tv4); //当前SKU扫描数量
tvWuliudanhao = findViewById(R.id.tv7); //物流单号
tvShangpinMingcheng = findViewById(R.id.tv8); //商品名称
tvDangqianSkuYingsao = findViewById(R.id.tv10); //当前sku总数量
tvYisaoSkupinzhongnum = findViewById(R.id.tv11); //已扫sku品种数
tvYingsaoskupinzhongshu = findViewById(R.id.tv13); //应扫SKU品种数
tvYisaoSkuZongshu = findViewById(R.id.tv14); //已扫SN总数
tvYingsaoskuZongshu = findViewById(R.id.tv16); //应扫SKU总数
btXiugai.setEnabled(false);
btTijiao.setEnabled(false);
etTiaomaShuru.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if ((s.toString().indexOf("\n")) >= 0) {
if (isYundanhao) {
yundanhaosaomiao();
} else {
strWeiyima = etTiaomaShuru.getText().toString();
if (isNoXiugai) {
for (int i = 0; i < snlists.size(); i++) {
if (snlists.get(i).getSn().equals(strWeiyima)) {
isNoChongfu = false;
}
}
if (isNoChongfu) {
Log.i("TAG110", "=: "+snlists.size()+":"+new Gson().toJson(snlists));
weiYimaSaomiaoForWeiXiugai();
} else {
weiYimaChongfu();
}
} else {
for (int i = 0; i < xgSnLists.size(); i++) {
if (xgSnLists.get(i).getSn().equals(strWeiyima)) {
isNoChongfu = false;
}
}
if (isNoChongfu) {
Log.i("TAG120", "=: "+xgSnLists.size()+":"+new Gson().toJson(xgSnLists));
weiYimaSaomiaoForYiXiugai();
} else {
weiYimaChongfu();
}
}
}
etTiaomaShuru.setText("");
if (intYisaoSnZongshu > 0) {
btXiugai.setEnabled(true);
}
}
}
});
btXiugai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
xiugaiSet();
}
});
btTijiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tijiaoset();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
switch (resultCode) {
case 1:
Bundle bundle = data.getBundleExtra("xiugaihou");
strXiugaihousn = bundle.getString("shanchuhou");
xiugaihousninfo = new Gson().fromJson(strXiugaihousn, Sninfo.class);
xgSnLists = xiugaihousninfo.getSnlist();
intXghDanqSku = nextSkuNo(xiugaihousninfo, f);
intXghDangqianSn = nextSnNo(xiugaihousninfo, f);
if (intXghDangqianSn < intDangqianSn || intXghDanqSku < intDangqianSku) {
isNoXiugai = false;
} else {
// intXghDanqSku = 0;
// intXghDangqianSn = 0;
isNoXiugai = true;
}
tvShangpinMingcheng.setText(f.getSn().get(intXghDanqSku).getName());
tvDangqianSkuYisao.setText("" + intXghDangqianSn);
tvDangqianSkuYingsao.setText(f.getSn().get(intXghDanqSku).getQty());
tvYisaoSkupinzhongnum.setText("" + intXghDanqSku);
tvYisaoSkuZongshu.setText("" + xiugaihousninfo.getSnlist().size());
Log.i("TAG125", "onR: "+xgSnLists.size()+":"+new Gson().toJson(xgSnLists));
}
}
}
void yundanhaosaomiao() {
strYundanhao = etTiaomaShuru.getText().toString().trim();
tvWuliudanhao.setText(strYundanhao);
isYundanhao = false;
callFirstsn = qqjk.getStr(strYundanhao, strKeyForQqjk);
callFirstsn.enqueue(new Callback<Firstsn>() {
@Override
public void onResponse(Call<Firstsn> call, Response<Firstsn> response) {
f = response.body();
if (Boolean.getBoolean(f.getResult())){
tvShangpinMingcheng.setText(response.body().getSn().get(intDangqianSku).getName());
tvDangqianSkuYingsao.setText(response.body().getSn().get(intDangqianSku).getQty());
tvYingsaoskupinzhongshu.setText("" + response.body().getSku_num());
tvYingsaoskuZongshu.setText("" + response.body().getSku_qty());
etTiaomaShuru.setText("");
etTiaomaShuru.setHint("请扫描唯一码");
}
if (!Boolean.getBoolean(f.getResult())){
new AlertDialog.Builder(WeiYiMa119.this)
.setTitle("警告")
.setMessage("运单号码错误,请重新扫描")
.setPositiveButton("确认",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
qingkongUI();
}
}
)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// finish();
}
})
.show();
}
Log.i("TAG001", "onResponse: "+f.getResult());
sns = f.getSn();
sninfo.setL(strYundanhao);
sninfo.setT(strKeyForQqjk);
snlists = new ArrayList<Snlist>();
}
@Override
public void onFailure(Call<Firstsn> call, Throwable t) {
}
});
}
void weiYimaSaomiaoForWeiXiugai() {
Snlist snlist = new Snlist();
snlist.setSku(f.getSn().get(intDangqianSku).getSku());
snlist.setSn(strWeiyima);
snlists.add(snlist);
Log.i("TAG111", "weiYimaSaomiaoForWeiXiugai: "+snlists.size()+":"+new Gson().toJson(snlists));
if (intYisaoSnZongshu < f.getSku_qty()) {
if (intDangqianSn < Integer.parseInt(sns.get(intDangqianSku).getQty())) {
intDangqianSn = intDangqianSn + 1;
tvShangpinMingcheng.setText(f.getSn().get(intDangqianSku).getName());
tvDangqianSkuYingsao.setText(f.getSn().get(intDangqianSku).getQty());
etTiaomaShuru.setText("");
if (intDangqianSn == Integer.parseInt(sns.get(intDangqianSku).getQty())
&& intDangqianSku != f.getSku_num() - 1) {
intDangqianSku = intDangqianSku + 1;
intDangqianSn = 0;
tvYisaoSkupinzhongnum.setText("" + intDangqianSku);
tvShangpinMingcheng.setText(f.getSn().get(intDangqianSku).getName());
tvDangqianSkuYingsao.setText(f.getSn().get(intDangqianSku).getQty());
etTiaomaShuru.setText("");
}
}
intYisaoSnZongshu = intYisaoSnZongshu + 1;
tvDangqianSkuYisao.setText("" + intDangqianSn);
tvYisaoSkupinzhongnum.setText("" + intDangqianSku);
tvYisaoSkuZongshu.setText("" + intYisaoSnZongshu);
if (intYisaoSnZongshu == (f.getSku_qty())) {
intDangqianSn = 0;
intYisaoSnZongshu = 0;
intDangqianSku = 0;
tvYisaoSkupinzhongnum.setText("" + f.getSku_num());
etTiaomaShuru.setText("");
isYundanhao = true;
btTijiao.setEnabled(true);
etTiaomaShuru.setEnabled(false);
}
}
sninfo.setSnlist(snlists);
strJson = new Gson().toJson(sninfo);
}
void tijiaoset() {
new AlertDialog.Builder(WeiYiMa119.this)
.setTitle("提醒")
.setMessage("你确认提交吗?")
.setPositiveButton("确认",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://117.50.38.18/jitiao/index.php/index/yzapi/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Sendjk sendjk01 = retrofit.create(Sendjk.class);
callFanhui = sendjk01.getStr(strJson);
callFanhui.enqueue(new Callback<Fanhui>() {
@Override
public void onResponse(Call<Fanhui> call, Response<Fanhui> response) {
qingkongUI();
}
@Override
public void onFailure(Call<Fanhui> call, Throwable t) {
}
});
}
}
)
.show();
}
void qingKongset() {
new AlertDialog.Builder(WeiYiMa119.this)
.setTitle("提醒")
.setMessage("你确认要清空之前的数据吗?")
.setPositiveButton("确认",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
qingkongUI();
}
}
)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// finish();
}
})
.show();
}
void qingkongUI() {
intDangqianSn = 0;
intYisaoSnZongshu = 0;
intDangqianSku = 0;
tvYingsaoskupinzhongshu.setText("0");
tvYingsaoskuZongshu.setText("0");
tvYisaoSkupinzhongnum.setText("0");
tvYisaoSkuZongshu.setText("0");
tvDangqianSkuYisao.setText("0");
tvDangqianSkuYingsao.setText("0");
tvShangpinMingcheng.setText("");
tvWuliudanhao.setText("");
etTiaomaShuru.setEnabled(true);
etTiaomaShuru.setHint("请扫描物流单号");
btTijiao.setEnabled(false);
}
void xiugaiSet() {
Intent intent = new Intent();
intent.setClass(WeiYiMa119.this, XiuGai.class);
Bundle bundle = new Bundle();
if (isNoXiugai) {
// sninfo.setSnlist(snlists);
// strJson = new Gson().toJson(sninfo);
bundle.putString("saomiao", strJson);
Log.i("TAG121", "weiYimaSaomiaoForWeiXiugai: "+new Gson().toJson(strJson));
}
if (!isNoXiugai) {
String strXgSnLists=new Gson().toJson(xiugaihousninfo);
bundle.putString("saomiao", strXgSnLists);
Log.i("TAG122", "weiYimaSaomiaoForWeiXiugai: "+xgSnLists.size()+":--"+strXgSnLists);
}
intent.putExtra("Message", bundle);
startActivityForResult(intent, 0);
}
int sumSkuSn(Sninfo s, String sku) {
int i = 0;
for (Snlist sn : s.getSnlist()) {
if (sn.getSku().equals(sku)) {
i = i + 1;
}
}
return i;
}
int nextSkuNo(Sninfo s, Firstsn f) {
int i = 0;
for (Sn sn : f.getSn()) {
if (Integer.parseInt(sn.getQty()) == sumSkuSn(s, sn.getSku())) {
i = i + 1;
} else {
break;
}
}
return i;
}
int nextSnNo(Sninfo s, Firstsn f) {
int i = 0;
i = sumSkuSn(s, f.getSn().get(nextSkuNo(s, f)).getSku());
return i;
}
void weiYimaSaomiaoForYiXiugai() {
if (intXghDangqianSn < intDangqianSn || intXghDanqSku < intDangqianSku) {
Snlist snlist = new Snlist();
snlist.setSku(f.getSn().get(intXghDanqSku).getSku());
snlist.setSn(etTiaomaShuru.getText().toString());
xgSnLists.add(snlist);
xiugaihousninfo.setSnlist(xgSnLists);
Log.i("TAG121", "yxg: "+xgSnLists.size()+":"+new Gson().toJson(xgSnLists));
intXghDanqSku = nextSkuNo(xiugaihousninfo, f);
intXghDangqianSn = nextSnNo(xiugaihousninfo, f);
tvShangpinMingcheng.setText(f.getSn().get(intXghDanqSku).getName());
tvDangqianSkuYingsao.setText(f.getSn().get(intXghDanqSku).getQty());
tvDangqianSkuYisao.setText("" + intXghDangqianSn);
tvYisaoSkupinzhongnum.setText("" + intXghDanqSku);
tvYisaoSkuZongshu.setText("" + xiugaihousninfo.getSnlist().size());
if (intXghDangqianSn == intDangqianSn && intXghDanqSku == intDangqianSku) {
isNoXiugai = true;
Log.i("TAG123", "x=: "+xgSnLists.size()+":"+new Gson().toJson(xgSnLists));
sninfo = xiugaihousninfo;
Log.i("TAG124", "s=: "+xgSnLists.size()+":"+new Gson().toJson(snlists));
}
}
}
void weiYimaChongfu(){
new AlertDialog.Builder(WeiYiMa119.this)
.setTitle("警告")
.setMessage("唯一码重复,请重新扫描")
.setPositiveButton("确认",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}
)
.show();
isNoChongfu = true;
}
}