def by_route_station():
    route_name_input = request.values.get('route_name')
    timestamp_input = request.values.get('timestamp')
    if route_name_input and timestamp_input:
        route = Route.query.filter(
            Route.route_name == route_name_input).first()
        if route:
            sql = "SELECT COUNT((station_id)) FROM route_station;"
            result = op_db.login_add(sql)  # 执行sql
            length = result[0].get('COUNT((station_id))')
            i = 1
            # 循环查询该路线所有站点的人群密度信息
            res_list = []
            while i <= length:
                # 首先查询路线站点列表是否有该站点的存在
                station = Route_station.query.filter(
                    Route_station.station_id == i).first()
                if station:  # 若存在,则返回密度信息
                    station_then = Station.query.filter(
                        Station.station_id == i).first()
                    station_name = station_then.station_name
                    density = Count_station(timestamp_log=timestamp_input,
                                            station_id_log=i)
                    res_dic = {
                        'station_name': station_name,
                        'density': density
                    }
                    res_list.append(res_dic)
                i += 1
            return json.dumps(res_list, ensure_ascii=False)
        else:
            res = {'error_code': 404, 'msg': '抱歉暂无该路线信息!'}
            return json.dumps(res, ensure_ascii=False)
Exemple #2
0
def add():
    user_id_log = request.values.get("user_id")
    phone = request.values.get("phone")
    user_name = request.values.get("user_name")
    email = request.values.get("email")
    password = request.values.get("password")
    sex = request.values.get("sex")
    if sex:
        if sex == '男':
            sex = 1
        elif sex == '女':
            sex = 0
    else:
        sex = 1
    birth = request.values.get("born_date")

    if user_id_log and phone and user_name and password and birth:  # 必填参数校验

        if not user_id_log.isdigit():  # 判断ID号格式是否正确
            res = {"error_code": 3006, "msg": "ID号输入错误"}

        else:
            sql = "select* from user where user_id='%s';" % user_id_log  # 查看数据库中是否有这个ID号,有的话说明重复
            result = op_db.login_add(sql)  # 执行sql
            if result:
                res = {"error_code": 1000, "msg": "ID号已经存在!"}

            else:
                sql = "INSERT INTO user(user_id, phone, user_name, email, password, sex, born_date)" \
                      "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')" \
                      % (user_id_log, phone, user_name, email, password, sex, birth)
                op_db.login_add(sql)
                # user_add = User(user_id=user_id_log, phone=phone, user_name=user_name, email=email, password=password,
                #                 sex=sex, born_date=birth)
                # db.session.add(user_add)
                # db.session.commit()
                res = {"error_code": 200, "msg": "新增成功! "}
    else:
        res = {"error_code": 3007, "msg": "必填参数未填写!"}
    return json.dumps(res, ensure_ascii=False)  # 防止出现乱码
Exemple #3
0
def login():
    # 登录需要两个参数,user_id和pwd
    user_id_log = request.values.get('user_id')
    passwd_log = request.values.get('password')
    if user_id_log and passwd_log:  # 非空为真
        # 需要先写一个导入数据库的函数
        sql = "SELECT * FROM user WHERE user_id='%s' AND password='******';" % (
            user_id_log, passwd_log)
        result = op_db.login_add(sql)  # 执行sql
        if result:
            res = {"error_code": 1000, "mag": "登录成功!"}  # 接口返回的都是json
        else:
            res = {"error_code": 3001, "mag": "账号或密码错误!"}
    else:
        res = {"error_code": 3000, "mag": "必填参数未填,请查看接口文档!"}

    return json.dumps(res,
                      ensure_ascii=False)  # 防止出现乱码;json.dumps()函数是将字典转化为字符串
