from flask import Flask, request
from mvp import Mvp
import json

app = Flask(__name__)


@app.route('/behavioral_statistics', methods=['GET', 'POST'])
def behavioral_statistics():
    """
        父选项对应的标准化值
    :return:
    """
    city = request.args.get('city', default=None, type=str)
    age = request.args.get('age', default=None, type=str)
    crowd = request.args.get('crowd', default=None, type=str)
    print(city, age, crowd)
    mvp = Mvp()
    scores = mvp.query_behavioral_info(city, age, crowd)
    mvp.close()
    return json.dumps(scores, ensure_ascii=False)


@app.route('/infos', methods=["GET", 'POST'])
def get_city_age_crowd():
    """
        测试数据中城市 年龄 人群分类信息
    :return:
    """
    mvp = Mvp()
    infos = {'城市': mvp.citys, '年龄段': mvp.age, '人群分类': mvp.crowd}
    mvp.close()
    return json.dumps(infos, ensure_ascii=False)


@app.route('/crowd_people', methods=['GET', 'POST'])
def crowd_people():
    """
        人群分类人数统计
    :return:
    """
    mvp = Mvp()
    people_count = mvp.get_crowd_people()
    mvp.close()
    return json.dumps(people_count, ensure_ascii=False)


@app.route('/set_behavior_tag', methods=['GET', 'POST'])
def set_behavior_tag():
    """
        模块标准化值
    :return:
    """
    mvp = Mvp()
    mvp.close()
    return json.dumps(mvp.module_scores, ensure_ascii=False)


@app.route('/insert_into', methods=['GET', 'POST'])
def insert_info():
    mvp = Mvp()
    mvp.insert()
    query_data = mvp.query_data()
    mvp.close()
    return json.dumps(query_data, ensure_ascii=False)


@app.route('/insert_score', methods=['GET', 'POST'])
def insert_score():
    mvp = Mvp()
    mvp.tag_module_score_insert()
    mvp.close()
    return '!!!分数添加成功!!!'


@app.route('/shanghai_85', methods=['GET', 'POST'])
def shanghai_85():
    mvp = Mvp()
    data = mvp.shanghai_85_module_score_insert()
    mvp.close()
    return json.dumps(data, ensure_ascii=False)


@app.route('/tag_tree', methods=['GET', 'POST'])
def tag_tree():
    mvp = Mvp()
    tags = mvp.tag_data
    mvp.close()
    return json.dumps(tags, ensure_ascii=False)


@app.route('/update_data', methods=['GET', 'POST'])
def update_data():
    mvp = Mvp()
    mvp.update_data()
    mvp.close()
    return '!!!score update finished!!!'


if __name__ == '__main__':
    app.run(
        host='0.0.0.0',
        port=5001
    )
