#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/4/28 0028 下午 5:33
# @Author  : hj
# @Software: PyCharm



from jinja2 import Environment, FileSystemLoader
from common.re_companyName import strQ2B
from flask_cors import *
import sys,os,flask,pymysql
from DBUtils.PooledDB import PooledDB
from flask import render_template,url_for
from common.crm_change_standard import getStandardName


interface_path = os.path.dirname(__file__)
sys.path.insert(0, interface_path)  # 将当前文件的父目录加入临时系统变量
server = flask.Flask(__name__)
server.config['JSON_AS_ASCII'] = False
CORS(server,supports_credentials=True) #跨域请求

MYSQL_HOST = '123.57.19.89'  # 外网地址
PORT = 3306  # 外网端口
USER = 'jeecms'
PASSWORD = '123456.a'
DB = 'wandoujia'
pool_app = PooledDB(pymysql,8, host=MYSQL_HOST, user=USER, passwd=PASSWORD, db=DB, port=PORT)

MYSQL_HOST = '43.247.184.94'  # 外网地址
PORT = 7800  # 外网端口
USER = 'nice'
PASSWORD = 'Niceee@2020'
DB = 'AnnualReport'
pool_website = PooledDB(pymysql,8, host=MYSQL_HOST, user=USER, passwd=PASSWORD, db=DB, port=PORT)

# 获取公司logo信息
def appInfo(company_name):
    conn = pool_app.connection()
    cursor = conn.cursor()
    app_info = []
    sql = "select * from app_infoss where com_name = '%s'" % company_name
    print(sql, "select_app======")
    cursor.execute(sql)
    gsb_app_info = cursor.fetchall()
    for row in gsb_app_info:
        app = {}
        app_name = row[1]
        app_url = row[4]
        app_logo = row[5]
        app['app_name'] = app_name
        if 'http' not in app_url:
            app['app_url'] = 'https://' + app_url
        else:
            app['app_url'] = app_url
        app['app_logo'] = app_logo
        app_info.append(app)
    return app_info

# 获取公司网站信息
def websiteInfo(company_name):
    conn = pool_website.connection()
    cursor = conn.cursor()
    website_info = []
    sql = "select DISTINCT(`name`),website from report_webinfo_all_order where gsb_company_name = '%s'" % company_name
    print(sql, "select_web======")
    cursor.execute(sql)
    gsb_website_info = cursor.fetchall()
    for row in gsb_website_info:
        website = {}
        website_url = row[1]
        website_name = row[0]
        website['website_name'] = website_name
        if 'http' not in website_url:
            website['website_url'] = 'https://' + website_url
        # elif 'http' in website_url and 'https' not in website_url:
        #     website['website_url'] =
        else:
            website['website_url'] = website_url
        website_info.append(website)
    return website_info

def app_website(server):
    @server.route('/app-website/<companyName>')
    def generate_html(companyName):
        # companyName = strQ2B(companyName)
        standard_company_name = getStandardName(companyName)
        app_info = appInfo(standard_company_name)
        website_info = websiteInfo(standard_company_name)
        return render_template('potential_business.html',**locals())
#
# if __name__ == "__main__":
#     server.run(debug=True)


