package com.bonus.canteen.core.notice.print.v2.business.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.common.redis.RedisUtil;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.device.api.DeviceApi;
import com.bonus.canteen.core.notice.print.v2.po.PrintResponsePO;
import com.bonus.canteen.core.notice.print.v2.vo.NoticePrintTemplateSettingVO;
import com.bonus.canteen.core.order.mq.MqUtil;
import com.bonus.common.houqin.constant.LeConstants;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

@Service
public class NoticePrinterXnBusiness extends NoticePrinterAbstractJiaBoBusiness {
   private static final Logger log = LoggerFactory.getLogger(NoticePrinterXnBusiness.class);
   @Autowired
   @Lazy
   private DeviceApi deviceApi;

//   public Integer getPrintStatus(String sn, String ip) {
//      DeviceInfoVO deviceInfoVO = this.deviceApi.getDeviceInfoVoBySn(sn);
//      return ObjectUtil.isNull(deviceInfoVO) ? LeConstants.COMMON_NO : deviceInfoVO.getOnlineState();
//   }

//   protected PrintResponsePO executePrint(Map<String, String> paramMap, List<NoticePrintTemplateSettingVO> settingList, String sn, String ip, Long recordId) {
//      Vector<Byte> bytes = this.buildPrintData(paramMap, settingList);
//      byte[] byteArray = new byte[bytes.size()];
//
//      for(int i = 0; i < bytes.size(); ++i) {
//         byteArray[i] = (Byte)bytes.get(i);
//      }
//
//      String msg = DeviceBase64Util.encode(byteArray);
//      XnPrintDataPO xnPrintDataPO = new XnPrintDataPO();
//      String orderId = (String)paramMap.get("orderId");
//      if (ObjectUtil.isNull(orderId)) {
//         orderId = String.valueOf(recordId);
//      }
//
//      xnPrintDataPO.setOrderId(orderId);
//      xnPrintDataPO.setPrintBody(msg);
//      Long var10000 = TenantContextHolder.getTenantId();
//      String printKey = "yst:print:" + var10000 + ":" + sn + ":last:id";
//      var10000 = TenantContextHolder.getTenantId();
//      String printLock = "yst:print:" + var10000 + ":" + sn + ":last:id:lock";
//      RLock lock = RedisUtil.getLock(printLock);
//
//      try {
//         if (lock.tryLock(10L, TimeUnit.SECONDS)) {
//            Integer lastPrintId = RedisUtil.getRedisAtomicInteger(printKey, -1L, (Boolean)null);
//            xnPrintDataPO.setLastPrintId(lastPrintId);
//            xnPrintDataPO.setPrintId(RedisUtil.getRedisAtomicInteger(printKey, -1L, true));
//            MqUtil.pushToSingleDevice(xnPrintDataPO, (String)"device_print", sn);
//         }
//      } catch (Exception var18) {
//      } finally {
//         if (lock.isHeldByCurrentThread() && lock.isLocked()) {
//            lock.unlock();
//         }
//
//      }
//
//      return PrintResponsePO.getSuccessPO((String)null);
//   }
}
