#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-05-27 9:53
# @Author  : zhangyunfei
# @File    : main.py
# @Software: PyCharm

import ast
import json
from hanshujisuan import heming
from flask import Flask,request,jsonify
server = Flask(__name__)
server.config['JSON_AS_ASCII'] =False

@server.route('/gsb/heming', methods=['POST'])
def my_handler():
    # get request_body
    if request.method == 'POST':
        input_data = request.json
        print(input_data)
        if len(input_data) != 0:
            if input_data.get("cityname") != None and input_data.get("keyword") != None and input_data.get("btname") != None:
                try:
                    if input_data.get("cityname") == "":
                        jsondata = {
                            "code": "500",
                            "message": "缺少cityname值",
                            "data": {}
                        }
                        # jsondata = json.dumps(jsondata, ensure_ascii=False)
                        # infostr = jsondata
                        return jsonify(jsondata)
                    elif input_data.get("keyword") == "":
                        jsondata = {
                            "code": "500",
                            "message": "缺少keyname值",
                            "data": {}
                        }
                        # jsondata = json.dumps(jsondata, ensure_ascii=False)
                        # infostr = jsondata
                        return jsonify(jsondata)
                    elif input_data.get("btname") == "":
                        jsondata = {
                            "code": "500",
                            "message": "缺少btname值",
                            "data": {}
                        }
                        # jsondata = json.dumps(jsondata, ensure_ascii=False)
                        # infostr = jsondata
                        return jsonify(jsondata)
                    elif input_data.get("orgname") == None and input_data.get("sitcity") == None:
                        input_data.update({"orgname": "有限公司"})
                        input_data.update({"sitcity": "1"})
                        info = heming(input_data)
                        # return value must be iterable
                        return jsonify(info)
                    elif input_data.get("orgname") == None:
                        input_data.update({"orgname": "有限公司"})
                        if input_data.get("sitcity") == "":
                            input_data.update({"sitcity": "1"})
                            info = heming(input_data)
                            # return value must be iterable
                            return jsonify(info)
                        else:
                            info = heming(input_data)
                            # return value must be iterable
                            return jsonify(info)
                    elif input_data.get("sitcity") == None:
                        input_data.update({"sitcity": "1"})
                        if input_data.get("orgname") == "":
                            input_data.update({"orgname": "有限公司"})
                            info = heming(input_data)
                            # return value must be iterable
                            return jsonify(info)
                        else:
                            info = heming(input_data)
                            # return value must be iterable
                            return jsonify(info)
                    else:
                        info = heming(input_data)
                        # return value must be iterable
                        return jsonify(info)
                except Exception as e:
                    jsondata = {
                        "code": "500",
                        "message": "异常："+str(e),
                        "data": {}
                    }
                    return jsonify(jsondata)
            else:
                jsondata = {
                    "code": "500",
                    "message": "缺少参数",
                    "data": {}
                }
                # jsondata = json.dumps(jsondata, ensure_ascii=False)
                # infostr = jsondata
                return jsonify(jsondata)
        else:
            jsondata = {
                "code": "500",
                "message": "数据结构传输错误",
                "data": {}
            }
            # jsondata = json.dumps(jsondata, ensure_ascii=False)
            # infostr = jsondata
            return jsonify(jsondata)


# server.run(host='192.168.1.131', port=35502, debug=True)
server.run(host='0.0.0.0', port=35502, debug=True)
