package com.jeequan.jeepay.core.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jeequan.jeepay.core.model.PrefixConfig;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

/*
* 序列号生成 工具类
*
* @author terrfly
* @author jmdhappy
*
* @date 2021/6/8 16:56
*/
public class SeqKit {

	private static final AtomicLong PAY_ORDER_SEQ = new AtomicLong(0L);
	private static final AtomicLong REFUND_ORDER_SEQ = new AtomicLong(0L);
	private static final AtomicLong MHO_ORDER_SEQ = new AtomicLong(0L);
	private static final AtomicLong MHO_REFUND_ORDER_SEQ = new AtomicLong(0L);
	private static final AtomicLong TRANSFER_ID_SEQ = new AtomicLong(0L);
	private static final AtomicLong DIVISION_BATCH_ID_SEQ = new AtomicLong(0L);

	private static final AtomicLong QRCODE_CARD_ID_SEQ = new AtomicLong(0L); //码牌自增ID

	private static final AtomicLong CASH_PLUGIN_CDKEY_ID_SEQ = new AtomicLong(0L); // 收银插件激活码自增ID

	private static final AtomicLong CASHOUT_ID_SEQ = new AtomicLong(0L);

	private static final AtomicLong FLOW_NO_ID_SEQ = new AtomicLong(0L);

	private static final AtomicLong TK_ID_SEQ = new AtomicLong(0L);

	private static final String PAY_ORDER_SEQ_PREFIX = "P";
	private static final String REFUND_ORDER_SEQ_PREFIX = "R";
	private static final String MHO_ORDER_SEQ_PREFIX = "M";
	private static final String MHO_REFUND_ORDER_SEQ_PREFIX = "MR";
	private static final String TRANSFER_ID_SEQ_PREFIX = "T";
	private static final String DIVISION_BATCH_ID_SEQ_PREFIX = "D";

	private static final String MCH_APPLYMENT_MODIFY_PREFFIX = "SYBBG";

	private static final String CASH_PLUGIN_CDKEY_SEQ_PREFFIX = "5";

	private static final String CASHOUT_ID_SEQ_PREFIX = "C";

	private static final String ACCOUNT_ID_SEQ_PREFIX = "AC";

	private static final String APPID_PREFIX="2081";

	public static final String TOKEN_PREFIX= "Q8";

	/** 是否使用MybatisPlus生成分布式ID **/
	private static final boolean IS_USE_MP_ID = true;

	/** 生成支付订单号 兼容银盛 全部以日期开头 **/
	public static String genPayOrderId() {
		return DateUtil.format(new Date(), DatePattern.PURE_DATE_FORMAT) + IdWorker.getIdStr() + RandomUtil.randomStringUpper(3);
	}

