import json
import pymysql

base_props = {
    "internal_id": '内部编号',
    "base_name": '基地名称',
    "name_abbr": '名称缩写',
    "region": '国家/地区',
    "category": '分类',
    "status": '状态',
    "other_names": '其他名称',
    "dept": '部门',
    "dept_trans": '部门(译)',
    "military_type": '军种/部门',
    "manage_code": '管理代码',
    "alloc_region": '部署国家/地区',
    "port_address": '邮政地址',
    "remarks": '备注',
}
extended_props = {
    'leader_info': '领导信息',
    'website_info': '网页资料',
    'faa_info': 'FAA信息',
    'facility_info': '设施信息',
    'unit_info': '入住单位'
}

if __name__ == '__main__':
    result_file = open(r"E:\yuxin\nuofang-data\base\result0513.json", "r", encoding='utf-8')
    result_json_array = json.loads(result_file.read())
    result_file.close()
    db = pymysql.connect(host='39.101.194.63', port=23306,
                         user='root', passwd='passok123A', db='nfm', charset='utf8mb4')
    cursor = db.cursor()

    for json_item in result_json_array:
        t_item_content = {}
        if 'leader_info' in json_item:
            t_item_content['leader_info'] = json.dumps(json_item['leader_info'], ensure_ascii=False)
        if 'website_info' in json_item:
            t_item_content['website_info'] = json.dumps(json_item['website_info'], ensure_ascii=False)
        if 'faa_info' in json_item:
            t_item_content['faa_info'] = json.dumps(json_item['faa_info'], ensure_ascii=False)
        if 'facility_info' in json_item:
            t_item_content['facility_info'] = json.dumps(json_item['facility_info'], ensure_ascii=False)
        if 'unit_info' in json_item:
            t_item_content['unit_info'] = json.dumps(json_item['unit_info'], ensure_ascii=False)
        base_info = json_item['base_info']
        for _ in base_props:
            val = base_props[_]
            if val in base_info:
                t_item_content[_] = base_info[val]
        t_item_content['page_name'] = json_item['file_name'].replace(".json", "")
        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 nfm.m_base_page (%s) VALUES (%s)' % (cols, vals)
        cursor.execute(sql_insert)
    db.commit()
    db.close()
