Пример #1
0
 def add_credit():
     """
     Add User Credit
     :return:Response
     """
     db = DAOUtils.get_db()
     input_json = request.get_json()
     user = DAOUtils.get_user_dao().get_user(db,
                                             "USER_ID = '{USER_ID}'",
                                             USER_ID=input_json['user_id'])
     if user:
         try:
             user.CREDIT += input_json['amount']
             LogUtils.insert_user_log(db,
                                      user_id=input_json['user_id'],
                                      action='會員存款',
                                      remark=str(json.dumps(input_json)))
             DAOUtils.commit(db)
             result = dict()
             result['user_id'] = user.USER_ID
             result['name'] = user.NAME
             return Flask(__name__).make_response((jsonify(result), 200))
         except:
             DAOUtils.rollback(db)
             error_result = dict()
             error_result['error'] = str(sys.exc_info())
             return Flask(__name__).make_response(
                 (jsonify(error_result), 406))
         finally:
             DAOUtils.close(db)
     else:
         DAOUtils.close(db)
         return Flask(__name__).make_response(('', 404))
Пример #2
0
    def cart_to_order():
        """
        Generate Order From Cart, and Delete Cart
        :return:Response
        """
        db = DAOUtils.get_db()
        input_json = request.get_json()

        carts = DAOUtils.get_cart_dao().get_carts(
            db, "USER_ID = '{USER_ID}'", USER_ID=input_json['user_id'])

        if carts:
            try:
                ord_no = DAOUtils.get_order_dao().get_max_order_no(db)
                order_header = OrderHeader(ord_no=ord_no,
                                           user_id=input_json['user_id'],
                                           sub_total=0)
                DAOUtils.get_order_dao().insert_order_header(db, order_header)
                sub_total = 0

                for cart in carts:
                    sub_total += cart.AMOUNT * cart.PRODUCT.ITEM_PRICE
                    order_item = OrderItem(ord_no=ord_no,
                                           item_id=cart.ITEM_ID,
                                           amount=cart.AMOUNT)
                    DAOUtils.get_order_dao().insert_order_item(db, order_item)
                    DAOUtils.get_cart_dao().delete_cart(db, cart)

                user = DAOUtils.get_user_dao().get_user(
                    db, "USER_ID = '{USER_ID}'", USER_ID=input_json['user_id'])
                # 額度不足以結帳
                if sub_total > user.CREDIT:
                    error_result = dict()
                    error_result['error'] = '額度不足以結帳'
                    return Flask(__name__).make_response(
                        (jsonify(error_result), 403))
                else:
                    order_header.SUB_TOTAL = sub_total
                    user.CREDIT -= sub_total
                    LogUtils.insert_user_log(db,
                                             user_id=input_json['user_id'],
                                             action='購物車結帳',
                                             remark=ord_no)
                    DAOUtils.commit(db)
                    return Flask(__name__).make_response(('', 201))
            except:
                DAOUtils.rollback(db)
                error_result = dict()
                error_result['error'] = str(sys.exc_info())
                return Flask(__name__).make_response(
                    (jsonify(error_result), 406))
            finally:
                DAOUtils.close(db)
        else:
            DAOUtils.close(db)
            return Flask(__name__).make_response(('', 404))
