daily/Utils/DataToDbUtils.py

80 lines
2.7 KiB
Python
Raw Normal View History

2023-03-02 09:27:05 +08:00
import pandas as pd
from Utils.SqlAlchemyUtils import get_db_i
from Models.PostModel import Post
from Models.DepartmentModel import Department
from Models.UserModel import UserInfo
import json
def user_table_to_db():
data = json.load(open("Config/Company1.json", "r", encoding="utf-8"))
db = get_db_i()
d_name_dic = {item.name: item.id for item in db.query(Department).all()}
d_id_dic = {item.id: item.name for item in db.query(Department).all()}
# 创建职务表
# db.query(Post).delete()
# for d_type in data:
# item = data[d_type]
# for d in item:
# p_list = item[d]
# for p_name in p_list:
# d_id = d_name_dic[d]
# db.add(Post(belong=d_id, name=p_name))
# db.commit()
items = [item for item in db.query(Post).all()]
post_dic = {}
for item in db.query(Post).all():
if item.belong not in post_dic:
post_dic[item.belong] = {}
if item.name not in post_dic[item.belong]:
post_dic[item.belong][item.name] = item.id
dt = pd.read_excel("远东员工花名册.xlsx")
line_count = dt.shape[0]
for i in range(line_count):
row = dt.loc[i]
email = row["邮箱"]
name = row["姓名"]
d1 = row["部门1"]
d2 = row["部门2"]
d3 = row["部门3"]
p1 = row["职务1"]
p2 = row["职务2"]
p3 = row["职务3"]
ds = set()
ps = set()
if not pd.isna(d1):
print(d1, p1)
ds.add(d_name_dic[d1])
ps.add(post_dic[d_name_dic[d1]][p1])
if not pd.isna(d2):
print(d2, p2)
ds.add(d_name_dic[d2])
ps.add(post_dic[d_name_dic[d2]][p2])
if not pd.isna(d3):
print(d3, p3)
ds.add(d_name_dic[d3])
ps.add(post_dic[d_name_dic[d3]][p3])
d_t = ",".join([str(item) for item in ds])
p_t = ",".join([str(item) for item in ps])
user = UserInfo(email=email, name=name, department=d_t, post=p_t)
db.add(user)
db.commit()
# row = dt.loc[i]
# if row['部门1'] not in post_dic:
# post_dic[[row['部门1']]] = []
# if row['职务1'] not in post_dic[row['部门1']]:
# post_dic[row['部门1']].append(row['职务1'])
#
# if row['部门2'] not in post_dic:
# post_dic[[row['部门2']]] = []
# if row['职务2'] not in post_dic[row['部门2']]:
# post_dic[row['部门2']].append(row['职务2'])
#
# if row['部门3'] not in post_dic:
# post_dic[[row['部门3']]] = []
# if row['职务3'] not in post_dic[row['部门3']]:
# post_dic[row['部门3']].append(row['职务3'])