diff --git a/DBHelper/DBConfig.json b/DBHelper/DBConfig.json new file mode 100644 index 0000000..a4424c3 --- /dev/null +++ b/DBHelper/DBConfig.json @@ -0,0 +1,12 @@ +{ + "MongoDB": { + "guarantee": "root:RYIHrqml#LSW6#!*@116.63.130.34:27020", + "tyc": "root:gP@DwMSVd5Sh6EiH@116.63.130.34:27019" + }, + "Mysql": { + + }, + "Redis": { + + } +} \ No newline at end of file diff --git a/DBHelper/MongoHelper.py b/DBHelper/MongoHelper.py new file mode 100644 index 0000000..4dcc5bc --- /dev/null +++ b/DBHelper/MongoHelper.py @@ -0,0 +1,243 @@ +import re +import os +import json + +import gridfs +import pymongo + +from urllib import parse +from bson import ObjectId +from gridfs import GridFS + + +class MongoHelper: + + def __init__(self, param): + """ + param: + type:str + desc: 选择连接哪个MongoDB数据库 + """ + with open(os.path.abspath(os.path.dirname(__file__) + '/DBConfig.json')) as f: + db_configs = json.load(f) + this_mongo_cfg = db_configs['MongoDB'][param] + m = re.match('([\s\S].*?):([\s\S].*)@([\s\S].*)', this_mongo_cfg) + parsed_mongo_config = "{}:{}@{}".format(parse.quote_plus(m.group(1)), parse.quote_plus(m.group(2)), m.group(3)) + self.client = pymongo.MongoClient('mongodb://{}'.format(parsed_mongo_config)) + + def find_single_column(self, param1, param2, param3, param4): + """ + 查询符合条件的第一条数据的某个指定字段值 + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: str 一个指定查询字段 + return: + type: None or dict + desc: 查询结果为空,返回None; 查询结果正常,返回查询结果的第一条数据; + """ + collection = self.client[param1][param2] + column = {**{'_id': False}, **{param4: 1}} + record = list(collection.find(param3, column)) + return None if record == [] else record[0][param4] + + def find_single_data(self, param1, param2, param3, param4): + """ + 查询符合条件的第一条数据 + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: list 查询字段 + return: + type: bool or dict + desc: 查询结果为空,返回False; 查询结果正常,返回查询结果的第一条数据; + """ + collection = self.client[param1][param2] + columns = {**{'_id': False}, **dict(zip(param4, [1] * len(param4)))} + record = list(collection.find(param3, columns)) + return False if record == [] else record[0] + + def find_single_data_with_single_sort(self, param1, param2, param3, param4, param5): + """ + 查询符合条件的第一条数据,按单个排序条件返回 + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: list 查询字段 + param5: dict 排序条件 例如 {"name": 1} 表示按照name字段正序返回 + return: + type: bool or dict + desc: 查询结果为空,返回False; 查询结果正常,返回查询结果的第一条数据; + """ + collection = self.client[param1][param2] + columns = {**{'_id': False}, **dict(zip(param4, [1] * len(param4)))} + record = list(collection.find(param3, columns).sort(list(param5.keys())[0], list(param5.values())[0]).limit(1)) + return False if record == [] else record[0] + + def find_all_data(self, param1, param2, param3, param4): + """ + 查询符合条件的所有数据 + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: list 查询字段 + return: + type: list + desc: 查询结果 + """ + collection = self.client[param1][param2] + columns = {**{'_id': False}, **dict(zip(param4, [1] * len(param4)))} + record = list(collection.find(param3, columns)) + return record + + def find_all_data_with_count(self, param1, param2, param3): + """ + 查询所有符合条件的数据,并返回统计数量 + param1: str 数据库 + param2: str 数据表 + param3: str 查询条件 + return: int 符合条件的数据数量 + """ + collection = self.client[param1][param2] + num = collection.find(param3).count() + return num + + def find_all_data_with_single_sort(self, param1, param2, param3, param4, param5): + """ + 查询符合条件的数据,按单个排序条件返回 + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: list 查询字段 + param5: dict 排序条件 例如 {"name": 1} 表示按照name字段正序返回 + return: + type: bool or dict + desc: 查询结果为空,返回False; 查询结果正常,返回查询结果的第一条数据; + """ + collection = self.client[param1][param2] + columns = {**{'_id': False}, **dict(zip(param4, [1] * len(param4)))} + record = list(collection.find(param3, columns).sort(list(param5.keys())[0], list(param5.values())[0])) + return False if record == [] else record + + def find_data_with_aggregate(self, param1, param2, param3): + """ + 根据聚合条件查询 + param1: str 数据库 + param2: str 数据集 + param3: + type: list + desc: 聚合条件 + demo: [{'$match':{'price':{'$gte':50}}}, {'$group': {'_id': "$fName", 'count': {'$sum': 1}}}] + """ + collection = self.client[param1][param2] + data = list(collection.aggregate(param3)) + return data + + def find_data_by_page_with_sort(self, param1, param2, param3, param4, param5, param6, param7): + """ + 根据聚合翻页查询,且按照需求字段排序返回 + param1: str 数据库 + param2: str 数据集 + param3: dict 查询条件 + param4: list 显示字段 + param5: dict 排序条件 例如 {"name": 1} 表示按照name字段正序返回 + param6: int 即 page_size 每页数据条数 + param7: int 即 page_no 当前页码 + """ + collection = self.client[param1][param2] + columns = {**{'_id': False}, **dict(zip(param4, [1] * len(param4)))} + page_size = int(param6) + page_no = int(param7) + skip_num = page_size * (page_no - 1) + record = list(collection.find(param3, columns).sort(list(param5.keys())[0], list(param5.values())[0]).limit(page_size).skip(skip_num)) + return record + + def insert_single_data(self, param1, param2, param3): + """ + 插入一条数据 + param1: str 数据库 + param2: str 数据集 + param3: obj 插入数据 + return: None + """ + collection = self.client[param1][param2] + collection.insert_one(param3) + + def insert_many_data(self, param1, param2, param3): + """ + 插入多条数据 + param1: str 数据库 + param2: str 数据集 + param3: obj 插入数据 + return: None + """ + collection = self.client[param1][param2] + collection.insert_many(param3) + + def upsert_single_data(self, param1, param2, param3, param4): + """ + 插入单条数据 + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: dict 更新或新插入的数据 + return: + None + """ + collection = self.client[param1][param2] + collection.update_one(param3, {"$set": param4}, upsert=True) + + def update_single_data(self, param1, param2, param3, param4): + """ + 插入单条数据 + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: dict 更新或新插入的数据 + return: + None + """ + collection = self.client[param1][param2] + collection.update_one(param3, {"$set": param4}) + + def delete_single_data(self, param1, param2, param3): + """ + 根据查询条件删除一条文档 + param1: str 数据库 + param2: str 数据集 + param3: obj 查询条件 + return: None + """ + collection = self.client[param1][param2] + collection.delete_one(param3) + return True + + def find_file(self, param1, param2, param3): + """ + 读取一个文件 + param1: str 数据库 + param2: str 存储桶 + param3: str 文件id + return: + type: binary? + desc: 二进制文件流 + """ + try: + # 实例化一个文件存储器 + gfs = GridFS(self.client[param1], collection=param2) + # 二进制读取文件 + data_stream = gfs.get(ObjectId(param3)).read() + # 返回文件二进制流 + return data_stream + except gridfs.errors.NoFile: + return False + + def delete_file(self, param1, param2, param3): + """ + 根据id删除文件 + param1: str 数据库 + param2: str 存储桶 + param3: str 文件fid + """ + fs = GridFS(self.client[param1], param2) + fs.delete(ObjectId(param3)) diff --git a/DBHelper/MongoHelperInstance.py b/DBHelper/MongoHelperInstance.py new file mode 100644 index 0000000..6786c8a --- /dev/null +++ b/DBHelper/MongoHelperInstance.py @@ -0,0 +1,8 @@ +from DBHelper.MongoHelper import MongoHelper + +# 天眼查数据库 +DB_TYC = MongoHelper("tyc") + +# 股交项目数据库 +DB_GUA = MongoHelper("guarantee") + diff --git a/DBHelper/__init__.py b/DBHelper/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/routes.py b/routes.py index c683ada..2dbbdb2 100644 --- a/routes.py +++ b/routes.py @@ -60,8 +60,9 @@ def upload_staff_data(): @verify_token def upload_risk_info(): """保存担保所需风险信息""" - name = json.loads(request.data)['企业名称'] - res = save_guarantee_risk_info(name) + data = json.loads(request.data) + name, scope, limit = data['企业名称'], data['数据范围'], data['时间范围'] + res = save_guarantee_risk_info(name, scope, limit) if res == '保存成功': return {"info": res}, 200 else: diff --git a/tyc.py b/tyc.py index 16f0c28..f5447ed 100644 --- a/tyc.py +++ b/tyc.py @@ -1,12 +1,16 @@ """ 天眼查API """ +import datetime import json +import time + import requests from common import judege_api_code, general_request from db import conserve_data, find_db_data, conserve_data_by_hold from setting import DB_SFFX, DB_JYFX, DB_TYFX, DB_GSBJ, DB_JYZK, DB_ZSCQ, DB_GSFZ +from DBHelper.MongoHelperInstance import DB_TYC token = "32737167-cb63-4ce9-9397-d66169488f51" headers = {'Authorization': token} @@ -408,18 +412,74 @@ def save_full_data(name): return res +# 根据企业名称查询司法风险 +def get_risk_info(param1, param2, param3, param4): + """ + param1: 数据库 + param2: 数据集 + param1: 企业名称 + param1: 录入时间 + """ + record = DB_TYC.find_single_data( + param1, + param2, + {'企业名称': param3}, + [param2, '录入时间'] + ) + target_date = datetime.datetime.strptime(record['录入时间'], "%Y-%m-%d %H:%M:%S") + current_date = datetime.datetime.now() + minus = current_date - target_date + if minus.days <= param4: + return True + else: + return False + + # 保存担保相关风险数据 -def save_guarantee_risk_info(name): - res = judicial_risk(name) - if res == '保存成功': - res = final_case(name) - if res == '保存成功': - res = consumption_restriction_order(name) - if res == '保存成功': - res = administrative_sanction(name) - return res +def save_guarantee_risk_info(name, scope, limit): + """ + scope: 如果是其他只调用司法风险接口,如果为空则调用所有相关接口 + limit: 为时间范围,整型,可填3、7、14、30,单位为天 + """ + if not scope: + dishonest = get_risk_info('司法风险', '失信人', name, limit) + executed = get_risk_info('司法风险', '被执行人', name, limit) + legal_action = get_risk_info('司法风险', '诉讼', name, limit) + if not dishonest and not executed and not legal_action: + judicial_risk(name) + final = get_risk_info('司法风险', '终本案件', name, limit) + if not final: + final_case(name) + consumption = get_risk_info('司法风险', '限制消费令', name, limit) + if not consumption: + consumption_restriction_order(name) + penalties = get_risk_info('经营风险', '行政处罚', name, limit) + if not penalties: + administrative_sanction(name) + else: + for item in scope: + if item == '其他': + dishonest = get_risk_info('司法风险', '失信人', name, limit) + executed = get_risk_info('司法风险', '被执行人', name, limit) + legal_action = get_risk_info('司法风险', '诉讼', name, limit) + if not dishonest and not executed and not legal_action: + judicial_risk(name) + if item == '终本案件': + final = get_risk_info('司法风险', '终本案件', name, limit) + if not final: + final_case(name) + if item == '限制消费令': + consumption = get_risk_info('司法风险', '限制消费令', name, limit) + if not consumption: + consumption_restriction_order(name) + if item == '行政处罚': + penalties = get_risk_info('经营风险', '行政处罚', name, limit) + if not penalties: + administrative_sanction(name) + + return '保存成功' # if __name__ == '__main__': - # r = save_guarantee_risk_info('吉林森工中小企业融资担保有限责任公司') - # print(r) +# r = save_guarantee_risk_info('吉林森工中小企业融资担保有限责任公司', ['终本案件'], 30) +# print(r)