import json
import os

import pymysql

base_props = {
    'internal_code': '内部编号',
    'page_name': '标题',
    'unit_name': '单位名称',
    'region': '国家/地区',
    'name_abbr': '名称缩写',
    'nickname': '绰号',
    'other_names': '其他名称',
    'status': '状态',
    'uic': '单位识别码（UIC）',
    'base_address': '驻地',
    'type_or_dept': '军种/部门',
    'description': '说明',
    'update_time': '更新日期',
    'address': '地址',
    'phone': '电话',
    'twitter': 'Twitter',
    'facebook': 'Facebook',
    'linkedin': 'LinkedIn',
    'email': '邮箱',
    'website': '网址',
    'other_contact_info': '其他联系方式',
}
extended_props = {
    'leader_info': '领导信息',
    'website_info': '网页资料',
}

result_file_path = r"E:\yuxin\nuofang-data\structure\result0512"

if __name__ == '__main__':
    db = pymysql.connect(host='39.101.194.63', port=23306,
                         user='root', passwd='passok123A', db='nfm', charset='utf8mb4')
    cursor = db.cursor()
    l_item = {}
    for single_json_file in os.listdir(result_file_path):
        result_file = open(result_file_path + "\\" + single_json_file, "r", encoding='utf-8')
        result_json = json.loads(result_file.read())
        result_file.close()

        t_item_content = {}
        base_info = result_json['base_info']
        if 'contact_info' in result_json:
            for _ in result_json['contact_info']:
                base_info[_] = result_json['contact_info'][_]
        if 'leader_info' in result_json:
            t_item_content['leader_info'] = json.dumps(result_json['leader_info'], ensure_ascii=False)
        if 'website_info' in result_json:
            t_item_content['website_info'] = json.dumps(result_json['website_info'], ensure_ascii=False)

        for _ in base_props:
            val = base_props[_]
            if val in base_info:
                t_item_content[_] = base_info[val]
        t_item_content['page_name'] = "".join(single_json_file.replace(".json", "").split("-")[1:])
        col_list = [_ for _ in t_item_content]
        val_list = ["'" + t_item_content[_].replace("'", "’") + "'" for _ in t_item_content]
        cols = ", ".join(col_list)
        vals = ", ".join(val_list)
        sql_insert = 'INSERT INTO m_struct_page (%s) VALUES (%s)' % (cols, vals)
        cursor.execute(sql_insert)
    db.commit()
    db.close()
