Exemple #1
0
def add_customer(customer: Customer):
    sql_text = '''
                INSERT INTO User(userName, carId, carModel, carPhone, createdTime)
                VALUES ('{}', '{}', '{}', '{}', '{}')''' \
        .format(customer.username(), customer.car_id(), customer.car_model(), customer.phone(), customer.create_time())
    return execute(sql_text)
Exemple #2
0
    def user(self, keyword, get_data):
        try:
            if self.request.method == 'POST':
                if keyword == "add":
                    try:
                        username = get_data.pop("username")
                        phone = get_data.pop("carPhone")
                        car_model = get_data.pop("carModel")
                        car_id = get_data.pop("carId")
                    except Exception as e:
                        print(e)
                        raise ApiException(ErrorCode.ParameterMiss)

                    customer = Customer()
                    customer.username(username)
                    customer.car_model(car_model)
                    customer.phone(phone)
                    customer.car_id(car_id)
                    customer.create_time(time_utils.get_now())

                    temp_user = customer_handler.get_customer_by_key(
                        "carId", car_id)
                    if temp_user:
                        try:
                            customer_handler.update_customer_by_car_id(
                                customer)
                        except Exception as update_exception:
                            print(update_exception)
                            raise ApiException(ErrorCode.ParameterError)
                        customer_id = temp_user[0][0]
                    else:
                        try:
                            customer_id = customer_handler.add_customer(
                                customer)
                        except Exception as insert_exception:
                            print(insert_exception)
                            raise ApiException(ErrorCode.UserMore)

                    return set_return_dicts({"userId": customer_id})

                else:
                    raise ApiException(ErrorCode.ErrorRequest)

            elif self.request.method == "GET":

                if not self.storeId:
                    raise ApiException(ErrorCode.PCError)

                if keyword == "find":
                    key = get_data.get("key", "")
                    if key not in ["carPhone", "carId"]:
                        raise ApiException(ErrorCode.ParameterError)

                    value = get_data.get("value", "")

                    if self.connect:
                        temp = SocketServer("user {} {} {}".format(
                            self.storeId, key, value))
                        if not temp:
                            temp = self.find_customer(key, value)

                    else:
                        temp = self.find_customer(key, value)
                    result = []
                    key_temp = []
                    if temp == 'restart':
                        raise ApiException(ErrorCode.ReStartPC)
                    else:
                        for data in temp:
                            key = data.get("phone") + data.get(
                                "carId") + data.get("carModel") + data.get(
                                    "userName")
                            if key in key_temp:
                                pass
                            else:
                                result.append(data)
                                key_temp.append(key)

                    return set_return_dicts(result)

                elif keyword == 'order':
                    car_id = get_data.get("carId", "")
                    phone = get_data.get("carPhone", "")

                    if not car_id:
                        raise ApiException(ErrorCode.ParameterMiss)

                    if self.connect:
                        all_order_money = 0.0
                        result = SocketServer("userorder {} {} {}".format(
                            self.storeId, car_id, phone))
                        if result:
                            order_number = len(result)
                            for data in result:
                                all_order_money += data.get("totalPrice")
                        else:
                            result, order_number, all_order_money = self.get_order(
                                car_id)

                    else:
                        result, order_number, all_order_money = self.get_order(
                            car_id)
                    if result == 'restart':
                        raise ApiException(ErrorCode.ReStartPC)
                        # result = []
                    else:
                        print('sort')
                        try:
                            result.sort(key=lambda obj: obj.get('createdTime'),
                                        reverse=True)
                        except Exception as sortE:
                            print(sortE)
                    try:
                        for data in result:
                            print(data)
                            msg = data.get("msg")
                            for msg_data in msg:
                                temp = {}
                                attribute = msg_data.get("attribute")
                                for k, v in attribute.items():
                                    if v != "" and v != "-":
                                        temp[k] = v
                                msg_data['attribute'] = temp
                    except Exception as forException:
                        print(forException)

                    send_msg = {
                        'orderMsg': result,
                        'orderNumber': order_number,
                        'allOrderMoney': all_order_money
                    }
                    print(send_msg)
                    return set_return_dicts(send_msg)

                else:
                    raise ApiException(ErrorCode.ErrorRequest)

        except ApiException as e:
            return set_return_dicts(forWorker=e.error_result['forWorker'],
                                    code=e.error_result['errorCode'],
                                    forUser=e.error_result['forUser'])