	/** 生成退款订单号 **/
	public static String genRefundOrderId() {
		if(IS_USE_MP_ID) {
			return REFUND_ORDER_SEQ_PREFIX + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d",REFUND_ORDER_SEQ_PREFIX,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) REFUND_ORDER_SEQ.getAndIncrement() % 10000);
	}


	/** 模拟生成商户订单号 **/
	public static String genMhoOrderId() {
		if(IS_USE_MP_ID) {
			return MHO_ORDER_SEQ_PREFIX + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d", MHO_ORDER_SEQ_PREFIX,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) MHO_ORDER_SEQ.getAndIncrement() % 10000);
	}

	/** 模拟生成商户退款订单号 **/
	public static String genMhoRefundOrderId() {
		if(IS_USE_MP_ID) {
			return MHO_REFUND_ORDER_SEQ_PREFIX + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d", MHO_REFUND_ORDER_SEQ_PREFIX,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) MHO_REFUND_ORDER_SEQ.getAndIncrement() % 10000);
	}

	/** 模拟生成商户订单号 **/
	public static String genTransferId() {
		if(IS_USE_MP_ID) {
			return TRANSFER_ID_SEQ_PREFIX + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d", TRANSFER_ID_SEQ_PREFIX,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) TRANSFER_ID_SEQ.getAndIncrement() % 10000);
	}

	/** 模拟生成分账批次号 **/
	public static String genDivisionBatchId() {
		if(IS_USE_MP_ID) {
			return DIVISION_BATCH_ID_SEQ_PREFIX + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d", DIVISION_BATCH_ID_SEQ_PREFIX,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) DIVISION_BATCH_ID_SEQ.getAndIncrement() % 10000);
	}

	/** 模拟生成分账批次号 **/
//	public static String genMchApplymentApplyNo() {
//		if(IS_USE_MP_ID) {
//			return MCH_APPLYMENT_SEQ_PREFFIX + DateUtil.format(new Date(), "yyMMdd")+ RandomUtil.randomNumbers(6);
//		}
//		return String.format("%s%s%04d", MCH_APPLYMENT_SEQ_PREFFIX,
//				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
//				(int) DIVISION_BATCH_ID_SEQ.getAndIncrement() % 10000);
//	}



	public static String getMchAppId() {
		return APPID_PREFIX + DateUtil.format(new Date(), "yyMMdd")+ RandomUtil.randomNumbers(6);

	}


	/** 模拟生成分账批次号, 避免mp自增太长 导致查询失败 **/
	public static Long genQrcodeCardId() {

		return Long.parseLong(String.format("%s%02d",
				DateKit.currentTimeMillis() + "",
				(int) QRCODE_CARD_ID_SEQ.getAndIncrement() % 100)
		);

	}

	/** 生成收银插件  全局唯一激活码 **/
	public static String genPluginCdKey() {
		return String.format("%s%s%02d",CASH_PLUGIN_CDKEY_SEQ_PREFFIX,
				DateKit.currentTimeMillis() + "",
				(int) CASH_PLUGIN_CDKEY_ID_SEQ.getAndIncrement() % 100);
	}

	/** 生成三方账户提现单号 **/
	public static String genCashoutRecordId() {
		if(IS_USE_MP_ID) {
			return CASHOUT_ID_SEQ_PREFIX + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d", CASHOUT_ID_SEQ_PREFIX,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) CASHOUT_ID_SEQ.getAndIncrement() % 10000);
	}

	/** 生成账户编号 **/
	public static String genAccountNo() {
		if(IS_USE_MP_ID) {
			return ACCOUNT_ID_SEQ_PREFIX + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d", ACCOUNT_ID_SEQ_PREFIX,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) CASHOUT_ID_SEQ.getAndIncrement() % 10000);
	}

	/**
	 * 生成流水号
	 * @param prefix
	 * @return
	 */
    public static String genFlowNo(String prefix) {
		if(IS_USE_MP_ID) {
			return prefix + IdWorker.getIdStr();
		}
		return String.format("%s%s%04d", prefix,
				DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN),
				(int) FLOW_NO_ID_SEQ.getAndIncrement() % 10000);
    }

	/**
	 * 生成新的渠道号
	 */
	public static String genIsvNo() {
		PrefixConfig bean = SpringUtil.getBean(PrefixConfig.class);
		return bean.getIsv() + DateUtil.year(new Date()) + RandomUtil.randomNumbers(10);
	}

	public static String genAgentNo() {
		PrefixConfig bean = SpringUtil.getBean(PrefixConfig.class);
		return bean.getAgent() + DateUtil.currentSeconds() + RandomUtil.randomString("0123456789ABCDEF", 1);
	}

	public static String genMchNo() {
		PrefixConfig bean = SpringUtil.getBean(PrefixConfig.class);
		return bean.getMch() + DateUtil.currentSeconds() + RandomUtil.randomString("0123456789ABCDEF", 1);
	}

	public static String genMchApplyNo() {
		PrefixConfig bean = SpringUtil.getBean(PrefixConfig.class);
		return bean.getMchApplyment() + DateUtil.format(new Date(), "yyMMdd") + RandomUtil.randomNumbers(6);
	}

	public static String genMchNotifyApplyNo() {
		PrefixConfig bean = SpringUtil.getBean(PrefixConfig.class);
		String randomCharPool = RandomUtil.BASE_NUMBER + RandomUtil.BASE_CHAR.toUpperCase();
		// 商户变更的数据基数会大一些，这里采用8位随机数
		return bean.getMchNotifyNo() + DateUtil.format(new Date(), "yyMMdd") + RandomUtil.randomString(randomCharPool, 8);
	}

	public static String genStoreNo() {
		PrefixConfig bean = SpringUtil.getBean(PrefixConfig.class);
		return bean.getStore() + DateUtil.format(new Date(), "yyMMdd") + RandomUtil.randomNumbers(4);
	}

	/** 生成账户编号 **/
	public static String genTk() {
		return TOKEN_PREFIX + DateUtil.format(new Date(), "yyMMddHHmmss") + RandomUtil.randomNumbers(3);
	}

	public static String genTransOrderId() {
		String randomCharPool = RandomUtil.BASE_NUMBER + RandomUtil.BASE_CHAR.toUpperCase();
		return DateUtil.format(new Date(), "yyMMdd") + RandomUtil.randomString(randomCharPool, 8);
	}
}

