from fw import rds

class VcScore:
    def __init__(self,record):
        self.id=record['id']
        self.user_id=record['user_id']
        self.channel_id=record['channel_id']
        self.target_date=record['target_date']
        self.sec=record['sec']
        self.non_active_sec=record['non_active_sec']
        self.active_sec=record['active_sec']
        self.active_mute_sec=record['active_mute_sec']
        self.status=record['status']
        return

class VcScores:
    def __init__(self):
        return

    def get(self,user_id,channel_id):
        sql="SELECT * FROM vc_scores WHERE user_id=%s and channel_id=%s and status=0;"
        params=(str(user_id),str(channel_id))
        records=rds.executeSQL(sql,params)
        
        if len(records)==0:
            return None
        
        dataset=[]
        for record in records:
            data=VcScore(record)
            dataset.append(data)
        
        return dataset

    def first(self,user_id,channel_id,target_date):
        sql="SELECT * FROM vc_scores WHERE user_id=%s and channel_id=%s and target_date=date_format(%s,%s) and status=0;"
        params=(str(user_id),str(channel_id),str(target_date),'%Y-%m-%d')
        records=rds.executeSQL(sql,params)
        
        if len(records)==0:
            return None
        
        record=records[0]
        data=VcScore(record)
        
        return data

    def new(self,user_id,channel_id,target_date):
        sql="INSERT INTO vc_scores (user_id,channel_id,target_date) VALUES(%s,%s,%s);"
        params=(str(user_id),str(channel_id),str(target_date))
        result=rds.executeSQL(sql,params,True)
        
        return result

    def set_sec(self,id,sec):
        sql="UPDATE vc_scores SET sec =%s where id=%s;"
        params=(str(sec),str(id))
        result=rds.executeSQL(sql,params,True)
        
        return result

    def set_non_active_sec(self,id,non_active_sec):
        sql="UPDATE vc_scores SET non_active_sec =%s where id=%s;"
        params=(str(non_active_sec),str(id))
        result=rds.executeSQL(sql,params,True)
        
        return result

    def set_active_sec(self,id,active_sec):
        sql="UPDATE vc_scores SET active_sec =%s where id=%s;"
        params=(str(active_sec),str(id))
        result=rds.executeSQL(sql,params,True)
        
        return result

    def set_active_mute_sec(self,id,active_mute_sec):
        sql="UPDATE vc_scores SET active_mute_sec =%s where id=%s;"
        params=(str(active_mute_sec),str(id))
        result=rds.executeSQL(sql,params,True)
        
        return result
