from collectSports.items import *
import uuid


class Storage(object):
    def __init__(self):
        pass

    def zaopan(self, re):
        data = CollectsportsItem()
        data['csource'] = 'zaopan'
        data['zq_league'] = []
        data['zq_match'] = []

        for index in re['data']:
            lg = ZqLeagueItem()
            content = []
            content.append({'league_id': index['league_id'], 'source': 'hg0088'})
            i_uuid = uuid.uuid4()
            lg['uuid'] = i_uuid
            lg['league_id'] = index['league_id']
            lg['league_name'] = index['league_name']
            lg['content'] = content
            mc = ZqMatch()
            mc['match_id'] = index['match_id']
            mc['half_match_id'] = index['half_match_id']
            mc['host_id'] = index['host_id']
            mc['guest_id'] = index['guest_id']
            mc['host_name'] = index['host_name']
            mc['guest_name'] = index['guest_name']
            mc['is_half'] = 0
            mc['league_id'] = index['league_id']
            mc['is_roll'] = index['is_roll']  # 是否滚球
            mc['mdate'] = index['mdate']
            mc['mtime'] = index['mtime']
            mc['is_corner'] = index['is_corner']
            mc['uuid'] = i_uuid
            mc_half = ZqMatch()
            if index['half_match_id']:
                mc_half['match_id'] = index['half_match_id']
            # else:
            #     mc_half['match_id'] = index['match_id']
                mc_half['half_match_id'] = 0
                mc_half['host_id'] = index['host_id']
                mc_half['guest_id'] = index['guest_id']
                mc_half['host_name'] = index['host_name']
                mc_half['guest_name'] = index['guest_name']
                mc_half['is_half'] = 1
                mc_half['league_id'] = index['league_id']
                mc_half['is_roll'] = index['is_roll']  # 是否滚球
                mc_half['mdate'] = index['mdate']
                mc_half['mtime'] = index['mtime']
                mc_half['is_corner'] = index['is_corner']
                mc_half['uuid'] = i_uuid
                data['zq_match'].append(mc_half)
            data['zq_league'].append(lg)
            data['zq_match'].append(mc)
        return data

    def zhibo(self, re):
        data = CollectsportsItem()
        data['csource'] = 'zhibo'
        data['zq_zhibo'] = []
        for index in re:
            lg = ZqZhibo()
            lg['game_type'] = index['game_type']
            lg['start_time'] = index['start_time']
            lg['host_team'] = index['host_team']
            lg['guest_team'] = index['guest_team']
            lg['doing'] = index['doing']
            lg['showid'] = index['showid']
            lg['shower'] = index['shower']
            lg['league_name'] = index['league_name']
            data['zq_zhibo'].append(lg)

        return data

    def chain(self, re):
        data = CollectsportsItem()
        data['csource'] = 'chain'
        data['zq_league'] = data['zq_chain'] = []
        for index in re:

            lg = ZqLeagueItem()
            lg['start_time'] = index['start_time']
            lg['league_id'] = index['league_id']
            lg['league_name'] = index['league_name']
            lg['league_subname'] = index['league_subname']
            lg['team_num'] = index['dcount']
            lg['game_type'] = index['game_type']

            for item in index['items']:
                mc = ZqChain()
                mc['league_id'] = index['league_id']
                mc['status'] = item['status']
                mc['code'] = item['code']
                mc['name'] = item['name']
                mc['enabled'] = item['enabled']
                mc['odds'] = item['odds']
                data['zq_chain'].append(mc)
            data['zq_league'].append(lg)

        return data

    def today(self, re):
        data = CollectsportsItem()
        data['csource'] = 'today'
        data['zq_today'] = []

        for index in re:
            lg = ZqToday()
            lg['match_id'] = index['match_id']
            lg['is_roll'] = index['is_roll']
            lg['is_corner'] = index['is_corner']
            data['zq_today'].append(lg)
        return data
