Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
Kivii.Biz.Finances.V2.5
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陶然
Kivii.Biz.Finances.V2.5
Commits
04819511
Commit
04819511
authored
Apr 23, 2025
by
Neo Turing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
573f861f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
3 deletions
+68
-3
Extension.cs
Src/Extensions/Extension.cs
+56
-0
InvoiceExtension.cs
Src/Extensions/InvoiceExtension.cs
+12
-3
No files found.
Src/Extensions/Extension.cs
View file @
04819511
...
@@ -13,6 +13,7 @@ namespace Kivii.Finances
...
@@ -13,6 +13,7 @@ namespace Kivii.Finances
/// </summary>
/// </summary>
internal
static
class
Extension
internal
static
class
Extension
{
{
#
region
获取枚举类型描述
/// <summary>
/// <summary>
/// 获取枚举值的描述特性
/// 获取枚举值的描述特性
/// </summary>
/// </summary>
...
@@ -71,5 +72,60 @@ namespace Kivii.Finances
...
@@ -71,5 +72,60 @@ namespace Kivii.Finances
return
result
;
return
result
;
}
}
#
endregion
#
region
获取随机字符,用于生成子流水号时如果仍然重复则添加随机字符后缀
// 全局Random实例,用于生成种子
private
static
readonly
Random
_global
=
new
Random
();
// 线程本地Random实例,每个线程有自己独立的实例
[
ThreadStatic
]
private
static
Random
_local
;
/// <summary>
/// 线程安全的随机字符串生成方法
/// </summary>
/// <param name="length">要生成的字符串长度,默认3</param>
/// <returns>返回随机字符串,如果length<=0则返回空字符串</returns>
public
static
string
GetRandomCharsThreadSafe
(
int
length
=
3
)
{
// 参数校验:如果传入长度小于等于0,直接返回空字符串
if
(
length
<=
0
)
{
return
string
.
Empty
;
}
// 定义可选的字符集合(包含大小写字母和数字)
const
string
chars
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
;
// 检查当前线程是否已经初始化了Random实例
if
(
_local
==
null
)
{
// 加锁确保多线程环境下只有一个线程能进入初始化
lock
(
_global
)
{
// 再次检查,防止其他线程已经初始化
if
(
_local
==
null
)
{
// 使用全局Random生成种子,创建线程本地Random实例
_local
=
new
Random
(
_global
.
Next
());
}
}
}
// 创建用于存储结果的字符数组
char
[]
result
=
new
char
[
length
];
// 循环生成每个随机字符
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
// 从chars字符串中随机选取一个字符
result
[
i
]
=
chars
[
_local
.
Next
(
chars
.
Length
)];
}
// 将字符数组转换为字符串并返回
return
new
string
(
result
);
}
#
endregion
}
}
}
}
Src/Extensions/InvoiceExtension.cs
View file @
04819511
...
@@ -21,11 +21,20 @@ namespace Kivii.Finances
...
@@ -21,11 +21,20 @@ namespace Kivii.Finances
{
{
invoice
.
RootKvid
.
ThrowIfEmpty
(
"RootKvid不能为空"
);
invoice
.
RootKvid
.
ThrowIfEmpty
(
"RootKvid不能为空"
);
if
(
conn
==
null
)
conn
=
KiviiContext
.
GetOpenedDbConnection
<
Invoice
>();
if
(
conn
==
null
)
conn
=
KiviiContext
.
GetOpenedDbConnection
<
Invoice
>();
var
rootInvoice
=
conn
.
SingleById
<
Invoice
>(
invoice
.
RootKvid
);
var
query
=
conn
.
From
<
Invoice
>();
query
.
Where
(
o
=>
o
.
Kvid
==
invoice
.
RootKvid
);
query
.
Select
(
o
=>
new
{
o
.
SerialNumber
,
o
.
Kvid
});
var
rootInvoice
=
conn
.
Single
(
query
);
rootInvoice
.
ThrowIfNull
(
"主发票记录不存在"
);
rootInvoice
.
ThrowIfNull
(
"主发票记录不存在"
);
var
allcount
=
conn
.
Count
<
Invoice
>(
o
=>
o
.
RootKvid
==
rootInvoice
.
Kvid
);
//allcount不需要加1,因为Root的也统计进去了
//allcount不需要加1,因为Root的也统计进去了
return
$"
{
rootInvoice
.
SerialNumber
}
-
{
allcount
}
"
;
var
allcount
=
conn
.
Count
<
Invoice
>(
o
=>
o
.
RootKvid
==
rootInvoice
.
Kvid
);
var
subSerialNumber
=
$"
{
rootInvoice
.
SerialNumber
}
-
{
allcount
}
"
;
var
rtns
=
subSerialNumber
;
if
(
conn
.
Exists
<
Invoice
>(
o
=>
o
.
SerialNumber
==
subSerialNumber
))
{
rtns
=
$"
{
subSerialNumber
}
.
{
Extension
.
GetRandomCharsThreadSafe
()}
"
;
}
return
rtns
;
}
}
/// <summary>
/// <summary>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment