Subversion Repositories SmartDukaan

Rev

Rev 25027 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21789 ashik.ali 1
package com.spice.profitmandi.service.tag;
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.model.ProfitMandiConstants;
5
import com.spice.profitmandi.common.model.TagListingModel;
33247 ranu 6
import com.spice.profitmandi.dao.entity.catalog.*;
21789 ashik.ali 7
import com.spice.profitmandi.dao.entity.dtr.Retailer;
24888 amit.gupta 8
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
22608 ashik.ali 9
import com.spice.profitmandi.dao.enumuration.catalog.TagType;
33247 ranu 10
import com.spice.profitmandi.dao.repository.catalog.*;
21789 ashik.ali 11
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
24888 amit.gupta 12
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
33247 ranu 13
import org.apache.commons.collections.CollectionUtils;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Qualifier;
16
import org.springframework.cache.annotation.Cacheable;
17
import org.springframework.stereotype.Component;
21789 ashik.ali 18
 
33247 ranu 19
import java.time.LocalDateTime;
20
import java.util.*;
21
import java.util.stream.Collectors;
24888 amit.gupta 22
 
21789 ashik.ali 23
@Component
24
public class TagServiceImpl implements TagService {
25
 
26
	@Autowired
22925 ashik.ali 27
	private TagRepository tagRepository;
21789 ashik.ali 28
 
29
	@Autowired
23781 ashik.ali 30
	@Qualifier("catalogItemRepository")
22925 ashik.ali 31
	private ItemRepository itemRepository;
21789 ashik.ali 32
 
33
	@Autowired
22925 ashik.ali 34
	private TagListingRepository tagListingRepository;
21789 ashik.ali 35
 
36
	@Autowired
22925 ashik.ali 37
	private RetailerTagRepository retailerTagRepository;
21789 ashik.ali 38
 
39
	@Autowired
24888 amit.gupta 40
	private VendorItemPricingRepository vendorItemPricingRepository;
41
 
42
	@Autowired
22925 ashik.ali 43
	private RetailerRepository retailerRepository;
21789 ashik.ali 44
 
45
	@Autowired
22925 ashik.ali 46
	private PinCodeTagRepository pinCodeTagRepository;
21789 ashik.ali 47
 
48
	@Override
49
	public void createTagListings(List<TagListingModel> tagListingModels) throws ProfitMandiBusinessException {
21801 ashik.ali 50
		if(tagListingModels.isEmpty()){
22925 ashik.ali 51
			throw new ProfitMandiBusinessException("tagListing", tagListingModels, "TGLSTNG_1001");
21801 ashik.ali 52
		}
21789 ashik.ali 53
		Set<Integer> tagIds = new HashSet<>();
54
		Set<Integer> itemIds = new HashSet<>();
55
		for(TagListingModel tagListingModel : tagListingModels){
56
			tagIds.add(tagListingModel.getTagId());
57
			itemIds.add(tagListingModel.getItemId());
58
		}
59
 
60
		List<Tag> tags = tagRepository.selectByIds(tagIds);
61
 
62
		if(tags.size() != tagIds.size()){
63
			Set<Integer> foundTagIds = new HashSet<>();
64
			for(Tag tag : tags){
65
				foundTagIds.add(tag.getId());
66
			}
67
			Collection<?> invalidTagIds = CollectionUtils.subtract(tagIds, foundTagIds);
22925 ashik.ali 68
			ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.TAG_ID, invalidTagIds, "TG_1001");
21789 ashik.ali 69
			throw profitMandiBusinessException;
70
		}
71
 
72
		List<Item> items = itemRepository.selectByIds(itemIds);
73
 
74
		if(items.size() != itemIds.size()){
75
			Set<Integer> foundItemIds = new HashSet<>();
76
			for(Item item : items){
77
				foundItemIds.add(item.getId());
78
			}
79
			Collection<?> invalidItemIds = CollectionUtils.subtract(itemIds, foundItemIds);
22925 ashik.ali 80
			ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, invalidItemIds, "TG_1002");
21789 ashik.ali 81
			throw profitMandiBusinessException;
82
		}
83
 
84
 
85
		for(TagListingModel tagListingModel : tagListingModels){
22563 amit.gupta 86
			this.createTagListing(tagListingModel.getTagId(), tagListingModel.getItemId(), tagListingModel.getSellingPrice(), tagListingModel.getMop(), 
87
					tagListingModel.getSupportPrice(), tagListingModel.getMaxDiscountPrice(), tagListingModel.getStartDate());
21789 ashik.ali 88
		}
