from DBUtils.PooledDB import PooledDB
import pymysql,json
from flask import request

MYSQL_HOST = '47.95.76.74'  # 外网地址
PORT = 3306  # 外网端口
USER = 'root'
PASSWORD = 'Gongsibao2018'
DB = 'ent_data'
pool = PooledDB(pymysql, 8, host=MYSQL_HOST, user=USER, passwd=PASSWORD, db=DB, port=PORT)

"""检索类别英文名字"""
def select_label(lable_type):
    conn = pool.connection()
    cursor = conn.cursor()
    english_name = ""
    sql = 'SELECT * FROM `tj_label_yingshe` where name = "%s";' % lable_type
    cursor.execute(sql)
    label_english_info = cursor.fetchall()
    for row in label_english_info:
        english_name = row[2]
    cursor.close()
    conn.close()
    return english_name


def select_label_content(lable_type):
    conn = pool.connection()
    cursor = conn.cursor()
    sql = 'SELECT * FROM `tj_hx_type_connect` where label_type1 = "%s";' %lable_type
    print(sql)
    print(sql)
    cursor.execute(sql)
    label_list = []
    label_info = cursor.fetchall()
    for row in label_info:
        label_dict = {}
        label_dict['label_type_1'] = row[1]
        label_dict['label_type_2'] = row[2]
        label_dict['label'] = row[3]
        label_list.append(label_dict)
    cursor.close()
    conn.close()
    return label_list

def checkbox_info(label_list):
    for item in label_list:
        label = item['label']
        lable_type =  item['label_type_1']
        data_dict ={}
        checkbox_data = []
        label_list = label.replace('[','').replace(']','').replace("'","").split(',')
    try:
        data_dict['title'] = lable_type
        data_dict['english_name'] = select_label(lable_type)
        data_dict['value'] = []
        data_dict['list'] = label_list
        checkbox_data.append(data_dict)
        return json.dumps({"status": 200, "msg": "成功", "type": "checkbox", "data": checkbox_data},
                          ensure_ascii=False)
    except:
        return json.dumps({"status": 404, "msg": "失败", "type": "checkbox", "data": []}, ensure_ascii=False)

def select_info(label_list):
    data_list1 = []
    value_list = []
    for item in label_list:
        data_list2 = []
        label = item['label']
        lable_type2 = item['label_type_2']
        label_list1 = label.replace('[', '').replace(']', '').replace("'", "").split(',')
        for index,value in enumerate(label_list1):
            label_dict2 = {}
            label_dict2['id'] = index+1
            label_dict2['value'] = value
            label_dict2['lable'] = value
            if value not in value_list:
                value_list.append(value)
                data_list2.append(label_dict2)
        data_list1_dict = {}
        data_list1_dict['title'] = lable_type2
        print(lable_type2)
        data_list1_dict['english_name'] = select_label(lable_type2)
        data_list1_dict['value'] = ""
        data_list1_dict['list'] = data_list2
        data_list1.append(data_list1_dict)
        print(len(data_list1))
    try:
        return json.dumps({"status": 200, "msg": "成功", "type": "select", "data": data_list1},
                          ensure_ascii=False)
    except:
        return json.dumps({"status": 404, "msg": "失败", "type": "select", "data": []}, ensure_ascii=False)

def hx_connect(server):
    @server.route('/gsb/api/hx_connect', methods=['POST'])
    def label_main():
        if request.method == 'POST':
            requestdata = request.json
            lable_type = requestdata.get('lable_type')
            label_list = select_label_content(lable_type)
            if lable_type in ['证照标签', '产品信息', '风险标签']:
                info = checkbox_info(label_list)
            else:
                info = select_info(label_list)
            return info
