Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
my_flask_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 0962db41
authored
May 17, 2024
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
添加技工导入脚本
1 parent
86095b1e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
176 additions
and
0 deletions
技工数据导入.py
技工数据导入.py
0 → 100644
View file @
0962db4
import
math
import
mysql.connector
import
pandas
as
pd
# 定义Excel文件路径
# excel_path = r"C:\Users\mima123456\Desktop\工作簿2.xlsx"
excel_path
=
r"D:\data01\导入详单2024.5.16.xlsx"
# 使用pandas读取Excel文件
df
=
pd
.
read_excel
(
excel_path
,
sheet_name
=
"可导入详单2024.5.17"
)
df
.
fillna
(
"nan"
,
inplace
=
True
)
def
select_sql
(
sql
):
try
:
# conn = mysql.connector.connect(
# host='192.168.3.227',
# user='dev_user',
# password='df234fl',
# database='glsx_car_service'
# )
conn
=
mysql
.
connector
.
connect
(
host
=
"192.168.5.1"
,
port
=
"3307"
,
user
=
"user_yangll"
,
password
=
"vGxw9jWg"
,
database
=
"glsx_car_service"
,
)
cursor
=
conn
.
cursor
()
cursor
.
execute
(
sql
)
return
cursor
.
fetchone
()[
0
]
except
Exception
as
msg
:
print
(
"sql执行Exception: "
+
str
(
msg
))
finally
:
if
cursor
is
not
None
:
cursor
.
close
()
if
conn
is
not
None
:
conn
.
close
()
def
execute_sql
(
sql
):
# conn = mysql.connector.connect(
# host='192.168.3.227',
# user='dev_user',
# password='df234fl',
# database='glsx_car_service'
# )
conn
=
mysql
.
connector
.
connect
(
host
=
"192.168.5.1"
,
port
=
"3307"
,
user
=
"user_yangll"
,
password
=
"vGxw9jWg"
,
database
=
"glsx_car_service"
,
)
try
:
cursor
=
conn
.
cursor
()
cursor
.
execute
(
sql
)
conn
.
commit
()
except
Exception
as
msg
:
print
(
"sql执行Exception: "
+
str
(
msg
))
# 发生错误时回滚事务
conn
.
rollback
()
finally
:
if
cursor
is
not
None
:
cursor
.
close
()
if
conn
is
not
None
:
conn
.
close
()
def
progress
():
mylist
=
device
()
# indent_no = 66666666610
indent_no
=
66666666776
for
userid
in
mylist
:
type_
=
"sys_order_service_item_gps"
indent_no
=
repertory
(
indent_no
,
userid
,
type_
)
type_
=
"sys_order_service_item_gps_wireless"
indent_no
=
repertory
(
indent_no
,
userid
,
type_
)
def
device
():
# 插入数据
my_list
=
[]
for
index
,
row
in
df
.
iterrows
():
print
(
row
)
type_
=
row
[
3
]
# 技工
mechanic_user_
=
row
[
7
]
# 服务商
provider_user_
=
row
[
8
]
if
type_
==
"有线"
:
type_
=
"sys_order_service_item_gps"
if
type_
==
"无线"
:
type_
=
"sys_order_service_item_gps_wireless"
if
mechanic_user_
is
None
or
mechanic_user_
==
'""'
or
mechanic_user_
==
"nan"
:
# 如果技工不存在 则使用服务商
sql0
=
"select user_id from sys_manager where provider_name = '{}'"
.
format
(
provider_user_
)
print
(
sql0
)
userid
=
select_sql
(
sql0
)
else
:
# 如果技工存在 则使用技工
sql0
=
"select id from sys_user where name = '{}' order by id desc limit 1"
.
format
(
mechanic_user_
)
print
(
sql0
)
userid
=
select_sql
(
sql0
)
if
userid
is
None
:
continue
my_list
.
append
(
userid
)
sql1
=
(
"INSERT INTO glsx_car_service.sys_device (device_type, device_no, device_status, user_id, stock_time, remark, create_time, create_by, update_time, update_by, device_source) "
"VALUES('{}', '{}', 1, {}, now(), '批量导入', now(), -1, now(), -1, 1)"
.
format
(
type_
,
row
[
1
],
userid
)
)
print
(
sql1
)
write_sql
(
sql1
)
# execute_sql(sql1)
return
pd
.
unique
(
my_list
)
def
repertory
(
indent_no
,
userid
,
type_
):
sql2
=
(
"select count(1) from sys_device where user_id={} and device_type='{}'"
.
format
(
userid
,
type_
)
)
print
(
sql2
)
dnum
=
select_sql
(
sql2
)
if
dnum
>
0
:
sql3
=
"INSERT INTO sys_device_repertory (user_id, device_type, device_num, create_time, update_time,version) VALUES ({}, '{}', {}, now(), now(), 1) ON DUPLICATE KEY UPDATE device_num = {} "
.
format
(
userid
,
type_
,
dnum
,
dnum
)
print
(
sql3
)
write_sql
(
sql3
)
# execute_sql(sql3)
sql4
=
(
"INSERT INTO glsx_car_service.sys_indent ( indent_no, send_type, user_id, device_type, device_amount, indent_status,is_canceled, phone, receiver_name, district_code, address, remark,create_by, create_time, update_by, update_time) "
"VALUES ('{}', 2, {}, '{}', {}, 40, 0, '13825236036', '林经理', 120101,'广联赛讯', '手动添加',{}, now(), {}, now())"
.
format
(
indent_no
,
userid
,
type_
,
dnum
,
userid
,
userid
)
)
print
(
sql4
)
write_sql
(
sql4
)
# execute_sql(sql4)
sql5
=
"update sys_device set indent_no='{}' where user_id={} and device_type='{}'"
.
format
(
indent_no
,
userid
,
type_
)
print
(
sql5
)
write_sql
(
sql5
)
# execute_sql(sql5)
indent_no
+=
1
return
indent_no
def
write_sql
(
sql
):
print
(
sql
)
with
open
(
"sql01.txt"
,
"a+"
,
encoding
=
"utf8"
)
as
fw
:
fw
.
write
(
sql
.
strip
())
fw
.
write
(
"
\n
"
)
if
__name__
==
"__main__"
:
progress
()
Write
Preview
Markdown
is supported
Attach a file
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 post a comment