-
Notifications
You must be signed in to change notification settings - Fork 2
/
Item.java
158 lines (134 loc) · 4.9 KB
/
Item.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.allan.shoppingMall.domains.item.domain.item;
import com.allan.shoppingMall.common.domain.BaseEntity;
import com.allan.shoppingMall.common.exception.ErrorCode;
import com.allan.shoppingMall.common.exception.order.OrderFailException;
import com.allan.shoppingMall.domains.category.domain.CategoryItem;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype")
@Table(name = "items")
@NoArgsConstructor
@Slf4j
public class Item extends BaseEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemId;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Long price;
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ItemImage> itemImages = new ArrayList<>();
@Enumerated(value = EnumType.STRING)
@Column(name="item_color") // 나중에 nullable 처리 필요.
private Color color;
@Column(nullable = false, name = "stock_quantity")
private Long stockQuantity = 0l;
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ItemFabric> itemFabrics = new ArrayList<>();
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ItemDetail> itemDetails = new ArrayList<>();
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ItemSize> itemSizes = new ArrayList<>();
@OneToMany(mappedBy = "item", cascade = {CascadeType.REMOVE, CascadeType.PERSIST}, orphanRemoval = true)
private List<CategoryItem> categoryItems = new ArrayList<>();
public Item(String name, Long price, Color color) {
this.name = name;
this.price = price;
this.color = color;
}
/**
* 양방향 매핑을 위한 연관 관계 편의 메소드.
* ClothesFabric 등록함.
* @param itemImages
*/
public void changeItemImages(List<ItemImage> itemImages){
for(ItemImage itemImage : itemImages){
this.itemImages.add(itemImage);
itemImage.changeItem(this);
}
}
/**
* 재고량 차감 메소드.
* @param stockQuantity
*/
public void subtractStockQuantity(Long stockQuantity){
if(this.stockQuantity < stockQuantity) {
throw new OrderFailException(ErrorCode.ITEM_STOCK_QUANTITY_EXCEEDED.getMessage(), ErrorCode.ITEM_STOCK_QUANTITY_EXCEEDED);
}
this.stockQuantity -= stockQuantity;
}
/**
* 재고량 증가 메소드.
* @param
* @return
*/
public void addStockQuantity(Long stockQuantity){
this.stockQuantity += stockQuantity;
}
/**
* 양방향 매핑을 위한 연관 관계 편의 메소드.
* itemFabric 등록함.
* @param itemFabrics
*/
public void changeItemFabrics(List<ItemFabric> itemFabrics){
for(ItemFabric itemFabric : itemFabrics){
this.itemFabrics.add(itemFabric);
itemFabric.changeItem(this);
}
}
/**
* 양방향 매핑을 위한 연관 관계 편의 메소드.
* itemDetail 등록함.
* @param itemDetails
*/
public void changeItemDetails(List<ItemDetail> itemDetails){
for(ItemDetail itemDetail : itemDetails){
this.itemDetails.add(itemDetail);
itemDetail.changeItem(this);
}
}
/**
* 양방향 매핑을 위한 연관 관계 편의 메소드.
* itemSize 등록함.
* 사이즈별로 수량을 관리 해야 하기 때문에 ItemSize에 모든 수량이 곧 clothes 상품의 재고량이 된다.
* @param itemSizes
*/
public void changeItemSizes(List<ItemSize> itemSizes){
long totalQuantity = 0l;
for(ItemSize itemSize : itemSizes){
totalQuantity += itemSize.getStockQuantity();
this.itemSizes.add(itemSize);
itemSize.changeItem(this);
}
addStockQuantity(totalQuantity); // 재고량을 증가하기 위한 메소드.
}
/**
* 양방향 매핑을 위한 연관 관계 편의 메소드.
* 카테고리 상품 도메인 추가를 위한 메소드 입니다.
* @param categoryItems
*/
public void changeCategoryItems(List<CategoryItem> categoryItems){
for(CategoryItem categoryItem : categoryItems){
if(!this.categoryItems.contains(categoryItem)){
this.categoryItems.add(categoryItem);
categoryItem.changeitem(this);
}
}
}
public void changeName(String name){
this.name = name;
}
public void changePrice(Long price){
this.price = price;
}
public void changeColor(Color color){
this.color = color;
}
}