Пример #3
0
    def __delete_cart():
        """
        Remove Item from Cart
        :return: Response
        """
        db = DAOUtils.get_db()
        input_json = request.get_json()
        user = DAOUtils.get_user_dao().get_user(
            db,
            condition="USER_ID = '{USER_ID}'",
            USER_ID=input_json['user_id'])
        product = DAOUtils.get_product_dao().get_product(
            db, "ITEM_ID = '{ITEM_ID}'", ITEM_ID=input_json['item_id'])

        if user and product:
            try:
                carts = DAOUtils.get_cart_dao().get_carts(
                    db,
                    condition="USER_ID = '{USER_ID}' AND ITEM_ID = '{ITEM_ID}'",
                    USER_ID=input_json['user_id'],
                    ITEM_ID=input_json['item_id'])
                if carts:
                    for cart in carts:
                        DAOUtils.get_cart_dao().delete_cart(db, cart)
                        product.AMOUNT += cart.AMOUNT
                    LogUtils.insert_user_log(db,
                                             user_id=input_json['user_id'],
                                             action='移除商品至購物車',
                                             remark=str(
                                                 json.dumps(input_json)))
                    DAOUtils.commit(db)
                    return Flask(__name__).make_response(('', 200))
                else:
                    return Flask(__name__).make_response(('', 404))
            except:
                DAOUtils.rollback(db)
                error_result = dict()
                error_result['error'] = str(sys.exc_info())
                return Flask(__name__).make_response(
                    (jsonify(error_result), 406))
            finally:
                DAOUtils.close(db)
        else:
            DAOUtils.close(db)
            return Flask(__name__).make_response(('', 404))
Пример #4
0
 def __add_cart():
     """
     Add Item to Cart
     :return: Response
     """
     db = DAOUtils.get_db()
     input_json = request.get_json()
     user = DAOUtils.get_user_dao().get_user(
         db,
         condition="USER_ID = '{USER_ID}'",
         USER_ID=input_json['user_id'])
     product = DAOUtils.get_product_dao().get_product(
         db, "ITEM_ID = '{ITEM_ID}'", ITEM_ID=input_json['item_id'])
     if user and product:
         if input_json['amount'] > product.AMOUNT:
             DAOUtils.close(db)
             error_result = dict()
             error_result['error'] = '商品庫存不足'
             return Flask(__name__).make_response(
                 (jsonify(error_result), 403))
         else:
             product.AMOUNT -= input_json['amount']
             cart = Cart(user_id=input_json['user_id'],
                         item_id=input_json['item_id'],
                         amount=input_json['amount'])
             try:
                 DAOUtils.get_cart_dao().insert_cart(db, cart)
                 LogUtils.insert_user_log(db,
                                          user_id=input_json['user_id'],
                                          action='加入商品至購物車',
                                          remark=str(
                                              json.dumps(input_json)))
                 DAOUtils.commit(db)
                 return Flask(__name__).make_response(('', 201))
             except:
                 DAOUtils.rollback(db)
                 error_result = dict()
                 error_result['error'] = str(sys.exc_info())
                 return Flask(__name__).make_response(
                     (jsonify(error_result), 406))
             finally:
                 DAOUtils.close(db)
     else:
         DAOUtils.close(db)
         return Flask(__name__).make_response(('', 404))
Пример #5
0
 def add_user():
     """
     Add New User
     :return:Response
     """
     db = DAOUtils.get_db()
     input_json = request.get_json()
     user = User(account=input_json['account'],
                 pwd=input_json['password'],
                 name=input_json['name'],
                 credit=input_json['credit'])
     try:
         DAOUtils.get_user_dao().insert_user(db, user)
         DAOUtils.commit(db)
         return Flask(__name__).make_response(('', 201))
     except:
         DAOUtils.rollback(db)
         error_result = dict()
         error_result['error'] = str(sys.exc_info())
         return Flask(__name__).make_response((jsonify(error_result), 406))
     finally:
         DAOUtils.close(db)
Пример #6
0
    def login():
        """
        User Login
        :return:Response
        """
        db = DAOUtils.get_db()
        input_json = request.get_json()
        user = DAOUtils.get_user_dao().get_user(
            db,
            condition="ACCOUNT = '{ACCOUNT}' AND PASSWORD = '******'",
            ACCOUNT=input_json['account'],
            PASSWORD=md5(input_json['password'].encode('utf-8')).hexdigest())

        if user:
            result = dict()
            result['user_id'] = user.USER_ID
            result['name'] = user.NAME
            user.LAST_LOGIN_TIME = int(round(time.time() * 1000))
            DAOUtils.commit(db)
            DAOUtils.close(db)
            return Flask(__name__).make_response((jsonify(result), 200))
        else:
            DAOUtils.close(db)
            return Flask(__name__).make_response(('', 404))