89
	}
90
 
91
	@Override
22563 amit.gupta 92
	public void createTagListing(int tagId, int itemId, float sellingPrice, float mop, float priceSupport, float maxDiscount, LocalDateTime startDate) {
22220 amit.gupta 93
		List<TagListing> tatListingList = tagListingRepository.selectByItemIdsAndTagIds(new HashSet<Integer>(Arrays.asList(itemId)), new HashSet<Integer>(Arrays.asList(tagId)));
94
		if(tatListingList.size() == 0) {
95
			TagListing tagListing = new TagListing();
96
			tagListing.setActive(true);
97
			tagListing.setCreatedBy(1);
98
			tagListing.setItemId(itemId);
99
			tagListing.setSupportPrice(priceSupport);
100
			tagListing.setSellingPrice(sellingPrice);
101
			tagListing.setMop(mop);
102
			tagListing.setStartDate(startDate);
103
			tagListing.setTagId(tagId);
22563 amit.gupta 104
			tagListing.setMaxDiscountPrice(maxDiscount);
22220 amit.gupta 105
			tagListingRepository.persist(tagListing);
106
		} else {
107
			TagListing tl = tatListingList.get(0);
108
			tl.setSellingPrice(sellingPrice);
109
			tl.setMop(mop);
110
			tl.setSupportPrice(priceSupport);
22563 amit.gupta 111
			tl.setMaxDiscountPrice(maxDiscount);
22220 amit.gupta 112
			tagListingRepository.persist(tl);
113
 
114
		}
21789 ashik.ali 115
	}
116
	@Override
117
	public List<Retailer> getRetailersByTagId(int tagId) {
118
		List<RetailerTag> retailerTags = retailerTagRepository.selectByTagId(tagId);
21801 ashik.ali 119
		if(retailerTags.isEmpty()){
120
			return new ArrayList<>();
121
		}
21789 ashik.ali 122
		List<Integer> retailerIds = new ArrayList<>();
123
		for(RetailerTag retailerTag : retailerTags){
124
			retailerIds.add(retailerTag.getRetailerId());
125
		}
21801 ashik.ali 126
		return retailerRepository.selectByIds(retailerIds);
127
	}
128
 
129
	@Override
33247 ranu 130
    public List<Tag> getTagsByPinCode(String pinCode) throws ProfitMandiBusinessException {
21801 ashik.ali 131
		Set<Integer> tagIds = this.getTagIdsByPinCode(pinCode);
132
		if(tagIds.isEmpty()){
133
			return new ArrayList<>();
21791 amit.gupta 134
		}
21801 ashik.ali 135
		return tagRepository.selectByIds(tagIds);
21789 ashik.ali 136
	}
137
 
138
	@Override
21801 ashik.ali 139
	public Set<Integer> getTagIdsByPinCode(String pinCode){
21789 ashik.ali 140
		List<PinCodeTag> pinCodeTags = pinCodeTagRepository.selectByPinCode(pinCode);
141
		Set<Integer> tagIds = new HashSet<>();
142
		for(PinCodeTag pinCodeTag : pinCodeTags){
143
			tagIds.add(pinCodeTag.getTagId());
144
		}
21801 ashik.ali 145
		return tagIds;
21789 ashik.ali 146
	}
21801 ashik.ali 147
 
22014 ashik.ali 148
	@Cacheable(value = "pinCodeByTagId", cacheManager = "timeoutCacheManager")
21789 ashik.ali 149
	@Override
150
	public Set<String> getPinCodesByTagId(int tagId) {
151
		List<PinCodeTag> pinCodeTags = pinCodeTagRepository.selectByTagId(tagId);
152
		Set<String> pinCodes = new HashSet<>();
153
		for(PinCodeTag pinCodeTag : pinCodeTags){
154
			pinCodes.add(pinCodeTag.getPinCode());
155
		}
156
		return pinCodes;
157
	}
158
	@Override
33247 ranu 159
    public List<Tag> getTagsByRetailerId(int retailerId) throws ProfitMandiBusinessException {
21789 ashik.ali 160
		List<RetailerTag> retailerTags = retailerTagRepository.selectByRetailerId(retailerId);
21801 ashik.ali 161
		if(!retailerTags.isEmpty()){
162
			return new ArrayList<>();
163
		}
21789 ashik.ali 164
		Set<Integer> tagIds = new HashSet<>();
165
		for(RetailerTag retailerTag : retailerTags){
166
			tagIds.add(retailerTag.getTagId());
167
		}
168
		return tagRepository.selectByIds(tagIds);
169
	}
