php生成订单号-当天从1开始自增

/**
 * 生成订单号
 *	-当天从1开始自增
 *	-订单号模样:20190604000001
 * @param Client $redis
 * @param $key
 * @param $back:序号回退,如果订单创建失败,事务回滚可用
 * @return string
 */
public static function createOrderSn($key, $back=0)
{
    $sn = Redis::get($key);//laravel redis读取,替换一下
    $snDate = substr($sn,0,8);
    $snNo = intval(substr($sn,8));
    $curDate = date('Ymd');
    if($back==1){//序号回退
        if($curDate==$snDate){
            $snNo = ($snNo>1) ? ($snNo-1) : 1;
            $sn = $curDate.sprintf("%06d",$snNo);
        }
    }else{//序号增加
        if(empty($sn)){
            $sn = $curDate.'000001';
        }else{
            $snNo = ($curDate==$snDate) ? ($snNo+1) : 1;
            $sn = $curDate.sprintf("%06d",$snNo);
        }
    }
    Redis::set($key,$sn);//laravel redis写入,替换一下
    return $sn;
}

自己封装的..

posted @ 2019-06-04 19:15  pine007  阅读(1859)  评论(0编辑  收藏  举报