PrintHandle.java 15.3 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
package com.ectrip.cyt.center;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.TextUtils;
import android.widget.Toast;

import com.ectrip.cyt.bean.ConfigBean;
import com.ectrip.cyt.config.MyApp;
import com.ectrip.cyt.constant.LogoType;
import com.ectrip.cyt.constant.SelectAction;
import com.ectrip.cyt.constant.constant;
import com.ectrip.cyt.db.DbManager;
import com.ectrip.cyt.ui.BaseActivity;
import com.ectrip.cyt.ui.MainActivity;
import com.ectrip.cyt.utils.LogUtil;
import com.ectrip.cyt.utils.SharedPreferences2Obj;
import com.ectrip.trips.check.R;
import com.sunmi.printerx.SdkException;
import com.sunmi.printerx.api.LineApi;
import com.sunmi.printerx.api.PrintResult;
import com.sunmi.printerx.enums.Align;
import com.sunmi.printerx.enums.DividingLine;
import com.sunmi.printerx.style.BaseStyle;
import com.sunmi.printerx.style.TextStyle;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android_serialport_api.print_tool.BasewinRebPosPortTool;
import android_serialport_api.print_tool.MobilePrintTool;
import android_serialport_api.print_tool.SerialIDPortTool;
import android_serialport_api.print_tool.SerialNewHandlePortTool;
import android_serialport_api.print_tool.SerialNewPortTool;
import android_serialport_api.print_tool.SerialPortTool;
import android_serialport_api.print_tool.SerialSmallPortTool;

/**
 * @author jigo 打印处理
 */
public class PrintHandle {
    private String TAG = PrintHandle.class.getSimpleName();
    private Context context;
    private String printInfo;
    private final int ID_POS = 5;// 身份证pos
    private final int GENERAL_POS = 6;// 一般pos
    private final int SMALL_POS = 7;// 小的pos
    private final int HANDSET = 8; // 手持机
    private final int MOBILE = 9;// 手机
    private final int New_POS = 10; // 新版的pos机
    private final int New_IDPOS = 11; // 新版的身份证
    private final int New_HANDSET_POS = 12; // 新版的手持机pos机
    private final int BASEWIN_REB_POS = 13; // 盛本的红色pos机
    private final int SHANGMI_POS = 16;

    private int isPrint = -1;// 是否打印
    private String isStatistic = "0";
    private int Select = 0;

    private static PrintHandle printHandle = null;

    private Integer type = null;// 设备类型
    private Resources resources;

    public static PrintHandle getInstance() {
        if (printHandle == null) {
            synchronized (PrintHandle.class) {

                printHandle = new PrintHandle();
            }
        }
        return printHandle;
    }

    private boolean isClick = true;