21801 ashik.ali 170
 
171
	@Override
33247 ranu 172
    public Set<Integer> getFofoTagIdsByPinCode(String pinCode) throws ProfitMandiBusinessException {
21801 ashik.ali 173
		List<Tag> pinCodeTags = this.getTagsByPinCode(pinCode);
174
		Set<Integer> pinCodeTagIds = new HashSet<>();
175
		for(Tag tag : pinCodeTags){
176
			if(tag.getType() == TagType.FOFO){
177
				pinCodeTagIds.add(tag.getId());
178
			}
179
		}
180
		return pinCodeTagIds;
181
	}
182
	private Map<Integer, Map<String, Object>> createTagsMap(List<Tag> tags){
183
		Map<Integer, Map<String, Object>> tagsMap = new HashMap<>();
184
		for(Tag tag: tags){
185
			if(!tagsMap.containsKey(tag.getId())){
186
				Map<String, Object> tagMap = new HashMap<>();
187
				tagMap.put(ProfitMandiConstants.LABEL, tag.getLabel());
188
				tagMap.put(ProfitMandiConstants.CREATED_BY, tag.getCreatedBy());
189
				tagMap.put(ProfitMandiConstants.CREATE_TIMESTAMP, tag.getCreateTimestamp());
190
				tagMap.put(ProfitMandiConstants.DESCRIPTION, tag.getDescription());
191
				tagMap.put(ProfitMandiConstants.TYPE, tag.getType());
192
				tagMap.put(ProfitMandiConstants.UPDATE_TIMESTAMP, tag.getUpdateTimestamp());
193
				tagsMap.put(tag.getId(), tagMap);
194
			}
195
		}
196
		return tagsMap;
197
	}
198
 
199
	private Map<Integer, Map<String, Object>> createItemsMap(List<Item> items){
200
		Map<Integer, Map<String, Object>> itemsMap = new HashMap<>();
201
		for(Item item: items){
202
			if(!itemsMap.containsKey(item.getId())){
203
				Map<String, Object> itemMap = new HashMap<>();
204
				itemMap.put(ProfitMandiConstants.BRAND, item.getBrand());
205
				itemMap.put(ProfitMandiConstants.MODEL_NAME, item.getModelName());
206
				itemMap.put(ProfitMandiConstants.MODEL_NUMBER, item.getModelNumber());
207
				itemMap.put(ProfitMandiConstants.COLOR, item.getColor());
208
				itemsMap.put(item.getId(), itemMap);
209
			}
210
		}
211
		return itemsMap;
212
	}
213
	@Override
