/**
 * Created by Djidjelly Siclait on 12/3/2016.
 */
package com.evapps.Service.Auxiliary;

import com.evapps.Entity.History;
import com.evapps.Entity.Product;
import com.evapps.Repository.HistoryRepository;
import com.evapps.Repository.ProductRepository;
import com.evapps.Repository.ReceiptRepository;
import com.evapps.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
public class StatisticService {

    // Repositories
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ReceiptRepository receiptRepository;
    @Autowired
    private UserRepository userRepository;

    // Functions
    // History related Statistics
    public Product ProductViewStatistics(boolean option){

        Map<Integer, Integer> statistic = fetchProductLegend();

        try {
            for (History history: historyRepository.findAll())
                for (Product product: history.getBrowsingHistory())
                    statistic.replace(product.getProductId(), statistic.get(product.getProductId()) + 1);

            if (option) // Max
                return productRepository.findByProductId(getMaxValue((Integer[]) statistic.values().toArray()));
            else // Min
                return productRepository.findByProductId(getMinValue((Integer[]) statistic.values().toArray()));
            
        } catch (Exception exp) {
            //
        }

        return null;
    }

    // Auxiliary Functions
    private Map<Integer, Integer> fetchProductLegend(){
        Map<Integer, Integer> legend = new HashMap<>();

        for (Product p:
             productRepository.findAll()) {
            legend.put(p.getProductId(), 0);
        }

        return legend;
    }

    private Integer getMaxValue(Integer[] list){
        Arrays.sort(list);

        return list[list.length - 1];
    }

    private Integer getMinValue(Integer[] list){
        Arrays.sort(list);

        return list[0];
    }


}

@@ -154,16 +154,50 @@
 roduct;%0A
+import com.evapps.Entity.Receipt;%0A
 import c
@@ -228,32 +228,32 @@
 toryRepository;%0A
-
 import com.evapp
@@ -945,17 +945,17 @@
 Product 
-P
+p
 roductVi
@@ -1587,21 +1587,9 @@
 ));%0A
-            
 %0A
+
     
@@ -1665,23 +1665,722 @@
 n null;%0A
-
     %7D%0A%0A
+    public Product productPurchaseStatistics(boolean option)%7B%0A%0A        Map%3CInteger, Integer%3E statistic = fetchProductLegend();%0A%0A        try %7B%0A            for (Receipt receipt: receiptRepository.findAll())%0A                for (Integer p: receipt.getProductList())%0A                    statistic.replace(p, statistic.get(p) + 1);%0A%0A            if (option) // Max%0A                return productRepository.findByProductId(getMaxValue((Integer%5B%5D) statistic.values().toArray()));%0A            else // Min%0A                return productRepository.findByProductId(getMinValue((Integer%5B%5D) statistic.values().toArray()));%0A%0A        %7D catch (Exception exp) %7B%0A            //%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A%0A
     // A

Add productPurchaseStatistics