You are on page 1of 34

HỌC VIỆN CÔNG NGHỆ BƯU CHÍNH VIỄN THÔNG

BÁO CÁO BÀI TẬP LỚN


MÔN KIẾN TRÚC VÀ THIẾT KẾ PHẦN MỀM

Giảng viên : Trần Đình Quế


Nhóm lớp : 54
Nhóm BTL : 1
Họ tên : Đào Bích Diệp – B19DCCN112

Hà Nội, tháng 5 năm 2023


MỤC LỤC
1. Use case tổng quan hệ thống .............................................................................................................. 2
2. Phân rã thành các microservice ......................................................................................................... 2
3. Use case cho từng chức năng .............................................................................................................. 2
3.1. Chức năng Đăng nhập : ................................................................................................................. 3
3.2. Chức năng Tìm kiếm sản phẩm .................................................................................................... 3
3.3. Chức năng Thêm vào giỏ hàng ..................................................................................................... 3
3.4. Chức năng Order ........................................................................................................................... 3
3.5. Chức năng Quản lý bán hàng ........................................................................................................ 4
3.6. Chức năng quản lý kho hàng......................................................................................................... 4
3.8. Chức năng Thống kế ..................................................................................................................... 5
3.9. năng quản lý khách hàng và nhân viên ......................................................................................... 6
4. Biểu đồ lớp thiết kế cho từng service................................................................................................. 6
4.1. User Service .................................................................................................................................. 6
4.2. Product Service: ............................................................................................................................ 7
4.3. CommentService: .......................................................................................................................... 7
4.4. ImageService................................................................................................................................. 7
5. Xây dựng lược đồ cơ sở dữ liệu cho từng service................................................................................. 8
6. Cài đặt hệ thống ................................................................................................................................ 10
7. Sử dụng kỹ thuật học máy để phân loại khách hàng hay tư vấn sản phẩm .................................... 26
7.1. Dữ liệu Nguồn dữ liệu : Customer Segment Classification | Kaggle ............................................... 26
7.2. Loading data..................................................................................................................................... 26
7.3. Data preliminary exploration ........................................................................................................... 27
7.4. Cleaning data ................................................................................................................................... 27
7.5. Features selection ............................................................................................................................. 28
7.7. Machine learning ............................................................................................................................. 31
7.8. Deep learning ................................................................................................................................... 32

1
1. Use case tổng quan hệ thống

2. Phân rã thành các microservice

3. Use case cho từng chức năng

2
3.1. Chức năng Đăng nhập :

3.2. Chức năng Tìm kiếm sản phẩm

3.3. Chức năng Thêm vào giỏ hàng

3.4. Chức năng Order

3
3.5. Chức năng Quản lý bán hàng

3.6. Chức năng quản lý kho hàng

4
3.7. Chức năng Đề xuất sản phẩm

3.8. Chức năng Thống kế

5
3.9. năng quản lý khách hàng và nhân viên

4. Biểu đồ lớp thiết kế cho từng service


4.1. User Service

6
4.2. Product Service:

4.3. CommentService:

4.4. ImageService

4.5. CartService

7
4.6. OrderService

4.7. Payment service

4.8. Shipment service

5. Xây dựng lược đồ cơ sở dữ liệu cho từng service


5.1. User Service

5.2. Product Service:

8
5.3. Comment Service :

5.4. Image service :

5.5. Cart service :

5.6. Order service

9
5.7. Payment Service :

5.8. Shipment service

6. Cài đặt hệ thống


6.1. User service
class user_registration(models.Model):
#fields of our table.
fname = models.CharField(max_length=50)
lname = models.CharField(max_length=50)
email = models.CharField(max_length=50)
mobile = models.CharField(max_length=12)
password = models.CharField(max_length=50)
address = models.CharField(max_length=200)

def __str__(self):

return '%s %s %s %s %s %s' % (self.fname, self.lname, self.


email, self.mobile, self.password, self.address)

@csrf_exempt

