CenterHandle.java 24.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
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<OrderInfo> orderInfos;
    private String password; // 密码
    private String verifyPassword; // 是否验证 TRUE FALSE
    private ListOnclickCallback onclickCallback;
    private Integer Select;
    private Resources resources;
    private String consumedSeq;
    private List<ProductInfoQuery> products;
    private ArrayList<ProductOfConsumeOrder> productOfConsumeOrders;
    private Integer type;
    private int times = 0;
    private boolean isAll;
    private String idStr;
    private boolean isRealId;
    public CenterHandle(Context context, List<OrderInfo> 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<String, String> 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
     */
    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;
            ProductOfConsumeOrder consumeOrder = new ProductOfConsumeOrder(products.get(i).getProductId(), base);
            productOfConsumeOrders.add(consumeOrder);
        }

        HttpHelper.getInstance(context).consumeOrder(
                orderInfos.get(position).getOrderId(),
                orderInfos.get(position).getOrderAmount(), 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<DataTrans> {
        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<String, String> 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(),
                            orderInfos.get(position).getOrderAmount(), 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<String, String> 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(),
                                orderInfos.get(position).getOrderAmount(), 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<String> 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<ProductInfoQuery> 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<String> 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();
    }
}