模擬統一社會信用代碼(符合規範) python

統一社會信用代碼規則:

https://zh.wikisource.org/zh-hans/GB_32100-2015_%E6%B3%95%E4%BA%BA%E5%92%8C%E5%85%B6%E4%BB%96%E7%BB%84%E7%BB%87%E7%BB%9F%E4%B8%80%E7%A4%BE%E4%BC%9A%E4%BF%A1%E7%94%A8%E4%BB%A3%E7%A0%81%E7%BC%96%E7%A0%81%E8%A7%84%E5%88%99

組織機構代碼規則:

https://zh.wikisource.org/zh-hans/GB_11714-1997_%E5%85%A8%E5%9B%BD%E7%BB%84%E7%BB%87%E6%9C%BA%E6%9E%84%E4%BB%A3%E7%A0%81%E7%BC%96%E5%88%B6%E8%A7%84%E5%88%99

 

class Social(object):
    '''
    統一社會信用代碼 + 組織結構代碼校驗
    '''

    def __init__(self):
        '''
        Constructor
        '''
        # 統一社會信用代碼中不使用I,O,S,V,Z
        # ''.join([str(i) for i in range(10)])
        # import string
        # string.ascii_uppercase  # ascii_lowercase |  ascii_letters
        # dict([i for i in zip(list(self.string), range(len(self.string)))])
        # dict(enumerate(self.string))
        # list(d.keys())[list(d.values()).index(10)]
        # chr(97)  --> 'a'
        self.string1 = '0123456789ABCDEFGHJKLMNPQRTUWXY'
        self.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}
        # 第i位置上的加權因子
        self.social_credit_weighting_factor = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]

        # GB11714-1997全國組織機構代碼編制規則中代碼字符集
        self.string2 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        self.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}
        # 第i位置上的加權因子
        self.organization_weighting_factor = [3, 7, 9, 10, 5, 8, 4, 2]

    def gen_check_code(self, weighting_factor, ontology_code, modulus, check_code_dict):
        '''
        @param weighting_factor: 加權因子
        @param ontology_code:本體代碼
        @param modulus:  模數(求餘用)
        @param check_code_dict: 字符字典
        '''
        total = 0
        for i in range(len(ontology_code)):
            if ontology_code[i].isdigit():
                # print(ontology_code[i], weighting_factor[i])
                total += int(ontology_code[i]) * weighting_factor[i]
            else:
                num = check_code_dict.get(ontology_code[i], -1)
                if num < 0:
                    return -1
                total += num * weighting_factor[i]
        diff = modulus - total % modulus
        return diff
def unified_social_credit_code():
    """統一社會信用代碼"""
    
    department = "123456789999999999999999"  # 登記管理部門代碼
    agency = "11111111111111112121212345999"  # 機構類別
    organization_num = str(random.randint(11111111, 99999999))
    u = Social()
    # 組織機構代碼校驗位
    check_code = u.gen_check_code(u.organization_weighting_factor, organization_num, 11, u.ORGANIZATION_CHECK_CODE_DICT)
    organization_code = organization_num + str(check_code)  # 組織機構代碼
    address_code = get_address(True)
    # 沒有校驗碼的社會統一代碼
    un_code = random.choice(department) + random.choice(agency) + str(address_code) + organization_code
    # 社會 校驗位
    social_num = u.gen_check_code(u.social_credit_weighting_factor, un_code, 31, u.SOCIAL_CREDIT_CHECK_CODE_DICT)
    if social_num == 31:
        social_num = 0
    social_dict = {value: key for key, value in u.SOCIAL_CREDIT_CHECK_CODE_DICT.items()}
    # 兩位證書 轉換成 協議裏面的值
    social_key = social_dict[social_num]
    code = un_code + social_key
    return code

這樣 就生成了 符合 規範的 統一社會信用代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章