10
def user_login(request):
uname = request.POST.get("User Name")
password = request.POST.get("Password")
resp = {}
if uname and password:
respdata = user_validation(uname, password)
# If a user is valid then it gives success as a response.
if respdata == "Valid User":
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Welcome to Ecommerce website......'
elif respdata == "Invalid User":
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Invalid credentials.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'All fields are mandatory.'
return HttpResponse(json.dumps(resp), content_type='application/json')

@csrf_exempt
def registration_req(request):
### The Following are the input fields.
fname = request.POST.get("First Name")
lname = request.POST.get("Last Name")
email = request.POST.get("Email Id")
mobile = request.POST.get("Mobile Number")
password = request.POST.get("Password")
cnf_password = request.POST.get("Confirm Password")
address = request.POST.get("Address")
resp = {}
#### In this if statement, checking that all fields are available.
if fname and lname and email and mobile and password and cnf_password and
address:
if len(str(mobile)) == 10:
if password == cnf_password:
respdata = data_insert(fname, lname, email, mobile,
password, address)
### If it returns value then will show success.
if respdata:

11
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'User is registered Successfully.'
### If it is not returning any value then the show will fail.
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Unable to register user, Please try again.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Password and Confirm Password should be same.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Mobile Number should be 10 digit.'

else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'All fields are mandatory.'
return HttpResponse(json.dumps(resp), content_type='application/json')

def getAllUser(request) :
resp = {}
data = []
if request.method == 'GET':
users = user_registration.objects.all()
for user in users.values():
data.append(user)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')
6.2. Product service
class product_details(models.Model):
product_id = models.CharField(max_length=10)
product_category = models.CharField(max_length=50)
product_name = models.CharField(max_length=100)
availability = models.CharField(max_length=15)
price = models.CharField(max_length=10)

12
def __str__(self):
return '%s %s %s %s %s' % (self.product_id, self.product_category,
self.product_name, self.availability, self.price)

#lấy thông tin tất cả sản phẩm


@csrf_exempt
def get_product_data(request):
data = []
resp = {}
# This will fetch the data from the database.
prodata = product_details.objects.all()
for tbl_value in prodata.values():
data.append(tbl_value)
# If data is available then it returns the data.
if data:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Data is not available.'
return HttpResponse(json.dumps(resp), content_type='application/json')
def getProdata(id):
products = product_details.objects.filter(pk=id)
for p in products.values():
return p

def getProductByID(request, proid):


resp = {}
# This will fetch the data from the database.
print(proid)
data = getProdata(proid)
if data:
print(data)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'

13
resp['message'] = 'Data is not available.'
return HttpResponse(json.dumps(resp), content_type='application/json')

6.3. Comment service:


class product_comment(models.Model):
comment = models.TextField()
rating = models.IntegerField() #one to five star
uname = models.CharField(max_length=250)
product_id = models.CharField(max_length=250)
# orderitem_id = models.IntegerField()

@csrf_exempt
def add_comment(request):
uname = request.POST.get("User Name")
proid = request.POST.get("Product id")
comment = request.POST.get("Comment")
rating = request.POST.get("Rating") # one to five star
# user_id = models.IntegerField()
# product_id = models.IntegerField()

