package com.zhongshu.card.server.core.util;

import com.github.microservice.core.helper.ApplicationContextHolder;
import com.zhongshu.card.server.core.dao.org.CollectionIdDao;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author TRX
 * @CreateDate: 2023/3/29
 * @Version: 1.0
 */
public class NextNoUtil {

    private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    public static String getNextPaymentNo(String sign) {
        if (sign == null) {
            sign = "P";
        }
        CollectionIdDao collectionIdDao = ApplicationContextHolder.getContext().getBean(CollectionIdDao.class);
        String collectionName = sign + longSdf.format(new Date());
        Long no = collectionIdDao.getNextId(collectionName);
        if (no != null) {
            return String.format("%s%s%04d", sign, longSdf.format(new Date()), no);
        }
        return "";
    }

    /**
     * 生成编码 （每天从 0001开始）
     *
     * @param sign 开头，如：XS
     * @return 如：XS202303290001
     */
    public static String getNextNo(String sign) {
        CollectionIdDao collectionIdDao = ApplicationContextHolder.getContext().getBean(CollectionIdDao.class);
        String collectionName = sign + shortSdf.format(new Date());
        Long no = collectionIdDao.getNextId(collectionName);
        if (no != null) {
            return String.format("%s%s%04d", sign, shortSdf.format(new Date()), no);
        }
        return "";
    }

    /**
     * 生成编码 （每天从 0001开始）
     *
     * @param oid  企业ID
     * @param sign 开头，如：XS
     * @return 如：XS202303290001
     */
    public static String getNo(String oid, String sign) {
        CollectionIdDao collectionIdDao = ApplicationContextHolder.getContext().getBean(CollectionIdDao.class);
        String collectionName = sign + shortSdf.format(new Date());
        Long no = collectionIdDao.getNextId(oid, collectionName);
        if (no != null) {
            return String.format("%s%s%04d", sign, shortSdf.format(new Date()), no);
        }
        return "";
    }

    /**
     * 生成编码 （一直累加）
     *
     * @param oid  企业ID
     * @param sign 开头，如：XS
     * @return 如：XS202303290001
     */
    public static String getNextNo(String oid, String sign) {
        CollectionIdDao collectionIdDao = ApplicationContextHolder.getContext().getBean(CollectionIdDao.class);
        Long no = collectionIdDao.getNextId(oid, sign);
        if (no != null) {
            return String.format("%s%s%04d", sign, shortSdf.format(new Date()), no);
        }
        return "";
    }
}
