I want to create one report that you select product and storeName and returns all the sales per date (within a range).
The db view that helped me with other reports looks like this: product - store_name - date
So far my approach is to return all records from the db in a list and then I do the following:
public void salesReport(String product, String store, String date){
List<RecordSalesInfo> salesResults = salesDao.getSales();
Map<String, Integer> mapper = new HashMap();
//calculation
for (RecordSalesInfo record : salesResults) {
StringBuilder key = new StringBuilder();
key.append(record.getProduct()).append(":")
.append(record.getStoreName()).append(":")
.append(record.getWageredDate()).append(":");
if (mapper.containsKey(key.toString())) {
Integer get = mapper.get(key.toString());
get++;
mapper.put(key.toString(), get);
} else {
mapper.put(key.toString(), 1);
}
}
for (String key : mapper.keySet()) {
if(key.toString.equals("Pen:London:13June2016"){
System.out.println("sales:" + mapper.get(key.toString);
}
}
}
the query in the salesDao(saving as "RecordSalesInfo") is:
SELECT
rs.product AS product,
rs.store_name AS store,
rs.date AS date,
rs.product_id AS productId
FROM sales rs
ORDER BY rs.product,rs.store_name,rs.date
The reason I didn't query "Count(blabla) where product='a' and store_name='b' and date='c' " is because the user changes the input using a jSlider very often (input=product,store,date), that means too many queries. So I thought it is better to take all the results from db and then display what the user needs.
A) Is there a better way to do this?
B) In the next phase the user will enter only the product and the store and I have to return the list of the sales by date, looking like this:
Pen - London (manual input) 12June2016 100 15June2016 30 19July2016 67
With what I have done so far, I can't get only the dates that I have sales, and I have to "search" from the hashMap for all dates(specific range). I think as a solution to change the key to "product:storeName" one the existing map and have as a value another map where the String will be the date and the Integer the amount of sales.
Again is there a better way on doing that?