"""
  @author: LinTao
  @time: 2022/9/11 15:04
  @desc:
"""
import time

from flask import session
from sqlalchemy import Table

from common.database import dbconnect

dbsession, md, DBase = dbconnect()


class Credit(DBase):
    __table__ = Table('credit', md, autoload=True)


    def insert_credit(self, type, target, credit):
        """插入积分明细数据"""
        now = time.strftime('%Y-%m-%d %H:%M:%S')
        credit = Credit(userid=session.get('userid'), category=type, target=target,
                        credit=credit, createtime=now, updatetime=now)
        dbsession.add(credit)
        dbsession.commit()


    def check_payed_article(self, articleid):
        """判断用户是否已经消耗积分"""
        result = dbsession.query(Credit).filter_by(userid=session.get('userid'),
                                                   target=articleid).all()
        if len(result) > 0:
            return True
        else:
            return False




