# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : customer.py
# @Time       : 2021/1/11 下午5:50
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
from flask import request
from flask_restful import Resource, reqparse

from app.api.engine import redis
from app.service.customer import get_customer_list


class CustomerList(Resource):
    """"""
    def __init__(self):
        """."""
        token = request.headers.get('Authorization', '')
        token = 's:sid:' + token.split(' ')[1]
        ifa_id = redis.get(token)
        if not ifa_id:
            return {"code":'9005', "message": "请登录", "attributes": []}
        self.ifa_id = ifa_id.decode().replace('\'', '')
        self.parser = reqparse.RequestParser()

    def get(self):
        """获取客户列表分页数据."""
        self.parser.add_argument('name', type=str, required=False, help='客户名')
        self.parser.add_argument('pageNumber', type=int, required=True)
        self.parser.add_argument('pageSize', type=int, required=True)
        args = self.parser.parse_args()
        args['ifa_id'] = self.ifa_id
        data = get_customer_list(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp