import biz
import pycomm
from biz.baseBiz import baseBiz
class zqChain(baseBiz):
    def init(self):
        self.csrc=biz.getCurrentSource()
        
    
    def getOutUid(self,lgid):
        return self.csrc+str(lgid)
    def update(self,data):
        da={}
        for i in data:
            da[i]=data[i]
        uid=self.getOutUid(da['league_id'])
        hasLeague=self.getLeague(da['league_id'])
        da['out_uid']=uid
        da['src']=self.csrc
        da['out_league_id']=da['league_id']
        da.pop('league_id')
        if hasLeague==-1:
            self.zqLeague.save(da)
        else:
            self.zqLeague.update(da," out_uid='%s'" % uid)


    def getLeague(self,oleague_id):
        cond={}
        cond['where']="out_uid='%s'" %(self.getOutUid(oleague_id))
        cond['fields']="league_id"
        data=self.zqLeague.select(cond)
        if not data:
            return -1
        return data