import pymysql
import json
from flask import Flask, render_template, request
from flask_cors import CORS


def talk(recommendId):
    connection = pymysql.connect(host='47.95.76.74', port=3306, user='root', passwd='Gongsibao2018', db='ent_data')
    cur = connection.cursor()
    try:
        list_test=[]
        update_sql='UPDATE talking t1 ,tj_recommend_do_status t2 set t1.recommendId=t2.recommend_id where t1.recommend_product=t2.product_name'
        cur.execute(update_sql)
        connection.commit()
        select_sql='select start_text,res_text,mq_text,end_text from talking where recommendId="%s"'%(recommendId)
        cur.execute(select_sql)
        s=cur.fetchone()
        for i in range(0,len(s)):
            list_test.append(s[i])
        print(list_test)
        cur.close()
        connection.close()
        return True,list_test
    except:
        cur.close()
        connection.close()
        return False,{}

def talkName(recommend_product):
    connection = pymysql.connect(host='47.95.76.74', port=3306, user='root', passwd='Gongsibao2018', db='ent_data')
    cur = connection.cursor()
    try:
        list_test=[]
        select_sql='select start_text,res_text,mq_text,end_text from talking where recommend_product="%s"'%(recommend_product)
        cur.execute(select_sql)
        s=cur.fetchone()
        for i in range(0,len(s)):
            list_test.append(s[i])
        print(list_test)
        cur.close()
        connection.close()
        return True,list_test
    except:
        cur.close()
        connection.close()
        return False,{}

# 服务器接口API
app = Flask(__name__)

@app.route('/api/talking', methods = ['POST'])

def SingleLogoApi1():
    try:
        recommendId = request.get_json().get("recommendId")
        print(recommendId)
        isSuss, talkings = talk(recommendId)
        if isSuss:
            return json.dumps({"status": 0, "msg": "成功", "data": talkings}, ensure_ascii=False)
        else:
            return json.dumps({"status": -1, "msg": "返回数据为空", "data": {}}, ensure_ascii=False)
    except:
        return json.dumps({"status": -1, "msg":"调用服务出错","data": {}}, ensure_ascii=False)


# 服务器接口API
# app = Flask(__name__)
def talking(server):
    @server.route('/api/talking', methods = ['POST'])
    def SingleLogoApi1():
        try:
            recommendId = request.get_json().get("recommendId")
            print(recommendId)
            isSuss, talkings = talk(recommendId)
            if isSuss:
                return json.dumps({"status": 0, "msg": "成功", "data": talkings}, ensure_ascii=False)
            else:
                return json.dumps({"status": -1, "msg": "返回数据为空", "data": {}}, ensure_ascii=False)
        except:
            return json.dumps({"status": -1, "msg": "调用服务出错", "data": {}}, ensure_ascii=False)


def talking_product(server):
    @server.route('/api/talkingName', methods=['POST'])
    def SingleLogoApi():
        try:
            recommend_product = request.get_json().get("recommend_product")
            isSuss, talkings = talkName(recommend_product)
            if isSuss:
                return json.dumps({"status": 0, "msg": "成功", "data": talkings}, ensure_ascii=False)
            else:
                return json.dumps({"status": -1, "msg": "返回数据为空", "data": {}}, ensure_ascii=False)
        except:
            return json.dumps({"status": -1, "msg": "调用服务出错", "data": {}}, ensure_ascii=False)
# if __name__ == '__main__':
#     CORS(app, supports_credentials=True)#允许跨域
#     # app.run(host='172.16.18.2', port=15501, debug=True)
#     app.run(host='127.0.0.1', port=5000, debug=True)

# talk("商标注册")
