import datetime

import pymongo
import requests
from fitz import fitz
import BaseCore
baseCore = BaseCore.BaseCore()
cursor_ = baseCore.cursor_
cnx_ = baseCore.cnx_
db_storage = pymongo.MongoClient('mongodb://114.115.221.202:27017', username='admin', password='ZZsn@9988').RESCenter[
    'REITsProjDynamics']

datas = db_storage.find({'exchange':'上海证券交易所'})
for data in datas:
    mongoId = data['_id']
    for a in data['file'].keys():
        if data['file'][f'{a}']:
            path_1 = data['file'][f'{a}']['declaration']
            path_2 = data['file'][f'{a}']['fdbkResp']
            path_3 = data['file'][f'{a}']['cover']
            if path_1:
                # sql = f"select id from clb_sys_attachment where full_path='{path_1}' and type_id=15"
                # cursor_.execute(sql)
                # id = cursor_.fetchone()
                # print(id)
                sql = f"update clb_sys_attachment set item_id='{mongoId}' where full_path='{path_1}' and type_id=15"
                cursor_.execute(sql)
                cnx_.commit()
            if path_2:
                # sql = f"select id from clb_sys_attachment where full_path='{path_2}'and type_id=15"
                # cursor_.execute(sql)
                # id = cursor_.fetchone()
                # print(id)
                sql = f"update clb_sys_attachment set item_id='{mongoId}' where full_path='{path_2}' and type_id=15"
                cursor_.execute(sql)
                cnx_.commit()
            if path_3:
                # sql = f"select id from clb_sys_attachment where full_path='{path_3}'and type_id=15"
                # cursor_.execute(sql)
                # id = cursor_.fetchone()
                # print(id)
                sql = f"update clb_sys_attachment set item_id='{mongoId}' where full_path='{path_3}' and type_id=15"
                cursor_.execute(sql)
                cnx_.commit()
