daily/Utils/DataToDbUtils.py

108 lines
3.8 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
2023-03-07 16:42:34 +08:00
from Models.UserModel import UserInfo,User
from Models.DailyModel import Daily
2023-03-02 09:27:05 +08:00
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'])
2023-03-07 16:42:34 +08:00
def load_daily():
db=get_db_i()
data=pd.read_excel('动态类数据.xlsx')
length=data.shape[0]
d_name_dic = {item.name: item.id for item in db.query(Department).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
for i in range(length):
row=data.loc[i].to_dict()
报送类型=row['报送类型']
报送人=row['报送人']
报送时间=row['报送时间']
标题=row['标题']
标题="" if pd.isna(标题) else 标题
正文=row['正文']
部门=row['部门']
职务=row['职务']
email=db.query(User).filter_by(name=报送人).first().email
department_id=d_name_dic[部门]
post_id=post_dic[department_id][职务]
db.add(Daily(type=报送类型,fill_user=email,title=标题,content=正文,post=post_id,department=department_id,daily_time=报送时间))
db.commit()