package cn.estsh.i3plus.core.apiservice.util;

import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SysOrderNoRule;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;

/**
 * @Description : 单号生成工具类
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2018-11-21 13:26
 * @Modify:
 **/
public class OrderNoMakeUtil {

    public static SysOrderNoRule next(SysOrderNoRule orderNoRule) {
        // 数据校验
        if(orderNoRule == null || StringUtils.isBlank(orderNoRule.getOrderNoRule())){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("单号生成规则不存在")
                    .build();
        }

        StringBuilder orderNo = new StringBuilder(orderNoRule.getOrderNoRule());
        String[] dateTime = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(System.currentTimeMillis()).split("-");
        // 单号表达式
        replace(orderNo, PlatformConstWords.YEAR, dateTime[0]);
        replace(orderNo, PlatformConstWords.MONTH, dateTime[1]);
        replace(orderNo, PlatformConstWords.DAY, dateTime[2]);
        replace(orderNo, PlatformConstWords.HOUR, dateTime[3]);
        replace(orderNo, PlatformConstWords.MINUTE, dateTime[4]);
        replace(orderNo, PlatformConstWords.SECOND, dateTime[5]);

        // 流水号长度补全表达式
        String serialNoFormatStr = MessageFormat.format(PlatformConstWords.SERIAL_NO_FORMAT, orderNoRule.getSerialNoLength());

        // 判断是否首次生成（SerialNo初始值-1）
        if (orderNoRule.getSerialNo() > 0) {
            String newNo = orderNo.substring(0,orderNo.indexOf(PlatformConstWords.SERIAL_NO));
            String oldNo = orderNoRule.getOrderNo().substring(0,orderNo.indexOf(PlatformConstWords.SERIAL_NO));
            // 前缀（日期）不同后是否重置流水号
            if(!newNo.equals(oldNo) && orderNoRule.getIsCycle() == CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()){
                orderNoRule.setSerialNo(orderNoRule.getSerialNoSeed());
            } else {
                orderNoRule.setSerialNo(orderNoRule.getSerialNo() + orderNoRule.getSerialNoIncrement());
            }
        } else {
            orderNoRule.setSerialNo(orderNoRule.getSerialNoSeed());
        }
        replace(orderNo, PlatformConstWords.SERIAL_NO, String.format(serialNoFormatStr, orderNoRule.getSerialNo()));

        orderNoRule.setOrderNo(orderNo.toString());
        return orderNoRule;
    }

    private static StringBuilder replace(StringBuilder no, String constant, String param) {
        int day = no.indexOf(constant);
        if (day != -1) {
            no.replace(day, day + constant.length(), param);
        }
        return no;
    }

}
