def get_product_info():
    args_dic = {}
    oid = request.args.get('sellerid')
    pname = request.args.get('productname')
    firsttype = request.args.get('type1')
    secondtype = request.args.get('type2')
    type_ids = None
    if firsttype is not None:
        tp_args_dic = {'type1': firsttype}
        if secondtype is not None:
            tp_args_dic['type2'] = secondtype
        type_ids = [typ['typeid'] for typ in Type.get_type(tp_args_dic)]

    if oid is not None:
        args_dic['oid'] = oid
    if pname is not None:
        args_dic['pname'] = pname
    if type_ids is not None:
        args_dic['types'] = type_ids

    products = Product.get_product(args_dic)

    typeid_list = [product['typeid'] for product in products]
    types = Type.get_type({'typeid_list': typeid_list})
    types_dic = {}
    for typ in types:
        types_dic[typ['typeid']] = typ

    for product in products:
        product['type1'] = types_dic[product['typeid']]['type1']
        product['type2'] = types_dic[product['typeid']]['type2']

    return jsonify({'data': products})
    def get_popular_product(cls, args_dic):
        args_list = ['oid']
        relationship_dic = {
            'detailedoid': 'detailed_orderID',
            'orderid': 'Order_orderID',
            'oid': 'Official_user_official_userID',
            'pid': 'Product_itemID',
            'status': 'status',
        }
        query_dic = {}
        for arg in args_list:
            if args_dic.get(arg) != None:
                    query_dic[relationship_dic[arg]] = args_dic.get(arg)

        results = db_app.session().query(cls.Product_itemID,
                                         func.sum(cls.dPrice).label('sums'),
                                         func.sum(cls.iNumber).label('numbers')). \
                                            group_by(cls.Product_itemID). \
                                            filter(cls.Order_orderID.in_(args_dic['orderid_list'])).\
                                            filter(cls.status != 'canceled').\
                                            filter_by(**query_dic).\
                                            order_by(db_app.desc('sums'))

        result_list = []
        for i, res in enumerate(results.all()):
            if i >= 10: break
            args_dic['pid'] = res[0]
            prod = Product.get_product(args_dic)[0]
            prod['totalprice'] = (float)(res[1])
            prod['totalnumber'] = (float)(res[2])
            result_list.append(prod)
        
        return result_list
    def get_type_product(cls, args_dic):
        args_list = ['oid']
        query_dic = {}
        for arg in args_list:
            if args_dic.get(arg) != None:
                    query_dic['Official_user_official_userID'] = args_dic.get(arg)

        results = db_app.session().query(cls.Product_itemID,
                                         func.sum(cls.dPrice).label('sums'),
                                         func.sum(cls.iNumber).label('numbers')). \
                                            group_by(cls.Product_itemID). \
                                            filter(cls.Product_itemID.in_(args_dic['pid_list'])).\
                                            filter(cls.status != 'canceled').\
                                            filter_by(**query_dic).\
                                            order_by(db_app.desc('sums'))

        result_list = []
        for i, res in enumerate(results.all()):
            args_dic['pid'] = res[0]
            prod = Product.get_product(args_dic)[0]
            prod['totalprice'] = (float)(res[1])
            prod['totalnumber'] = (float)(res[2])
            result_list.append(prod)
        
        return result_list
def put_product_info():
    prid = request.args.get('uid', type=int)
    product = Product().query.filter_by(pid=prid).first()
    type1 = request.form.get('type1')
    type2 = request.form.get('type2')
    types = [
        typ['typeid'] for typ in Type.get_type({
            'type1': type1,
            'type2': type2
        })
    ]
    if len(types) == 0:
        return jsonify({'status': 'fail'})

    type_id = types[0]
    if product == None:
        return jsonify({'status': 'fail'})
    else:
        product.iName = request.form.get('productname')
        product.typeID = type_id
        product.iPrice = request.form.get('price')
        product.remain = request.form.get('remain')
        product.description = request.form.get('description')
        db_app.session.commit()
        return jsonify({'status': 'success'})
def del_product_info():
    prid = request.args.get('uid', type=int)
    product = Product().query.filter_by(itemID=prid).first()
    if product == None:
        return jsonify({'status': 'fail'})
    else:
        db_app.session.delete(product)
        db_app.session.commit()
        return jsonify({'status': 'success'})
def add_product_info():
    type1 = request.form.get('type1')
    type2 = request.form.get('type2')
    types = [
        typ['typeid'] for typ in Type.get_type({
            'type1': type1,
            'type2': type2
        })
    ]
    if len(types) == 0:
        return jsonify({'status': 'fail'})

    type_id = types[0]
    product = Product(
        iName=request.form.get('productname'),
        Type_typeID=type_id,
        iPrice=request.form.get('price'),
        remain=request.form.get('remain'),
        description=request.form.get('description'),
        Official_user_official_userID=request.form.get('sellerid'))
    db_app.session.add(product)
    db_app.session.commit()
    return jsonify({'status': 'success'})
def get_type():
    args_dic = {}
    type1 = request.args.get('type1')
    type2 = request.args.get('type2')
    sellerid = request.args.get('sellerid', type=int)
    if type1: args_dic['type1'] = type1
    if type2: args_dic['type2'] = type2
    if sellerid: args_dic['oid'] = sellerid
    print(args_dic)
    type_list = Type.get_type(args_dic)
    print(len(type_list))
    args_dic['types'] = [types['typeid'] for types in type_list]
    products = Product.get_product(args_dic)
    print(len(products))
    args_dic['pid_list'] = [prod['uid'] for prod in products]
    products = DetailOrder.get_type_product(args_dic)
    print(len(products))
    totals = 0
    totaln = 0
    for product in products:
        totals += product['totalprice']
        totaln += product['totalnumber']

    return jsonify({'data': {'totalsum': totals, 'totalnumber': totaln}})