def min_station(lon_m, lat_m):
    sql = "SELECT COUNT(*) FROM station;"
    result = op_db.login_add(sql)  # 执行sql
    length = result[0].get('COUNT(*)')
    i = 1
    min = 6371000  # 地球平均半径,6371km
    id = 0
    while i <= length:
        station = Station.query.filter(Station.station_id == i).first()
        lon = station.longitude
        lat = station.latitude
        dis = get_distance_hav(lng0=lon, lat0=lat, lng1=lon_m, lat1=lat_m)
        if min >= dis:
            min = dis
            id = i
        i += 1
    station_need = Station.query.filter(Station.station_id == id).first()
    station_name = station_need.station_name
    return station_name
Exemple #5
0
def add():
    # user_id_log = request.values.get("user_id")
    phone = request.values.get("phone")
    user_name = request.values.get("user_name")
    password = request.values.get("password")
    if_re = re.search(r"\W", phone)
    if phone and user_name and password:  # 必填参数校验
        sql = "select* from user where phone='%s';" % phone  # 查看数据库中是否有这个手机号,有的话说明重复
        result = op_db.login_add(sql)  # 执行sql
        if result:
            res = {"error_code": 1000, "msg": "电话号已经存在!"}
        elif phone.isspace or if_re or len(phone) < 11:
            res = {"error_code": 1001, "msg": "电话号格式不正确!"}
        else:
            # sql = "INSERT INTO user(user_id, phone, user_name, email, password, sex, born_date)" \
            #       "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')" \
            #       % (user_id_log, phone, user_name, email, password, sex, birth)
            # op_db.login_add(sql)
            user_add = User(phone=phone,
                            user_name=user_name,
                            email='null',
                            password=password,
                            delete_type=0,
                            sex=1,
                            head_img='null',
                            address='null',
                            born_date='1999-1-1',
                            longitude=0,
                            latitude=0,
                            production='null')
            db.session.add(user_add)
            db.session.commit()
            user_find = User.query.filter(User.phone == phone).first()
            user_id_return = user_find.user_id
            res = {
                "error_code": 200,
                "msg": "新增成功!",
                "user_id": user_id_return
            }
    else:
        res = {"error_code": 3007, "msg": "必填参数未填写!"}
    return json.dumps(res, ensure_ascii=False)  # 防止出现乱码
def by_route_bus():
    route_name_input = request.values.get('route_name')
    timestamp_input = request.values.get('timestamp')
    if route_name_input and timestamp_input:
        # 获取指定路线信息
        route = Route.query.filter(Route.route_name == route_name_input).first()
        if route:  # 若该路线存在
            route_id_need = route.route_id
            # print(route_id_need)
            sql = "SELECT COUNT((bus_id)) FROM bus WHERE route_id='%s';" % route_id_need
            result = op_db.login_add(sql)  # 执行sql
            # 获取路线所包含的站点数
            length = result[0].get('COUNT((bus_id))')
            bus_need = Bus.query.filter(Bus.route_id == route_id_need).first()
            begin_id = bus_need.bus_id
            # 以列表形式向前端输出该路线各个公交的人群密度信息
            res_list = []
            i = 1
            while i <= length:
                bus = Bus.query.filter(Bus.bus_id == begin_id).first()
                if bus:
                    # bus_then = Station.query.filter(Station.station_id == i).first()
                    # station_name = station_then.station_name
                    density = Count_bus(timestamp_log=timestamp_input, bus_id_log=begin_id)
                    res_dic = {
                        'bus_id': begin_id, 'density': density
                    }
                    res_list.append(res_dic)
                begin_id += 1
                i += 1
            return json.dumps(res_list, ensure_ascii=False)
        else:
            res = {
                'error_code': 405, 'msg': '抱歉暂无该路线信息!'
            }
            return json.dumps(res, ensure_ascii=False)
    else:
        res = {
            'error_code': 404, 'msg': '关键参数未填!'
        }
        return json.dumps(res, ensure_ascii=False)
