Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
financial-system
Project
Project
Details
Activity
Releases
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
xianyang
financial-system
Commits
a603b921
Commit
a603b921
authored
Jun 19, 2023
by
xianyang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化导出,从cos下载
parent
8d8f8c0f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
4 deletions
+90
-4
views.py
app/api/account/views.py
+3
-1
env.py
core/config/env.py
+9
-0
db_link.py
libs/db_link.py
+28
-0
export.py
libs/export.py
+50
-3
No files found.
app/api/account/views.py
View file @
a603b921
import
json
from
typing
import
Optional
from
fastapi
import
Depends
,
APIRouter
,
Request
,
Query
from
fastapi.responses
import
RedirectResponse
from
sqlalchemy.orm
import
Session
from
app
import
get_db
from
app.api.account
import
schemas
,
crud
...
...
@@ -115,7 +116,8 @@ def finance_info_excel(data: schemas.FinanceDetails, request: Request,
field_head
=
[
'订单号'
,
'出入账方式'
,
'礼物类型'
,
'金额'
,
'时间'
]
data
=
crud
.
delete_specify_field
(
statement_list
,
data
.
unique_tag
)
# return statement_crud.data_to_file(db, data, "财务明细", headers, field_head)
return
TableToFile
(
db
,
data
,
"财务明细"
,
headers
,
field_head
)
.
main_method
()
url
=
TableToFile
(
db
,
data
,
"财务明细"
,
headers
,
field_head
)
.
main_method
()
return
HttpResultResponse
(
data
=
url
)
@
router
.
get
(
"/finance/fourth/details"
)
...
...
core/config/env.py
View file @
a603b921
...
...
@@ -34,6 +34,7 @@ config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Toke
client
=
CosS3Client
(
config
)
COS_PATH
=
'https://fj-dc-test-1256890024.cos.ap-guangzhou.myqcloud.com'
# 测试
COS_RERURN_PATH
=
'/images/'
tencent
=
apo
.
get
(
"TencentCloud"
)
class
Env
(
BaseSettings
):
...
...
@@ -70,6 +71,10 @@ class TestingEnv(Env):
CLEARING_CENTER_PORT
:
int
=
5454
KEY
=
"dK8tZ1jM0wA6oE3j"
PHP_URL
=
"http://106.55.103.148:8787"
TX_SECRET_ID
=
tencent
.
get
(
"secret_id"
)
TX_SECRET_KEY
=
tencent
.
get
(
"secret_key"
)
TX_REGION
=
tencent
.
get
(
"region"
)
TX_BUCKET
=
tencent
.
get
(
"bucket"
)
class
ProdEnv
(
Env
):
...
...
@@ -95,6 +100,10 @@ class ProdEnv(Env):
CLEARING_CENTER_PORT
:
int
=
5454
KEY
=
"dK8tZ1jM0wA6oE3j"
PHP_URL
=
"http://219.152.95.226:6750"
TX_SECRET_ID
=
tencent
.
get
(
"secret_id"
)
TX_SECRET_KEY
=
tencent
.
get
(
"secret_key"
)
TX_REGION
=
tencent
.
get
(
"region"
)
TX_BUCKET
=
tencent
.
get
(
"bucket"
)
env
=
TestingEnv
()
# 开发环境
...
...
libs/db_link.py
View file @
a603b921
...
...
@@ -60,3 +60,31 @@ class LinkMysql(object):
# rb_info = pika.PlainCredentials(rb.get('username'), rb.get('password'))
# self.connection = pika.BlockingConnection(parameters=pika.ConnectionParameters(rb.get('host'), rb.get('port'), rb.get('vhost'), rb_info))
# self.channel = self.connection.channel()
from
qcloud_cos
import
CosConfig
from
qcloud_cos
import
CosS3Client
# 连接服务器
class
LinkMonitor
(
object
):
def
__init__
(
self
):
conn
=
CosConfig
(
Region
=
env
.
TX_REGION
,
SecretId
=
env
.
TX_SECRET_ID
,
SecretKey
=
env
.
TX_SECRET_KEY
,
Token
=
None
,
Scheme
=
'https'
)
self
.
client
=
CosS3Client
(
conn
)
# 断点上传
def
upload_block
(
self
,
path
,
key
,
folder
=
""
):
# 正常情况日志级别使用INFO,需要定位时可以修改为DEBUG,此时SDK会打印和服务端的通信信息
# logging.basicConfig(level=logging.INFO, stream=sys.stdout)
token
=
None
scheme
=
'https'
# 指定使用 http/https 协议来访问 COS,默认为 https,可不填
response
=
self
.
client
.
upload_file
(
Bucket
=
env
.
TX_BUCKET
,
Key
=
folder
+
key
+
'.xlsx'
,
LocalFilePath
=
path
,
EnableMD5
=
False
,
progress_callback
=
None
)
libs/export.py
View file @
a603b921
...
...
@@ -10,6 +10,8 @@ from app.api.statement.guild import query_token
from
starlette.responses
import
StreamingResponse
from
datetime
import
datetime
from
app.api.export
import
crud
from
core.config.env
import
env
from
libs.db_link
import
LinkMonitor
from
libs.log_utils
import
Logger
...
...
@@ -192,6 +194,51 @@ class TableToFile(object):
# Logger().logger.info(f"返回压缩文件!!!")
# return response
# def main_method(self):
# """主函数"""
# Logger().logger.info('开始导出')
# user = query_token(self.db, self.header)
# params = {"source": self.name, "method": "data_to_file", "status": 1}
# if len(self.data) == 0:
# params["status"] = 3
# crud.create_export_data(self.db, params, user)
# Logger().logger.info(f'导出没有数据')
# return None
# try:
# bk = pd.DataFrame(self.data)
# if self.data[0].get('create_time'):
# if isinstance(self.data[0]['create_time'], int):
# bk['create_time'] = bk['create_time'].apply(
# lambda x: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(x)))
# bk.columns = self.field_list # 修改pandas头
# write_data = bk.to_dict(orient='records')
# with pd.ExcelWriter(f'static/{self.name}.xlsx') as writer:
# # bk.to_excel(writer, sheet_name='Sheet1', index=False)
# threads = []
# rows_per_thread = math.ceil(len(write_data) / 5)
# for i in range(5):
# sheet_name = 'sheet' + str(i + 1)
# threads.append(threading.Thread(target=self.thread_task,
# args=[bk.iloc[i * rows_per_thread: rows_per_thread * (i+1)], writer, sheet_name]))
# # 启动线程
# for y in threads:
# y.start()
# # 等待所有线程完成
# for z in threads:
# z.join()
# # 记录导出
# crud.create_export_data(self.db, params, user)
# import zipfile
# with zipfile.ZipFile('ceshi.zip', 'w') as zip:
# # 将指定文件添加到压缩文件中
# zip.write(f"static/{self.name}.xlsx")
# return
# except Exception as e:
# Logger().logger.info(f'导出异常:{str(e)}')
# params["status"] = 2
# crud.create_export_data(self.db, params, user)
from
fastapi.responses
import
RedirectResponse
def
main_method
(
self
):
"""主函数"""
Logger
()
.
logger
.
info
(
'开始导出'
)
...
...
@@ -224,13 +271,13 @@ class TableToFile(object):
# 等待所有线程完成
for
z
in
threads
:
z
.
join
()
file
=
open
(
writer
,
'rb'
)
SITE_ROOT
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)))
LinkMonitor
()
.
upload_block
(
f
'{SITE_ROOT}/static/{self.name}.xlsx'
,
self
.
name
,
'finance/'
)
# 记录导出
crud
.
create_export_data
(
self
.
db
,
params
,
user
)
return
StreamingResponse
(
file
,
media_type
=
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
except
Exception
as
e
:
Logger
()
.
logger
.
info
(
f
'导出异常:{str(e)}'
)
params
[
"status"
]
=
2
crud
.
create_export_data
(
self
.
db
,
params
,
user
)
return
f
"https://{env.TX_BUCKET}.cos.ap-guangzhou.myqcloud.com/finance/{self.name}.xlsx"
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