from flask import Flask, render_template
from flask import request
from flask_cors import CORS
from service.name_recommend import company_name_explain
from service.preference_selecter import preference_select
from common import common_result
import datetime

server = Flask(__name__,static_url_path="",static_folder="")
server.config['JSON_AS_ASCII'] = False
CORS(server,supports_credentials=True) #跨域请求




base_result = []
now_time = str(datetime.datetime.now())[:10]
#偏好选项信息
@server.route('/preference_select', methods=['POST'])
def preference_select_info():
    if request.method == 'POST':
        global base_result
        global now_time
        now = str(datetime.datetime.now())[:10]
        if now != now_time:
            now_time = now
            base_result = []
            if len(base_result) == 0:
                base_result = preference_select()

        if len(base_result) == 0:
            base_result = preference_select()  


        return common_result.to_result(base_result)


#公司起名结果信息
@server.route('/treasure_name', methods=['POST'])
def treasure_name_info():
    if request.method == 'POST':
        request_data = request.json
        if not request_data or not request_data.get('city') or not request_data.get('preference') or not request_data.get('cate'):
            return common_result.to_result(None, 300, "参数错误")
        result = company_name_explain(request_data)
        return common_result.to_result(result)

if __name__ == '__main__':
    server.run(debug=True,host='0.0.0.0',port=80)
    # server.run(debug=True, host='192.168.201.35', port=)