def edit():
    user_id_return = request.values.get("user_id")
    user_info = User.query.filter(User.user_id == user_id_return).first()
    """获取当前个人信息"""
    phone_this = user_info.phone
    user_name_this = user_info.user_name
    password_this = user_info.password
    email_this = user_info.email
    sex_this = user_info.sex
    if sex_this == 1:
        sex_this = '男'
    else:
        sex_this = '女'
    head_img_this = user_info.head_img
    if head_img_this is None:
        head_img_this = 'https://gw.alipayobjects.com/zos/rmsportal/MRhHctKOineMbKAZslML.jpg'
    address_this = user_info.address
    birth_this = user_info.born_date
    longitude_this = str(user_info.longitude)
    if longitude_this is None:
        longitude_this = 'null'
    latitude_this = str(user_info.latitude)
    if latitude_this is None:
        latitude_this = 'null'
    production_this = user_info.production

    if request.method == 'GET':
        res = {
            "error_code": 200,
            "phone": phone_this,
            "user_name": user_name_this,
            "password": password_this,
            "email": email_this,
            "sex": sex_this,
            "head_img": head_img_this,
            "address": address_this,
            "birth": str(birth_this),
            "longitude": longitude_this,
            "latitude": latitude_this,
            "production": production_this
        }

        return json.dumps(res, ensure_ascii=False)

    if request.method == 'POST':
        """提交修改后的个人信息"""
        phone_new = request.values.get("phone")
        if phone_new is None:
            phone_new = phone_this

        user_name_new = request.values.get("user_name")
        if user_name_new is None:
            user_name_new = user_name_this

        password_new = request.values.get("password")
        if password_new is None:
            password_new = password_this

        email_new = request.values.get("email")
        if email_new is None:
            email_new = email_this

        sex_new = request.values.get("sex")
        if sex_new:
            if sex_new == '男':
                sex_new = 1
            elif sex_new == '女':
                sex_new = 0
        else:
            sex_new = 1

        head_img_new = request.form["head_img"]
        if head_img_new is None:
            head_img_new = head_img_new

        address_new = request.values.get("address")
        if address_new is None:
            address_new = address_this

        birth_new = request.values.get("born_date")
        if birth_new is None:
            birth_new = birth_this

        longitude_new = request.values.get("longitude")
        if longitude_new is None:
            longitude_new = 0

        latitude_new = request.values.get("latitude")
        if latitude_new is None:
            latitude_new = 0

        production_new = request.values.get("production")
        if production_new is None:
            production_new = production_this

        sql = "select* from user where phone='%s';" % phone_new  # 查看数据库中是否有这个手机号,有的话说明重复
        result = op_db.login_add(sql)  # 执行sql
        # if_re = re.search(r"\W", phone_new)
        if result:
            res = {"error_code": 1000, "msg": "电话号已经存在!"}
        elif len(phone_new) < 11 or len(phone_new) > 12:
            # 是否有空格、特殊符号、长度是否为11位
            res = {"error_code": 1001, "msg": "电话号格式不正确!"}
        else:
            user_info.phone = phone_new
            user_info.user_name = user_name_new
            user_info.password = password_new
            user_info.email = email_new
            user_info.sex = sex_new
            user_info.head_img = head_img_new
            user_info.address = address_new
            user_info.born_date = birth_new
            user_info.longitude = longitude_new
            user_info.latitude = latitude_new
            user_info.production = production_new
            db.session.commit()
            res = {"error_code": 200, "msg": "修改成功!"}

        return json.dumps(res, ensure_ascii=False)  # 防止出现乱码