url = "http://127.0.0.1:8001/get_product_data/{}".format(proid)
d1 = {}
# d1["User Name"] = uname
# d1['Product Id'] = proid
# data = json.dumps(d1)
headers = {'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)
val1 = json.loads(response.content.decode('utf-8'))
product_name = val1.get("data").get("product_name")
respdata =comment_data_insert(comment=comment, product_id= proid, rating=rating,
uname=uname)
resp = {}
if respdata:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Product is ready to dispatch.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Failed to update shipment details.'
return HttpResponse(json.dumps(resp), content_type='application/json')

14
# if var1
#
@csrf_exempt
def show_product_comment(request):
uname = request.POST.get("User Name")
proid = request.POST.get("Product id")
comments = product_comment.objects.filter(product_id=proid, uname=uname)

data = []
resp = {}
# This will fetch the data from the database.
for tbl_value in comments.values():
data.append(tbl_value)
# If data is available then it returns the data.
if data:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Data is not available.'

6.4. Image service


class Image(models.Model):
url = models.CharField(max_length=200)
product_id=models.IntegerField()
description=models.CharField(max_length=200)

@csrf_exempt
def add_image(request, product_id):
prod_id = product_id
url = request.POST.get('URL')
des = request.POST.get('Description')
resp = {}
if url and des:
image = Image(product_id=prod_id, url=url, description=des)
if image:
image.save()
resp['status'] = 'Success'

15
resp['status_code'] = '200'
resp['message'] = f"Add image to product {prod_id} successfully"
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Data is invalid"
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"All fields are mandatory"
return HttpResponse(json.dumps(resp), content_type='application/json')

@csrf_exempt
def delete_image(request, product_id):
resp= {}
if request.method == 'DELETE':
images = Image.objects.filter(product_id=product_id)
for image in images:
image.delete()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = f"Delete image successfully"
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Failed method"
return HttpResponse(json.dumps(resp), content_type='application/json')

6.5. Cart service


class Cart(models.Model):
user_id = models.IntegerField()
product_id = models.IntegerField()
quantity = models.IntegerField()

#lấy thông tin tất cả giỏ hàng


def getAllCartItem(request):
resp = {}
data = []
list_cart_items = Cart.objects.all()
for cart_item in list_cart_items.values():
data.append(cart_item)

16
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')

#lấy thông tin các sản phẩm trong giỏ hàng theo người dùng
def getCartItemByUser(request,user_id):
resp = {}
data = []
list_cart_items = Cart.objects.filter(user_id=user_id)
if len(list_cart_items) == 0 :
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = f"User {user_id} haven't add any product yet"
else :
for cart_item in list_cart_items.values():
data.append(cart_item)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')

#lấy thông tin sản phẩn trong giỏ hàng theo id


def getCartItemById(request,cart_id):
resp = {}
cart = Cart.objects.filter(pk=cart_id).values()[0]
print(cart)
if cart :
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = cart
print(cart)
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Not found cart'
return HttpResponse(json.dumps(resp), content_type='application/json')

#thêm sản phẩm vào giỏ hàng


@csrf_exempt
def add_to_cart(request):
user_id = request.POST.get("User Id")

17
product_id = request.POST.get("Product Id")
quantity = request.POST.get("Quantity")
resp = {}
if user_id and product_id and quantity:
cart = Cart(user_id=user_id, product_id=product_id, quantity=quantity)
cart.save()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Add to cart successfully'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = "All fields are mandatory"
return HttpResponse(json.dumps(resp), content_type='application/json')
#cập nhật giỏ hàng
@csrf_exempt
def update_cart(request, cart_id):
if request.method == 'POST':
quantity = request.POST.get("Quantity")
resp = {}
list_cart = Cart.objects.filter(pk=cart_id).values()
if len(list_cart) > 0:
cart = list_cart[0]
cart_update = Cart(pk=cart_id, user_id=cart['user_id'],
product_id=cart['product_id'],
quantity=quantity)
cart = Cart.objects.get(pk=cart_id)
cart.delete()
cart_update.save()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Update to cart successfully'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Not found cart {cart_id}"
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = "Failed"
return HttpResponse(json.dumps(resp), content_type='application/json')

#xóa giỏ hàng

18
@csrf_exempt
def delete_cart(request, cart_id):
resp = {}
if request.method == 'DELETE':
cart = Cart.objects.get(pk=cart_id)
if cart:
print(cart)
cart.delete()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Delete cart successfully'
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Not found {cart_id}"
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Delete cart unsuccessfully'
return HttpResponse(json.dumps(resp), content_type='application/json')

6.6. Order service:


class Order(models.Model):
user_id = models.IntegerField(default=1)
cart_id = models.IntegerField(default=1)
total = models.FloatField(default=0.0)
shipment_id = models.IntegerField(default=1)

#lấy thông tin tất cả order


@csrf_exempt
def getAllOrder(request) :
resp = {}
data = []
if request.method == 'GET':
list_order = Order.objects.all()
for order in list_order.values():
data.append(order)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp, default=str), content_type='application/json')

