api-datamanager/Company/Basic/Change/ChangeImpl.py

289 lines
9.8 KiB
Python
Raw Normal View History

from datetime import datetime
2022-09-13 14:48:19 +08:00
from bson import ObjectId
2022-09-13 14:48:19 +08:00
2022-08-31 14:58:06 +08:00
from Company.Basic.Business.BusinessImpl import BusinessImpl
from Company.Basic.Change.ChangehObj import BusinessChangeObj
from Company.CompanyUtils import CompanyUtils
from DBHelper.MongoHelperInstance import DB_TYC, DB_TEST
from Utils.CommonUtil import CommonUtils
class ChangeImpl(object):
"""工商变更"""
@staticmethod
2022-09-13 14:48:19 +08:00
def verify_time(**kwargs):
"""校验更新时间"""
def calculate_jet_lag(begin_time):
"""计算时差"""
end_time = CommonUtils.get_current_time()
time_1_struct = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
time_2_struct = datetime.strptime(begin_time, "%Y-%m-%d %H:%M:%S")
seconds = (time_1_struct - time_2_struct).seconds
if seconds > 60:
return True
else:
return False
condition = kwargs['condition']
start_time = DB_TEST.find_all_data(
'WR_DataBase_v2_test',
'C3.6_工商变更',
condition,
['更新时间']
)
if start_time:
res = calculate_jet_lag(start_time[-1]['更新时间'])
else:
res = True
return res
def auto_update(self, **kwargs):
2022-08-31 14:58:06 +08:00
"""新增/更新(自动)"""
condition = kwargs['condition']
def save_data(**args):
2022-09-13 14:48:19 +08:00
"""查询天眼查数据"""
2022-08-31 14:58:06 +08:00
company_name = args['cname']
company_id = args['cid']
records = DB_TYC.find_single_column(
'公司背景',
'变更记录',
{'企业名称': company_name},
'变更记录'
)
if records:
records = records['result']
list_data = list()
for record in records:
dict_data = dict()
dict_data['企业ID'] = company_id
dict_data['变更时间'] = record['changeTime']
dict_data['变更项目'] = record['changeItem']
dict_data['变更前'] = record['contentBefore']
dict_data['变更后'] = record['contentAfter']
dict_data['更新时间'] = CommonUtils.get_current_time()
list_data.append(dict_data)
return list_data
else:
return False
if len(condition['cid']):
cid = condition['cid']
cname = DB_TEST.find_single_column(
'WR_DataBase_v2_test',
'C3.1_工商信息',
{'企业ID': cid},
'企业名称'
)
2022-09-13 14:48:19 +08:00
# 判断更新时间
verify_res = self.verify_time(condition={"企业ID": cid})
if not verify_res:
return 'Please try again in a minute'
2022-08-31 14:58:06 +08:00
update = save_data(cname=cname, cid=cid)
if update:
2022-09-13 14:48:19 +08:00
DB_TEST.insert_many_data(
"WR_DataBase_v2_test",
"C3.6_工商变更",
update
)
2022-08-31 14:58:06 +08:00
return 'success'
else:
return 'update failed'
elif len(condition['cname']):
cname = condition['cname']
cid = DB_TEST.find_single_column(
'WR_DataBase_v2_test',
'C3.1_工商信息',
{'企业名称': cname},
'企业ID'
)
if not cid:
2022-09-07 16:12:29 +08:00
res = BusinessImpl.auto_update(condition={"cname": cname, "cid": ""})
if res == 'success':
cid = DB_TEST.find_single_column(
'WR_DataBase_v2_test',
'C3.1_工商信息',
{'企业名称': cname},
'企业ID'
)
else:
return 'new failed'
2022-09-13 14:48:19 +08:00
# 判断更新时间
verify_res = self.verify_time(condition={"企业ID": cid})
2022-09-13 14:48:19 +08:00
if not verify_res:
return 'Please try again in a minute'
2022-08-31 14:58:06 +08:00
insert = save_data(cname=cname, cid=cid)
if insert:
2022-09-13 14:48:19 +08:00
DB_TEST.insert_many_data(
"WR_DataBase_v2_test",
"C3.6_工商变更",
insert
)
2022-08-31 14:58:06 +08:00
return 'success'
else:
return 'new failed'
else:
return 'parameter exception'
2022-09-13 14:48:19 +08:00
def manu_update(self, **kwargs):
2022-09-09 13:42:02 +08:00
"""新增/更新(手动)"""
cid = kwargs['cid']
cname = kwargs['cname']
template = kwargs['template']
data = CompanyUtils.excel_sheet_parser(file=template)
if len(cid):
2022-09-13 14:48:19 +08:00
# 判断更新时间
verify_res = self.verify_time(condition={"企业ID": cid})
if not verify_res:
return 'Please try again in a minute'
2022-09-09 13:42:02 +08:00
if data:
2022-09-13 14:48:19 +08:00
data_list = list()
2022-09-09 13:42:02 +08:00
for item in data:
change = BusinessChangeObj()
change.cid = cid
2022-09-13 14:48:19 +08:00
change.change_time = CommonUtils.excel_time_stamp(item['变更时间'])
2022-09-09 13:42:02 +08:00
change.change_project = item['变更项目']
change.before = item['变更前']
change.after = item['变更后']
change.update_time = CommonUtils.get_current_time()
2022-09-13 14:48:19 +08:00
data_list.append(change.fields_toggle())
DB_TEST.insert_many_data(
"WR_DataBase_v2_test",
"C3.6_工商变更",
data_list
)
2022-09-09 13:42:02 +08:00
return 'success'
else:
return 'update failed'
elif len(cname):
cid = DB_TEST.find_single_column(
'WR_DataBase_v2_test',
'C3.1_工商信息',
{'企业名称': cname},
'企业ID'
)
if not cid:
res = BusinessImpl.auto_update(condition={"cname": cname, "cid": ""})
if res == 'success':
cid = DB_TEST.find_single_column(
'WR_DataBase_v2_test',
'C3.1_工商信息',
{'企业名称': cname},
'企业ID'
)
else:
return 'new failed'
verify_res = self.verify_time(condition={"企业ID": cid})
2022-09-13 14:48:19 +08:00
if not verify_res:
return 'Please try again in a minute'
2022-09-09 13:42:02 +08:00
if data:
2022-09-13 14:48:19 +08:00
data_list = list()
2022-09-09 13:42:02 +08:00
for item in data:
change = BusinessChangeObj()
change.cid = cid
change.change_time = CommonUtils.excel_time_stamp(item['变更时间'])
change.change_project = item['变更项目']
change.before = item['变更前']
change.after = item['变更后']
change.update_time = CommonUtils.get_current_time()
2022-09-13 14:48:19 +08:00
data_list.append(change.fields_toggle())
DB_TEST.insert_many_data(
"WR_DataBase_v2_test",
"C3.6_工商变更",
data_list
)
2022-09-09 13:42:02 +08:00
return 'success'
else:
return 'parameter exception'
2022-08-31 14:58:06 +08:00
@staticmethod
def delete(**kwargs):
"""删除"""
param = kwargs['param']
2022-09-13 14:48:19 +08:00
_id = param['_id']
2022-08-31 14:58:06 +08:00
res = DB_TEST.delete_single_data(
"WR_DataBase_v2_test",
"C3.6_工商变更",
2022-09-13 14:48:19 +08:00
{"_id": ObjectId(_id)}
2022-08-31 14:58:06 +08:00
)
if res:
result = {
"info": "success",
}
else:
result = {
"info": "no result"
}
return result
@staticmethod
def edit(**kwargs):
"""编辑"""
param = kwargs['param']
2022-09-13 14:48:19 +08:00
_id = param['_id']
2022-08-31 14:58:06 +08:00
body = param['body']
try:
change = BusinessChangeObj()
change.change_time = body['变更时间']
change.change_project = body['变更项目']
change.before = body['变更前']
change.after = body['变更后']
change.update_time = CommonUtils.get_current_time()
update_data = change.fields_toggle()
DB_TEST.update_single_data(
"WR_DataBase_v2_test",
"C3.6_工商变更",
2022-09-13 14:48:19 +08:00
{"_id": ObjectId(_id)},
2022-08-31 14:58:06 +08:00
update_data
)
return 'success'
except Exception:
return 'edit failed'
@staticmethod
def view(**kwargs):
"""查看"""
query = kwargs['query']
cid = query['cid']
page_num = int(query['page_num'])
page_size = int(query['page_size'])
page_size = 10 if page_size > 10 else page_size
total = DB_TEST.find_all_data_with_count(
'WR_DataBase_v2_test',
'C3.6_工商变更',
{"企业ID": cid}
)
record = DB_TEST.find_data_by_page_with_sort(
"WR_DataBase_v2_test",
"C3.6_工商变更",
{"企业ID": cid},
[],
{'_id': 1},
page_size,
page_num
)
if record:
for rec in record:
rec['_id'] = str(rec['_id'])
2022-09-01 16:28:59 +08:00
update_time = record[-1]['更新时间']
2022-08-31 14:58:06 +08:00
result = {
"info": "success",
2022-09-01 16:28:59 +08:00
"result": {
"total": total,
"update_time": update_time,
"records": record
}
2022-08-31 14:58:06 +08:00
}
else:
result = {
"info": "no result"
}
return result