from DBUtils.PooledDB import PooledDB
import pymysql
import sys,os,flask
from flask import request
from flask_cors import *
import json




"""读取tj_hx_type_connect表，获取大类"""
"""返回格式：["基本标签", "产品标签", "风险标签", "经营标签", "偏好标签"]"""

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():
    conn = pool.connection()
    cursor = conn.cursor()
    sql = 'SELECT * FROM `tj_hx_type_connect`;'
    cursor.execute(sql)
    label_info_list = []
    label_info = cursor.fetchall()
    for row in label_info:
        label_info_dict = {}
        label_info_dict['label_type_1'] = row[1]
        label_info_dict['label_type_2'] = row[3]
        label_info_dict['label'] = row[2]
        label_info_list.append(label_info_dict)
    cursor.close()
    conn.close()
    return label_info_list

def label_type_1(label_info_list):
    label_type_1_list = []
    for item in label_info_list:
        label_type_1 = item['label_type_1']
        if label_type_1 not in label_type_1_list:
            label_type_1_list.append(label_type_1)
    return label_type_1_list

def label_type1(server):
    @server.route('/gsb/api/label_type1', methods=['POST'])
    def label_type1_main():
        if request.method == 'POST':
            try:
                label_info_list = select_label()
                print(label_info_list,111)
                label_type_1_list = label_type_1(label_info_list)
                print(label_type_1_list,222)
                return json.dumps({"status": 200, "msg": "成功", "lable_type": label_type_1_list}, ensure_ascii=False)
            except:
                return json.dumps({"status": 404, "msg": "失败", "data": []}, ensure_ascii=False)