    public void print(String printInfo, Context context) {
        this.context = context;
        resources = context.getResources();
        LogUtil.d(TAG, "printInfo = " + printInfo);
        if (TextUtils.isEmpty(printInfo)) {
            return;
        }
//		int index = printInfo.indexOf("证件号码");
//		if (printInfo.length()>27){
//			String noString = printInfo.substring(26, 27);
//			if (noString.equals("\n")) {
//				String buf = printInfo.substring(index + 13, index + 21);
//				printInfo = printInfo.replace(buf, "****");
//			}
//
//		}

        type = SharedPreferences2Obj.getInstance(context)
                .setName("MachineType").getObject("type", Integer.class);

        LogUtil.d(TAG, "type = " + type);

        if (isClick == false) {
            return;
        }

        if (isClick == true) {

            this.printInfo = printInfo;
            try {
                ArrayList<ConfigBean> beans = DbManager.GetConfigs();
                if (beans != null && beans.get(0) != null) {
                    // 判断是否为打印
                    isPrint = beans.get(0).getEc_isPrint();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            isStatistic = SharedPreferences2Obj.getInstance(context)
                    .setName("SelectAction")
                    .getObject("isStatistic", String.class); // 非统计
            Select = SharedPreferences2Obj.getInstance(context).
                    setName("SelectAction").getObject("Select", Integer.class);
            if (isStatistic != null) {
                if (isStatistic.equals("0")) { // 非统计
                    String titleName = SharedPreferences2Obj
                            .getInstance(context).setName("SelectAction")
                            .getObject("titleName", String.class);
                    if (titleName != null) {
                        if (titleName.equals(resources
                                .getString(R.string.check))) {
                            if (isPrint == 1) { // 1的时候打印小票
                                printInfo(type);
                            }
                        } else if (titleName.equals(resources
                                .getString(R.string.reprint))) {
                            printInfo(type);
                        }
                    } else {
                        printInfo(type);
                    }
                } else if (isStatistic.equals("1")) {
                    printInfo(type);
                }
            }
        }
    }

    private void printInfo(Integer type) {

        if (type != null) {
            switch (type) {
                case ID_POS:
                    try {
                        idPosPrint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case GENERAL_POS:
                    try {
                        generalPrint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case SMALL_POS:
                    try {
                        smallPosprint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case HANDSET:
                    ((BaseActivity) context).MToast(context,
                            context.getString(R.string.not_support_print),
                            Toast.LENGTH_SHORT);
                    // try {
                    // handlePrint();
                    // } catch (Exception e) {
                    // e.printStackTrace();
                    // }
                    break;
                case MOBILE:
                    try {
                        mobilePrint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case New_POS:
                    try {
                        newPosPrint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//					Toast.makeText(context, "不支持", Toast.LENGTH_SHORT).show();
                    break;
                case New_IDPOS:
                    try {
                        handlePosPrint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case New_HANDSET_POS:
                    try {
                        handlePosPrint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case BASEWIN_REB_POS:
                    try {
                        basewinPosPrint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
                case SHANGMI_POS:
                    printSm();
                    break;
                default:
                    break;
            }
        }
    }

    /************************ 关闭打印 *******************************************/
    public void printClose() {
        if (type != null) {
            switch (type) {
                case ID_POS:
                    break;
                case GENERAL_POS:
                    break;
                case SMALL_POS:
                    break;
                case HANDSET:
                    break;
                case MOBILE:
                    break;
                case New_POS:
                    try {
                        if (serialNewPortTool != null) {
                            serialNewPortTool.close();
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    break;
                case New_IDPOS:
                    break;
                case New_HANDSET_POS:
                    try {
                        if (serialNewHandlePortTool != null) {
                            serialNewHandlePortTool.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case BASEWIN_REB_POS:
                    try {
                        if (basewinRebPosPortTool != null) {
                            basewinRebPosPortTool.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

                default:
                    break;
            }
        }
    }

    /***************************** 普通pos机 ***********************************/
    SerialPortTool portTool;

    private void generalPrint() {
        portTool = new SerialPortTool();
        portTool.init(context, printInfo);
    }

    /******************************** 带身份证打印 ****************************************************/
    SerialIDPortTool idPortTool;

    private void idPosPrint() {
        idPortTool = new SerialIDPortTool();
        idPortTool.init(context, printInfo);
    }

    /******************************* 小pos机(手持pos机) ****************************************/
    SerialSmallPortTool smallPortTool;

    private void smallPosprint() {
        smallPortTool = new SerialSmallPortTool();
        smallPortTool.init(context, printInfo);
    }

    /********************************* 手机 ***************************************************/
    private void mobilePrint() {
        Intent intent = new Intent(context, MobilePrintTool.class);
        intent.putExtra("info", printInfo);
        context.startActivity(intent);
    }

    /********************************* 手持机 ******************************************************/
    private void handlePrint() {
        Intent intent = new Intent(context, MobilePrintTool.class);
        intent.putExtra("info", printInfo);
        context.startActivity(intent);
    }

    /******************************** 新版pos机 ***************************************************/
    SerialNewPortTool serialNewPortTool;

    private void newPosPrint() {
        serialNewPortTool = new SerialNewPortTool();
        serialNewPortTool.init(context, printInfo);
    }

    /********************************* 新版手持pos机 ***********************************************/
    SerialNewHandlePortTool serialNewHandlePortTool;

    private void handlePosPrint() {
        serialNewHandlePortTool = new SerialNewHandlePortTool();
        serialNewHandlePortTool.init(context, printInfo);
    }

    /********************************* 盛本红色pos机 ***********************************************/
    BasewinRebPosPortTool basewinRebPosPortTool;

    private void basewinPosPrint() {
        basewinRebPosPortTool = new BasewinRebPosPortTool();
        basewinRebPosPortTool.init(context, printInfo);
    }
    /********************************* 商米pos机 ***********************************************/
    private void printSm() {
        try {
            LineApi api = MainActivity.selectPrinter.lineApi();
            //需要监听打印结果时可以开启事务模式,只需要调用一次
            api.enableTransMode(true);
            printSmallTicket();
            api.enableTransMode(false);
        } catch (SdkException e) {
            e.printStackTrace();
        }
    }

    private void printSmallTicket() {
        int printNum = MyApp.getInstance().getPrintNum();
        try {
            LogUtil.d(TAG, "商米打印 = " + printInfo);
            LineApi api = null;
            for (int k = 0; k < printNum; k++) {
                api = MainActivity.selectPrinter.lineApi();
                api.initLine(BaseStyle.getStyle().setAlign(Align.CENTER));
                if (isStatistic != null && isStatistic.equals("1")) {
                    api.printText("统计信息", TextStyle.getStyle().enableBold(true));
                } else {
                    if (Select == 2) {
                        api.printText("订单信息(重打小票)", TextStyle.getStyle().enableBold(true));
                    } else {
                        api.printText("订单信息", TextStyle.getStyle().enableBold(true));
                    }
                }
                api.initLine(BaseStyle.getStyle().setAlign(Align.LEFT));

                if (isStatistic != null && isStatistic.equals("1")) {

                } else {
                    if (Select == SelectAction.Query.getValue()) { //打印时间
                        SimpleDateFormat df = new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss");// 设置日期格式
                        api.printText("检票时间:" + df.format(new Date()), TextStyle.getStyle());
                    } else if (Select == SelectAction.Reprint.getValue()) {// 重打时间
                        SimpleDateFormat df = new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss");// 设置日期格式
                        api.printText("重打时间:" + df.format(new Date()), TextStyle.getStyle());
                    }
                }
                api.printText(printInfo, TextStyle.getStyle());
                api.printDividingLine(DividingLine.EMPTY, 30);
                api.printText("取票人签名:" , TextStyle.getStyle());
                api.printDividingLine(DividingLine.EMPTY, 30);
                if (constant.logoMark == LogoType.CYT.getValue()) {
                    api.printText("----畅游通—智慧旅游O2O平台----", TextStyle.getStyle());
                    api.printText("--------www.jingqu.cn---------", TextStyle.getStyle());
                }else if (constant.logoMark == LogoType.QNE.getValue()) {
                    api.printText("----欢迎使用去哪儿网O2O系统----", TextStyle.getStyle());
                    api.printText("--------www.qunar.com---------", TextStyle.getStyle());
                }
                api.autoOut();
                api.printTrans(new PrintResult() {
                    @Override
                    public void onResult(int resultCode, String message) {
                        if (resultCode == 0) {
                            //打印成功
                            LogUtil.d(TAG, "打印成功");
                        } else {
                            //打印失败
                            LogUtil.d(TAG, "打印失败");
                        }
                    }
                });
            }
        } catch (SdkException e) {
            e.printStackTrace();
        }
    }
}