Subversion Repositories SmartDukaan

Rev

Rev 34906 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.service;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.affiliate.AffiliateEarnings;
import com.spice.profitmandi.dao.entity.affiliate.AffiliateProduct;
import com.spice.profitmandi.dao.entity.fofo.Customer;
import com.spice.profitmandi.dao.entity.fofo.PendingOrderItem;
import com.spice.profitmandi.dao.repository.affiliate.AffiliateRepository;
import com.spice.profitmandi.dao.repository.affiliateLink.AffiliateLinkRepository;
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
import com.spice.profitmandi.dao.repository.fofo.PendingOrderItemRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AffiliateServiceImpl implements AffiliateService {

    private static final Logger logger = LogManager.getLogger(AffiliateServiceImpl.class);

    @Autowired
    AffiliateRepository affiliateRepository;

    @Autowired
    AffiliateLinkRepository affiliateLinkRepository;

    @Autowired
    PendingOrderItemRepository orderItemRepository;

    @Autowired
    CustomerRepository customerRepository;

    @Override
    public void createEarnings(AffiliateEarnings earnings) {
        affiliateRepository.persist(earnings);
    }

    @Override
    public List<AffiliateEarnings> getEarnings(int customerId) {
        return affiliateRepository.findEarningsByCustomerId(customerId);
    }

    @Override
    public Map<String,Object> getAffiliateData(int customerId) throws ProfitMandiBusinessException {
        Map<String,Object> resMap = new HashMap<>();
        Customer customer = customerRepository.selectById(customerId);
        if(customer!=null){
            List<AffiliateProduct> affiliateLinks = affiliateLinkRepository.findProductsByCustomerId(customerId);
            logger.info("AffiliateLinks size: {}",affiliateLinks);
            List<AffiliateEarnings> affiliateEarnings = affiliateRepository.findEarningsByCustomerId(customerId);
            String code = customer.getFirstName().substring(0, 1).toUpperCase() + customer.getLastName().substring(0, 1).toUpperCase() + customer.getId();
            List<PendingOrderItem> orderItems = orderItemRepository.selectAllByCustomer(code);
            logger.info("affiliateEarnings: {}",affiliateEarnings);
            resMap.put("affiliateLinks", affiliateLinks);
            resMap.put("affiliateEarnings", affiliateEarnings);
            resMap.put("affiliateOrders", orderItems);
        } else {
            resMap.put("affiliateLinks", "[]");
            resMap.put("affiliateEarnings", "[]");
            resMap.put("affiliateOrders", "[]");
        }

        return resMap;
    }
}