def login():
    # 登录需要两个参数,user_id和pwd
    user_id_log = request.values.get('user_id')
    passwd_log = request.values.get('password')
    lon_log = request.values.get('longitude')
    lat_log = request.values.get('latitude')
    timestamp = request.values.get('timestamp')
    role = request.values.get('role')  # 1为管理员,0为普通用户

    if role == '1':
        # 管理员登录
        if user_id_log and passwd_log:  # 非空为真
            # 需要先写一个导入数据库的函数
            sql = "SELECT * FROM user WHERE user_id='%s' AND password='******';" % (
                user_id_log, passwd_log)
            result = op_db.login_add(sql)  # 执行sql
            if result:
                user = User.query.filter(User.user_id == user_id_log).first()
                show_like_bus = Show_like_bus(user_id_l=user_id_log,
                                              timestamp=timestamp)  # 展示收藏公交信息
                show_like_station = Show_like_station(
                    user_id_l=user_id_log, timestamp=timestamp)  # 展示收藏站点信息
                head_image = user.head_img
                if head_image is None:
                    head_image = "https://gw.alipayobjects.com/zos/rmsportal/MRhHctKOineMbKAZslML.jpg"
                admin = Admin.query.filter(
                    Admin.user_id == user_id_log).first()
                if admin:
                    admin_id = admin.admin_id
                    if lon_log and lat_log:
                        """根据用户当前经纬度位置进行推送"""
                        user.longitude = lon_log
                        user.latitude = lat_log
                        db.session.commit()
                        station_name = min_station(lon_m=lon_log,
                                                   lat_m=lat_log)
                        station = Station.query.filter(
                            Station.station_name == station_name).first()
                        station_id = station.station_id
                        nearest_density = Count_station(
                            timestamp_log=timestamp, station_id_log=station_id)
                        # 接口返回的都是json
                        res = {
                            "error_code": 1000,
                            "msg": "登录成功!",
                            "admin_id": admin_id,
                            "head_img": head_image,
                            "station_name": station_name,
                            "nearest_density": nearest_density
                            # , "like_bus_density": show_like_bus, "like_station_density": show_like_station
                        }
                    else:
                        res = {
                            "error_code": 1001,
                            "msg": "登录成功!但无法获取您的位置。",
                            "admin_id": admin_id,
                            "head_img": head_image
                            # , "like_bus_density": show_like_bus, "like_station_density": show_like_station
                        }
                else:
                    res = {"error_code": 404, "msg": "抱歉您不是管理员!"}
            else:
                res = {"error_code": 3001, "msg": "账号或密码错误!"}
        else:
            res = {"error_code": 3000, "msg": "必填参数未填,请查看接口文档!"}

        return json.dumps(
            res, ensure_ascii=False)  # 防止出现乱码;json.dumps()函数是将字典转化为字符串

    if role == '0':
        # 普通用户登录
        if user_id_log and passwd_log:  # 非空为真
            # 需要先写一个导入数据库的函数
            sql = "SELECT * FROM user WHERE user_id='%s' AND password='******';" % (
                user_id_log, passwd_log)
            result = op_db.login_add(sql)  # 执行sql
            if result:
                user = User.query.filter(User.user_id == user_id_log).first()
                # show_like_bus = Show_like_bus(user_id_l=user_id_log, timestamp=timestamp)  # 展示收藏公交信息
                # show_like_station = Show_like_station(user_id_l=user_id_log, timestamp=timestamp)  # 展示收藏站点信息
                head_image = user.head_img
                if head_image is None:
                    head_image = "https://gw.alipayobjects.com/zos/rmsportal/MRhHctKOineMbKAZslML.jpg"
                if lon_log and lat_log:
                    """根据用户当前经纬度位置进行推送"""
                    user.longitude = lon_log
                    user.latitude = lat_log
                    db.session.commit()
                    print(float(lon_log))
                    print(float(lat_log))
                    station_name = min_station(lon_m=float(lon_log),
                                               lat_m=float(lat_log))
                    station = Station.query.filter(
                        Station.station_name == station_name).first()
                    station_id = station.station_id
                    s_lon = station.longitude
                    s_lat = station.latitude
                    map_img = Map(lon=s_lon,
                                  lat=s_lat,
                                  st_name=station_name,
                                  my_lon=lon_log,
                                  my_lat=lat_log)
                    nearest_density = Count_station(timestamp_log=timestamp,
                                                    station_id_log=station_id)
                    # 接口返回的都是json
                    res = {
                        "error_code": 1000,
                        "msg": "登录成功!",
                        "head_img": head_image,
                        "station_name": station_name,
                        "nearest_density": nearest_density,
                        "map_img": map_img
                        # , "like_bus_density": show_like_bus, "like_station_density": show_like_station
                    }
                else:
                    res = {
                        "error_code": 1001,
                        "msg": "登录成功!但无法获取您的位置。",
                        "head_img": head_image
                        # , "like_bus_density": show_like_bus, "like_station_density": show_like_station
                    }
            else:
                res = {"error_code": 3001, "msg": "账号或密码错误!"}
        else:
            res = {"error_code": 3000, "msg": "必填参数未填,请查看接口文档!"}

        return json.dumps(
            res, ensure_ascii=False)  # 防止出现乱码;json.dumps()函数是将字典转化为字符串

    if role is None:
        # 未选择登录身份
        res = {"error_code": 404, "msg": "未选择登录身份!"}
        return json.dumps(res, ensure_ascii=False)
