package com.bonus.canteen.core.common.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.allocation.api.AllocMetadataApi;
import com.bonus.canteen.core.common.constant.LePayConstants;
import com.bonus.canteen.core.common.enums.MetadataModelTypeEnum;
import com.bonus.canteen.core.common.redis.RedisUtil;
import com.bonus.canteen.core.order.deprecated.dto.OrdLeCodeDTO;
import com.bonus.canteen.core.pay.custom.PayCustomBusiness;
import com.bonus.common.houqin.i18n.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LeCodeUtil {
   private static final Logger log = LoggerFactory.getLogger(LeCodeUtil.class);
   private static final PayCustomBusiness payCustomBusiness = (PayCustomBusiness)SpringContextHolder.getBean(PayCustomBusiness.class);
   private static final AllocMetadataApi allocMetadataApi = (AllocMetadataApi)SpringContextHolder.getBean(AllocMetadataApi.class);

   public static boolean isLeCode(String qrCode) {
      return StrUtil.startWith(qrCode, "xnzn");
   }

   public static boolean isAliPayCode(String qrCode) {
      return StrUtil.isNotBlank(qrCode) && qrCode.matches("^(25|26|27|28|29|30)\\d{14,22}$");
   }

   public static boolean isWechatPayCode(String qrCode) {
      return StrUtil.isNotBlank(qrCode) && qrCode.matches("^(10|11|12|13|14|15)\\d{16}$");
   }

   public static boolean isABCCode(String qrCode) {
      return StrUtil.startWithAny(qrCode, new CharSequence[]{"6232", "62356", "62556"}) && LePayConstants.ABC_CODE_LENGTH == qrCode.length();
   }

   public static boolean isICBCCode(String qrCode) {
      return StrUtil.startWith(qrCode, "62351") && StrUtil.length(qrCode) == LePayConstants.ICBC_CODE_LENGTH;
   }

   public static boolean isDingDingCode(String qrCode) {
      return StrUtil.isNotBlank(qrCode) && qrCode.startsWith("dingbadge");
   }

   public static boolean isDcepCode(String qrCode) {
      return StrUtil.isNotBlank(qrCode) && LePayConstants.DCEP_PAY_CODE_PRE.contains(StrUtil.sub(qrCode, 0, 5)) && StrUtil.length(qrCode) == LePayConstants.DCEP_CODE_LENGTH;
   }

   public static boolean isUnionPayCode(String qrCode) {
      return StrUtil.startWith(qrCode, "62") && qrCode.length() >= LePayConstants.UNIONPAY_CODE_MIN_LENGTH && qrCode.length() <= LePayConstants.UNIONPAY_CODE_MAX_LENGTH;
   }

   public static boolean isECNYPayCode(String qrCode) {
      return LePayConstants.DCEP_PAY_CODE_PRE.contains(StrUtil.sub(qrCode, 0, 5)) && StrUtil.length(qrCode) == LePayConstants.DCEP_CODE_LENGTH;
   }

   public static OrdLeCodeDTO readLeCode(String qrCode) {
      if (!isLeCode(qrCode)) {
         throw new ServiceException("二维码异常");
      } else {
         String leniucode = qrCode.substring(4);

         try {
            OrdLeCodeDTO ordLeCode = (OrdLeCodeDTO)JSONObject.parseObject(leniucode, OrdLeCodeDTO.class);
            return ordLeCode;
         } catch (Exception var3) {
            log.error("二维码{}信息解析异常：{}", qrCode, var3);
            throw new ServiceException("二维码异常");
         }
      }
   }

   public static Long getPlatformIdThenExpire(OrdLeCodeDTO ordLeCode) {
      Long currentTimeStamp = System.currentTimeMillis();
      long intervalMinute = Math.abs((currentTimeStamp - ordLeCode.getT()) / 60000L);
      int effectiveMinutes = Integer.parseInt(allocMetadataApi.getMetadataValue(MetadataModelTypeEnum.MOBILE, "qrCodeEffectMinutes", "1"));
      if ((long)effectiveMinutes <= intervalMinute) {
         throw new ServiceException("二维码过期");
      } else {
         Long var10000 = TenantContextHolder.getTenantId();
         String authCodeKey = "yst:" + var10000 + "authCode:" + ordLeCode.getP() + ":" + ordLeCode.getT();
         if (payCustomBusiness.leCodeEffectiveOnce()) {
            if (RedisUtil.setNx(authCodeKey, LeConstants.COMMON_YES, effectiveMinutes * 60)) {
               return ordLeCode.getP();
            } else {
               throw new ServiceException("二维码过期");
            }
         } else {
            return ordLeCode.getP();
         }
      }
   }

   public static Long getPlatformIdThenExpire(String qrCode) {
      OrdLeCodeDTO ordLeCode = readLeCode(qrCode);
      return getPlatformIdThenExpire(ordLeCode);
   }

   public static Long getPlatformId(OrdLeCodeDTO ordLeCode) {
      return (ordLeCode.ifTake() || ordLeCode.ifPersonalPay() || ordLeCode.ifCoupon()) && ordLeCode.ifFromMobile() ? getPlatformIdThenExpire(ordLeCode) : ordLeCode.getP();
   }
}
