欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java异常处理

程序员文章站 2022-05-28 17:00:46
...
数据重复异常类
/**
 * 数据重复异常
 * 
 * 
 */
@SuppressWarnings("serial")
public class DuplicateDataExcepption extends Exception {

	// 重复的数据信息
	private Object data;

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	/**
	 * 
	 * @param data 重复的数据
	 */
	public DuplicateDataExcepption(Object data) {
		super();
		this.data = data;
	}

	public DuplicateDataExcepption(String message) {
		super(message);
	}
	
	public DuplicateDataExcepption(String message, Object data) {
		super(message);
		this.data = data;
	}
}



库存异常类

/**
 * 库存不足异常
 * 
 *
 */
public class InventoryShortageException extends RuntimeException {
	
	private static final long serialVersionUID = -2059212682755129635L;

	/**
	 * 实际库存信息
	 */
	private GoodsInventory goodsInventory;
	
	/**
	 * 扣减库存数
	 */
	private int decreaseCount;
	
//	public InventoryShortageException() {
//		super();
//	}

	/**
	 * 
	 * @param goodsInventory 实际库存信息
	 * @param decreaseCount 扣减库存数
	 */
	public InventoryShortageException(GoodsInventory goodsInventory, int decreaseCount) {
		super();
		this.goodsInventory = goodsInventory;
		this.decreaseCount = decreaseCount;
	}
	
	/**
	 * 
	 * @param goodsSku SKU
	 * @param availableCount 实际库存
	 * @param decreaseCount 扣减库存数
	 */
	public InventoryShortageException(String goodsSku, int availableCount, int decreaseCount) {
		super();
		GoodsInventory tmp = new GoodsInventory();
		tmp.setGoodsSku(goodsSku);
		tmp.setCount(availableCount);
		this.goodsInventory = tmp;
		
		this.decreaseCount = decreaseCount;
	}

	public GoodsInventory getGoodsInventory() {
		return goodsInventory;
	}

	public void setGoodsInventory(GoodsInventory goodsInventory) {
		this.goodsInventory = goodsInventory;
	}

	public int getAmount() {
		return decreaseCount;
	}

	public void setAmount(int amount) {
		this.decreaseCount = amount;
	}

	@Override
	public String getMessage() {
		StringBuilder sb = new StringBuilder();
		sb.append("库存不足 (")
		.append("sku: ")
		.append(this.goodsInventory.getGoodsSku())
		.append(", storeId: ")
		.append(this.goodsInventory.getStoreId())
		.append(", storeShelfId: ")
		.append(this.goodsInventory.getStoreShelfId())
		.append("实际库存:").append(this.goodsInventory.getCount())
		.append("需要扣减: ").append(this.decreaseCount)
		.append(")");
		return sb.toString();
	}
	
	
}




业务层类

/**
	 * 验证sku是否库存
	 * @param order
	 * @return
	 */
	private boolean checkInventory(GoodsTransferOrder order){
		int count=0;
		for (GoodsTransferOrderItem item : order.getItems()) {
			String sku=item.getGoodsSku();
			Integer decreaseCount=item.getGoodsCount();
			Integer storeId=item.getFromStoreId();
			Integer storeShelfId=item.getFromStoreShelfId();
			if (sku == null || decreaseCount == 0) {
				return false;
			}
			
			Integer storeSum = this.goodsInventoryService.getInventory(sku, storeId, storeShelfId);
			
			//前提是存在库存信息,并且库存数足够 ,才能通过;否则抛出异常
			if (storeSum == null || storeSum < decreaseCount) {
				throw new InventoryShortageException(sku,storeSum, decreaseCount);
			}else{
				count++;
			}
		}
		
		return count==order.getItems().size();
	}




控制器类

