package com.ectrip.cyt.center; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.res.Resources; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.view.animation.AnticipateOvershootInterpolator; import android.widget.Button; import android.widget.Toast; import com.ectrip.cyt.base.DataTrans; import com.ectrip.cyt.base.OrderInfo; import com.ectrip.cyt.callback.HttpCallback; import com.ectrip.cyt.callback.ListOnclickCallback; import com.ectrip.cyt.config.DevicTool; import com.ectrip.cyt.config.MyApp; import com.ectrip.cyt.constant.DeviceType; import com.ectrip.cyt.constant.OrderCode; import com.ectrip.cyt.constant.OrderStutas; import com.ectrip.cyt.db.DbManager; import com.ectrip.cyt.request.ProductOfConsumeOrder; import com.ectrip.cyt.response.ConsumeOrderResponse; import com.ectrip.cyt.response.ProductInfoQuery; import com.ectrip.cyt.spinnerwheel.adapters.NumericWheelAdapter; import com.ectrip.cyt.ui.BaseActivity; import com.ectrip.cyt.utils.LogUtil; import com.ectrip.cyt.utils.SharedPreferences2Obj; import com.ectrip.cyt.utils.SoundUtil; import com.ectrip.trips.check.R; import com.ectrip.trips.net.DataTool; import com.ectrip.trips.net.HttpHelper; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import antistatic.spinnerwheel.AbstractWheel; /** * @author jigo 部分检票和全部检票处理 */ public class CenterHandle { public final String TAG = "CenterHandle"; private Context context; private AlertDialog dialog; // 部分检票弹框 ProgressDialog dialog2 = null; private AbstractWheel wheel;// 部分检票滚轮 private Button reduce;// 部分检票减按钮 private Button plus;// 部分检票加按钮 private List orderInfos; private String password; // 密码 private String verifyPassword; // 是否验证 TRUE FALSE private ListOnclickCallback onclickCallback; private Integer Select; private Resources resources; private String consumedSeq; private List products; private ArrayList productOfConsumeOrders; private Integer type; private int times = 0; private boolean isAll; private String idStr; private boolean isRealId; public CenterHandle(Context context, List orderInfos, String password, String verifyPassword, ListOnclickCallback onclickCallback) { resources = context.getResources(); this.context = context; this.orderInfos = orderInfos; this.password = password; this.verifyPassword = verifyPassword; this.onclickCallback = onclickCallback; Select = SharedPreferences2Obj.getInstance(context) .setName("SelectAction").getObject("Select", Integer.class); type = SharedPreferences2Obj.getInstance(context) .setName("MachineType").getObject("type", Integer.class); } private void getConsumedSeq(int position) { HashMap pid = MyApp.getInstance().getPid(); if (pid != null) { if (pid.get("order").equals(orderInfos.get(position).getOrderId())) { consumedSeq = pid.get("consumedSeq"); } else { consumedSeq = DevicTool.getInstance().getDevId() + System.currentTimeMillis(); } } else { consumedSeq = DevicTool.getInstance().getDevId() + System.currentTimeMillis(); } } /** * 全部检票 * * @param position * @param pwd * @param verifyPassword */ String allQuantity; public void allTicket(int position, String pwd, String verifyPassword) { isAll = true; getConsumedSeq(position); products = orderInfos.get(position).getProducts(); Integer totle = new Integer(orderInfos.get(position).getOrderAmount()); productOfConsumeOrders = new ArrayList<>(); for (int i = 0; i < products.size(); i++) { Integer base = new Integer(products.get(i).getBaseQuantity()) * totle; allQuantity = products.get(i).getQuantity(); ProductOfConsumeOrder consumeOrder = new ProductOfConsumeOrder(products.get(i).getProductId(), base); productOfConsumeOrders.add(consumeOrder); } HttpHelper.getInstance(context).consumeOrder( orderInfos.get(position).getOrderId(), allQuantity, pwd, verifyPassword, consumedSeq, productOfConsumeOrders, new CenterConsumeCallback(position, true, 0)); } /** * 部分检票 * * @param position * @param pwd * @param verifyPassword */ public void partTicket(int position, String pwd, String verifyPassword,String id,boolean isId) { isAll = false; idStr = id; isRealId = isId; getConsumedSeq(position); products = orderInfos.get(position).getProducts(); Integer totle = new Integer(orderInfos.get(position).getOrderAmount()); productOfConsumeOrders = new ArrayList<>(); for (int i = 0; i < products.size(); i++) { ProductOfConsumeOrder consumeOrder = new ProductOfConsumeOrder(products.get(i).getProductId(), 1); productOfConsumeOrders.add(consumeOrder); } HttpHelper.getInstance(context).consumeOrderPart( orderInfos.get(position).getOrderId(), orderInfos.get(position).getOrderAmount(), pwd, verifyPassword, consumedSeq, productOfConsumeOrders,id,isId, new CenterConsumeCallback(position, false, 1)); } /** * 部分检票弹窗 */ LayoutInflater inflater; View layout; @SuppressLint("InflateParams") public void dialog(int num, final int position) { getConsumedSeq(position); try { if (dialog == null) { inflater = LayoutInflater.from(context); layout = inflater.inflate(R.layout.consume_dialog, null); Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.part_print); builder.setCancelable(false); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setView(layout).setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { products = orderInfos.get(position).getProducts(); productOfConsumeOrders = new ArrayList<>(); for (int i = 0; i < products.size(); i++) { Integer base = new Integer(products.get(i).getBaseQuantity()) * (wheel.getCurrentItem() + 1); ProductOfConsumeOrder consumeOrder = new ProductOfConsumeOrder(products.get(i).getProductId(), base); productOfConsumeOrders.add(consumeOrder); } isAll = false; HttpHelper.getInstance(context).consumeOrder( orderInfos.get(position).getOrderId(), (wheel.getCurrentItem() + 1) + "", password, verifyPassword, consumedSeq, productOfConsumeOrders, new CenterConsumeCallback(position, false, wheel.getCurrentItem() + 1)); } }); builder.setNegativeButton(R.string.cancle_label, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (dialog != null) { dialog.dismiss(); } } }); dialog = builder.create(); } initWheel(layout, R.id.wheelView, num); if (dialog != null && !dialog.isShowing()) { if (MyApp.getInstance().isHandset) { dialog.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); } dialog.show(); } } catch (Exception e) { e.printStackTrace(); } } int position = 0;//表示单选列表中默认选择的位置为0 public void showDialog(final String str[]) { position = 0; AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(context) .setTitle("请选择票码") .setCancelable(false) .setSingleChoiceItems(str, position, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { position = which;//得到被点击的序号which LogUtil.d(TAG, "position = " + position); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加“确定”按钮 @Override public void onClick(DialogInterface dialog, int which) { LogUtil.d(TAG, "选择的票码 = " + str[position] + "\n" + "位置 = " + position); dialog.dismiss(); String titcketCode = str[position]; if (!TextUtils.isEmpty(titcketCode)) { isAll = false; HttpHelper.getInstance(context).consumeOrder( orderInfos.get(position).getOrderId(), "1", password, verifyPassword, consumedSeq, productOfConsumeOrders, new CenterConsumeCallback(position, false, 1)); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.show(); } private void initWheel(View view, int id, final int num) { wheel = (AbstractWheel) view.findViewById(id); wheel.setViewAdapter(new NumericWheelAdapter(context, 1, num)); wheel.setCyclic(true); wheel.setInterpolator(new AnticipateOvershootInterpolator()); reduce = (Button) view.findViewById(R.id.reduce); reduce.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int current = wheel.getCurrentItem() - 1; if (current < 0) { return; } wheel.setCurrentItem(current); } }); plus = (Button) view.findViewById(R.id.plus); plus.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int current = wheel.getCurrentItem() + 1; if (num == current) { return; } wheel.setCurrentItem(current); } }); } /** * @author jigo 查询订单 */ public class CenterConsumeCallback extends HttpCallback { int position; private String signed; private String data; private boolean isAll; private int costNum;//消费数量 public CenterConsumeCallback(int position, boolean isAll, int costNum) { this.position = position; this.isAll = isAll; this.costNum = costNum; } @Override public void onStopCallback() { } @Override public void onPreCallback() { if (dialog2 == null) { // 显示ProgressDialog dialog2 = new ProgressDialog(context); dialog2.setMessage(context.getString(R.string.get_order)); dialog2.setCanceledOnTouchOutside(false); dialog2.setCancelable(true); if (type == DeviceType.HANDSET.getValue()) { dialog2.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); } try { if (dialog2 != null && !dialog2.isShowing()) { dialog2.show(); } } catch (Exception e) { e.printStackTrace(); } } else { dialog2.setMessage(context.getString(R.string.get_order)); if (!dialog2.isShowing()) { dialog2.show(); } } } @Override public void onFailureCallback(String FailureStr) { //保存失败的流水号 HashMap pid = new HashMap<>(); pid.put("consumedSeq", consumedSeq); pid.put("order", orderInfos.get(position).getOrderId()); MyApp.getInstance().setPid(pid); if (dialog2 != null && dialog2.isShowing()) { try { dialog2.dismiss(); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } if (times < 2) { times++; if (isAll) { HttpHelper.getInstance(context).consumeOrder( orderInfos.get(position).getOrderId(), allQuantity, password, verifyPassword, consumedSeq, productOfConsumeOrders, new CenterConsumeCallback(position, true, 0)); } else { // HttpHelper.getInstance(context).consumeOrder( // orderInfos.get(position).getOrderId(), // (wheel.getCurrentItem() + 1) + "", // password, verifyPassword, consumedSeq, productOfConsumeOrders, // new CenterConsumeCallback(position, false, wheel.getCurrentItem() + 1)); HttpHelper.getInstance(context).consumeOrderPart( orderInfos.get(position).getOrderId(), "1", password, verifyPassword, consumedSeq, productOfConsumeOrders,idStr,isRealId, new CenterConsumeCallback(position, false, 1)); } } else { MyApp.getInstance().setPid(null); // ((Activity)context).finish(); try { ((BaseActivity) context).MToast(context, FailureStr, Toast.LENGTH_SHORT); } catch (Exception e) { e.printStackTrace(); } } } @Override public void onCompletedCallback(DataTrans result) { } @Override public void afterCompletedCallback(DataTrans result) { if (dialog2 != null && dialog2.isShowing()) { try { dialog2.dismiss(); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } if (result != null) { //成功清除流水号 // times=0; MyApp.getInstance().setPid(null); signed = result.getSigned(); data = result.getData(); parse2(result.getData(), ConsumeOrderResponse.class, context); } else { //保存失败的流水号 HashMap pid = new HashMap<>(); pid.put("consumedSeq", consumedSeq); pid.put("order", orderInfos.get(position).getOrderId()); MyApp.getInstance().setPid(pid); if (times < 2) { times++; if (isAll) { HttpHelper.getInstance(context).consumeOrder( orderInfos.get(position).getOrderId(), allQuantity, password, verifyPassword, consumedSeq, productOfConsumeOrders, new CenterConsumeCallback(position, true, 0)); } else { // HttpHelper.getInstance(context).consumeOrder( // orderInfos.get(position).getOrderId(), // (wheel.getCurrentItem() + 1) + "", // password, verifyPassword, consumedSeq, productOfConsumeOrders, // new CenterConsumeCallback(position, false, wheel.getCurrentItem() + 1)); HttpHelper.getInstance(context).consumeOrder( orderInfos.get(position).getOrderId(), "1", password, verifyPassword, consumedSeq, productOfConsumeOrders, new CenterConsumeCallback(position, false, 1)); } } else { ((Activity) context).finish(); MyApp.getInstance().setPid(null); ((BaseActivity) context).MToast(context, resources.getString(R.string.get_order_error), Toast.LENGTH_SHORT); } } } @Override public void onAgainParseCallback(Object parse) { if (parse != null) { //成功清除流水号 times = 0; MyApp.getInstance().setPid(null); ConsumeOrderResponse consumeOrderResponse = (ConsumeOrderResponse) parse; String code = consumeOrderResponse.getCode(); if (code != null && OrderCode.SUCESS.getValue().equals(code)) { if (signed == null || signed.equals(DataTool.getSign(MyApp .getInstance().getSignkey(), data))) { //播放语音 SoundUtil.getInstents((Activity) context).playWavSound(R.raw.huanyinguanglin); List PrintInfos = consumeOrderResponse .getPrintInfos(); if (PrintInfos != null) { String printInfo = null; try { printInfo = saveInfo(PrintInfos); // 保存订单打印信息 } catch (Exception e) { e.printStackTrace(); } try { if (printInfo != null) { onclickCallback .onPrintInfosCallback(printInfo);// 打票回调,各个自用各自的打印模块 } } catch (Exception e) { e.printStackTrace(); } List products = orderInfos.get(position).getProducts(); if (isAll) { orderInfos.get(position).setOrderAmount("0"); orderInfos.get(position).setOrderStatus( OrderStutas.CONSUMED.getValue()); for (int i = 0; i < products.size(); i++) { products.get(i).setConsumedQuantity(products.get(i).getTotalQuantity()); products.get(i).setQuantity("0"); } } else { Integer count = new Integer(orderInfos.get(position).getOrderAmount()); orderInfos.get(position).setOrderAmount((count - costNum) + ""); if (count - costNum == 0) { orderInfos.get(position).setOrderStatus( OrderStutas.CONSUMED.getValue()); } for (int i = 0; i < products.size(); i++) { Integer bastint = new Integer(products.get(i).getBaseQuantity());//基数 Integer cost = new Integer(products.get(i).getConsumedQuantity());//之前已消费 Integer quantity = new Integer(products.get(i).getQuantity());//之前剩余 products.get(i).setConsumedQuantity((cost + costNum * bastint) + ""); products.get(i).setQuantity((quantity - costNum * bastint) + ""); } } if (Select != null && Select == 1) { onclickCallback.onCenterCallback(); } } } else { onFailureCallback(resources.getString(R.string.sign_not_pass)); ((BaseActivity) context).MToast(context, resources.getString(R.string.sign_not_pass), Toast.LENGTH_SHORT); } } else { // onFailureCallback(consumeOrderResponse.getDescribe()); ((BaseActivity) context).MToast(context, consumeOrderResponse.getDescribe(), Toast.LENGTH_SHORT); } } } } /** * @param PrintInfos * @return 保存打印信息 */ @SuppressLint("SimpleDateFormat") public String saveInfo(List PrintInfos) { // 保存打印票的信息 String[] str = null; String orderNumber = null; StringBuilder sb = new StringBuilder(); for (String temp : PrintInfos) { if (temp != null) { if (temp.contains("证件号码") && temp.length() > 24) { String buf = temp.substring(13, 21); temp = temp.replace(buf, "****"); } if (temp.contains(resources.getString(R.string.orderid))) { try { str = temp.split(resources.getString(R.string.orderid)); orderNumber = str[1].split(":")[1].trim(); } catch (Exception e) { e.printStackTrace(); } } sb.append(temp + "\n"); } } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式 String currentData = df.format(new Date());// new // Date()为获取当前系统时间 try { if (orderNumber != null && DbManager.queryOrderNumber(orderNumber) == null) { DbManager .insertPrint(currentData, sb.toString(), str[1].trim()); } else { DbManager .ModifyPrint(currentData, sb.toString(), str[1].trim()); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } }