package com.neusoft.smart.pos.utils;

import com.neusoft.smart.pos.framework.utils.LocalDateTimeUtils;
import com.neusoft.smart.pos.framework.utils.RedisUtils;

/**
 * 序列号生成工具类
 */
public class SequenceUtils {

    public static final String SEQ_PREFIX = "SEQ_";
    public static final String ORDER_PREFIX = "ORDER_";
    public static final String GRANT_ORDER_PREFIX = "GRANT_ORDER_";

    /**
     * 获取订单每日序列号(每天从1开始)
     * @param key
     * @return
     */
    public static Integer getOrderSequenceNoForDay(String key){
        Integer sequenceNo =  0;
        try{
            Long seconds = LocalDateTimeUtils.betweenNowSeconds(LocalDateTimeUtils.getCurrentDayEnd());
            sequenceNo = RedisUtils.setIncr(SEQ_PREFIX+ORDER_PREFIX+LocalDateTimeUtils.getDateTime(LocalDateTimeUtils.SIMPLE_DATE_FORMAT)+"_"+key,seconds).intValue();
        }catch (Exception e){
            e.printStackTrace();
            return sequenceNo;
        }
        return sequenceNo;
    }
    
    /**
     * 获取预授权订单每日序列号(每天从1开始)
     * @param key
     * @return
     */
    public static Integer getGrantOrderSequenceNoForDay(String key){
        Integer sequenceNo =  0;
        try{
            Long seconds = LocalDateTimeUtils.betweenNowSeconds(LocalDateTimeUtils.getCurrentDayEnd());
            sequenceNo = RedisUtils.setIncr(SEQ_PREFIX+GRANT_ORDER_PREFIX+LocalDateTimeUtils.getDateTime(LocalDateTimeUtils.SIMPLE_DATE_FORMAT)+"_"+key,seconds).intValue();
        }catch (Exception e){
            e.printStackTrace();
            return sequenceNo;
        }
        return sequenceNo;
    }

}