19
#Tạo một order mới với input là User Id và Cart Id
@csrf_exempt
def create_order(request):
user_id = request.POST.get("User Id")
cart_id = request.POST.get("Cart Id")
print(cart_id)
total = getTotal(cart_id)
print(total)
shipment_id = request.POST.get("Shipment Id")
resp = {}
if user_id and cart_id and total and shipment_id:
order = Order(user_id=user_id, cart_id=cart_id, total=total,
shipment_id=shipment_id)
if order:
order.save()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Create a new order successfully'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Data is invalid'
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'All fields are mandatory'
return HttpResponse(json.dumps(resp, default=str), content_type='application/json')

#lấy tổng đơn hàng


def getTotal(card_id):
#get list product through api cart
url = 'http://127.0.0.1:8006/get_cart/{}'.format(card_id)
listItem = requests.get(url).json()['data']
product_id = listItem['product_id']
quantity = listItem['quantity']
url = 'http://127.0.0.1:8001/get_product_data/{}'.format(product_id)
product= requests.get(url).json()['data']
print(product)
price = float(product['price'])
return float(quantity)*price

#kiểm tra thông tin sản phẩm có tồn tại không

20
def checkProduct(id):
url = 'http://127.0.0.1:8001/get_product_data/'
listProduct = requests.get(url).json()
for product in listProduct['data']:
if product['product_id'] == id:
return True
return False

#xử lý nếu order thành công


def orderSuccess(request, order_id):
resp = {}
if request.method == 'DELETE':
order = Order.objects.get(pk=order_id)
print(order)
order.delete()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Update order successfully'
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Update order unsuccessfully'
return HttpResponse(json.dumps(resp), content_type='application/json')

#lấy thông tin chi tiết một order theo id


def getOrderDetail(request, order_id):
resp = {}
data = []
if request.method == 'GET':
orders = Order.objects.filter(pk=order_id)
for order in orders.values():
order
if order:
data.append(order)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Error'
return HttpResponse(json.dumps(resp, default=str), content_type='application/json')

21
6.7. Shipment service :
class shipment(models.Model):
### The following are the fields of our table.
fname = models.CharField(max_length=50)
lname = models.CharField(max_length=50)
email = models.CharField(max_length=50)
mobile = models.CharField(max_length=12)
address = models.CharField(max_length=200)
shipment_status = models.CharField(max_length=20)
order_id = models.IntegerField(max_length=10, default=1)
product_id=models.CharField(max_length=10, default="")
total = models.FloatField(default=1)

#đăng ký một thông tin shipment mới


@csrf_exempt
def shipment_reg_update(request):
if request.method == 'POST':
if 'application/json' in request.META['CONTENT_TYPE']:
val1 = json.loads(request.body)
### This is for reading the inputs from JSON.
fname = val1.get("First Name")
lname = val1.get("Last Name")
email = val1.get("Email Id")
mobile = val1.get("Mobile Number")
address = val1.get("Address")
shipment_status = "ready to dispatch"

resp = {}

respdata = ship_data_insert(fname, lname, email, mobile,


address, shipment_status)
### If it returns value then will show success.
if respdata:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Product is ready to dispatch.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Failed to update shipment details.'
return HttpResponse(json.dumps(resp), content_type='application/json')
22
def shipment_data(uname):
data = ship_obj.objects.filter(email = uname)
for val in data.values():
return val
### This function is used for getting the shipment status

#lấy thông tin trạng thái đơn hàng


