Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
Notekunn committed Jan 1, 2021
1 parent 094de90 commit 065510b
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 50 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
<artifactId>jfreechart</artifactId>
<version>1.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.toedter/jcalendar -->
<dependency>
<groupId>com.toedter</groupId>
<artifactId>jcalendar</artifactId>
<version>1.4</version>
</dependency>


</dependencies>
<name>Quản lý quán trà sữa</name>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/controllers/AdminDashboardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private void onMenuChange(MenuItem item) {
case "TK":
view.setPanel(statisticalView);
statisticalController.setView(statisticalView);
statisticalController.addEvent();
statisticalController.initData();
break;
default:
Expand Down
50 changes: 39 additions & 11 deletions src/main/java/controllers/admin/StatisticalController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package controllers.admin;

import dao.StatisticalDao;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import utils.Debouncer;
import utils.IntervalIncrease;
import views.admin.StatisticalView;

Expand All @@ -15,6 +21,7 @@ public class StatisticalController {
StatisticalView view;
StatisticalDao statisticalDao = new StatisticalDao();
DecimalFormat formatter = new DecimalFormat("###,###,###");
Debouncer debouncer = new Debouncer();

public StatisticalController() {
}
Expand All @@ -28,21 +35,42 @@ public void setView(StatisticalView view) {
}

public void addEvent() {
Runnable onDateChange = () -> {
Date startDate = view.getDateChooserStart().getDate();
Date endDate = view.getDateChooserEnd().getDate();
if (startDate.after(endDate)) {
view.showError("Ngày bắt đầu không thể sau ngày kết thúc");
return;
}
try {
renderData(new Timestamp(startDate.getTime()), new Timestamp(endDate.getTime()));
} catch (SQLException ex) {
view.showError(ex);
}

};
PropertyChangeListener eventListener = evt -> debouncer.debounce("date_change", onDateChange, 3000);//Chờ 3s không thay đổi mới hiển thị data

view.getDateChooserStart().addPropertyChangeListener(eventListener);
view.getDateChooserEnd().addPropertyChangeListener(eventListener);
}

public void initData() {
try {
int totalIncome = statisticalDao.getTotalIncome();
view.getLbTotalOrder().setText(statisticalDao.getTotalOrder() + "");
view.getLbTotalEmployee().setText(statisticalDao.getTotalEmployee() + "");
view.getLbTotalCustomer().setText(statisticalDao.getTotalCustomer() + "");
IntervalIncrease.create(totalIncome, 1500, 25, (i) -> {
view.getLbTotalIncome().setText(formatter.format(i));
});
} catch (Exception e) {
view.showError(e);
}
Calendar c = Calendar.getInstance();
c.setTime(new Date(System.currentTimeMillis()));
view.getDateChooserEnd().setCalendar(c);
c.add(Calendar.DATE, -30);
view.getDateChooserStart().setCalendar(c);
}

public void renderData(Timestamp start, Timestamp end) throws SQLException {
int totalIncome = statisticalDao.getTotalIncome(start, end);
view.getLbTotalOrder().setText(statisticalDao.getTotalOrder(start, end) + "");
view.getLbTotalEmployee().setText(statisticalDao.getTotalEmployee() + "");
view.getLbTotalCustomer().setText(statisticalDao.getTotalCustomer() + "");
IntervalIncrease.create(totalIncome, 1500, 25, (i) -> {
view.getLbTotalIncome().setText(formatter.format(i));
});
}

}
48 changes: 48 additions & 0 deletions src/main/java/utils/Debouncer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* createAt Jan 2, 2021
*
* @author Đỗ Tuấn Anh <[email protected]>
*/
public class Debouncer {

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final ConcurrentHashMap<Object, Future<?>> delayedMap = new ConcurrentHashMap<>();

public void debounce(final Object key, final Runnable runnable, long delay) {
final Future<?> prev = delayedMap.put(key, scheduler.schedule(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} finally {
delayedMap.remove(key);
}
}
}, delay, TimeUnit.MILLISECONDS));
if (prev != null) {
prev.cancel(true);
}
}

public void shutdown() {
scheduler.shutdownNow();
}

public static void main(String[] args) {
Debouncer debouncer = new Debouncer();
debouncer.debounce(Void.class, () -> {
System.out.println("123123");
}, 0);
debouncer.debounce(Void.class, () -> {
System.out.println("1231234");
}, 300);
}
}
Loading

0 comments on commit 065510b

Please sign in to comment.