33247 ranu 214
    public List<Map<String, Object>> getTagListingDetail(int tagId) throws ProfitMandiBusinessException {
21801 ashik.ali 215
		List<TagListing> tagListings = tagListingRepository.selectByTagId(tagId);
216
		List<Map<String, Object>> tagListingsList = new ArrayList<>();
217
		if(tagListings.isEmpty()){
218
			return new ArrayList<>();
219
		}
220
		Set<Integer> tagIds = new HashSet<>();
221
		Set<Integer> itemIds = new HashSet<>();
222
 
223
		for(TagListing tagListing : tagListings){
224
			tagIds.add(tagListing.getTagId());
225
			itemIds.add(tagListing.getItemId());
226
		}
227
		List<Tag> tags = tagRepository.selectByIds(tagIds);
24888 amit.gupta 228
		Map<Integer, List<VendorItemPricing>> itemVipsMap = vendorItemPricingRepository.selectAll(new ArrayList<>(itemIds)).stream().collect(
229
				Collectors.groupingBy(x->x.getItemId(), Collectors.toList()));
21801 ashik.ali 230
		Map<Integer, Map<String, Object>> tagsMap = this.createTagsMap(tags);
231
 
232
		for(TagListing tagListing : tagListings){
233
			Map<String, Object> tagListingMap = new HashMap<>();
234
			tagListingMap.put(ProfitMandiConstants.CREATED_BY, tagListing.getCreatedBy());
235
			tagListingMap.put(ProfitMandiConstants.CREATE_TIMESTAMP, tagListing.getCreatedTimestamp());
24889 amit.gupta 236
			if(itemVipsMap.get(tagListing.getItemId())!=null) {
237
				tagListingMap.put("TP", itemVipsMap.get(tagListing.getItemId()).get(0).getTp());
238
			} else {
239
				tagListingMap.put("TP", null);
240
			}
21801 ashik.ali 241
			tagListingMap.put(ProfitMandiConstants.SELLING_PRICE, tagListing.getSellingPrice());
22209 amit.gupta 242
			tagListingMap.put(ProfitMandiConstants.MOP, tagListing.getMop());
21801 ashik.ali 243
			tagListingMap.put(ProfitMandiConstants.START_DATE, tagListing.getStartDate());
244
			tagListingMap.put(ProfitMandiConstants.SUPPORT_PRICE, tagListing.getSupportPrice());
22563 amit.gupta 245
			tagListingMap.put(ProfitMandiConstants.MAX_DISCOUNT, tagListing.getMaxDiscountPrice());
21801 ashik.ali 246
			tagListingMap.put(ProfitMandiConstants.ITEM_ID, tagListing.getItemId());
247
			tagListingMap.put(ProfitMandiConstants.TAG_ID, tagListing.getTagId());
248
			tagListingMap.put(ProfitMandiConstants.TAG, tagsMap.get(tagListing.getTagId()));
25027 amit.gupta 249
			tagListingMap.put(ProfitMandiConstants.ACTIVE, String.valueOf(tagListing.isActive()));
21801 ashik.ali 250
			tagListingsList.add(tagListingMap);
251
		}
252
		return tagListingsList;
253
	}
21867 ashik.ali 254
	private Set<Integer> toTagIds(List<Tag> tags){
255
		if(tags.isEmpty()){
256
			return new HashSet<>();
257
		}
258
		Set<Integer> tagIds = new HashSet<>();
259
		for(Tag tag : tags){
260
			tagIds.add(tag.getId());
261
		}
262
		return tagIds;
263
	}
22009 ashik.ali 264
	@Cacheable(value = "pinCodeNegativesTagIds", cacheManager = "timeoutCacheManager")
21867 ashik.ali 265
	@Override
266
	public Set<Integer> getPinCodeNegativeTagIds() {
22009 ashik.ali 267
		List<Tag> tags = tagRepository.selectAllByPinAll(true);
21867 ashik.ali 268
		return this.toTagIds(tags);
269
	}
22009 ashik.ali 270
	@Cacheable(value = "pinCodePositiveTagIds", cacheManager = "timeoutCacheManager")
21867 ashik.ali 271
	@Override
272
	public Set<Integer> getPinCodePositiveTagIds() {
22009 ashik.ali 273
		List<Tag> tags = tagRepository.selectAllByPinAll(false);
21867 ashik.ali 274
		return this.toTagIds(tags);
275
	}
22009 ashik.ali 276
	@Cacheable(value = "userNegativesTagIds", cacheManager = "timeoutCacheManager")
21867 ashik.ali 277
	@Override
278
	public Set<Integer> getUserNegativeTagIds() {
22009 ashik.ali 279
		List<Tag> tags = tagRepository.selectAllByUserAll(true);
21867 ashik.ali 280
		return this.toTagIds(tags);
281
	}
22009 ashik.ali 282
	@Cacheable(value = "userPositiveTagIds", cacheManager = "timeoutCacheManager")
21867 ashik.ali 283
	@Override
284
	public Set<Integer> getUserPositiveTagIds() {
22009 ashik.ali 285
		List<Tag> tags = tagRepository.selectAllByUserAll(false);
21867 ashik.ali 286
		return this.toTagIds(tags);
287
	}
22009 ashik.ali 288
	@Override
33247 ranu 289
    public Set<Integer> getUserTagIdsByTagIds(Set<Integer> tagIds) throws ProfitMandiBusinessException {
22009 ashik.ali 290
		List<RetailerTag> retailerTags = retailerTagRepository.selectByTagIds(tagIds);
291
		Set<Integer> retailerTagIds = new HashSet<>();
292
		if(retailerTags.isEmpty()){
293
			return retailerTagIds;
294
		}
295
		for(RetailerTag retailerTag : retailerTags){
296
			retailerTagIds.add(retailerTag.getTagId());
297
		}
298
		return retailerTagIds;
299
	}
21789 ashik.ali 300
}