/**
* 生成单号
* @param null $model 模型
* @param string $key 缓存key
* @param string $prefix 单号前缀
* @param string $field 单号字段
* @return bool|string
*/
public static function GetRef($model = null,$key = '',$prefix = 'NO',$field = 'ref')
{
for ($i = 0; $i < 10; $i++) {
$curDate = date('Ymd');
$id = RedisTrait::String('get',$key);
if(!$id){
$id = $model->where($field,'like',$prefix.$curDate.'%')->orderBy('id','desc')->withTrashed()->count();
}
$id = intval($id)+1;
$ref = $prefix.$curDate.sprintf("%06d", $id);
if (!$model->where($field, $ref)->exists()) {
RedisTrait::String('put',$key,$id,86400);
return $ref;
}
}
return false;
}
//使用
Class::GetRef((new Order()),Order::CACHE_KEY_SN,'SN-','order_no');