#!/usr/bin/env python
# -*- coding: utf-8 -*-
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=[]
        select_name_sql='SELECT product_name FROM `tj_recommend_do_status` where recommend_id ="%s"'%(recommendId)
        #update_sql='UPDATE tj_talking t1 ,tj_recommend_do_status t2 set t1.recommend_id=t2.recommend_id where t1.recommend_product=t2.product_name'
        cur.execute(select_name_sql)
        name=cur.fetchone()
        print(name)
        #connection.commit()
        select_sql='select start_text,res_text,mq_text,end_text from tj_talking where recommend_product="%s"'%(name)
        cur.execute(select_sql)
        s=cur.fetchone()
        for i in range(0,len(s)):
            list_test.append(s[i])
        print(list_test)
        # if s:
        #     for i in range(0,len(s)):
        #         list_test.append(s[i])
        #     print(list_test)
        # else:
        #     list_test.append('暂无话术，感谢您')
        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 tj_talking where commerce_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

def talking(server):
    @server.route('/api/talking', methods = ['POST'])
    def SingleLogoApi1():
        try:
            recommendId = request.get_json().get("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  根据推荐名字
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)


# talk("商标注册")