def admin():
    admin_permit = request.values.get("admin_id")

    """选择何种操作则值为1"""
    # 删除用户信息
    dele_user = request.values.get("delete_user")

    # 站台信息
    edit_station = request.values.get("edit_station")
    dele_station = request.values.get("delete_station")
    add_station = request.values.get("add_station")

    # 路线信息
    edit_route = request.values.get("edit_route")
    dele_route = request.values.get("delete_route")
    add_route = request.values.get("add_route")

    # 公交信息
    edit_bus = request.values.get("edit_bus")
    dele_bus = request.values.get("delete_bus")
    add_bus = request.values.get("add_bus")

    if admin_permit:

        """
        ====================================
        删除用户信息
        ====================================
        """
        if dele_user == '1':
            # 获取要删除用户信息
            user_id_dele = request.values.get("user_id")
            if user_id_dele:
                # print(type(user_id_dele))
                d_user = User.query.filter(User.user_id == user_id_dele).first()
                me = Admin.query.filter(Admin.admin_id == admin_permit).first()
                me_id = me.user_id
                # print(type(me_id))
                if user_id_dele == str(me_id):  # 防止删除自己
                    res = {
                        "error_code": 400, "msg": "错误操作!您在删除自己!"
                    }
                elif d_user:
                    db.session.delete(d_user)
                    db.session.commit()
                    res = {
                        "error_code": 200, "msg": "删除成功!"
                    }
                else:
                    res = {
                        "error_code": 404, "msg": "删除失败!未找到该用户!"
                    }
            else:
                res = {
                    "error_code": 1001, "msg": "user_id未填!"
                }

            return json.dumps(res, ensure_ascii=False)  # 防止出现乱码

        """
        ====================================
        编辑路线信息
        ====================================
        """
        if edit_route == '1':
            route_id_edit = request.values.get("route_id")

            if route_id_edit:
                route_need = Route.query.filter(Route.route_id == route_id_edit).first()

                '''获取所需路线信息'''
                if request.method == 'GET':
                    if route_need:
                        route_need_name = route_need.route_name
                        res = {
                            "error_code": 200, "route_id": route_id_edit, "route_name": route_need_name
                        }
                        return json.dumps(res, ensure_ascii=False)
                    else:
                        res = {
                            "error_code": 404, "msg": "未查询到该路线信息!"
                        }
                        return json.dumps(res, ensure_ascii=False)

                '''修改所需路线信息'''
                if request.method == 'POST':
                    if route_need:
                        route_name_new = request.values.get("route_name")
                        route_need.route_name = route_name_new
                        db.session.commit()
                        res = {
                            "error_code": 200, "msg": "修改成功!"
                        }
                        return json.dumps(res, ensure_ascii=False)
                    else:
                        res = {
                            "error_code": 404, "msg": "未查询到该路线信息!"
                        }
                        return json.dumps(res, ensure_ascii=False)
            else:
                res = {
                    "error_code": 1001, "msg": "route_id未填!"
                }
                return json.dumps(res, ensure_ascii=False)

        """
        ====================================
        删除路线信息
        ====================================
        """
        if dele_route == '1':
            route_name_dele = request.values.get("route_name")
            if route_name_dele:
                d_route = Route.query.filter(Route.route_name == route_name_dele).first()
                if d_route:
                    db.session.delete(d_route)
                    db.session.commit()
                    res = {
                        "error_code": 200, "msg": "删除成功!"
                    }
                else:
                    res = {
                        "error_code": 404, "msg": "删除失败!未找到该路线!"
                    }
            else:
                res = {
                    "error_code": 1001, "msg": "route_name未填!"
                }

            return json.dumps(res, ensure_ascii=False)  # 防止出现乱码

        """
        ===================================
        添加路线信息
        ===================================
        """
        if add_route == '1':
            new_route = request.values.get("route_name")
            if new_route:
                sql = "select* from route where route_name='%s';" % new_route  # 查看数据库中是否有这条路线,有的话说明重复
                result = op_db.login_add(sql)  # 执行sql
                if result:
                    res = {
                        "error_code": 1000, "msg": "路线已经存在!"
                    }
                else:
                    route_add = Route(route_name=new_route)
                    db.session.add(route_add)
                    db.session.commit()
                    route_find = Route.query.filter(Route.route_name == new_route).first()
                    route_id_return = route_find.route_id
                    res = {
                        "error_code": 200, "msg": "新增成功!", "route_id": route_id_return
                    }
                return json.dumps(res, ensure_ascii=False)

            else:
                res = {
                    "error_code": 1001, "msg": "route_name未填!"
                }
                return json.dumps(res, ensure_ascii=False)

        """
        =========================================
        编辑站点信息
        =========================================
        """
        if edit_station == '1':
            station_id_edit = request.values.get("station_id")
            if station_id_edit:
                station_need = Station.query.filter(Station.station_id == station_id_edit).first()

                if station_need:
                    station_need_name = station_need.station_name  # 站点名
                    station_need_type = station_need.station_type  # 站点类型(1)
                    station_need_lon = str(station_need.longitude)  # 站点经度
                    station_need_lat = str(station_need.latitude)  # 站点纬度

                    '''获取所需站点信息'''
                    if request.method == 'GET':
                        res = {
                            "error_code": 200, "station_name": station_need_name, "station_type": station_need_type,
                            "station_longitude": station_need_lon, "station_latitude": station_need_lat
                        }
                        return json.dumps(res, ensure_ascii=False)

                    '''修改所需站点信息'''
                    if request.method == 'POST':
                        station_name_edit = request.values.get("station_name")
                        if station_name_edit is None:
                            station_name_edit = station_need_name

                        station_type_edit = request.values.get("station_type")
                        if station_type_edit is None:
                            station_type_edit = station_need_type

                        station_lon_edit = request.values.get("station_longitude")
                        if station_lon_edit is None:
                            station_lon_edit = station_need_lon

                        station_lat_edit = request.values.get("station_latitude")
                        if station_lat_edit is None:
                            station_lat_edit = station_need_lat
                        station_need.station_name = station_name_edit
                        station_need.station_type = int(station_type_edit)
                        station_need.longitude = station_lon_edit
                        station_need.latitude = station_lat_edit
                        db.session.commit()
                        res = {
                            "error_code": 200, "msg": "修改成功!"
                        }
                        return json.dumps(res, ensure_ascii=False)
                else:
                    res = {
                        "error_code": 404, "msg": "未查询到该站点信息!"
                    }
                    return json.dumps(res, ensure_ascii=False)

            else:
                res = {
                    "error_code": 1001, "msg": "station_id未填!"
                }
                return json.dumps(res, ensure_ascii=False)

        """
        =======================================
        删除站点信息
        =======================================
        """
        if dele_station == '1':
            station_name_dele = request.values.get("station_name")
            if station_name_dele:
                d_station = Station.query.filter(Station.station_name == station_name_dele).first()
                if d_station:
                    db.session.delete(d_station)
                    db.session.commit()
                    res = {
                        "error_code": 200, "msg": "删除成功!"
                    }
                else:
                    res = {
                        "error_code": 404, "msg": "删除失败!未找到该站点!"
                    }
            else:
                res = {
                    "error_code": 1001, "msg": "station_name未填!"
                }

            return json.dumps(res, ensure_ascii=False)  # 防止出现乱码

        """
        ===========================================
        添加新的站点
        ===========================================
        """
        if add_station == '1':
            new_station_name = request.values.get("station_name")
            get_lon_lat = Get_lon_lat(new_station_name)
            new_station_type = int(request.values.get("station_type"))
            new_station_lon = get_lon_lat[0]
            new_station_lat = get_lon_lat[1]
            if new_station_name and new_station_type:
                sql = "select* from station where station_name='%s';" % new_station_name  # 查看数据库中是否有这个站点,有的话说明重复
                result = op_db.login_add(sql)  # 执行sql
                if result:
                    res = {
                        "error_code": 1000, "msg": "站点已经存在!"
                    }
                else:
                    station_add = Station(station_name=new_station_name, station_type=new_station_type, delete_type=0,
                                          longitude=new_station_lon, latitude=new_station_lat)
                    db.session.add(station_add)
                    db.session.commit()
                    station_find = Station.query.filter(Station.station_name == new_station_name).first()
                    station_id_return = station_find.station_id
                    res = {
                        "error_code": 200, "msg": "新增成功!", "station_id": station_id_return
                    }
                return json.dumps(res, ensure_ascii=False)
            else:
                res = {
                    "error_code": 1001, "msg": "station_name和station_type未填!"
                }
                return json.dumps(res, ensure_ascii=False)

        """
        ===========================================
        编辑公交信息
        ===========================================
        """
        if edit_bus == '1':
            bus_id_edit = request.values.get("bus_id")
            if bus_id_edit:
                bus_need = Bus.query.filter(Bus.bus_id == bus_id_edit).first()

                if bus_need:
                    bus_need_route = bus_need.route_id  # 路线ID
                    bus_need_user = bus_need.user_id  # 用户ID
                    bus_need_type = bus_need.type_id  # 公交类型(1)
                    bus_need_per = bus_need.permit_passengers  # 准载人数
                    bus_need_date = bus_need.start_date  # 开启日期
                    bus_need_bel = bus_need.belong_company  # 所属公司

                    '''获取所需公交信息'''
                    if request.method == 'GET':
                        res = {
                            "error_code": 200, "route_id": bus_need_route, "user_id": bus_need_user,
                            "type_id": bus_need_type, "permit_passengers": bus_need_per,
                            "start_date": str(bus_need_date), "belong_company": bus_need_bel
                        }
                        return json.dumps(res, ensure_ascii=False)

                    '''修改所需公交信息'''
                    if request.method == 'POST':
                        if bus_need:
                            bus_route_edit = request.values.get("route_id")
                            if bus_route_edit is None:
                                bus_route_edit = bus_need_route

                            bus_user_edit = request.values.get("user_id")
                            if bus_user_edit is None:
                                bus_user_edit = bus_need_user

                            bus_type_edit = request.values.get("type_id")
                            if bus_type_edit is None:
                                bus_type_edit = bus_need_type

                            bus_per_edit = request.values.get("permit_passengers")
                            if bus_per_edit is None:
                                bus_per_edit = bus_need_per

                            bus_date_edit = request.values.get("start_date")
                            if bus_date_edit is None:
                                bus_date_edit = str(bus_need_date)

                            bus_bel_edit = request.values.get("belong_company")
                            if bus_bel_edit is None:
                                bus_bel_edit = bus_need_bel

                            bus_need.route_id = int(bus_route_edit)
                            bus_need.user_id = int(bus_user_edit)
                            bus_need.type_id = int(bus_type_edit)
                            bus_need.permit_passengers = int(bus_per_edit)
                            bus_need.start_date = datetime.datetime.strptime(bus_date_edit, '%Y-%m-%d')
                            bus_need.belong_company = bus_bel_edit
                            db.session.commit()
                            res = {
                                "error_code": 200, "msg": "修改成功!"
                            }
                            return json.dumps(res, ensure_ascii=False)
                        else:
                            res = {
                                "error_code": 404, "msg": "未查询到该公交信息!"
                            }
                            return json.dumps(res, ensure_ascii=False)
                else:
                    res = {
                        "error_code": 404, "msg": "未查询到该公交信息!"
                    }
                    return json.dumps(res, ensure_ascii=False)
            else:
                res = {
                    "error_code": 1001, "msg": "bus_id未填!"
                }
                return json.dumps(res, ensure_ascii=False)

        """
        ===========================================
        删除公交信息
        ===========================================
        """
        if dele_bus == '1':
            bus_id_dele = request.values.get("bus_id")
            if bus_id_dele:
                d_bus = Bus.query.filter(Bus.bus_id == bus_id_dele).first()
                if d_bus:
                    db.session.delete(d_bus)
                    db.session.commit()
                    res = {
                        "error_code": 200, "msg": "删除成功!"
                    }
                else:
                    res = {
                        "error_code": 404, "msg": "删除失败!未找到该公交!"
                    }
            else:
                res = {
                    "error_code": 1001, "msg": "bus_id未填!"
                }

            return json.dumps(res, ensure_ascii=False)  # 防止出现乱码

        """
        =====================================
        添加公交信息
        =====================================
        """
        if add_bus == '1':
            new_route_id = request.values.get("route_id")
            if new_route_id is None:
                new_route_id = 1

            new_user_id = int(request.values.get("user_id"))
            if new_user_id is None:
                new_user_id = 1

            new_type_id = request.values.get("type_id")
            new_per = request.values.get("permit_passengers")
            new_date_str = request.values.get("start_date")
            if new_date_str is None:
                new_date_str = '2020-01-01'

            new_bel = request.values.get("belong_company")
            if new_bel is None:
                new_bel = '天府通公交公司'

            if new_type_id and new_per:
                new_date = datetime.datetime.strptime(new_date_str, '%Y-%m-%d')
                bus_add = Bus(route_id=new_route_id, user_id=new_user_id, delete_type=0, type_id=new_type_id,
                              permit_passengers=new_per, start_date=new_date, belong_company=new_bel)
                db.session.add(bus_add)
                db.session.commit()
                sql = "select count(*) from bus"
                result = op_db.login_add(sql)
                bus_id_return = result[0].get('count(*)')
                # bus_find = Bus.query.filter(Bus.route_id == new_route_id).all()
                # bus_id_return = bus_find.bus_id
                res = {
                    "error_code": 200, "msg": "新增成功!", "bus_id": bus_id_return
                }
                return json.dumps(res, ensure_ascii=False)
            else:
                res = {
                    "error_code": 1001, "msg": "type_id或permit_passengers未填!"
                }
                return json.dumps(res, ensure_ascii=False)

        else:
            res = {
                "error_code": 404, "msg": "未选择操作类型!"
            }
            return json.dumps(res, ensure_ascii=False)

    else:
        res = {
            "error_code": 404, "msg": "管理员ID有误!"
        }
        return json.dumps(res, ensure_ascii=False)