Bläddra i källkod

fix:修复书籍管理中没有卖家名称的问题

yangyi 1 månad sedan
förälder
incheckning
d916ad17ad

+ 119 - 102
client/src/main/java/space/anyi/client/controller/MainController.java

@@ -109,14 +109,7 @@ public class MainController {
     @FXML private TableColumn<Book, Integer> myBookStockCol;
     @FXML private TableColumn<Book, Void> myBookStatusCol;
     @FXML private TableColumn<Book, Void> myBookActionCol;
-    @FXML private TitledPane bookFormPane;
-    @FXML private Button saveBookBtn;
-    @FXML private TextField newTitle;
-    @FXML private TextField newAuthor;
-    @FXML private TextField newCategory;
-    @FXML private TextField newOriginalPrice;
-    @FXML private TextField newSellingPrice;
-    @FXML private TextField newStock;
+    
 
     // Statistics tab
     @FXML private Label statTotalAmount;
@@ -186,7 +179,6 @@ public class MainController {
     private final StatisticsService statisticsService = new StatisticsService();
 
     private Book selectedBook;
-    private Book editingBook;
 
     @FXML private Button prevPageBtn;
     @FXML private Button nextPageBtn;
@@ -611,7 +603,7 @@ public class MainController {
                 delBtn.setStyle("-fx-padding: 4px 12px; -fx-font-size: 12px;");
                 editBtn.setOnAction(e -> {
                     Book b = getTableView().getItems().get(getIndex());
-                    editBook(b);
+                    showBookDialog(b);
                 });
                 toggleBtn.setOnAction(e -> {
                     Book b = getTableView().getItems().get(getIndex());
@@ -672,77 +664,6 @@ public class MainController {
         }
     }
 
-    private void editBook(Book sel) {
-        editingBook = sel;
-        newTitle.setText(sel.getTitle());
-        newAuthor.setText(sel.getAuthor());
-        newCategory.setText(sel.getCategory());
-        newOriginalPrice.setText(sel.getOriginalPrice().toString());
-        newSellingPrice.setText(sel.getSellingPrice().toString());
-        newStock.setText(String.valueOf(sel.getStock()));
-        bookFormPane.setText("编辑书籍");
-        saveBookBtn.setText("保存修改");
-        bookFormPane.setExpanded(true);
-    }
-
-    @FXML
-    private void handleEditBook() {
-        Book sel = myBookTable.getSelectionModel().getSelectedItem();
-        if (sel == null) {
-            showAlert("请先选择要编辑的书籍");
-            return;
-        }
-        editBook(sel);
-    }
-
-    @FXML
-    private void handleAddBook() {
-        try {
-            String title = newTitle.getText().trim();
-            String author = newAuthor.getText().trim();
-            String category = newCategory.getText().trim();
-            BigDecimal originalPrice = new BigDecimal(newOriginalPrice.getText().trim());
-            BigDecimal sellingPrice = new BigDecimal(newSellingPrice.getText().trim());
-            int stock = Integer.parseInt(newStock.getText().trim());
-
-            if (editingBook != null) {
-                editingBook.setTitle(title);
-                editingBook.setAuthor(author);
-                editingBook.setCategory(category);
-                editingBook.setOriginalPrice(originalPrice);
-                editingBook.setSellingPrice(sellingPrice);
-                editingBook.setStock(stock);
-                R<Book> result = bookService.update(editingBook.getBookId(), editingBook);
-                if (result.getCode() == 200) {
-                    showInfo("书籍已更新!");
-                    clearBookForm();
-                    handleLoadMyBooks();
-                } else {
-                    showAlert(result.getMessage());
-                }
-            } else {
-                Book book = new Book();
-                book.setSellerId(LoginUserHolder.get().getUserid());
-                book.setTitle(title);
-                book.setAuthor(author);
-                book.setCategory(category);
-                book.setOriginalPrice(originalPrice);
-                book.setSellingPrice(sellingPrice);
-                book.setStock(stock);
-                R<Book> result = bookService.create(book);
-                if (result.getCode() == 200) {
-                    showInfo("书籍已上架!");
-                    clearBookForm();
-                    handleLoadMyBooks();
-                } else {
-                    showAlert(result.getMessage());
-                }
-            }
-        } catch (Exception e) {
-            showAlert("输入无效:" + e.getMessage());
-        }
-    }
-
     @FXML
     private void handleDeleteBook() {
         Book sel = myBookTable.getSelectionModel().getSelectedItem();
@@ -775,16 +696,120 @@ public class MainController {
     }
 
     @FXML
-    private void handleToggleBookForm() {
-        bookFormPane.setExpanded(!bookFormPane.isExpanded());
-        if (bookFormPane.isExpanded()) {
-            clearBookForm();
-        }
+    private void handleAddBookDialog() {
+        showBookDialog(null);
     }
 
-    @FXML
-    private void handleCancelBookForm() {
-        clearBookForm();
+    private void showBookDialog(Book existingBook) {
+        boolean isNew = (existingBook == null);
+        Dialog<Book> dialog = new Dialog<>();
+        dialog.setTitle(isNew ? "添加新书" : "编辑书籍");
+        dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
+        dialog.getDialogPane().setPrefWidth(480);
+
+        GridPane grid = new GridPane();
+        grid.setHgap(12);
+        grid.setVgap(10);
+        grid.setPadding(new javafx.geometry.Insets(20));
+        ColumnConstraints col1 = new ColumnConstraints(70);
+        ColumnConstraints col2 = new ColumnConstraints(300, 300, Double.MAX_VALUE);
+        grid.getColumnConstraints().addAll(col1, col2);
+
+        TextField titleField = new TextField();
+        titleField.setPromptText("请输入书名");
+        TextField authorField = new TextField();
+        authorField.setPromptText("请输入作者");
+        ComboBox<String> categoryCombo = new ComboBox<>();
+        // Same categories as browse page (excluding "全部")
+        categoryCombo.getItems().addAll("教材", "小说", "科技", "文学", "考试", "其他");
+        TextField origPriceField = new TextField();
+        origPriceField.setPromptText("请输入原价");
+        TextField sellPriceField = new TextField();
+        sellPriceField.setPromptText("请输入售价");
+        TextField stockField = new TextField();
+        stockField.setPromptText("请输入库存数量");
+
+        if (!isNew) {
+            titleField.setText(existingBook.getTitle());
+            authorField.setText(existingBook.getAuthor());
+            categoryCombo.getSelectionModel().select(existingBook.getCategory());
+            origPriceField.setText(existingBook.getOriginalPrice().toString());
+            sellPriceField.setText(existingBook.getSellingPrice().toString());
+            stockField.setText(String.valueOf(existingBook.getStock()));
+        } else {
+            categoryCombo.getSelectionModel().select(0);
+        }
+
+        Label titleLbl = new Label("书名:");
+        Label authorLbl = new Label("作者:");
+        Label categoryLbl = new Label("分类:");
+        Label origPriceLbl = new Label("原价:");
+        Label sellPriceLbl = new Label("售价:");
+        Label stockLbl = new Label("库存:");
+
+        int row = 0;
+        grid.add(titleLbl, 0, row);
+        grid.add(titleField, 1, row++);
+        grid.add(authorLbl, 0, row);
+        grid.add(authorField, 1, row++);
+        grid.add(categoryLbl, 0, row);
+        grid.add(categoryCombo, 1, row++);
+        grid.add(origPriceLbl, 0, row);
+        grid.add(origPriceField, 1, row++);
+        grid.add(sellPriceLbl, 0, row);
+        grid.add(sellPriceField, 1, row++);
+        grid.add(stockLbl, 0, row);
+        grid.add(stockField, 1, row++);
+
+        dialog.getDialogPane().setContent(grid);
+
+        Node okBtn = dialog.getDialogPane().lookupButton(ButtonType.OK);
+        if (isNew) {
+            okBtn.setDisable(true);
+            titleField.textProperty().addListener((obs, old, val) ->
+                okBtn.setDisable(val.trim().isEmpty() || authorField.getText().trim().isEmpty()));
+            authorField.textProperty().addListener((obs, old, val) ->
+                okBtn.setDisable(val.trim().isEmpty() || titleField.getText().trim().isEmpty()));
+        }
+
+        dialog.setResultConverter(btn -> {
+            if (btn == ButtonType.OK) {
+                try {
+                    Book book = isNew ? new Book() : existingBook;
+                    book.setTitle(titleField.getText().trim());
+                    book.setAuthor(authorField.getText().trim());
+                    book.setCategory(categoryCombo.getSelectionModel().getSelectedItem());
+                    book.setOriginalPrice(new BigDecimal(origPriceField.getText().trim()));
+                    book.setSellingPrice(new BigDecimal(sellPriceField.getText().trim()));
+                    book.setStock(Integer.parseInt(stockField.getText().trim()));
+                    return book;
+                } catch (NumberFormatException e) {
+                    return null;
+                }
+            }
+            return null;
+        });
+
+        dialog.showAndWait().ifPresent(book -> {
+            if (isNew) {
+                book.setSellerId(LoginUserHolder.get().getUserid());
+                R<Book> result = bookService.create(book);
+                if (result.getCode() == 200) {
+                    showInfo("书籍已上架!");
+                    handleLoadMyBooks();
+                } else {
+                    showAlert(result.getMessage());
+                }
+            } else {
+                R<Book> result = bookService.update(book.getBookId(), book);
+                if (result.getCode() == 200) {
+                    showInfo("书籍已更新!");
+                    handleLoadMyBooks();
+                } else {
+                    showAlert(result.getMessage());
+                }
+            }
+        });
     }
 
     @FXML
@@ -1150,6 +1175,11 @@ public class MainController {
         R<List<BookDetailDTO>> result = bookService.getPendingBooks();
         if (result.getCode() == 200 && result.getData() != null) {
             pendingBookTable.setItems(FXCollections.observableArrayList(result.getData()));
+            if (result.getData().isEmpty()) {
+                showInfo("暂无待审核书籍");
+            }
+        } else {
+            showAlert(result.getMessage() != null ? result.getMessage() : "加载待审核书籍失败");
         }
     }
 
@@ -1268,19 +1298,6 @@ public class MainController {
 
     // ── Helpers ──
 
-    private void clearBookForm() {
-        editingBook = null;
-        newTitle.clear();
-        newAuthor.clear();
-        newCategory.clear();
-        newOriginalPrice.clear();
-        newSellingPrice.clear();
-        newStock.clear();
-        bookFormPane.setText("添加新书");
-        saveBookBtn.setText("添加书籍");
-        bookFormPane.setExpanded(false);
-    }
-
     private void showUserDialog(User user) {
         boolean isNew = (user == null);
         Dialog<User> dialog = new Dialog<>();

+ 2 - 29
client/src/main/resources/fxml/seller.fxml

@@ -26,7 +26,7 @@
     <HBox spacing="10" alignment="CENTER_LEFT">
         <Label text="我的书籍" style="-fx-font-size: 16px; -fx-font-weight: bold;" HBox.hgrow="ALWAYS"/>
         <Button text="刷新" onAction="#handleLoadMyBooks" styleClass="btn,btn-secondary"/>
-        <Button text="+ 添加新书" onAction="#handleToggleBookForm" styleClass="btn,btn-success"/>
+        <Button text="+ 添加新书" onAction="#handleAddBookDialog" styleClass="btn,btn-success"/>
     </HBox>
     <TableView fx:id="myBookTable" VBox.vgrow="ALWAYS" prefHeight="200">
         <columns>
@@ -38,32 +38,5 @@
             <TableColumn fx:id="myBookActionCol" text="操作" prefWidth="200"/>
         </columns>
     </TableView>
-    <TitledPane fx:id="bookFormPane" text="添加新书" expanded="false" animated="true">
-        <GridPane hgap="16" vgap="10">
-            <padding><Insets top="16" right="20" bottom="16" left="20"/></padding>
-            <columnConstraints>
-                <ColumnConstraints prefWidth="70"/>
-                <ColumnConstraints hgrow="ALWAYS" prefWidth="200"/>
-                <ColumnConstraints prefWidth="30"/>
-                <ColumnConstraints prefWidth="70"/>
-                <ColumnConstraints hgrow="ALWAYS" prefWidth="200"/>
-            </columnConstraints>
-            <Label text="书名" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
-            <TextField fx:id="newTitle" styleClass="form-control" GridPane.rowIndex="0" GridPane.columnIndex="1"/>
-            <Label text="作者" GridPane.rowIndex="0" GridPane.columnIndex="3"/>
-            <TextField fx:id="newAuthor" styleClass="form-control" GridPane.rowIndex="0" GridPane.columnIndex="4"/>
-            <Label text="分类" GridPane.rowIndex="1" GridPane.columnIndex="0"/>
-            <TextField fx:id="newCategory" styleClass="form-control" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
-            <Label text="原价" GridPane.rowIndex="1" GridPane.columnIndex="3"/>
-            <TextField fx:id="newOriginalPrice" styleClass="form-control" GridPane.rowIndex="1" GridPane.columnIndex="4"/>
-            <Label text="售价" GridPane.rowIndex="2" GridPane.columnIndex="0"/>
-            <TextField fx:id="newSellingPrice" styleClass="form-control" GridPane.rowIndex="2" GridPane.columnIndex="1"/>
-            <Label text="库存" GridPane.rowIndex="2" GridPane.columnIndex="3"/>
-            <TextField fx:id="newStock" styleClass="form-control" GridPane.rowIndex="2" GridPane.columnIndex="4"/>
-            <HBox spacing="10" GridPane.rowIndex="3" GridPane.columnIndex="4" alignment="CENTER_RIGHT">
-                <Button text="取消" onAction="#handleCancelBookForm" styleClass="btn,btn-secondary"/>
-                <Button fx:id="saveBookBtn" text="添加书籍" onAction="#handleAddBook" styleClass="btn,btn-success"/>
-            </HBox>
-        </GridPane>
-    </TitledPane>
+    
 </VBox>