From e3c451ab42f574122244777ff7a1dfcd7ee1b9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Tue, 29 Mar 2022 05:12:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E6=B3=A8=E5=86=8C=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E7=94=A8=E6=88=B7=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=B8=80=E7=BA=A7?= =?UTF-8?q?=E8=A1=8C=E4=B8=9A=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DBHelper/DBConfig.json | 11 ++ DBHelper/MongoHelper.py | 203 +++++++++++++++++++++++++++++++++ DBHelper/__init__.py | 0 user/static/template/user.json | 3 +- user/user_impl.py | 29 +++++ 5 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 DBHelper/DBConfig.json create mode 100644 DBHelper/MongoHelper.py create mode 100644 DBHelper/__init__.py diff --git a/DBHelper/DBConfig.json b/DBHelper/DBConfig.json new file mode 100644 index 0000000..67c7ddf --- /dev/null +++ b/DBHelper/DBConfig.json @@ -0,0 +1,11 @@ +{ + "MongoDB": { + "tfse_v0.21": "root:UTlC9cCoglD1cI1*@116.63.130.34:27021" + }, + "Mysql": { + + }, + "Redis": { + + } +} \ No newline at end of file diff --git a/DBHelper/MongoHelper.py b/DBHelper/MongoHelper.py new file mode 100644 index 0000000..879733a --- /dev/null +++ b/DBHelper/MongoHelper.py @@ -0,0 +1,203 @@ +import os +import json + +import gridfs +import pymongo +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] + self.client = pymongo.MongoClient('mongodb://{}'.format(this_mongo_cfg)) + + def find_single_column(self, param1, param2, param3, param4): + """ + 查询符合条件的第一条数据的某个指定字段值 + 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 False if record == [] else record + + 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 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/__init__.py b/DBHelper/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/user/static/template/user.json b/user/static/template/user.json index 5fe81c0..a5aade3 100644 --- a/user/static/template/user.json +++ b/user/static/template/user.json @@ -13,5 +13,6 @@ "法人身份证": null, "营业执照fid": null, "身份证fid": null - } + }, + "一级行业": null } \ No newline at end of file diff --git a/user/user_impl.py b/user/user_impl.py index 54bc2a1..47d5520 100644 --- a/user/user_impl.py +++ b/user/user_impl.py @@ -6,6 +6,7 @@ import random import requests from werkzeug.security import check_password_hash, generate_password_hash +from DBHelper.MongoHelper import MongoHelper from user.user_auth import create_token from user.user_utils import make_id, check_mail_fmt, check_pwd_fmt, check_registered, check_verify_code from user.user_db import FIND_USER_INFO, UPDATE_VERIFY_CODE, UPDATE_USER_INFO_BY_CID, INSERT_USER_INFO, \ @@ -385,6 +386,33 @@ def tyc_company_verify(cid): def update_verify_status(): UPDATE_USER_INFO_BY_CID(cid, {"企业名称": name, "已认证": "是"}) + # 更新企业行业信息 + def update_industry_info(): + db = MongoHelper("tfse_v0.21") + + business_info = db.find_single_column( + "企业数据", + "基本工商信息", + {"企业ID": cid}, + "工商信息" + ) + + tyc_industry_l2 = business_info['行业'] + + fecr_industry_l1 = db.find_single_column( + "模型端", + "天眼查公司行业分类", + {"二级行业": tyc_industry_l2}, + "远东_一级行业" + ) + + db.update_single_data( + "应用端", + "企业用户", + {"企业ID": cid}, + {"一级行业": fecr_industry_l1} + ) + # 执行流程 def start_impl(): res = check_verify_info_complete() @@ -397,6 +425,7 @@ def tyc_company_verify(cid): init_company_data() update_verify_status() + update_industry_info() return True