def coverToTree(self, root, arrs, data): data["children"] = [] for ent in arrs: if ent["entsource"] == root: if Enterprise.objects(lcid=ent["enttarget"]): child = Enterprise.objects(lcid=ent["enttarget"]).only("lcid","entname","esdate").first().to_json() data["children"].append(json.loads(child)) for child in data["children"]: self.coverToTree(child['lcid'], arrs, child) return data
def getRelEnts(self, lcid): # 获得企业的相关企业信息 obj = {} obj["children"] = [] obj["parent"] = json.loads(Enterprise.objects(lcid=lcid).only("lcid", "entname", "regcap", "entindustry").first().to_json()) childrens = json.loads(Entsrelation.objects(entsource=lcid).only("enttarget", "entname", "entsource").to_json()) obj["investment"] = json.loads(Entprerelation.objects(enttarget=lcid).only("entpre", "conprop").to_json()) for relent in childrens: if Enterprise.objects(lcid=relent["enttarget"]).first() is not None: obj["children"].append(relent) self.rawdata.append(obj) # 自动递归 if len(obj["children"]): for relent in obj["children"]: self.getRelEnts(relent["enttarget"])
def get(self): name = request.args.get('name') # 从本地数据库中得到数据 if name: enterprises = Enterprise.objects(__raw__={'entname': {"$regex": name.encode('utf-8')}}).only('entname', "lcid", "regcap", "esdate", "address", "regcapcur") else: enterprises = Enterprise.objects[:50] if len(enterprises): return Status(200, 'success', json.loads(enterprises.to_json())).result else: return Status(404, 'fail', {'title': 'Not Found'}).result
def get(self, lcid): # 得到当前公司的关联企业 arrs = [] for ent in self.getRelEnts(lcid): for e in ent: arrs.append(json.loads(e.to_json())) data = {} # cover to json object data = json.loads(Enterprise.objects(lcid=lcid).only("lcid","entname","esdate").first().to_json()) result = self.coverToTree(lcid, arrs, data) return Status(200, 'success', result).result
def get(self, lcid): enterprise = Enterprise.objects(lcid=lcid).first() entprerelations = Entprerelation.objects(enttarget=lcid) entsrelations = Entsrelation.objects(entsource=lcid) entpatents = Entpatent.objects(lcid=lcid) obj = {} obj["enterprise"] = self.load_json(enterprise) obj["entprerelations"] = self.load_json(entprerelations) obj["entsrelations"] = self.load_json(entsrelations) obj["entpatents"] = self.load_json(entpatents) # relEnts = Entsrelation.objects(entsource=lcid).to_json() # print len(relEnts) if enterprise: return Status(200, 'success', obj).result else: return Status(404, 'fail', {'title':'Not Found'}).result