| 22653 |
ashik.ali |
1 |
package com.spice.profitmandi.service.scheme;
|
|
|
2 |
|
|
|
3 |
import java.util.HashMap;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
import java.util.Map;
|
|
|
6 |
import java.util.Set;
|
|
|
7 |
import java.util.function.Predicate;
|
|
|
8 |
|
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
10 |
import org.springframework.stereotype.Component;
|
|
|
11 |
|
|
|
12 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
13 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
14 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
|
|
15 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
|
|
16 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
|
|
17 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
|
|
18 |
import com.spice.profitmandi.dao.entity.fofo.SchemeIn;
|
|
|
19 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInId;
|
|
|
20 |
import com.spice.profitmandi.dao.entity.fofo.SchemeOut;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.fofo.SchemeOutId;
|
|
|
22 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeAmountType;
|
|
|
23 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
|
|
24 |
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
|
|
26 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
|
|
28 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInRepository;
|
|
|
29 |
import com.spice.profitmandi.dao.repository.fofo.SchemeOutRepository;
|
|
|
30 |
|
|
|
31 |
@Component
|
|
|
32 |
public class SchemeServiceImpl implements SchemeService {
|
|
|
33 |
|
|
|
34 |
@Autowired
|
|
|
35 |
private InventoryItemRepository inventoryItemRepository;
|
|
|
36 |
|
|
|
37 |
@Autowired
|
|
|
38 |
private FofoLineItemRepository fofoLineItemRepository;
|
|
|
39 |
|
|
|
40 |
@Autowired
|
|
|
41 |
private SchemeRepository schemeRepository;
|
|
|
42 |
|
|
|
43 |
@Autowired
|
|
|
44 |
private RetailerSchemeRepository retailerSchemeRepository;
|
|
|
45 |
|
|
|
46 |
@Autowired
|
|
|
47 |
private SchemeInRepository schemeInRepository;
|
|
|
48 |
|
|
|
49 |
@Autowired
|
|
|
50 |
private SchemeOutRepository schemeOutRepository;
|
|
|
51 |
|
|
|
52 |
@Override
|
|
|
53 |
public void saveScheme(Scheme scheme, Set<Integer> retailerIds) throws ProfitMandiBusinessException {
|
|
|
54 |
if(scheme.getExpireTimestamp() != null){
|
|
|
55 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "");
|
|
|
56 |
}
|
|
|
57 |
if(!scheme.isAll() && retailerIds.isEmpty()){
|
|
|
58 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailerIds, "");
|
|
|
59 |
}
|
|
|
60 |
if(scheme.getName().isEmpty()){
|
|
|
61 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, scheme.getName(), "");
|
|
|
62 |
}
|
|
|
63 |
if(scheme.getAmount() <= 0){
|
|
|
64 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, scheme.getAmount(), "");
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if(scheme.getAmountType() == SchemeAmountType.PERCENTAGE && scheme.getAmount() > 100){
|
|
|
68 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, scheme.getAmount(), "");
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if(scheme.getStartDate() == null){
|
|
|
72 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, scheme.getStartDate(), "");
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
if(scheme.getEndDate() == null){
|
|
|
76 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, scheme.getEndDate(), "");
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if(scheme.getStartDate().isAfter(scheme.getEndDate())){
|
|
|
80 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE, scheme.getStartDate() + ", " + scheme.getEndDate(), "");
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
if(scheme.getEndDate().isAfter(scheme.getEndDate())){
|
|
|
84 |
|
|
|
85 |
}
|
|
|
86 |
schemeRepository.persist(scheme);
|
|
|
87 |
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
@Override
|
|
|
91 |
public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
92 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
93 |
if(scheme.getActiveTimestamp() != null){
|
|
|
94 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(), "");
|
|
|
95 |
}
|
|
|
96 |
if(scheme.getExpireTimestamp() != null){
|
|
|
97 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "");
|
|
|
98 |
}
|
|
|
99 |
schemeRepository.updateActiveTimestampById(schemeId);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
@Override
|
|
|
103 |
public void expireSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
104 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
105 |
if(scheme.getActiveTimestamp() == null){
|
|
|
106 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(), "");
|
|
|
107 |
}
|
|
|
108 |
if(scheme.getExpireTimestamp() != null){
|
|
|
109 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "");
|
|
|
110 |
}
|
|
|
111 |
schemeRepository.updateExpireTimestampById(schemeId);
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
@Override
|
|
|
116 |
public void processSchemeIn(int inventoryItemId) throws ProfitMandiBusinessException {
|
|
|
117 |
InventoryItem inventoryItem = inventoryItemRepository.selectById(inventoryItemId);
|
|
|
118 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN);
|
|
|
119 |
Map<Integer, Scheme> notAllSchemeIdSchemeMap = new HashMap<>();
|
|
|
120 |
Map<Integer, Scheme> allSchemeIdSchemeMap = new HashMap<>();
|
|
|
121 |
|
|
|
122 |
for(Scheme scheme : schemes){
|
|
|
123 |
if(!scheme.isAll()){
|
|
|
124 |
notAllSchemeIdSchemeMap.put(scheme.getId(), scheme);
|
|
|
125 |
}else{
|
|
|
126 |
allSchemeIdSchemeMap.put(scheme.getId(), scheme);
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
for(Map.Entry<Integer, Scheme> entry : allSchemeIdSchemeMap.entrySet()){
|
|
|
130 |
this.createSchemeIn(entry.getValue(), inventoryItem.getUnitPrice(), inventoryItemId, entry.getKey());
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
if(!notAllSchemeIdSchemeMap.isEmpty()){
|
|
|
134 |
List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(notAllSchemeIdSchemeMap.keySet());
|
|
|
135 |
for(Map.Entry<Integer, Scheme> entry : notAllSchemeIdSchemeMap.entrySet()){
|
|
|
136 |
Predicate<RetailerScheme> retailerIdSchemeIdPredicate = new Predicate<RetailerScheme>()
|
|
|
137 |
{
|
|
|
138 |
@Override
|
|
|
139 |
public boolean test(RetailerScheme retailerScheme) {
|
|
|
140 |
if(retailerScheme.getRetailerId() == inventoryItem.getFofoId() && retailerScheme.getSchemeId() == entry.getKey()){
|
|
|
141 |
return true;
|
|
|
142 |
}
|
|
|
143 |
return false;
|
|
|
144 |
}
|
|
|
145 |
};
|
|
|
146 |
if(!retailerSchemes.isEmpty() && retailerSchemes.stream().anyMatch(retailerIdSchemeIdPredicate)){
|
|
|
147 |
this.createSchemeIn(entry.getValue(), inventoryItem.getUnitPrice(), inventoryItemId, entry.getKey());
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
private void createSchemeIn(Scheme scheme, float unitPrice, int inventoryItemId, int schemeId){
|
|
|
154 |
float amount = 0;
|
|
|
155 |
if(scheme.getAmountType() == SchemeAmountType.PERCENTAGE){
|
|
|
156 |
amount = unitPrice * scheme.getAmount() / 100;
|
|
|
157 |
}else{
|
|
|
158 |
amount = scheme.getAmount();
|
|
|
159 |
}
|
|
|
160 |
SchemeIn schemeIn = new SchemeIn();
|
|
|
161 |
SchemeInId schemeInId = new SchemeInId();
|
|
|
162 |
schemeInId.setInventoryItemId(inventoryItemId);
|
|
|
163 |
schemeInId.setSchemeId(schemeId);
|
|
|
164 |
schemeIn.setId(schemeInId);
|
|
|
165 |
schemeIn.setAmount(amount);
|
|
|
166 |
schemeInRepository.persist(schemeIn);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
private void createSchemeOut(Scheme scheme, float unitPrice, int fofoLineItemId, int schemeId){
|
|
|
170 |
float amount = 0;
|
|
|
171 |
if(scheme.getAmountType() == SchemeAmountType.PERCENTAGE){
|
|
|
172 |
amount = unitPrice * scheme.getAmount() / 100;
|
|
|
173 |
}else{
|
|
|
174 |
amount = scheme.getAmount();
|
|
|
175 |
}
|
|
|
176 |
SchemeOut schemeOut = new SchemeOut();
|
|
|
177 |
SchemeOutId schemeOutId = new SchemeOutId();
|
|
|
178 |
schemeOutId.setFofoLineItemId(fofoLineItemId);
|
|
|
179 |
schemeOutId.setSchemeId(schemeId);
|
|
|
180 |
schemeOut.setId(schemeOutId);
|
|
|
181 |
schemeOut.setAmount(amount);
|
|
|
182 |
schemeOutRepository.persist(schemeOut);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
@Override
|
|
|
186 |
public void processSchemeOut(int fofoLineItemId) throws ProfitMandiBusinessException {
|
|
|
187 |
FofoLineItem fofoLineItem = fofoLineItemRepository.selectById(fofoLineItemId);
|
|
|
188 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.OUT);
|
|
|
189 |
Map<Integer, Scheme> notAllSchemeSchemeMap = new HashMap<>();
|
|
|
190 |
Map<Integer, Scheme> allSchemeSchemeMap = new HashMap<>();
|
|
|
191 |
|
|
|
192 |
for(Scheme scheme : schemes){
|
|
|
193 |
if(!scheme.isAll()){
|
|
|
194 |
notAllSchemeSchemeMap.put(scheme.getId(), scheme);
|
|
|
195 |
}else{
|
|
|
196 |
allSchemeSchemeMap.put(scheme.getId(), scheme);
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
for(Map.Entry<Integer, Scheme> entry : allSchemeSchemeMap.entrySet()){
|
|
|
200 |
this.createSchemeOut(entry.getValue(), fofoLineItem.getSellingPrice(), fofoLineItemId, entry.getKey());
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
if(!notAllSchemeSchemeMap.isEmpty()){
|
|
|
204 |
List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(notAllSchemeSchemeMap.keySet());
|
|
|
205 |
for(Map.Entry<Integer, Scheme> entry : notAllSchemeSchemeMap.entrySet()){
|
|
|
206 |
Predicate<RetailerScheme> retailerIdSchemeIdPredicate = new Predicate<RetailerScheme>()
|
|
|
207 |
{
|
|
|
208 |
@Override
|
|
|
209 |
public boolean test(RetailerScheme retailerScheme) {
|
|
|
210 |
if(retailerScheme.getRetailerId() == fofoLineItem.getOrder().getFofoId() && retailerScheme.getSchemeId() == entry.getKey()){
|
|
|
211 |
return true;
|
|
|
212 |
}
|
|
|
213 |
return false;
|
|
|
214 |
}
|
|
|
215 |
};
|
|
|
216 |
if(!retailerSchemes.isEmpty() && retailerSchemes.stream().anyMatch(retailerIdSchemeIdPredicate)){
|
|
|
217 |
this.createSchemeIn(entry.getValue(), fofoLineItem.getSellingPrice(), fofoLineItemId, entry.getKey());
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
}
|