package com.iamberry.wechat.handles.admin;

import com.iamberry.wechat.core.entity.machineNumber.MachineNumber;
import com.iamberry.wechat.core.entity.machineNumber.MachineNumberLogs;
import com.iamberry.wechat.core.entity.wx.QRCJson;
import com.iamberry.wechat.face.machineNumber.MachineNumberService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.WeixinUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


class QrCode implements Runnable{
        private MachineNumberService machineNumberService;

        private static Logger logger = LoggerFactory.getLogger(QrCode.class);

        private static ArrayBlockingQueue<MachineNumberLogs> queues = new ArrayBlockingQueue<MachineNumberLogs>(100);


        public QrCode(MachineNumberService machineNumberService) {
            this.machineNumberService = machineNumberService;
        }

    public boolean add(MachineNumberLogs machineNumberLogs){
        System.out.println("将生成二维码记录放入队列中");
        //添加记录
        queues.offer(machineNumberLogs);
        return true;
    }

    @Override
     public void run() {
        boolean isStop = true;
         try {
             while (isStop) {
                 MachineNumberLogs machineNumberLogs =  queues.poll();
                 if(machineNumberLogs == null){
                     //如果队列为空停止执行
                     isStop = false;
                     //Thread.sleep(5000);//设置暂停时间
                 }else{
                        //生成条形码
                        String berQrcode = String.valueOf(new Date().getTime());
                         //获取二维码
                         System.out.println("正在获取二维码！批次："+machineNumberLogs.getLogsBatch()+"数量："+machineNumberLogs.getLogsNumber());
                         if(machineNumberLogs.getLogsNumber() > 0){
                         for(int i = 1;i<= machineNumberLogs.getLogsNumber();i++){
                             String json = String.format(NameUtils.QR_LIMIT_STR_SCENE, ResultInfo.barCodePrefix+berQrcode+i);
                             QRCJson qrc = WeixinUtil.createQrcode(json);
                             if (!(qrc == null || qrc.getUrl() == null || qrc.getUrl().isEmpty())) {
                                 MachineNumber machineNumber = new MachineNumber();
                                 machineNumber.setNumberBatch(machineNumberLogs.getLogsBatch());
                                 machineNumber.setNumberQrcode(qrc.getUrl());
                                 machineNumber.setNumberBarcode(berQrcode+i);//暂时为时间，后期要改
                                 machineNumberService.addMachineNumber(machineNumber);
                             }
                         }
                     }
                 }

             }
         } catch (Exception e1) {
             logger.error("",e1);
             throw new RuntimeException(e1.getMessage());
         }

     }


    public static void main(String[] args) {
        System.out.println(10*1.0/10*100);
    }
}