package com.example.wms;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.wms.entity.StockEntityQuery_data;
import com.example.wms.entity.StockQueryEntity;
import com.example.wms.entity.WorkShopReceiptEntity;
import com.example.wms.entity.WorkShopReceipt_data;
import com.example.wms.util.HttpHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
public class WorkShopReceipt extends AppCompatActivity {
private Button btn_clear;
private Button btn_save;
private ImageButton fh_btn;
private String result = "";
private EditText txtitemcode;
private EditText txtitemname;
private EditText txtmatspec;
private EditText txtfinalflag;
private EditText txtopration;
private EditText txtqty;
private EditText targetlocation;
private EditText txtedittxtmatspec;
WorkShopReceiptEntity workShopReceiptEntity=new WorkShopReceiptEntity();
WorkShopReceipt_data workShopReceipt_data=new WorkShopReceipt_data();
ProgressDialog progressDialog;
private LinkedList<WorkShopReceipt_data> mDate = new LinkedList<>();
private String StockData="";//库存数据
private StockQueryAdapter mAdapter = null;
private Context mContext;
private String QueryErrorMsg = "";//接口消息
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_shop_receipt);
txtitemcode = findViewById(R.id.edittxtitemcode);
txtitemname=findViewById(R.id.edittxtitemname);
targetlocation=findViewById(R.id.edittxttargetlocation);//
txtfinalflag=findViewById(R.id.edittxtfinalflag);
txtopration=findViewById(R.id.edittxtopration);
txtqty = findViewById(R.id.edittxtqty);
txtedittxtmatspec=findViewById(R.id.edittxtmatspec);
//清空 btn_clear
btn_clear = findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtitemcode.setText("");
txtitemname.setText("");
targetlocation.setText("");
txtfinalflag.setText("");
txtopration.setText("");
txtqty.setText("");
}
});
}
});
//保存
btn_save=findViewById(R.id.save_sr);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Code = txtitemcode.getText().toString() ;
String ItemName = txtitemname.getText().toString();
String Targetlocation =targetlocation.getText().toString() ;
String Finaflag = txtfinalflag.getText().toString();
String Opration = txtopration.getText().toString();
int Qty =Integer.parseInt(txtqty.getText().toString());
if(Qty<=0){
Toast.makeText(WorkShopReceipt.this,"数量必须大于0", Toast.LENGTH_LONG).show();
return;
}
String msg="条码["+Code+"]数量["+Qty+"]确定要收货吗?";
new AlertDialog.Builder(WorkShopReceipt.this).setTitle("系统提示")
.setMessage(msg)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog = new ProgressDialog(WorkShopReceipt.this);
progressDialog.setTitle("CATL");
progressDialog.setMessage("正在保存,请稍等......");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
SaveReceipt(Code,ItemName,Targetlocation,Finaflag,Opration,Qty);
Message msg = new Message();
msg.what = 0;
HttpHelper.handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
HttpHelper.handler = new Handler() {
public void handleMessage(Message msg) {
try {
if (progressDialog != null) {
progressDialog.dismiss();
}
JSONObject jsonObjectALL = new JSONObject(result);
String code = jsonObjectALL.optString("code", null);
String baseinfo = jsonObjectALL.optString("info", null);
//String data = jsonObjectALL.optString("data", null);
if (code.equals("1")) {
Toast.makeText(WorkShopReceipt.this,"保存成功"+baseinfo, Toast.LENGTH_LONG).show();
txtitemcode.setText("");
txtitemname.setText("");
targetlocation.setText("");
txtfinalflag.setText("");
txtopration.setText("");
txtqty.setText("");
txtedittxtmatspec.setText("");
}else {
Toast.makeText(WorkShopReceipt.this,"保存失败"+baseinfo, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
});
//返回
fh_btn = findViewById(R.id.fh_btn);
fh_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//扫描条码触发接口
txtitemcode=findViewById(R.id.edittxtitemcode);
txtitemcode.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) {
//1.判断条码长度
if (s.length() != 8) return;
//2.根据条码转换成json调用接口
Gson gson = new Gson();
workShopReceipt_data.setCode(s.toString());
workShopReceipt_data.setQty(1);
workShopReceipt_data.setFinaflag("");
workShopReceipt_data.setItemname("");
workShopReceipt_data.setOpration("");
workShopReceipt_data.setTargetlocation("");
String value = gson.toJson(workShopReceipt_data);
JSONObject root = new JSONObject();
try {
//存data的值
root.put("EncryptTokenNo", HttpHelper.EncryptTokenNo);
root.put("AppDeviceNo", HttpHelper.AppDeviceNo);
root.put("IPAddress", HttpHelper.IPAddress);
root.put("JsonData", value);
root.put("apptype", HttpHelper.AppType);
} catch (JSONException e) {
e.printStackTrace();
}
//调用PostInfo方法将返回的值存放在result中
result = PostInfo(root.toString());
//3.接口返回数据转换成实体
try {
JSONObject jsonObjectALL = new JSONObject(result);
String code = jsonObjectALL.optString("code", null);
String baseinfo = jsonObjectALL.optString("info", null);
String data = jsonObjectALL.optString("data", null);
if (code.equals("1")) {
if (!TextUtils.isEmpty(data)) {
JSONObject jsonObjectData = new JSONObject(data);
String BarCodeID = jsonObjectData.optString("BarCodeID", null);
String Operation = jsonObjectData.optString("Operation", null);
String TrayQty = jsonObjectData.optString("TrayQty", null);
String Resource = jsonObjectData.optString("Resource", null);
String TargetLocation = jsonObjectData.optString("TargetLocation", null);
String Item = jsonObjectData.optString("Item", null);
String Qty = jsonObjectData.optString("Qty", null);
String FinalFlag = jsonObjectData.optString("FinalFlag", null);
//绑定数据
txtitemname.setText(Item);//名称
targetlocation.setText(TargetLocation);//目标库位
txtfinalflag.setText(FinalFlag);//尾单标记
txtqty.setText(Qty);//数量
txtopration.setText(Operation);//工序
txtedittxtmatspec.setText("111");//物料规格
Toast.makeText(WorkShopReceipt.this, "接口调用成功"+baseinfo, Toast.LENGTH_LONG).show();
}
} else {
//提示信息
Toast.makeText(WorkShopReceipt.this, "接口调用失败" + baseinfo, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private String PostInfo(String json) {
return HttpHelper.Post(HttpHelper.url_WorkShopReceipt_qqq, json);
}
});
}
private String PostInfo_Send(String json) {
return HttpHelper.Post(HttpHelper.url_WorkShopReceipt, json);
}
public void SaveReceipt(String Code,String ItemName,String Targetlocation,String Finaflag,String Opration,int Qty){
//实体类转化为JSON
WorkShopReceipt_data workShopReceipt_data=new WorkShopReceipt_data();
workShopReceipt_data.setCode(Code);
workShopReceipt_data.setItemname(ItemName);
workShopReceipt_data.setTargetlocation(Targetlocation);
workShopReceipt_data.setFinaflag(Finaflag);
workShopReceipt_data.setOpration(Opration);
workShopReceipt_data.setQty(Qty);
Gson gson = new Gson();
String value = gson.toJson(workShopReceipt_data);
JSONObject root = new JSONObject();
try {
//存data的值
root.put("EncryptTokenNo", HttpHelper.EncryptTokenNo); //Token值
root.put("AppDeviceNo", HttpHelper.AppDeviceNo); //MAC地址
root.put("IPAddress", HttpHelper.IPAddress); //设备IP
root.put("JsonData", value);
// root.put("apptype", HttpHelper.AppType);
} catch (JSONException e) {
e.printStackTrace();
}
//调用PostInfo方法将返回的值存放在result中
result = PostInfo_Send(root.toString());
}
}