package co.dc.aptiv.mina;

import co.dc.aptiv.AptivApp;
import co.dc.aptiv.pojo.BillDetailPojo;
import co.dc.commons.utils.DateTimeUtil;
import co.dc.aptiv.service.BillService;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 一道采集逻辑处理类
 *
 * @author mx
 */
public class CollectorMinaHandler extends MinaAbstractHandler {


    private static final Log log = LogFactory.getLog(CollectorMinaHandler.class);

    public CollectorMinaHandler(CollectorMina collectorMina, QueryRunner queryRunner) {
        super(collectorMina, queryRunner);
    }

    private BillService billService;


    public CollectorMinaHandler(MinaAbstract mina, QueryRunner queryRunner, BillService billService) {
        super(mina, queryRunner);
        this.billService = billService;
    }


    /**
     * 处理收到的信息
     */
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        String data = (String) message;
        log.info("接收到采集器发来的信息[" + data + "]");
        if (data != null) {
            String str = replaceBlank(data).trim();
            //生产模式/点检模式
            log.info("当前生产状态=" + AptivApp.flag);
//            log.info("当前可扫描状态=" + AptivApp.scanStatus);
            if (AptivApp.flag) {
//                if (AptivApp.scanStatus) {
                    AptivApp.qrcodeService.receiveQrCode(str);
                    AptivApp.qrcodeService.getScanFrame().changeLight(AptivApp.qrcodeService.getScanFrame().label_scan2, true);
//                }
            } else {
                AptivApp.qrcodeService.checkReCode(str);
            }
        }
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\t|\r|\n");
            /* Pattern p = Pattern.compile("\\s*|\t|\r|\n");*/
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
