class AppBlackRoom(db.Model, BaseModel): # 指定数据库 __bind_key__ = 'analysis_repertory' __tablename__ = 'app_black_room' # table model id = db.Column(db.Integer, primary_key=True) app_package = db.Column(db.String(100), index=True, nullable=False, server_default=db.FetchedValue()) down_from = db.Column(db.SmallInteger, nullable=False, server_default=db.FetchedValue()) over_time = db.Column(db.DateTime, index=True) def getBlackRoom(self, app_package, down_from): where = and_( AppBlackRoom.app_package == app_package, AppBlackRoom.down_from == down_from, ) return db.session.query(AppBlackRoom).filter(where).order_by( desc('id')).limit(1).first() def putBlackRoom(self, app_package, down_from): oInfo = self.getBlackRoom(app_package, down_from) if oInfo is None: data = {} data['app_package'] = app_package data['down_from'] = down_from data['over_time'] = datetime.now() + timedelta(hours=+8) self.addByDict(data) else: oInfo['over_time'] = datetime.now() + timedelta(hours=+8) db.session.commit()
class AfRelationBase(db.Model, Base): # 指定数据库 __bind_key__ = 'xhh_antifraud' __tablename__ = 'af_relation_base' id = db.Column(db.BigInteger, primary_key=True) base_id = db.Column(db.BigInteger, nullable=False, server_default='0') strategy_request_id = db.Column(db.BigInteger, nullable=False, server_default='0') create_time = db.Column(db.DateTime, nullable=False) def getData(self, base_id, strategy_request_id): if base_id is None: return False if strategy_request_id is None: return False where = and_( AfRelationBase.base_id == int(base_id), AfRelationBase.strategy_request_id == int(strategy_request_id)) data = self.query.filter(where).first() return data def getInfo(self, base_id): if base_id is None: return False data = self.query.filter( AfRelationBase.base_id == int(base_id)).first() return data ''' 保存数据 ''' def saveData(self, base_id, strategy_request_id): if base_id is None: return False if strategy_request_id is None: return False #判断是否存在 get_data = self.getData(base_id, strategy_request_id) #存在不记录 if get_data: return True try: self.base_id = base_id self.strategy_request_id = strategy_request_id self.create_time = datetime.now() self.add() db.session.commit() return True except AttributeError as error: print(error) return False except sqlalchemy.exc.IntegrityError as error: print(error) return False
class DetailList(db.Model, BaseModel): __bind_key__ = 'analysis_repertory' __tablename__ = 'detail_list' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer) user_id = db.Column(db.BigInteger, index=True) mobile = db.Column(db.String(20), index=True) phone = db.Column(db.String(20), index=True) call_times = db.Column(db.Integer) use_time = db.Column(db.Integer) min_time = db.Column(db.DateTime) max_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) def addDetailList(self, dict_data): try: if dict_data is None or len(dict_data) == 0: return False self.aid = dict_data.aid self.user_id = dict_data.user_id self.mobile = dict_data.mobile self.phone = dict_data.phone self.call_times = dict_data.call_times self.create_time = datetime.now() self.add() db.session.commit() return True except Exception as e: logger.error("addDetailList:%s" % e) return False def getByUserPhone(self, mobile): db_phones = db.session.query(DetailList).filter(DetailList.mobile == mobile).limit(1000).all() phones = [] for i in db_phones: phones.append(self.row2dict(i)) return phones def saveDetailList(self, dict_res, data): createTime = data.create_time if data is not None else '0000-00-00 00:00:00' mobile = str(data.phone) if data is not None else '' aid = int(data.aid) if data is not None else 0 user = YiUser().getByMobile(mobile) user_id = int(user.user_id) if user is not None else 0 insertSql = [] for key, value in dict_res.items(): phone = str(key) min_time = str(value.get('min_time','0000-00-00 00:00:00')) max_time = str(value.get('max_time','0000-00-00 00:00:00')) use_time = int(value.get('use_time',0)) call_times = int(value.get('call_times',0)) data_dict = {'aid': aid, 'user_id': user_id, 'mobile' : mobile, 'call_times' : call_times, 'min_time' : min_time, 'max_time' : max_time, 'use_time' : use_time, 'phone' : phone, 'create_time' : createTime} insertSql.append(data_dict) db.session.execute(self.__table__.insert(),insertSql) db.session.commit()
class AfResult(db.Model, BaseModel): # 指定数据库 __bind_key__ = 'xhh_antifraud' __tablename__ = 'af_result' id = db.Column(db.BigInteger, primary_key=True) request_id = db.Column(db.BigInteger, nullable=False, index=True, server_default=db.FetchedValue()) aid = db.Column(db.Integer) user_id = db.Column(db.BigInteger, nullable=False, index=True, server_default=db.FetchedValue()) setting_id = db.Column(db.BigInteger, nullable=False, server_default=db.FetchedValue()) score = db.Column(db.Integer, server_default=db.FetchedValue()) result_status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) result_subject = db.Column(db.Text, nullable=False) create_time = db.Column(db.DateTime, nullable=False) def addResult(self, dict_data): try: if dict_data is None or len(dict_data) == 0: return False self.request_id = dict_data.get('request_id') self.user_id = dict_data.get('user_id') self.aid = dict_data.get('aid',1) self.setting_id = dict_data.get('setting_id', 0) self.score = dict_data.get('score', 0) self.result_status = dict_data.get('result_status') self.result_subject = dict_data.get('result_subject') self.create_time = datetime.now() self.add() db.session.commit() return True except Exception as e: logger.error("AfResult-addOne:%s" % e) return False
class ReverseDetailList(db.Model, BaseModel): __bind_key__ = 'analysis_repertory' __tablename__ = 'reverse_detail_list' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer) user_id = db.Column(db.BigInteger, index=True) mobile = db.Column(db.String(20), index=True) phone = db.Column(db.String(20), index=True) call_times = db.Column(db.Integer) use_time = db.Column(db.Integer) min_time = db.Column(db.DateTime) max_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) def getDetailByPhones(self, phones): if len(phones) == 0: return [] oUserPhones = db.session.query(ReverseDetailList.mobile).filter( ReverseDetailList.phone.in_(phones)).limit(10000).all() if oUserPhones: return [i.mobile for i in oUserPhones] else: return [] def saveDetailList(self, dict_res, data): createTime = data.create_time if data is not None else '0000-00-00 00:00:00' mobile = str(data.phone) if data is not None else '' aid = int(data.aid) if data is not None else 0 user = YiUser().getByMobile(mobile) user_id = int(user.user_id) if user is not None else 0 insertSql = [] for key, value in dict_res.items(): phone = str(key) min_time = str(value.get('min_time', '0000-00-00 00:00:00')) max_time = str(value.get('max_time', '0000-00-00 00:00:00')) use_time = int(value.get('use_time', 0)) call_times = int(value.get('call_times', 0)) data_dict = { 'aid': aid, 'user_id': user_id, 'mobile': mobile, 'call_times': call_times, 'min_time': min_time, 'max_time': max_time, 'use_time': use_time, 'phone': phone, 'create_time': createTime } insertSql.append(data_dict) db.session.execute(self.__table__.insert(), insertSql) db.session.commit()
class ReverseAddressList(db.Model, BaseModel): __bind_key__ = 'analysis_repertory' __tablename__ = 'reverse_address_list' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer) user_id = db.Column(db.BigInteger) user_phone = db.Column(db.String(20), index=True) phone = db.Column(db.String(20)) name = db.Column(db.String(32)) modify_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) def getAddrByPhones(self, phones): if len(phones) == 0: return [] oUserPhones = db.session.query(ReverseAddressList.user_phone).filter( ReverseAddressList.phone.in_(phones)).limit(10000).all() if oUserPhones: return [i.user_phone for i in oUserPhones] else: return [] def getAddrByPhone(self, phones, loan_time): if len(phones) == 0: return [] oUserPhones = db.session.query(ReverseAddressList.user_phone).filter( ReverseAddressList.phone == phones).filter( ReverseAddressList.create_time <= loan_time).limit( 10000).all() if oUserPhones: return [i.user_phone for i in oUserPhones] else: return []
class AddressList(db.Model, BaseModel): __bind_key__ = 'analysis_repertory' __tablename__ = 'address_list' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer) user_id = db.Column(db.BigInteger) user_phone = db.Column(db.String(20), index=True) phone = db.Column(db.String(20)) name = db.Column(db.String(32)) modify_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) def getByUserPhone(self, user_phone): db_phones = db.session.query(AddressList.phone).filter(AddressList.user_phone == user_phone).limit(2000).all() phones = [] # print(db_phones) for i in db_phones: phone = {'phone':str(i[0])} # phones.append(self.row2dict(i)) phones.append(phone) return phones def getByUserPhones(self, phones): if len(phones) == 0: return [] oUserPhones = db.session.query(AddressList.phone,AddressList.user_phone).filter(AddressList.user_phone.in_(phones)).limit(1000000).all() if oUserPhones: return oUserPhones else : return []
class YgyUserLoanFlow(db.Model, BaseModel): __bind_key__ = 'xhh_yigeyi' __tablename__ = 'yi_user_loan_flows' metadata = MetaData() id = db.Column(db.Integer, primary_key=True) loan_id = db.Column(db.Integer, nullable=False) admin_id = db.Column(db.Integer, nullable=False) loan_status = db.Column(db.Integer) relative = db.Column(db.String(1024)) reason = db.Column(db.String(1024)) create_time = db.Column(db.DateTime) admin_name = db.Column(db.String(64)) type = db.Column(db.Integer, server_default=db.FetchedValue()) advise = db.Column(db.String(1024))
class LoanUserLoanFlows(db.Model, BaseModel): __bind_key__ = 'spark' __tablename__ = 'loan_user_loan_flows' id = db.Column(db.Integer, primary_key=True) loan_id = db.Column(db.Integer, nullable=False) admin_id = db.Column(db.Integer, nullable=False) loan_status = db.Column(db.Integer, nullable=False) relative = db.Column(db.String(1024)) reason = db.Column(db.String(1024)) create_time = db.Column(db.DateTime) modify_time = db.Column(db.DateTime) version = db.Column(db.Integer, nullable=False)
class AppList(db.Model, Base): # 指定数据库 __bind_key__ = 'analysis_repertory' __tablename__ = 'app_list' # table model id = db.Column(db.BigInteger, primary_key=True) app_name = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue()) app_package = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue()) create_time = db.Column(db.DateTime, index=True) def getAppByPkgList(self, pkgList): data = {} if len(pkgList) <= 0: return data appList = db.session.query(AppList.id, AppList.app_package).filter( AppList.app_package.in_(pkgList)).all() if appList is not None and len(appList) > 0: for i in appList: data[i.id] = i.app_package return data def batchInsert(self, insertApp, diffAppList): db.session.execute(self.__table__.insert(), insertApp) db.session.commit() return self.getAppByPkgList(diffAppList) def getAppByAppid(self, appidList): if len(appidList) <= 0: return {} appList = db.session.query(AppList).filter( AppList.id.in_(appidList)).all() if appList is None or len(appList) == 0: return {} data = {} for appInfo in appList: each = {} each['app_id'] = appInfo.id each['app_name'] = appInfo.app_name each['app_package'] = appInfo.app_package data[appInfo.id] = each return data
class UserAppSnapshot(db.Model, Base): __bind_key__ = 'analysis_repertory' __tablename__ = 'user_app_snapshot' # table model id = db.Column(db.BigInteger, primary_key=True) mobile = db.Column(db.String(32), index=True, nullable=False, server_default=db.FetchedValue()) app_list = db.Column(db.Text, nullable=False) create_time = db.Column(db.DateTime, nullable=False) def getAppList(self, mobile): if len(mobile) == 0: return None return db.session.query(UserAppSnapshot.app_list).filter( UserAppSnapshot.mobile == mobile).order_by( desc('id')).limit(1).first() def saveAppList(self, mobile, time, app_list): if len(mobile) == 0 or len(time) == 0 or len(app_list) == 0: return False data = {} data['mobile'] = mobile data['app_list'] = app_list data['create_time'] = time self.addByDict(data) def getAppListWithTime(self, mobile, time): if mobile is None or time is None or len(mobile) != 11 or len( time) != 19: return [] where = and_(UserAppSnapshot.mobile == str(mobile), UserAppSnapshot.create_time <= time) appList = db.session.query( UserAppSnapshot.app_list).filter(where).order_by( desc('id')).limit(1).first() if appList is None or len(appList) < 0: return [] return appList.app_list
class YiFavoriteContact(db.Model, BaseModel): __bind_key__ = 'xhh_yiyiyuan' __tablename__ = 'yi_favorite_contacts' id = db.Column(db.BigInteger, primary_key=True) user_id = db.Column(db.BigInteger, nullable=False) contacts_name = db.Column(db.String(20), nullable=False) mobile = db.Column(db.String(20), nullable=False) relatives_name = db.Column(db.String(20), nullable=False) phone = db.Column(db.String(20), nullable=False) last_modify_time = db.Column(db.DateTime, nullable=False) create_time = db.Column(db.DateTime) def getByUserId(self, user_id): return self.query.filter_by(user_id=user_id).limit(1).first() def contactDue(self, dbContact): # 亲属联系人是否逾期 if dbContact is None: return {} mobiles = [] #亲属 if 'phone' in dbContact.keys(): mobiles.append(str(dbContact['phone'])) #常用 if 'mobile' in dbContact.keys(): mobiles.append(str(dbContact['mobile'])) if len(mobiles) == 0: return {} oUser = YiUser() overdue_users = oUser.isOverdueMobile(mobiles) overdue_mobiles = [user[0] for user in overdue_users] # # 判断是否逾期 contract_due_data = {} if 'phone' in dbContact.keys() and dbContact['phone'] is not None: if dbContact['phone'] in overdue_mobiles: contract_due_data['com_r_overdue'] = 1 else: contract_due_data['com_r_overdue'] = 0 if 'mobile' in dbContact.keys() and dbContact['mobile'] is not None: if dbContact['mobile'] in overdue_mobiles: contract_due_data['com_c_overdue'] = 1 else: contract_due_data['com_c_overdue'] = 0 return contract_due_data
class YiAddressList(db.Model, BaseModel): __bind_key__ = 'own_yiyiyuan' __tablename__ = 'yi_address_list' id = db.Column(db.BigInteger, primary_key=True) user_id = db.Column(db.BigInteger, nullable=False) phone = db.Column(db.String(20), nullable=False) name = db.Column(db.String(20), nullable=False) modify_time = db.Column(db.DateTime, nullable=False) create_time = db.Column(db.DateTime, nullable=False) def getByUserid(self, user_id): if not user_id: return None #@todo 这里会不会有问题 dbAddrLists = self.query.filter( YiAddressList.user_id == user_id).limit(10000).all() addrLists = [] for i in dbAddrLists: addrLists.append(self.row2dict(i)) return addrLists def blackNum(self, user_id): ''' 通讯录中有黑名单 ''' return 0
class TagInfo(db.Model, Base): # 指定数据库 __bind_key__ = 'xhh_antifraud' __tablename__ = 'tag_info_list' id = db.Column(db.BigInteger, primary_key=True) phone = db.Column(db.String(20), unique=True) source = db.Column(db.Integer) tag_type = db.Column(db.String(255)) status = db.Column(db.SmallInteger, default=0) type = db.Column(db.SmallInteger, nullable=False) modify_time = db.Column(db.DateTime, nullable=False) create_time = db.Column(db.DateTime, nullable=False) def addResult(self, dict_data, source=100): try: if not dict_data: return False self.phone = dict_data.get('phone') self.source = source self.tag_type = dict_data.get('tag_type') self.status = dict_data.get('status', 0) self.type = 1 self.modify_time = datetime.now() self.create_time = datetime.now() res = db.session.query(TagInfo).filter( TagInfo.phone == self.phone).first() tags = dict_data['tag_type'] for i in tags: if i == '其他' or i == '其它': tags.remove(i) if len(tags): if res is not None: res.type = 2 tag_list = res.tag_type.split(',') # 列表 for i in tags: if i not in tag_list: tag_list.append(i) res.tag_type = ','.join(tag_list) res.modify_time = datetime.now() db.session.commit() else: self.tag_type = ','.join(tags) self.add() db.session.commit() logger.info('存储到数据库的手机号%s的标识是%s' % (self.phone, self.tag_type)) except Exception as e: logger.error("TagInfo-addOne:%s" % e) return False
class AfIncrMatching(db.Model, Base): __bind_key__ = 'xhh_antifraud' __tablename__ = 'af_incr_matching' id = db.Column(db.BigInteger, primary_key=True) base_id = db.Column(db.BigInteger, nullable=False, server_default="0") user_id = db.Column(db.BigInteger, nullable=False, server_default="0") proportion = db.Column(db.DECIMAL(10, 4), nullable=False, server_default="0.0000") status = db.Column(db.SmallInteger, nullable=False) create_time = db.Column(db.DateTime, nullable=False) modify_time = db.Column(db.DateTime, nullable=False) ''' 通过user_id获取信息 ''' def getOne(self, base_id): if not base_id: return False info = db.session.query(AfIncrMatching).filter( AfIncrMatching.base_id == base_id).first() return info ''' 修改数据 ''' def updateOne(self, tag_list, proportion): info = self.getOne(tag_list.base_id) if not info: return False info.user_id = tag_list.user_id info.proportion = proportion info.modify_time = datetime.now() db.session.commit() return True ''' 保存数据 ''' def saveData(self, tag_list, proportion, status): self.base_id = tag_list.base_id self.user_id = tag_list.user_id self.proportion = proportion self.status = status self.create_time = datetime.now() self.modify_time = datetime.now() db.session.add(self) db.session.commit() return True
class ApiOtherData(db.Model, Base): __bind_key__ = 'xhh_antifraud' __tablename__ = 'api_other_data' id = db.Column(db.BigInteger, primary_key=True) base_id = db.Column(db.BigInteger, nullable=False, server_default='0') learning_letter = db.Column(db.Integer, nullable=False, server_default='0') learning_letter_contrast = db.Column(db.String(32), nullable=False, server_default='') ocial_security = db.Column(db.Integer, nullable=False, server_default='0') ocial_security_contrast = db.Column(db.String(32), nullable=False, server_default='') accumulation_fund = db.Column(db.Integer, nullable=False, server_default='0') accumulation_fund_contrast = db.Column(db.String(32), nullable=False, server_default='') max_account_detail_balance = db.Column(db.String(32), nullable=False, server_default='') create_time = db.Column(db.DateTime, nullable=False)
class AfJcardMatch(db.Model, Base): __bind_key__ = 'xhh_antifraud' __tablename__ = 'af_jcard_match' id = db.Column(db.BigInteger, primary_key=True) request_id = db.Column(db.BigInteger, nullable=False, index=True) aid = db.Column(db.Integer, nullable=False) user_id = db.Column(db.BigInteger, nullable=False, index=True) jcard_result = db.Column(db.Text) create_time = db.Column(db.DateTime, nullable=False) def getRelationData(self, user_id, request_id, aid): ''' 获取已存在关系(relation)的数据 ''' where = and_( AfJcardMatch.request_id == request_id, AfJcardMatch.user_id == user_id, AfJcardMatch.aid == aid, ) res = db.session.query(AfJcardMatch) \ .filter(where) \ .order_by(AfJcardMatch.id.desc()) \ .limit(1) \ .first() return res def getRelation(self, url): # 获取详情 strings = self._getByUrl(url) data = json.loads(strings) return data def _getByUrl(self, url): # 获取url中的内容, 设置超时 html = self._getByUrl2(url) if html is None: # 重试 html = self._getByUrl2(url) if html is None: raise Exception(1000, 'cant download by ' + url) return html def _getByUrl2(self, url): socket.setdefaulttimeout(25) try: response = urllib.request.urlopen(url) html = response.read() except Exception as e: logger.error('url get fail %s' % e) html = None return html
class YiUserRemitList(db.Model, BaseModel): __bind_key__ = 'xhh_yiyiyuan' __tablename__ = 'yi_user_remit_list' user_id = db.Column(db.BigInteger, primary_key=True) loan_id = db.Column(db.BigInteger, nullable=False, index=True) remit_status = db.Column(db.String(12)) fund = db.Column(db.Integer, nullable=False) create_time = db.Column(db.DateTime) def getMobiles(self, start_time, end_time): ''' 获取需要处理的数据, 默认查询1000条 ''' start_time = start_time end_time = end_time # 借款资方数据 sql = "SELECT u.mobile,u.user_id,l.loan_id FROM yi_user_remit_list l LEFT JOIN yi_user u ON l.user_id = u.user_id WHERE l.remit_status = 'SUCCESS' AND l.fund = 6 AND l.create_time >= '" + start_time + "' AND l.create_time <= '" + end_time + "' order by l.id DESC LIMIT 1000;" users = db.session.execute(sql, bind=self.get_engine()).fetchall() if len(users) == 0: return None return users
class AfMultiMatch(db.Model, Base): __bind_key__ = 'xhh_antifraud' __tablename__ = 'af_multi_match' id = db.Column(db.BigInteger, primary_key=True) request_id = db.Column(db.BigInteger, nullable=False, index=True) aid = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, nullable=False, index=True) relation_ship = db.Column(db.Text) create_time = db.Column(db.DateTime, nullable=False)
class SyncIdList(db.Model, Base): __bind_key__ = 'analysis_repertory' __tablename__ = 'sync_id_list' # class attr STATUS_INIT = 0 STATUS_DOING = 1 STATUS_SUCCESS = 2 SYNC_DETAIL = 'sync_detail' SYNC_APP = 'sync_app' SYNC_MSG = 'sync_msg' # table model id = db.Column(db.BigInteger, primary_key=True) start_id = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) end_id = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) sync_status = db.Column(db.Integer, index=True, nullable=False, server_default=db.FetchedValue()) sync_type = db.Column(db.String(32), index=True, nullable=False, server_default=db.FetchedValue()) create_time = db.Column(db.DateTime, index=True) modify_time = db.Column(db.DateTime, index=True) def get(self, user_id): return self.query.get(user_id) def getInitSyncId(self, sync_type): where = and_( SyncIdList.sync_status == self.STATUS_INIT, SyncIdList.sync_type == sync_type, ) db_sync = db.session.query(SyncIdList).filter(where).order_by( desc('id')).limit(1).first() return db_sync def lockStatus(self, status): self.sync_status = status self.modify_time = datetime.now() db.session.commit()
class Mobile(db.Model, BaseModel): __bind_key__ = 'sparrow' __tablename__ = 'mobile' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer) user_id = db.Column(db.BigInteger, index=True) mobile = db.Column(db.String(20), index=True) realname = db.Column(db.String(32)) identity = db.Column(db.String(20)) create_time = db.Column(db.DateTime) def getUidsByMobiles(self,mobiles): if len(mobiles) == 0: return [] oUsers = db.session.query(Mobile.mobile).filter(Mobile.mobile.in_(mobiles)).limit(1000).all() if oUsers : return [i.mobile for i in oUsers] else : return []
class PhoneTagList(db.Model, BaseModel): LIMIT = 500 __bind_key__ = 'analysis_repertory' __tablename__ = 'phone_tag_list' id = db.Column(db.BigInteger, primary_key=True) phone = db.Column(db.String(20), nullable=False) source = db.Column(db.BigInteger, nullable=False) tag_type = db.Column(db.String(255), nullable=False) modify_time = db.Column(db.DateTime, nullable=False) create_time = db.Column(db.DateTime, nullable=False) other_info = db.Column(db.Text) ''' 通过手机号获取标签条数 ''' def getLabelAll(self, phone_tuple): phone_tuple = list(set(phone_tuple)) chunk_phone_list = self.chunk(phone_tuple) all_list = [] for phone_list in chunk_phone_list: son_list = db.session.query(PhoneTagList). \ filter(PhoneTagList.phone.in_(phone_list)).\ limit(self.LIMIT).\ all() if len(son_list) > 0: all_list.extend(son_list) return all_list def chunk(self, biglist): #大列表分割成小列表 limit = self.LIMIT mlen = len(biglist) mc = math.ceil(mlen / limit) lst = [] for i in range(0, int(mc)): lst.append(biglist[i * limit:(i + 1) * limit]) return lst
class YiApplicationList(db.Model, BaseModel): __bind_key__ = 'xhh_yiyiyuan' __tablename__ = 'yi_application_list' id = db.Column(db.BigInteger, primary_key=True) user_id = db.Column(db.BigInteger, index=True) content = db.Column(db.Text, nullable=True) create_time = db.Column(db.DateTime, index=True) last_modify_time = db.Column(db.DateTime, index=True) type = db.Column(db.Integer) version = db.Column(db.Integer) def getDateByUserIds(self, start, end, type): where = and_(YiApplicationList.id >= start, YiApplicationList.id < end, YiApplicationList.type == type) oApp = db.session.query(YiApplicationList.user_id, YiApplicationList.content, YiApplicationList.last_modify_time).filter( where).limit(1000).all() return oApp def get_maxid(self): maxid = db.session.query(func.max(YiApplicationList.id)).scalar() return maxid
class YiAddressList(db.Model, BaseModel): __bind_key__ = 'own_yiyiyuan' __tablename__ = 'yi_address_list' id = db.Column(db.BigInteger, primary_key=True) user_id = db.Column(db.BigInteger, nullable=False) phone = db.Column(db.String(20), nullable=False) name = db.Column(db.String(20), nullable=False) modify_time = db.Column(db.DateTime, nullable=False) create_time = db.Column(db.DateTime, nullable=False) def getByUserid(self, user_id): if not user_id: return None #@todo 这里会不会有问题 dbAddrLists = self.query.filter( YiAddressList.user_id == user_id).limit(10000).all() addrLists = [] for i in dbAddrLists: addrLists.append(self.row2dict(i)) return addrLists def blackNum(self, user_id): ''' 通讯录中有黑名单 ''' return 0 #where = and_(YiAddressList.user_id == user_id, YiUser.status == 5) #t = db.session.query(func.count(distinct(YiAddressList.id)).label("user_count")).outerjoin( # YiUser, YiAddressList.phone == YiUser.mobile) #res = t.filter(where).limit(1).first() #return res.user_count def getMessageList(self, mobiles): if len(mobiles) == 0: return [] counts = db.session.query(YiAddressList).filter( YiAddressList.phone.in_(mobiles)).limit(1000).all() return counts
class ShopLoanUserLoan(db.Model, BaseModel): __bind_key__ = 'loan_shop' __tablename__ = 'loan_user_loan' metadata = MetaData() loan_id = db.Column(db.BigInteger, primary_key=True) parent_loan_id = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) number = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) settle_type = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) user_id = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) loan_no = db.Column(db.String(32)) amount = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue()) days = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) desc = db.Column(db.String(64)) start_date = db.Column(db.DateTime) end_date = db.Column(db.DateTime) type = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) service_fee = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue()) interest_fee = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue()) contract = db.Column(db.String(20)) contract_url = db.Column(db.String(128)) withdraw_fee = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue()) coupon_amount = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue()) business_type = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) bank_id = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) come_from = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) modify_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) version = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) def getAllLoanByUids(self, user_ids): '''通讯录有过贷款的数量 ''' if len(user_ids) == 0: return 0 #select count(1) from yi_user_loan where user_id in (5419061,2968724,2697378,2616279) loanCounts = db.session.query(ShopLoanUserLoan).filter( ShopLoanUserLoan.user_id.in_(user_ids)).count() return loanCounts def getLoanedByUids(self, user_ids): '''通讯录有过放款的数量 ''' if len(user_ids) == 0: return 0 #借款状态:1 初始;2 驳回;3 出款中;4 审核通过;5 已放款;6 还款成功;7 逾期 loanedStatus = [3, 5, 6, 7, 11] # select count(1) from yi_user_loan where user_id in (5419061,2968724,2697378,2616279) and status in (8,9,11,12,13); res = db.session.query(ShopLoanUserLoan).filter( ShopLoanUserLoan.user_id.in_(user_ids), ShopLoanUserLoan.status.in_(loanedStatus)).count() return res def lateApplyDay(self, user_ids): '''通讯录最近一次申请借款日 ''' if len(user_ids) == 0: return '' res = db.session.query( ShopLoanUserLoan.create_time.label('create_time')).filter( ShopLoanUserLoan.user_id.in_(user_ids)).order_by( desc(ShopLoanUserLoan.create_time)).first() if res: return res[0].strftime('%Y-%m-%d %H:%m:%S') else: return '' def getHistroyBadStatus(self, user_ids): '''通讯录中有过申请且历史最坏账单状态''' if len(user_ids) == 0: return 0 # status = [3,7,8,9,11,12,13] # loan_status = [3,7] # datas = db.session.query(YiLoan.status,YiLoan.repay_time,YiLoan.end_date, YiUserLoanFlow.admin_id,YiUserLoanFlow.loan_status).outerjoin(YiUserLoanFlow,and_(YiLoan.loan_id == YiUserLoanFlow.loan_id,YiUserLoanFlow.loan_status in loan_status)).filter(YiLoan.user_id.in_(user_ids),YiLoan.status.in_(status)).limit(1000).all() userid_str = '"' + '","'.join(str(val) for val in user_ids) + '"' #sql = "select ul.status,ul.repay_time,ul.last_modify_time,ul.end_date,ulf.admin_id,ulf.loan_status from yi_user_loan as ul left join yi_user_loan_flows as ulf on ul.loan_id = ulf.loan_id and ulf.loan_status in (3,7) where ul.user_id in (%s) and ul.status in (3,7,8,9,11,12,13) LIMIT 1000 " % userid_str sql = "select ul.status,lr.modify_time as repay_time, ul.modify_time as last_modify_time, ul.end_date,ulf.admin_id,ulf.loan_status from loan_user_loan as ul left join loan_repay as lr on (ul.loan_id=lr.loan_id) left join loan_overdue_loan as ol on (ol.loan_id=ul.loan_id) left join loan_user_loan_flows ulf on ulf.loan_id=ul.loan_id and ulf.loan_status in (3,7) where ul.user_id in (%s) and ul.status in (2,3,5) or ol.loan_status = 7 or lr.status in (4,6) limit 1000" % userid_str datas = db.session.execute(sql, bind=self.get_engine()).fetchall() if len(datas) == 0: return None else: returnData = [] for data in datas: if data.status == 3: returnData.append(-900) elif data.loan_status == 7 and data.admin_id and data.admin_id == -1: returnData.append(-800) elif data.loan_status == 7 and data.admin_id and data.admin_id == -2: returnData.append(-700) elif data.loan_status == 7 and data.admin_id and data.admin_id > 0: returnData.append(-600) elif data.status == 9: returnData.append(-500) elif data.status == 8: date = data.repay_time if data.repay_time else data.last_modify_time diffDay = 0 if data.end_date is not None and date is not None: diffDay = (date - data.end_date).days diffDay = diffDay - 1 returnData.append(diffDay) elif data.status in [11, 12, 13]: date = data.repay_time if data.repay_time else datetime.now( ) diffDay = (date - data.end_date).days returnData.append(diffDay) else: returnData.append(-900) if len(returnData) > 0: realadl_tot_reject_num = len([ dt for dt in returnData if dt in [-800, -900, -700, -600] ]) realadl_tot_freject_num = len( [dt for dt in returnData if dt == -700]) realadl_tot_sreject_num = len( [dt for dt in returnData if dt == -800]) realadl_tot_dlq14_num = len( [dt for dt in returnData if dt > 14]) tmp_num = len([ dt for dt in returnData if dt not in [-800, -900, -700, -600, -500] ]) realadl_dlq14_ratio = 999999 if tmp_num == 0 else float( '%.2f' % (realadl_tot_dlq14_num / tmp_num)) return { 'realadl_tot_reject_num': realadl_tot_reject_num, 'realadl_tot_freject_num': realadl_tot_freject_num, 'realadl_tot_sreject_num': realadl_tot_sreject_num, 'realadl_tot_dlq14_num': realadl_tot_dlq14_num, 'realadl_dlq14_ratio': realadl_dlq14_ratio, 'history_bad_status': max(returnData), 'realadl_dlq14_ratio_denominator': tmp_num } else: return None def getSuccessNum(self, mobile, identity): sql = 'select count(1) as success_num from loan_user_loan ul inner join loan_user u on ul.user_id = u.user_id where u.mobile = "%s" and u.identity = "%s" and ul.status in (6) ' % ( mobile, identity) count = db.session.execute(sql, bind=self.get_engine()).fetchone() return count[0] def getLastSuccLoan(self, user_id): returnData = { 'last_end_date': '', 'last_repay_time': '', 'last_success_loan_days': 0 } sql = "select ul.days, ul.end_date, lr.modify_time from loan_user_loan as ul left join loan_repay as lr on (ul.loan_id = lr.loan_id) where ul.user_id = %s and lr.status=6" % user_id data = db.session.execute(sql, bind=self.get_engine()).first() if data is not None: returnData['last_end_date'] = data.end_date.strftime( '%Y-%m-%d %H:%m:%S') returnData['last_repay_time'] = data.modify_time.strftime( '%Y-%m-%d %H:%m:%S') returnData['last_success_loan_days'] = data.days return returnData def getIsLoading(self, user_id): if not user_id: return 0 loan_status = [3, 4, 5] where = and_(ShopLoanUserLoan.status.in_(loan_status), ShopLoanUserLoan.user_id == user_id) data = db.session.query(ShopLoanUserLoan).filter(where).limit( 1).first() if data is None: return 0 return 1 def getApplyLoan(self, user_id): if not user_id: return 0 where = and_(ShopLoanUserLoan.user_id == user_id, ShopLoanUserLoan.number == 0) data = db.session.query(ShopLoanUserLoan).filter(where).count() return data
class StrategyRequest(db.Model, BaseModel): __bind_key__ = 'xhh_strategy' __tablename__ = 'st_strategy_request' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) req_id = db.Column(db.BigInteger, nullable=False, unique=True, server_default=db.FetchedValue()) user_id = db.Column(db.BigInteger, nullable=False, index=True, server_default=db.FetchedValue()) loan_id = db.Column(db.BigInteger, nullable=False, index=True, server_default=db.FetchedValue()) status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue()) come_from = db.Column(db.Integer, nullable=True, server_default=db.FetchedValue()) callbackurl = db.Column(db.String(255), nullable=False, server_default=db.FetchedValue()) create_time = db.Column(db.DateTime, nullable=False) modify_time = db.Column(db.DateTime, nullable=False) version = db.Column(db.Integer, nullable=False) def getById(self, id): db_strategy = self.query.get(int(id)) return db_strategy def getByReqId(self, db_base): if db_base.request_id is None: return None if db_base.aid is None: return None if db_base.user_id is None: return None where = and_( StrategyRequest.req_id == int(db_base.request_id), StrategyRequest.aid == int(db_base.aid), StrategyRequest.user_id == int(db_base.user_id), ) data = self.query.filter(where).order_by( StrategyRequest.id.desc()).first() return data def getByIdData(self, id): if id is None: return None where = and_(StrategyRequest.id == int(id)) data = self.query.filter(where).first() return data def getData(self, end_time): ''' 获取需要处理的数据, 默认查询一小时内 ''' # 1. 精确到分 end_time = datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S') end_time = end_time + timedelta(seconds=end_time.second - 360) # 2. 查询一小时内 #start_time = end_time - timedelta(seconds=3600) where = and_(StrategyRequest.aid.in_([1, 14]), StrategyRequest.create_time < end_time, StrategyRequest.status == 100) res = db.session.query(StrategyRequest) \ .filter(where) \ .order_by(StrategyRequest.id) \ .limit(1000) \ .all() return res def lock(self, data, status): if len(data) == 0: return False now = datetime.now() for o in data: o.modify_time = now o.status = status # 锁定 | 完成 db.session.commit() return True def finished(self, status): self.status = status self.modify_time = datetime.now() self.version = self.version + 1 db.session.commit() return True # 获取待处理数据 def getInitData(self): now = datetime.now() start_time = now + timedelta(seconds=now.second - 86400) where = and_( StrategyRequest.status == 0, StrategyRequest.create_time > start_time, ) res = db.session.query(StrategyRequest).filter(where).order_by( StrategyRequest.id).limit(1).all() return res
class LoanUser(db.Model, BaseModel): __bind_key__ = 'spark' __tablename__ = 'loan_user' user_id = db.Column(db.BigInteger, primary_key=True) user_no = db.Column(db.String(32)) mobile = db.Column(db.String(12)) invite_code =db.Column(db.String(32)) from_code = db.Column(db.String(32)) head_img = db.Column(db.String(128)) realname = db.Column(db.String(32)) identity = db.Column(db.String(20)) sex = db.Column(db.Integer, server_default=db.FetchedValue()) email = db.Column(db.String(64)) marriage = db.Column(db.Integer, server_default=db.FetchedValue()) status = db.Column(db.Integer, server_default=db.FetchedValue()) identity_valid = db.Column(db.Integer, server_default=db.FetchedValue()) come_from = db.Column(db.Integer, server_default=db.FetchedValue()) last_login_time = db.Column(db.DateTime) last_login_position = db.Column(db.Integer, server_default=db.FetchedValue()) create_time = db.Column(db.DateTime) modify_time = db.Column(db.DateTime) version = db.Column(db.Integer, server_default=db.FetchedValue()) def getUidsByMobiles(self,mobiles): if len(mobiles) == 0: return [] oUsers = db.session.query(LoanUser.user_id.label('user_id')).filter(LoanUser.mobile.in_(mobiles)).limit(1).all() if oUsers : return [i.user_id for i in oUsers] else : return [] def getUidCounts(self,mobiles): if len(mobiles) == 0: return 0 counts = db.session.query(LoanUser).filter(LoanUser.mobile.in_(mobiles)).limit(1000).count() return counts def getByIdentity(self, identity): db_user = db.session.query(LoanUser).filter(LoanUser.identity == identity).first() return db_user def getByMobile(self, mobile): db_user = db.session.query(LoanUser).filter(LoanUser.mobile == mobile).first() return db_user
class ReverseAddressList(db.Model, BaseModel): __bind_key__ = 'analysis_repertory' __tablename__ = 'reverse_address_list' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer) user_id = db.Column(db.BigInteger) user_phone = db.Column(db.String(20), index=True) phone = db.Column(db.String(20)) name = db.Column(db.String(32)) modify_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) def getAddrByPhones(self, phones): if len(phones) == 0: return [] oUserPhones = db.session.query(ReverseAddressList.user_phone).filter( ReverseAddressList.phone.in_(phones)).limit(10000).all() if oUserPhones: return [i.user_phone for i in oUserPhones] else: return [] def getAddrByPhone(self, phones, loan_time): if len(phones) == 0: return [] oUserPhones = db.session.query(ReverseAddressList.user_phone).filter( ReverseAddressList.phone == phones).filter( ReverseAddressList.create_time <= loan_time).limit( 10000).all() if oUserPhones: return [i.user_phone for i in oUserPhones] else: return [] """ 通过元组通讯录手机号获取间接人和创建时间返回字典 """ def getAddrByAll(self, phones): mobile_data = {} if len(phones) == 0: return mobile_data oUserPhones = db.session.query( ReverseAddressList.user_phone, ReverseAddressList.phone, ReverseAddressList.create_time).filter( ReverseAddressList.phone.in_(phones)).limit(10000).all() if oUserPhones: for i in oUserPhones: if i[0] not in mobile_data: #user_phone 手机号 phone通讯录中的手机号 create_time创建时间 mobile_data[i[0]] = { "user_phone": i[0], "phone": i[1], "create_time": i[2] } return mobile_data """ 获取创建时间 """ def getAddrByData(self, mobile, phones): mobile_data = {} if len(phones) == 0: return mobile_data oUserPhones = db.session.query( ReverseAddressList.user_phone, ReverseAddressList.phone, ReverseAddressList.create_time).filter( ReverseAddressList.user_phone == mobile).filter( ReverseAddressList.phone.in_(phones)).limit(10000).all() if oUserPhones: for i in oUserPhones: if i[0] not in mobile_data: # user_phone 手机号 phone通讯录中的手机号 create_time创建时间 mobile_data[i[0]] = { "user_phone": i[0], "phone": i[1], "create_time": i[2] } return mobile_data def getCreateTimeByPhone(self, phone, user_phone): if len(phone) == 0 or len(user_phone) == 0: return [] oUserPhones = db.session.query( ReverseAddressList.user_phone, ReverseAddressList.phone, ReverseAddressList.create_time).filter( ReverseAddressList.phone == phone).filter( ReverseAddressList.user_phone != user_phone).order_by( ReverseAddressList.create_time).limit(1) oUserPhones1 = db.session.query( ReverseAddressList.user_phone, ReverseAddressList.create_time).filter( ReverseAddressList.phone == phone).filter( ReverseAddressList.user_phone == user_phone).order_by( ReverseAddressList.create_time).limit(1) if oUserPhones: if oUserPhones1: list = [] for i in oUserPhones: for j in oUserPhones1: a = [ j.user_phone, i.user_phone, j.create_time, i.create_time ] list.append(a) return list else: return []
class LoanUserLoanExtend(db.Model, BaseModel): __bind_key__ = 'spark' __tablename__ = 'loan_user_loan_extend' id = db.Column(db.BigInteger, primary_key=True) loan_id = db.Column(db.BigInteger, nullable=False, server_default=db.FetchedValue()) user_id = db.Column(db.BigInteger, nullable=False, server_default=db.FetchedValue()) device_tokens = db.Column(db.String(64)) device_type = db.Column(db.String(20)) is_outmoney = db.Column(db.BigInteger, nullable=False, server_default=db.FetchedValue()) user_ip = db.Column(db.String(16)) loan_total = db.Column(db.BigInteger, nullable=False, server_default=db.FetchedValue()) loan_success = db.Column(db.BigInteger, nullable=False, server_default=db.FetchedValue()) modify_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) version = db.Column(db.BigInteger, nullable=False, server_default=db.FetchedValue()) def getSuccessNum(self, user_id): if user_id is None: return 0 where = and_(LoanUserLoanExtend.user_id == user_id, ) res = db.session.query(LoanUserLoanExtend).filter(where).order_by( desc(LoanUserLoanExtend.modify_time)).first() return res
class AfAddressTag(db.Model, BaseModel): # 指定数据库 __bind_key__ = 'xhh_antifraud' __tablename__ = 'af_address_tag' id = db.Column(db.BigInteger, primary_key=True) aid = db.Column(db.Integer, nullable=False, server_default="0") user_id = db.Column(db.BigInteger, nullable=False, server_default="0") loan_id = db.Column(db.BigInteger, nullable=False, server_default="0") ads_num = db.Column(db.Integer, nullable=False, server_default="0") ads_num_uniq = db.Column(db.Integer, nullable=False, server_default="0") advertis = db.Column(db.String(255), server_default="") express = db.Column(db.String(255), server_default="") harass = db.Column(db.String(255), server_default="") house_agent = db.Column(db.String(255), server_default="") cheat = db.Column(db.String(255), server_default="") company_tel = db.Column(db.String(255), server_default="") invite = db.Column(db.String(255), server_default="") taxi = db.Column(db.String(255), server_default="") education = db.Column(db.String(255), server_default="") insurance = db.Column(db.String(255), server_default="") ring = db.Column(db.String(255), server_default="") service_tel = db.Column(db.String(255), server_default="") delinquency = db.Column(db.String(255), server_default="") modify_time = db.Column(db.DateTime) create_time = db.Column(db.DateTime) ''' 保存数据 ''' def saveData(self, tag_list, proportion): try: self.aid = tag_list.aid self.user_id = proportion['user_id'] self.loan_id = proportion['loan_id'] self.ads_num = proportion['mail_list_num'] self.ads_num_uniq = proportion['weight_loss_num'] self.advertis = proportion['label_num']['advertisement_tel'] self.express = proportion['label_num']['express_tel'] self.harass = proportion['label_num']['harass_tel'] self.house_agent = proportion['label_num']['house_propert_tel'] self.cheat = proportion['label_num']['cheat_tel'] self.company_tel = proportion['label_num']['enterprise_tel'] self.invite = proportion['label_num']['recruit_tel'] self.taxi = proportion['label_num']['lease_car_tel'] self.education = proportion['label_num']['education_tel'] self.insurance = proportion['label_num']['insurance_tel'] self.ring = proportion['label_num']['sound_a_sound_tel'] self.service_tel = proportion['label_num']['customer_service_tel'] self.delinquency = proportion['label_num']['illegality_tel'] self.modify_time = datetime.now() self.create_time = datetime.now() self.add() db.session.commit() return True except AttributeError as error: print(error) return False except sqlalchemy.exc.IntegrityError as error: print(error) return False ''' 保存数据 ''' def saveResources(self, proportion): try: self.aid = proportion['aid'] self.user_id = proportion['user_id'] self.loan_id = proportion['loan_id'] self.ads_num = proportion['mail_list_num'] self.ads_num_uniq = proportion['weight_loss_num'] self.advertis = proportion['label_num']['advertisement_tel'] self.express = proportion['label_num']['express_tel'] self.harass = proportion['label_num']['harass_tel'] self.house_agent = proportion['label_num']['house_propert_tel'] self.cheat = proportion['label_num']['cheat_tel'] self.company_tel = proportion['label_num']['enterprise_tel'] self.invite = proportion['label_num']['recruit_tel'] self.taxi = proportion['label_num']['lease_car_tel'] self.education = proportion['label_num']['education_tel'] self.insurance = proportion['label_num']['insurance_tel'] self.ring = proportion['label_num']['sound_a_sound_tel'] self.service_tel = proportion['label_num']['customer_service_tel'] self.delinquency = proportion['label_num']['illegality_tel'] self.modify_time = datetime.now() self.create_time = datetime.now() self.add() db.session.commit() return True except AttributeError as error: logger.info("number_label: 通讯录记录失败:%s" % error) print(error) return False except Exception as error: logger.info("number_label: 通讯录记录失败:%s" % error) print(error) return False