"""信用中国-严重失信名单
链接地址 :
{
    source:
    type: 严重失信主体名单
    searchState: 1
    entityType: 1
    scenes: defaultscenario
    keyword: 雷州市白金银座演艺文化实业有限公司
    tyshxydm: 91440882315032592M
    page: 1
    pageSize: 10
}
"""
import time
from urllib import parse

import requests
from bs4 import BeautifulSoup
from retry import retry

from base.BaseCore import BaseCore
baseCore = BaseCore()
log = baseCore.getLogger()
cnx = baseCore.cnx
cursor = baseCore.cursor

@retry(tries=3,delay=1)
def getRequest(url,headers):

    req = requests.get(url=url,headers=headers,verify=False,timeout=30)
    json_data = req.json()
    return json_data

# 严重失信
def dishonesty():
    param = {
        'tableName':'credit_zgf_fr_sxbzxr',
        'searchState': '1',
        'scenes': 'defaultscenario',
        'keyword': '雷州市白金银座演艺文化实业有限公司',
        'tyshxydm': '91440882315032592M',
        'page': '1',
        'pageSize': '10'
    }

    url = f'https://public.creditchina.gov.cn/private-api/catalogSearch?tableName=credit_zgf_fr_sxbzxr&searchState=1&scenes=defaultscenario&keyword={param["keyword"]}&tyshxydm={param["tyshxydm"]}&page=1&pageSize=10'
    json_data = getRequest(url, headers)
    # print(json_data)
    if json_data['status'] == 1:
        pass
    total_size = json_data['data']['totalSize']
    for page in total_size:
        param_page = {
            'tableName': 'credit_zgf_fr_sxbzxr',
            'searchState': '1',
            'scenes': 'defaultscenario',
            'keyword': '雷州市白金银座演艺文化实业有限公司',
            'tyshxydm': '91440882315032592M',
            'page': f'{page}',
            'pageSize': '10'
        }
        url_page = f'https://public.creditchina.gov.cn/private-api/catalogSearch?tableName=credit_zgf_fr_sxbzxr&searchState=1&scenes=defaultscenario&keyword={param["keyword"]}&tyshxydm={param["tyshxydm"]}&page={param_page["page"]}&pageSize=10'
        json_data = getRequest(url_page, headers)
        # print(json_data)
        if json_data['status'] == 1:
            pass
        info_list = json_data['data']['list']
        for info in info_list:
            entity = info['entity']
            iname = entity['iname']  # 失信被执行人姓名/名称
            cardnumber = entity['cardnumber']  # 组织机构代码
            court_name = entity['court_name']  # 执行法院
            area_name = entity['area_name']  # 省份
            case_code = entity['case_code']  # 执行依据文号
            reg_date = entity['reg_date']  # 立案时间
            gist_cid = entity['gist_cid']  # 案号
            gist_unit = entity['gist_unit']  # 做出执行依据单位
            duty = entity['duty']  # 生效法律文书确定的义务
            performance = entity['performance']  # 被执行人的履行情况
            disreput_type_name = entity['disreput_type_name']  # 失信被执行人行为具体情形
            publish_date = entity['publish_date']  # 发布时间
            performed_part = entity['performed_part']  # 已履行部分
            unperform_part = entity['unperform_part']  # 未履行部分
            dataSource = info['dataSource']  # 数据来源

# 行政处罚
def punish():
    param = {
        'tableName':'credit_xyzx_fr_xzcf_new',
        'searchState': '1',
        'scenes': 'defaultscenario',
        'keyword': '雷州市白金银座演艺文化实业有限公司',
        'tyshxydm': '91440882315032592M',
        'page': '1',
        'pageSize': '10'
    }

    url = f'https://public.creditchina.gov.cn/private-api/catalogSearch?tableName=credit_xyzx_fr_xzcf_new&searchState=1&scenes=defaultscenario&keyword={param["keyword"]}&tyshxydm={param["tyshxydm"]}&page=1&pageSize=10'
    json_data = getRequest(url, headers)
    # print(json_data)
    if json_data['status'] == 1:
        pass
    #总条数
    total_size = json_data['data']['totalSize']
    if total_size > 0:
        pass
    else:
        log.info()
    for page in total_size:
        param_page = {
            'tableName': 'credit_xyzx_fr_xzcf_new',
            'searchState': '1',
            'scenes': 'defaultscenario',
            'keyword': '雷州市白金银座演艺文化实业有限公司',
            'tyshxydm': '91440882315032592M',
            'page': f'{page}',
            'pageSize': '10'
        }
        url_page = f'https://public.creditchina.gov.cn/private-api/catalogSearch?tableName=credit_xyzx_fr_xzcf_new&searchState=1&scenes=defaultscenario&keyword={param_page["keyword"]}&tyshxydm={param_page["tyshxydm"]}&page={param_page["page"]}&pageSize=10'
        json_data = getRequest(url_page, headers)
        # print(json_data)
        if json_data['status'] == 1:
            pass

        info_list = json_data['data']['list']

        for entity in info_list:
            cf_wsh = entity['cf_wsh']   # 行政处罚决定书文号
            cf_cflb = entity['cf_cflb']  # 处罚类别
            cf_jdrq = entity['cf_jdrq']     # 处罚决定日期
            cf_nr = entity['cf_nr']     # 处罚内容
            cf_nr_fk = entity['cf_nr_fk']   # 罚款金额（万元）
            cf_nr_wfff = entity['cf_nr_wfff']   # 没收违法所得、没收非法财物的金额（万元）
            cf_nr_zkdx = entity['cf_nr_zkdx']   # 暂扣或吊销证照名称及编号
            cf_wfxw = entity['cf_wfxw']     # 违法行为类型
            cf_sy = entity['cf_sy']     # 违法事实
            cf_yj = entity['cf_yj']     # 处罚依据
            cf_cfjg = entity['cf_cfjg']     # 处罚机关
            cf_cfjgdm = entity['cf_cfjgdm']     # 处罚机关统一社会信用代码
            cf_sjly = entity['cf_sjly']     # 数据来源
            cf_sjlydm = entity['cf_sjlydm']     # 数据来源单位统一社会信用代码


if __name__=='__main__':

    headers = {
        'Referer': 'https://www.creditchina.gov.cn/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"'
    }
    type_list = ['严重失信主体名单','行政管理']
    com_name = ''
    social_code = ''
    dishonesty()
    punish()
    # 报告链接
    url_report = f'https://public.creditchina.gov.cn/credit-check/pdf/clickDownload?companyName={com_name}&entityType=1&uuid=&tyshxydm={social_code}'
    report_json = getRequest(url_report, headers)
    reportNumber = report_json['data']['reportNumber']
    pdf_url = f'https://public.creditchina.gov.cn/credit-check/pdf/clickDownloadOBS?reportNumber={reportNumber}'
    # respon = requests.get(url=pdf_url,headers=headers,verify=False,timeout=30)