/**
	 * 添加单
	 * @param attr
	 * @param sessionUser
	 * @param goodsSku
	 * @param goodsName
	 * @param goodsCount
	 * @param fromStoreId
	 * @param fromStoreShelfId
	 * @param toStoreId
	 * @param toStoreShelfId
	 * @param fromStoreShelf
	 * @param toStoreShelf
	 * @param order
	 * @return
	 */
	@RequestMapping("save")
	public String save(RedirectAttributes attr,
			@ModelAttribute(App.SESSION_USER_KEY) SessionUser sessionUser,
			@RequestParam("goodsSku") List<String> goodsSku,
			@RequestParam("goodsName") List<String> goodsName,
			@RequestParam("goodsCount") List<Integer> goodsCount,
			@RequestParam("fromStoreId") List<Integer> fromStoreId,
			@RequestParam("fromStoreShelfId") List<Integer> fromStoreShelfId,
			@RequestParam("toStoreId") List<Integer> toStoreId,
			@RequestParam("toStoreShelfId") List<Integer> toStoreShelfId,
			@RequestParam("fromStoreShelf") List<String> fromStoreShelf,
			@RequestParam("toStoreShelf") List<String> toStoreShelf,
			GoodsTransferOrder order){
		
		List<GoodsTransferOrderItem> items = new ArrayList<>(3);
		order.setItems(items);
		try {
			
		for (int i = 0; i < goodsSku.size(); i++) {
			GoodsTransferOrderItem item = new GoodsTransferOrderItem();
			item.setGoodsSku(goodsSku.get(i));
			item.setGoodsName(goodsName.get(i));
			item.setFromStoreId(fromStoreId.get(i));
			item.setFromStoreShelfId(fromStoreShelfId.get(i));
			item.setGoodsCount(goodsCount.get(i));
	        item.setToStoreId(toStoreId.get(i));
	        item.setToStoreShelfId(toStoreShelfId.get(i));
	        item.setFromStoreShelf(fromStoreShelf.get(i));
	        item.setToStoreShelf(toStoreShelf.get(i));
			items.add(item);
			
			if(item.getFromStoreId().equals(item.getToStoreId())){
				throw new DuplicateDataExcepption("sku:"+item.getGoodsSku()+"---->调出仓库与调入仓库不能相同");
			}
			
		 }

		 if (order.getId() == null) {
				order.setStatus(1);
				String dateStr = dft.format(Calendar.getInstance().getTime());
				String fmt =String.format("LS-%s-",dateStr)+"%d";
				order.setSerialNumber(tableKeyService.nextSerialNumber("goods_transfer_order",fmt));
				// 制单人
				order.setOperatorId(sessionUser.getUserId());
				this.goodsTransferOrderService.save(order);
				attr.addFlashAttribute("successMessage", "g.tips.success");
			 }else {
				this.goodsTransferOrderService.update(order);
				attr.addFlashAttribute("successMessage", "g.tips.success");
			 }
		  } catch (InventoryShortageException e) {
				attr.addFlashAttribute("errorMessage", e.getGoodsInventory().getGoodsSku() + "库存不足");
		  }catch(DuplicateDataExcepption e){
			  attr.addFlashAttribute("errorMessage", e.getMessage());	 
		  }
		
		return "redirect:/allocateOrder/list";
	}
	


jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"  %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<c:if test="${!empty successMessage  }">
	<div class="alert alert-success alert-dismissable">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
		<spring:message code="${successMessage }" text="${successMessage }"  />
	</div>
</c:if>
<c:if test="${!empty infoMessage  }">
	<div class="alert alert-info alert-dismissable">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
		<spring:message code="${infoMessage }" text="${infoMessage }"  />
	</div>
</c:if>
<c:if test="${!empty warningMessage  }">
	<div class="alert alert-warning alert-dismissable">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
		<spring:message code="${warningMessage }" text="${warningMessage }"  />
	</div>
</c:if>
<c:if test="${!empty errorMessage  }">
	<div class="alert alert-danger alert-dismissable">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
		<spring:message code="${errorMessage }" text="${errorMessage }"  />
	</div>
</c:if>