六、随机生成企业社会信用码

# -*-coding: utf-8 -*-
# @Time    : 2023/12/13 7:40 PM
# @Author  : zhonglinglong
# @File    : mock_social_credit_code.py


"""
mock生成企业社会信用码
"""

import random


class MockSocialCreditCode:
    # 统一社会信用代码中不使用I, O, Z, S, V
    SOCIAL_CREDIT_CHECK_CODE_DICT = {
        '0': 0,
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9,
        'A': 10,
        'B': 11,
        'C': 12,
        'D': 13,
        'E': 14,
        'F': 15,
        'G': 16,
        'H': 17,
        'J': 18,
        'K': 19,
        'L': 20,
        'M': 21,
        'N': 22,
        'P': 23,
        'Q': 24,
        'R': 25,
        'T': 26,
        'U': 27,
        'W': 28,
        'X': 29,
        'Y': 30
    }

    # GB11714 - 1997 全国组织机构代码编制规则中代码字符集
    ORGANIZATION_CHECK_CODE_DICT = {
        '0': 0,
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9,
        'A': 10,
        'B': 11,
        'C': 12,
        'D': 13,
        'E': 14,
        'F': 15,
        'G': 16,
        'H': 17,
        'I': 18,
        'J': 19,
        'K': 20,
        'L': 21,
        'M': 22,
        'N': 23,
        'O': 24,
        'P': 25,
        'Q': 26,
        'R': 27,
        'S': 28,
        'T': 29,
        'U': 30,
        'V': 31,
        'W': 32,
        'X': 33,
        'Y': 34,
        'Z': 35
    }

    def mock_social_credit_code(self):
        """
        随机生成统一社会信用码
        :return:
        """
        code = self.__create_code__()
        weighting_factor = [1, 3, 9, 27, 19, 26, 16,17, 20, 29, 25, 13, 8, 24, 10, 30, 28]
        ontology_code = code[0:17]
        tmp_check_code = self.__gen_check_code__(weighting_factor, ontology_code, 31, self.SOCIAL_CREDIT_CHECK_CODE_DICT)
        return code[:17] + tmp_check_code

    def __create_code__(self):
        code = str(random.randint(int('1' * 16), int('9' * 17)))
        weighting_factor = [3, 7, 9, 10, 5, 8, 4, 2]
        organization_code = code[8:17]
        ontology_code = organization_code[0:8]
        tmp_check_code = self.__gen_check_code__( weighting_factor, ontology_code, 11, self.ORGANIZATION_CHECK_CODE_DICT)
        return code + tmp_check_code

    def __gen_check_code__(self, weighting_factor, ontology_code, modulus, check_code_dict):
        total = 0
        for i in range(len(ontology_code)):
            if ontology_code[i].isdigit():
                total += int(ontology_code[i]) * weighting_factor[i]
            else:
                total += check_code_dict[ontology_code[i]] * weighting_factor[i]
        tmp_check_code = modulus - total % modulus
        tmp_check_code = 0 if tmp_check_code == 31 else tmp_check_code
        tmp_check_code = list(check_code_dict.keys())[list(check_code_dict.values()).index(tmp_check_code)]

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bug来袭~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值