Subversion Repositories SmartDukaan

Rev

Rev 22859 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.scheme;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
import com.spice.profitmandi.dao.entity.catalog.Scheme;
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
import com.spice.profitmandi.dao.entity.fofo.SchemeIn;
import com.spice.profitmandi.dao.entity.fofo.SchemeInId;
import com.spice.profitmandi.dao.entity.fofo.SchemeOut;
import com.spice.profitmandi.dao.entity.fofo.SchemeOutId;
import com.spice.profitmandi.dao.enumuration.catalog.SchemeAmountType;
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
import com.spice.profitmandi.dao.repository.fofo.SchemeInRepository;
import com.spice.profitmandi.dao.repository.fofo.SchemeOutRepository;

@Component
public class SchemeServiceImpl implements SchemeService {

        @Autowired
        private InventoryItemRepository inventoryItemRepository;
        
        @Autowired
        private FofoLineItemRepository fofoLineItemRepository;
        
        @Autowired
        private SchemeRepository schemeRepository;
        
        @Autowired
        private RetailerSchemeRepository retailerSchemeRepository;
        
        @Autowired
        private SchemeInRepository schemeInRepository;
        
        @Autowired
        private SchemeOutRepository schemeOutRepository;
        
        @Override
        public void saveScheme(Scheme scheme, Set<Integer> retailerIds) throws ProfitMandiBusinessException {
                if(scheme.getExpireTimestamp() != null){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "");
                }
                if(!scheme.isAll() && retailerIds.isEmpty()){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailerIds, "");
                }
                if(scheme.getName().isEmpty()){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, scheme.getName(), "");
                }
                if(scheme.getAmount() <= 0){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, scheme.getAmount(), "");
                }
                
                if(scheme.getAmountType() == SchemeAmountType.PERCENTAGE && scheme.getAmount() > 100){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, scheme.getAmount(), "");
                }
                
                if(scheme.getStartDate() == null){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, scheme.getStartDate(), "");
                }
                
                if(scheme.getEndDate() == null){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, scheme.getEndDate(), "");
                }
                
                if(scheme.getStartDate().isAfter(scheme.getEndDate())){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE, scheme.getStartDate() + ", " + scheme.getEndDate(), "");
                }
                
                if(scheme.getEndDate().isAfter(scheme.getEndDate())){
                        
                }
                schemeRepository.persist(scheme);

        }

        @Override
        public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
                Scheme scheme = schemeRepository.selectById(schemeId);
                if(scheme.getActiveTimestamp() != null){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(), "");
                }
                if(scheme.getExpireTimestamp() != null){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "");
                }
                schemeRepository.updateActiveTimestampById(schemeId);
        }

        @Override
        public void expireSchemeById(int schemeId) throws ProfitMandiBusinessException {
                Scheme scheme = schemeRepository.selectById(schemeId);
                if(scheme.getActiveTimestamp() == null){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(), "");
                }
                if(scheme.getExpireTimestamp() != null){
                        throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "");
                }
                schemeRepository.updateExpireTimestampById(schemeId);

        }

        @Override
        public void processSchemeIn(int inventoryItemId) throws ProfitMandiBusinessException {
                InventoryItem inventoryItem = inventoryItemRepository.selectById(inventoryItemId);
                List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN);
                Map<Integer, Scheme> notAllSchemeIdSchemeMap = new HashMap<>();
                Map<Integer, Scheme> allSchemeIdSchemeMap = new HashMap<>();
                
                for(Scheme scheme : schemes){
                        if(!scheme.isAll()){
                                notAllSchemeIdSchemeMap.put(scheme.getId(), scheme);
                        }else{
                                allSchemeIdSchemeMap.put(scheme.getId(), scheme);
                        }
                }
                for(Map.Entry<Integer, Scheme> entry : allSchemeIdSchemeMap.entrySet()){
                        this.createSchemeIn(entry.getValue(), inventoryItem.getUnitPrice(), inventoryItemId, entry.getKey());
                }
                
                if(!notAllSchemeIdSchemeMap.isEmpty()){
                        List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(notAllSchemeIdSchemeMap.keySet());
                        for(Map.Entry<Integer, Scheme> entry : notAllSchemeIdSchemeMap.entrySet()){
                                Predicate<RetailerScheme> retailerIdSchemeIdPredicate = new Predicate<RetailerScheme>() 
                                {
                                        @Override
                                        public boolean test(RetailerScheme retailerScheme) {
                                                if(retailerScheme.getRetailerId() == inventoryItem.getFofoId() && retailerScheme.getSchemeId() == entry.getKey()){
                                                        return true;
                                                }
                                                return false;
                                        }
                                };
                                if(!retailerSchemes.isEmpty() && retailerSchemes.stream().anyMatch(retailerIdSchemeIdPredicate)){
                                        this.createSchemeIn(entry.getValue(), inventoryItem.getUnitPrice(), inventoryItemId, entry.getKey());
                                }
                        }
                }
        }
        
        private void createSchemeIn(Scheme scheme, float unitPrice, int inventoryItemId, int schemeId){
                float amount = 0;
                if(scheme.getAmountType() == SchemeAmountType.PERCENTAGE){
                        amount = unitPrice * scheme.getAmount() / 100;
                }else{
                        amount = scheme.getAmount();
                }
                SchemeIn schemeIn = new SchemeIn();
                SchemeInId schemeInId = new SchemeInId();
                schemeInId.setInventoryItemId(inventoryItemId);
                schemeInId.setSchemeId(schemeId);
                schemeIn.setId(schemeInId);
                schemeIn.setAmount(amount);
                schemeInRepository.persist(schemeIn);
        }
        
        private void createSchemeOut(Scheme scheme, float unitPrice, int fofoLineItemId, int schemeId){
                float amount = 0;
                if(scheme.getAmountType() == SchemeAmountType.PERCENTAGE){
                        amount = unitPrice * scheme.getAmount() / 100;
                }else{
                        amount = scheme.getAmount();
                }
                SchemeOut schemeOut = new SchemeOut();
                SchemeOutId schemeOutId = new SchemeOutId();
                schemeOutId.setFofoLineItemId(fofoLineItemId);
                schemeOutId.setSchemeId(schemeId);
                schemeOut.setId(schemeOutId);
                schemeOut.setAmount(amount);
                schemeOutRepository.persist(schemeOut);
        }

        @Override
        public void processSchemeOut(int fofoLineItemId) throws ProfitMandiBusinessException {
                FofoLineItem fofoLineItem = fofoLineItemRepository.selectById(fofoLineItemId);
                List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.OUT);
                Map<Integer, Scheme> notAllSchemeSchemeMap = new HashMap<>();
                Map<Integer, Scheme> allSchemeSchemeMap = new HashMap<>();
                
                for(Scheme scheme : schemes){
                        if(!scheme.isAll()){
                                notAllSchemeSchemeMap.put(scheme.getId(), scheme);
                        }else{
                                allSchemeSchemeMap.put(scheme.getId(), scheme);
                        }
                }
                for(Map.Entry<Integer, Scheme> entry : allSchemeSchemeMap.entrySet()){
                        this.createSchemeOut(entry.getValue(), fofoLineItem.getSellingPrice(), fofoLineItemId, entry.getKey());
                }
                
                if(!notAllSchemeSchemeMap.isEmpty()){
                        List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(notAllSchemeSchemeMap.keySet());
                        for(Map.Entry<Integer, Scheme> entry : notAllSchemeSchemeMap.entrySet()){
                                Predicate<RetailerScheme> retailerIdSchemeIdPredicate = new Predicate<RetailerScheme>() 
                                {
                                        @Override
                                        public boolean test(RetailerScheme retailerScheme) {
                                                if(retailerScheme.getRetailerId() == fofoLineItem.getOrder().getFofoId() && retailerScheme.getSchemeId() == entry.getKey()){
                                                        return true;
                                                }
                                                return false;
                                        }
                                };
                                if(!retailerSchemes.isEmpty() && retailerSchemes.stream().anyMatch(retailerIdSchemeIdPredicate)){
                                        this.createSchemeIn(entry.getValue(), fofoLineItem.getSellingPrice(), fofoLineItemId, entry.getKey());
                                }
                        }
                        
                }
        }

}