@csrf_exempt
def shipment_status(request):
if request.method == 'POST':
if 'application/json' in request.META['CONTENT_TYPE']:
variable1 = json.loads(request.body)
### This is for reading the inputs from JSON.
uname = variable1.get("User Name")
resp = {}
### It will call the shipment_data function.
respdata = shipment_data(uname)
### If it returns value then will show success.
if respdata:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = respdata
### If it is not returning any value then it will showfailed response.
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'User data is not available.'
return HttpResponse(json.dumps(resp), content_type ='application/json')

#lấy thông tin tất cả shipment


def getAllShipment(request) :
resp = {}
data = []
if request.method == 'GET':
ship_objs = ship_obj.objects.all()
for ship in ship_objs.values():
data.append(ship)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')

23
6.8. Payment service
class payment_status(models.Model):
### The following are the fields of our table.
username = models.CharField(max_length=50)
order_id = models.IntegerField(default=1)
status = models.CharField(max_length=15)

#lấy thông tin thanh toán


@csrf_exempt
def get_payment(request ):
uname = request.POST.get("User Name")
orderid = request.POST.get("Order id")
resp = {}
if uname and orderid :
respdata = store_data(uname, orderid)
respdata2 = ship_update(uname, orderid)
### If it returns value then will show success.
if respdata:
order_update(orderid=orderid)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Transaction is completed.'
### If it is returning null value then it will show failed.
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Transaction is failed, Please try again.'
### If any mandatory field is missing then it will be through a
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'All fields are mandatory.'
return HttpResponse(json.dumps(resp), content_type='application/json')
### This function is created for getting the username and password.

#cập nhật danh sách order sau khi thanh toán thành công
def order_update(orderid):
url = "http://127.0.0.1:8005/update_order/{}".format(orderid)
response = requests.delete(url)

#lấy thông tin giao dịch


24
@csrf_exempt
def user_transaction_info(request):
# uname = request.POST.get("User Name")
resp = {}
if request.method == 'POST':
if 'application/json' in request.META['CONTENT_TYPE']:
val1 = json.loads(request.body)
uname = val1.get('User Name')
resp = {}
if uname:
## Calling the getting the user info.
respdata = get_transaction_details(uname)
if respdata:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = respdata
### If a user is not found then it give failed as response.
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'User Not Found.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Fields is mandatory.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Request type is not matched.'
return HttpResponse(json.dumps(resp), content_type='application/json')

def getAllPayment(request) :
resp = {}
data = []
if request.method == 'GET':
list_pay = paystat.objects.all()
for pay in list_pay.values():
data.append(pay)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')

25
7. Sử dụng kỹ thuật học máy để phân loại khách hàng hay tư vấn sản phẩm
7.1. Dữ liệu
Nguồn dữ liệu : Customer Segment Classification | Kaggle
Giải thích : Data trên từ thông tin của khách hàng để đưa ra các phân loại khách hàng
theo 4 phân loại :
Segment A : Khách hàng khá giả, tự chủ, có nguồn thu nhập cao và thời gian mua sẵm
cao (độ tuổi từ khoảng 40-50, có gia đình, công việc
Segment B : Khách hàng cũng có mức thu nhập ổn định hoặc cao, nhưng lại đang tập
trung vào sự nghiệp, điểm chi tiêu thấp ➔ mua hàng và ưu tiên công việc hơn các hoạt
động giải trí : 20-30t, kinh nghiệm 0-1 năm
Segment C : KH có mức thu nhập và chi tiêu trung bình, có xu hướng thận trọng và thực
tế khi mua hàng, học ưu tiên giá trị và chất lượng. : Người đã có gia đình, mức thu nhập
vừa phải, …
Segment D : KH có thu nhập và điểm chi tiêu thấp hơn, có xu hướng ít quan tâm đến mua
sắm và giải trí, ưu tiên các nhu yếu phẩm cơ bản và có thu nhập hạn chế : công nhân cao
tuổi, đã nghỉ hưu, có thu nhập ổn định…
7.2. Loading data

26
7.3. Data preliminary exploration

7.4. Cleaning data

27
7.5. Features selection

28
29
7.6. Modelling data

30
7.7. Machine learning

31
7.8. Deep learning

32
33

You might also like