|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | +""" |
| 4 | +Topic: mysql数据表结构和数据合并 |
| 5 | +Desc : 有两个表,航信CRM企业表t_crm_company,税局企业资料表t_tax_company, |
| 6 | +现在需要将其记录合并,使用税号和名称组合来作为唯一性标识 |
| 7 | +""" |
| 8 | +import sys |
| 9 | +import copy |
| 10 | + |
| 11 | +import logging |
| 12 | +import datetime |
| 13 | +import mysql.connector |
| 14 | +from mysql.connector import errorcode |
| 15 | + |
| 16 | +# 查询航信CRM表 |
| 17 | +sql_select_taxcode_name = """ |
| 18 | +SELECT DISTINCT cust_tax_code,cust_name |
| 19 | +FROM t_crm_company |
| 20 | +WHERE update_time IS NULL OR update_time < '{}'; |
| 21 | +""" |
| 22 | +# 通过税号和名称查询税局表中记录 |
| 23 | +sql_select_tax_info = """ |
| 24 | +SELECT |
| 25 | + legal_person, |
| 26 | + business_scope, |
| 27 | + reg_code, |
| 28 | + tax_number, |
| 29 | + cust_tax_name, |
| 30 | + addr, |
| 31 | + reg_type, |
| 32 | + tax_institution, |
| 33 | + duty, |
| 34 | + is_back_taxes, |
| 35 | + is_overdue, |
| 36 | + status, |
| 37 | + valid_st_date, |
| 38 | + qualification_nm, |
| 39 | + business, |
| 40 | + notes |
| 41 | +FROM t_tax_company |
| 42 | +WHERE cust_tax_code='{}' AND cust_name='{}' AND (notes is NULL OR notes<>'税号无效') |
| 43 | +LIMIT 1; |
| 44 | +""" |
| 45 | +# 将税局的数据更新到航信表中 |
| 46 | +sql_update_crm = """ |
| 47 | +UPDATE t_crm_company |
| 48 | +SET |
| 49 | + legal_person={}, |
| 50 | + business_scope={}, |
| 51 | + reg_code={}, |
| 52 | + tax_number={}, |
| 53 | + cust_tax_name={}, |
| 54 | + addr={}, |
| 55 | + reg_type={}, |
| 56 | + tax_institution={}, |
| 57 | + duty={}, |
| 58 | + is_back_taxes={}, |
| 59 | + is_overdue={}, |
| 60 | + status={}, |
| 61 | + valid_st_date={}, |
| 62 | + qualification_nm={}, |
| 63 | + business={}, |
| 64 | + tax_notes={}, |
| 65 | + update_time=now() |
| 66 | +WHERE cust_tax_code='{}' AND cust_name='{}' |
| 67 | +""" |
| 68 | + |
| 69 | +logging.basicConfig(level=logging.INFO, |
| 70 | + format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', |
| 71 | + datefmt='%Y-%m-%d %H:%M:%S', |
| 72 | + handlers=[logging.FileHandler('d:/logs/merge_table.log', 'a', 'utf-8')]) |
| 73 | +_log = logging.getLogger('app.' + __name__) |
| 74 | + |
| 75 | + |
| 76 | +def _connect(): |
| 77 | + config = { |
| 78 | + 'user': 'root', |
| 79 | + 'password': 'mysql', |
| 80 | + 'host': '192.168.203.95', |
| 81 | + 'database': 'fastloan_test', |
| 82 | + 'raise_on_warnings': True, |
| 83 | + } |
| 84 | + cnx = None |
| 85 | + try: |
| 86 | + cnx = mysql.connector.connect(**config) |
| 87 | + except mysql.connector.Error as err: |
| 88 | + if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: |
| 89 | + print("Something is wrong with your user name or password") |
| 90 | + elif err.errno == errorcode.ER_BAD_DB_ERROR: |
| 91 | + print("Database does not exist") |
| 92 | + else: |
| 93 | + print(err) |
| 94 | + if cnx: |
| 95 | + cnx.close() |
| 96 | + return cnx |
| 97 | + |
| 98 | + |
| 99 | +def merge_table(): |
| 100 | + conn_ = _connect() |
| 101 | + _log.info('---------------------------分割线-----------------------------') |
| 102 | + _log.info('数据库更新start') |
| 103 | + cursor = conn_.cursor() |
| 104 | + cursor.execute(sql_select_taxcode_name.format('2015-09-10 00:00:00')) |
| 105 | + code_names = [list(r) for r in cursor.fetchall()] |
| 106 | + _log.info("待更新数据量大小为:{}".format(len(code_names))) |
| 107 | + |
| 108 | + _log.info('合并企业资料start') |
| 109 | + for i, d2 in enumerate(code_names): |
| 110 | + try: |
| 111 | + cursor.execute(sql_select_tax_info.format(d2[0], d2[1])) |
| 112 | + each_record = cursor.fetchone() |
| 113 | + if each_record: |
| 114 | + u_list = [ "'{}'".format(r.replace("'", ";")) if r else 'null' for r in each_record] |
| 115 | + u_list.extend([d2[0], d2[1]]) |
| 116 | + cursor.execute(sql_update_crm.format(*u_list)) |
| 117 | + except: |
| 118 | + _log.error('--合并企业资料Exception,taxcode={},name={}--'.format(d2[0], d2[1])) |
| 119 | + cursor.close() |
| 120 | + conn_.rollback() |
| 121 | + conn_.close() |
| 122 | + return |
| 123 | + if i % 50 == 0: |
| 124 | + _log.info("更新下标...i={}".format(i)) |
| 125 | + conn_.commit() |
| 126 | + conn_.commit() |
| 127 | + _log.info('合并企业资料end') |
| 128 | + _log.info('数据库更新end') |
| 129 | + cursor.close() |
| 130 | + conn_.close() |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + merge_table() |
| 135 | + pass |
0 commit comments