"""
年报上传到es
content 需要重新解析
lang语言
origin 来源 从数据库中获取
title 从数据库中获取
dic_info = {
                'attachmentIds': att_id,
                'author': '',
                'content': content,
                'contentWithTag': '',
                'createDate': time_now,
                'deleteFlag': '0',
                'id': '',
                'keyWords': '',
                'lang': lang,
                'origin': origin,
                'publishDate': datetime_string,
                'sid': '1684032033495392257',
                'sourceAddress': year_url,  # 原文链接
                'summary': '',
                'title': name_pdf.replace('.pdf', ''),
                'type': 1,
                'socialCreditCode': social_code,
                'year': year
}
"""
# 1.无年份和信用代码 另外存一个redis的key中
# 2.有信用代码 id 年份的 （1）es 中的id 需要更新为附件表中的id
# （2）通过信用代码 查出名称相同的个数 如果有两个的话，说明其中有一个没有在es库中 需要把es库中的id获取到，并删除没有在es库中的那个记录
#如果有一条记录，就需要把该记录上传


#todo:查出有一条记录的，先更新 其他的先保存到另一个redis中
import threading

import redis
import requests, re, time, pymysql,  fitz
import urllib3
from base import BaseCore

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

baseCore = BaseCore.BaseCore()
log = baseCore.getLogger()
pool = redis.ConnectionPool(host="114.115.236.206", port=6379, password='clbzzsn', db=6)
cnx = pymysql.connect(host='114.116.44.11', user='caiji', password='f7s0&7qqtK', db='clb_project', charset='utf8mb4')
cursor_ = cnx.cursor()
lock = threading.Lock()

taskType = '企业年报'
pathType = 'QYYearReport/'


def secrchATT(type_id, xydm):

    sel_sql = '''select * from clb_sys_attachment where item_id=%s '''
    lock.acquire()
    cursor_.execute(sel_sql, (type_id, xydm))
    selects = cursor_.fetchall()
    lock.release()
    return selects

def main():
    redis_conn = redis.Redis(connection_pool=pool)
    info_ = redis_conn.lpop("NianBao:id")
    if info_:
        pass
    else:
        log.info("++++已没有数据++++")

        return
    info = info_.decode()
    xydm = info.split('|')[0]
    att_id = info.split('|')[1]
    year = info.split('|')[2]
    if not xydm or not year:
        redis_conn.lpush('NianBao:info', info)
    else:
        selects = secrchATT(1, xydm)
        if len(selects) > 1:
            redis_conn.lpush('NianBao:info', info)
        elif len(selects) == 1:
            file_name = selects[1]
            origin = selects[18]
            create_time = selects[13]
            publishDate = selects[21]

            file_href = 'http://zzsn.luyuen.com' + str(selects[5])




def run_threads(num_threads):
    threads = []

    for i in range(num_threads):

        thread = threading.Thread(target=main, args=())

        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

if __name__ == "__main__":
    start = time.time()
    num_threads = 5
    run_threads(num_threads)

    log.info(f'5线程  总耗时{time.time() - start}秒')