# -*- coding: UTF-8 -*-
"""
@author:Zongxi.Li
@file:statement.py
@time:2021/02/02
"""
import json
from datetime import datetime, date
from decimal import Decimal

from flask_restful import Resource, reqparse
from app.service.statement_service import Statement
from app.utils.auth import login_require
from flask import g


class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, (datetime, date)):
            return obj.__str__()
        if isinstance(obj, Decimal):
            return obj.__float__()
        return json.JSONEncoder.default(self, obj)


class StatementController(Resource):
    def __init__(self):
        """."""
        self.parser = reqparse.RequestParser()

    @login_require
    def get(self):
        """"""
        # self.parser.add_argument('user_id', type=str, required=True, help='用户id不能为空')
        self.parser.add_argument('statement_time', required=False, help='对账单时间戳')
        args = self.parser.parse_args()
        print(args.get('statement_time'))
        # user_id = args.get('user_id')
        user_id = g.ifa_id
        print(user_id)
        if args.get('statement_time') != '':
            year = datetime.fromtimestamp(int(args.get('statement_time')) / 1000).year
            month = datetime.fromtimestamp(int(args.get('statement_time')) / 1000).month
        else:
            year = datetime.now().year
            month = datetime.now().month - 1
        data = Statement(user_id=user_id, year=year, month=month).summary()
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return json.loads(json.dumps(resp, cls=DateEncoder, ensure_ascii=False))
