"""
@autor:Yue
@date::13:48
@file:genPostmanJson.py
@project:autoInterface

"""
import re,json,jsonpath
from blueprints.tools.dealCasefile import readCaseFile,writeCaseFile
from blueprints.tools.dealScene import dealScene
from blueprints.tools.deals import  getenvconfig
from blueprints.tools.deals import  dealcasenumer
from blueprints.settings import baseConfig

postman_form={}
info={'name':'考拉主流程',"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"}
postman_form['info']=info


def form_item(case_number_name):
    '''

    :param case_number_name: 组成和keydate文件中的一致：[{}]
    :return:
    '''
    ##最外层item，eg：注册-授信，绑卡
    formItems=[]
    config=getenvconfig()
    casefile = dealcasenumer(case_number_name)

    scence=dealScene(config,casefile)
    #print(scence)
    for i in scence:
        for k,v in i.items():
            ##内层item eg：注册-授信
            fromItems_items=[]
            #postman-name
            formItems.append({"name":k})
            for rejson in v:
                r={}
                itemjson=readCaseFile(rejson)
                method=re.findall(r'(\(.*\))',itemjson.get('commit'))[0]
                name=itemjson.get('commit').replace(method,'')
                url=eval('baseConfig.{0}'.format(itemjson.get('type')))+itemjson.get('url')
                temp_body=itemjson.get('body')
                temp_param=itemjson.get('param')
                temp_header=itemjson.get('header') or itemjson.get('Header') or {"Content-Type":"application/x-www-form-urlencoded"}

                header=[]
                #print(temp_body,type(temp_body))

                for k,v in temp_header.items():
                    header.append({'key':k,'value': v})

                    if v.lower()=='application/x-www-form-urlencoded':
                        body = []
                        temp_urlencoded=[]
                        if temp_body and isinstance(body,dict):
                            for k,v in body.items():
                                temp_urlencoded.append({"key":k,"value":{{k}} if v=='%s' else v,"type": "text"})
                        elif temp_body and isinstance(body,dict):
                            pass


                        body.append({"mode":"urlencoded","urlencoded":temp_urlencoded})
                    elif v.lower()=='application/json':
                        body={}
                        body.update(**{"mode": "raw","options": {"raw": {}},"raw":json.dumps(temp_body)})


                # if temp_param:
                #     for k, v in temp_header.items():
                #         header.append({'key': k, 'value': v})
                #         param = []
                #         if v.lower() == 'application/x-www-form-urlencoded':
                #             temp_urlencoded = []
                #             if isinstance(param, dict):
                #                 for k, v in param.items():
                #                     temp_urlencoded.append(
                #                         {"key": k, "value": {{k}} if v == '%s' else v, "type": "text"})
                #
                #             body.append({"mode": "urlencoded", "urlencoded": temp_urlencoded})







                r.update(**{"name":name})
                r.update(**{"method": method.strip('(').strip(')')})
                r.update(**{'url':url})
                r.update(**{'header':header})
                r.update(**{'body': body})

                print(r)


if __name__ == '__main__':
    case_number_voice ="[{'voice':['758', '757', '756']}]"
    #form_item(case_number_voice)
    a={
    "msg": "success",
    "code": "0000",
    "businessCode": "0000",
    "data": {
        "totalLoanAmount": 10000,
        "totalReceiveAmount": 10000,
        "totalRepaymentAmount": 12055.32,
        "totalInterest": 4110.64,
        "totalServiceFee": 0,
        "totalOtherFee": 0,
        "alreadyPaidAmount": 0,
        "notPaidAmount": 12055.32,
        "lendTime": 1586349938,
        "applyTime": 1586349417,
        "sumPaidTerm": 13,
        "hasAllPaid": False,
        "repaymentPlans": [
            {
                "periodNo": 1,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1586349938,
                "dueTime": 1588867200,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 2,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1588867200,
                "dueTime": 1591545600,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 3,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1591545600,
                "dueTime": 1594137600,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 4,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1594137600,
                "dueTime": 1596816000,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 5,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1596816000,
                "dueTime": 1599494400,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 6,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1599494400,
                "dueTime": 1602086400,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 7,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1602086400,
                "dueTime": 1604764800,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 8,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1604764800,
                "dueTime": 1607356800,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 9,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1607356800,
                "dueTime": 1610035200,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 10,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1610035200,
                "dueTime": 1612713600,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 11,
                "repaymentStatus": 0,
                "principal": 833.33,
                "interest": 171.28,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1612713600,
                "dueTime": 1615132800,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.33,
                "actInterest": 171.28,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 12,
                "repaymentStatus": 0,
                "principal": 833.37,
                "interest": 171.24,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1615132800,
                "dueTime": 1617811200,
                "requiredRepayment": 1004.61,
                "actPrincipal": 833.37,
                "actInterest": 171.24,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 0
            },
            {
                "periodNo": 12,
                "repaymentStatus": 0,
                "principal": 10000,
                "interest": 2055.32,
                "overdueInterest": 0,
                "serviceFee": 0,
                "otherFee": 0,
                "repayableTime": 1615132800,
                "dueTime": 1588867200,
                "requiredRepayment": 12055.32,
                "actPrincipal": 10000,
                "actInterest": 2055.32,
                "actServiceFee": 0,
                "actOverdueInterest": 0,
                "actOtherFee": 0,
                "reliefAmount": 0,
                "repayAmount": 0,
                "overdueDays": 0,
                "repayTime": 946656000
            }
        ]
    },
    "success": True
}
    t=jsonpath.jsonpath(a,'$..requiredRepayment')[:-1]
    print(sum(t))
















