package org.tizzer.smmgr.system.view;
import com.alee.laf.button.WebButton;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.scroll.WebScrollPane;
import com.alee.laf.table.WebTable;
import com.alee.laf.text.WebTextField;
import org.tizzer.smmgr.system.common.Logcat;
import org.tizzer.smmgr.system.constant.IconManager;
import org.tizzer.smmgr.system.constant.ResultCode;
import org.tizzer.smmgr.system.constant.RuntimeConstants;
import org.tizzer.smmgr.system.handler.HttpHandler;
import org.tizzer.smmgr.system.model.request.QueryOneInsiderRequestDto;
import org.tizzer.smmgr.system.model.request.QueryTradeGoodsRequestDto;
import org.tizzer.smmgr.system.model.request.SaveTradeRecordRequestDto;
import org.tizzer.smmgr.system.model.response.QueryOneInsiderResponseDto;
import org.tizzer.smmgr.system.model.response.QueryTradeGoodsResponseDto;
import org.tizzer.smmgr.system.model.response.SaveTradeRecordResponseDto;
import org.tizzer.smmgr.system.utils.D9Util;
import org.tizzer.smmgr.system.utils.LafUtil;
import org.tizzer.smmgr.system.view.dialog.AddGoodsDialog;
import org.tizzer.smmgr.system.view.dialog.CheckoutDialog;
import org.tizzer.smmgr.system.view.dialog.TradeGoodsDialog;
import org.tizzer.smmgr.system.view.dialog.UpdateInsiderDialog;
import org.tizzer.smmgr.system.view.listener.TableCellListener;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
/**
* @author tizzer
* @version 1.0
*/
class StandardCollectionBoundary extends WebPanel {
private final static Object[] tableHead = {"商品条码", "商品名称", "原价", "折扣(%)", "现价", "数量", "小计"};
private DefaultTableModel tableModel;
private WebTable tradeGoodsTable;
private WebButton addGoodsButton;
private WebButton resetTradeButton;
private WebButton deleteRowButton;
private WebButton seeInsiderButton;
private WebButton cancelInsiderButton;
private WebTextField searchGoodsField;
private WebTextField searchInsiderField;
private WebButton checkoutButton;
//查询的交易商品缓存
private Object[][] tradeGoodsCache;
//会员信息缓存
private Object[] insiderCache;
//折扣
private int discount = 100;
//当前消费总额
private double currentCost = 0;
StandardCollectionBoundary() {
tradeGoodsTable = createTransactionTable();
addGoodsButton = createBootstrapButton("新增商品", IconManager.ADDGOODS, "brown.xml");
resetTradeButton = createBootstrapButton("清理台面", IconManager.RESETDESK, "brown.xml");
deleteRowButton = createBootstrapButton("删除记录", IconManager.DELETERECORD, "brown.xml");
searchGoodsField = createTrailingField("条码/名称/拼音码", null, null);
seeInsiderButton = createBootstrapButton("查看", null, "default.xml");
cancelInsiderButton = createBootstrapButton("取消", null, "default.xml");
searchInsiderField = createTrailingField("会员号/手机号", seeInsiderButton, cancelInsiderButton);
checkoutButton = createCheckoutButton();
this.setCheckoutButton("0.0");
this.setOpaque(false);
this.add(new WebScrollPane(tradeGoodsTable), "Center");
this.add(createCheckoutPane(), "South");
this.initListener();
}
private void initListener() {
new TableCellListener(tradeGoodsTable, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
TableCellListener tcl = (TableCellListener) e.getSource();
String value = String.valueOf(tcl.getNewValue());
if (value.matches("[1-9]([0-9]?)*")) {
//新数量
int newValue = Integer.parseInt(String.valueOf(tradeGoodsTable.getValueAt(tcl.getRow(), 5)));
//旧数量
int oldValue = Integer.parseInt(String.valueOf(tcl.getOldValue()));
if (newValue != oldValue) {
//现价
double presentCost = (double) tradeGoodsTable.getValueAt(tcl.getRow(), 4);
//更新小计
tradeGoodsTable.setValueAt((double) Math.round(presentCost * newValue * 100) / 100, tcl.getRow(), 6);
//更新当前消费总额
currentCost += (newValue - oldValue) * presentCost;
currentCost = (double) Math.round(currentCost * 100) / 100;
setCheckoutButton(String.valueOf(currentCost));
return;
}
}
tradeGoodsTable.setValueAt(tcl.getOldValue(), tcl.getRow(), tcl.getColumn());
}
});
addGoodsButton.addActionListener(e -> AddGoodsDialog.newInstance());
resetTradeButton.addActionListener(e -> {
int operation = JOptionPane.showConfirmDialog(RuntimeConstants.root, "<html><h3>确定要清理台面吗?</h3></html>", "询问", JOptionPane.OK_CANCEL_OPTION);
if (operation == JOptionPane.YES_OPTION) {
reset();
}
});
deleteRowButton.addActionListener(e -> {
if (tradeGoodsTable.getSelectedRow() == -1) {
LafUtil.showTip(deleteRowButton, "请至少选中表格中的一个商品");
return;
}
int[] rows = tradeGoodsTable.getSelectedRows();
for (int i = rows.length; i > 0; i--) {
//小计
double cost = (double) tradeGoodsTable.getValueAt(rows[i - 1], 6);
//更新当前消费总额
currentCost -= cost;
tableModel.removeRow(rows[i - 1]);
}
currentCost = (double) Math.round(currentCost * 100) / 100;
setCheckoutButton(String.valueOf(currentCost));
//后续台面校验
if (tradeGoodsTable.getRowCount() == 0 && discount != 100) {
int operation = JOptionPane.showConfirmDialog(RuntimeConstants.root, "<html><h3>您已清空交易区,是否要退出会员?</h3></html>", "询问", JOptionPane.OK_CANCEL_OPTION);
if (operation == JOptionPane.YES_OPTION) {
discount = 100;
insiderCache = null;
searchInsiderField.setText(null);
searchInsiderField.setEditable(true);
}
}
});
searchGoodsField.addActionListener(e -> {
String keyword = searchGoodsField.getText().trim();
if (!keyword.equals("")) {
QueryTradeGoodsResponseDto queryTradeGoodsResponseDto = queryTradeGoods(keyword);
if (queryTradeGoodsResponseDto.getCode() == ResultCode.OK) {
tradeGoodsCache = queryTradeGoodsResponseDto.getData();
if (tradeGoodsCache.length > 1) {
tradeGoodsCache = TradeGoodsDialog.newInstance(tradeGoodsCache);
}
refreshTable();
}
}
});
searchInsiderField.addActionListener(e -> {
String keyword = searchInsiderField.getText().trim();
if (!keyword.equals("")) {
QueryOneInsiderResponseDto queryOneInsiderResponseDto = queryOneInsider(keyword);
if (queryOneInsiderResponseDto.getCode() != ResultCode.OK) {
LafUtil.showTip(searchInsiderField, queryOneInsiderResponseDto.getMessage());
} else {
//缓存会员信息
insiderCache = queryOneInsiderResponseDto.getData();
//记录会员折扣
discount = queryOneInsiderResponseDto.getDiscount();
//刷新表格数据和当前总额