5.28

package com.example.baoli.controller;

import com.example.baoli.entity.InboundRecord;
import com.example.baoli.service.InboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/inbound")
public class InboundController {

@Autowired
private InboundService inboundService;

@GetMapping
public ResponseEntity<List<InboundRecord>> getAllInboundRecords() {
List<InboundRecord> records = inboundService.getAllInboundRecords();
return ResponseEntity.ok(records);
}

@GetMapping("/{id}")
public ResponseEntity<InboundRecord> getInboundRecordById(@PathVariable Long id) {
Optional<InboundRecord> record = inboundService.getInboundRecordById(id);
return record.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

@PostMapping
public ResponseEntity<InboundRecord> createInboundRecord(@RequestBody InboundRecord inboundRecord) {
try {
InboundRecord savedRecord = inboundService.saveInboundRecord(inboundRecord);
return ResponseEntity.ok(savedRecord);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}

@PutMapping("/{id}")
public ResponseEntity<InboundRecord> updateInboundRecord(@PathVariable Long id, @RequestBody InboundRecord inboundRecord) {
inboundRecord.setId(id);
InboundRecord updatedRecord = inboundService.saveInboundRecord(inboundRecord);
return ResponseEntity.ok(updatedRecord);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteInboundRecord(@PathVariable Long id) {
inboundService.deleteInboundRecordById(id);
return ResponseEntity.ok().build();
}

@GetMapping("/status/{status}")
public ResponseEntity<List<InboundRecord>> getInboundRecordsByStatus(@PathVariable String status) {
List<InboundRecord> records = inboundService.getInboundRecordsByStatus(status);
return ResponseEntity.ok(records);
}

@GetMapping("/search")
public ResponseEntity<List<InboundRecord>> searchInboundRecords(@RequestParam String keyword) {
List<InboundRecord> records = inboundService.searchInboundRecords(keyword);
return ResponseEntity.ok(records);
}

/**
* 审核入库记录
*/
@PutMapping("/{id}/approve")
public ResponseEntity<InboundRecord> approveInboundRecord(@PathVariable Long id, @RequestParam String approver) {
InboundRecord approvedRecord = inboundService.approveInboundRecord(id, approver);
if (approvedRecord != null) {
return ResponseEntity.ok(approvedRecord);
}
return ResponseEntity.notFound().build();
}

/**
* 驳回入库记录
*/
@PutMapping("/{id}/reject")
public ResponseEntity<InboundRecord> rejectInboundRecord(@PathVariable Long id, @RequestParam String reason) {
InboundRecord rejectedRecord = inboundService.rejectInboundRecord(id, reason);
if (rejectedRecord != null) {
return ResponseEntity.ok(rejectedRecord);
}
return ResponseEntity.notFound().build();
}

/**
* 批量审核入库记录
*/
@PutMapping("/batch-approve")
public ResponseEntity<List<InboundRecord>> batchApproveInboundRecords(@RequestBody BatchApprovalRequest request) {
try {
List<InboundRecord> approvedRecords = inboundService.batchApproveInboundRecords(request.getIds(), request.getApprover());
return ResponseEntity.ok(approvedRecords);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}

/**
* 批量驳回入库记录
*/
@PutMapping("/batch-reject")
public ResponseEntity<List<InboundRecord>> batchRejectInboundRecords(@RequestBody BatchRejectRequest request) {
try {
List<InboundRecord> rejectedRecords = inboundService.batchRejectInboundRecords(request.getIds(), request.getReason());
return ResponseEntity.ok(rejectedRecords);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}

// 内部类用于批量审核请求
public static class BatchApprovalRequest {
private List<Long> ids;
private String approver;

public List<Long> getIds() { return ids; }
public void setIds(List<Long> ids) { this.ids = ids; }

public String getApprover() { return approver; }
public void setApprover(String approver) { this.approver = approver; }
}

// 内部类用于批量驳回请求
public static class BatchRejectRequest {
private List<Long> ids;
private String reason;

public List<Long> getIds() { return ids; }
public void setIds(List<Long> ids) { this.ids = ids; }

public String getReason() { return reason; }
public void setReason(String reason) { this.reason = reason; }
}
}

 

posted @ 2025-05-28 22:59  混沌武士丞  阅读(6)  评论(0)    收藏  举报