from app.view_models.book import BookViewModel


class TradeInfo:
    def __init__(self,goods):
        self.total = 0
        self.trades = []
        self._parse(goods)

    def _parse(self,goods):
        self.total = len(goods)
        self.trades = [self._map_to_trade(single) for single in goods]

    def _map_to_trade(self,single):
        if single.create_time:
            time = single.create_datetime.strftime('%Y-%m-%d')
        else:
            time = '未知'
        return dict(
            user_name=single.user.nickname,
            time=time,
            id=single.id
        )

class MyTrades:
    def __init__(self,trades_of_mine,trade_count_list):
        self.trades = []
        self.__trades_of_mind = trades_of_mine
        self.__trades_count_list = trade_count_list

        self.trades = self.__parse()

    def __parse(self):
        temp_trades = []
        for trade in self.__trades_of_mind:
            my_trade = self.__matching(trade)
            temp_trades.append(my_trade)
        return temp_trades

    def __matching(self,trade):
        count = 0
        for trade_count in self.__trades_count_list:
            if trade_count['isbn'] == trade.isbn:
                count = trade_count['count']
        r = {
            'id':trade.id,
            'wishes_count': count,
            'book': BookViewModel(trade.book)
        }
        return r