校园商铺-4店铺注册功能模块-4Dto之ShopExecution的实现

1. DTO:添加店铺的返回类型

问题:为什么不直接用实体类Shop呢?

原因:在操作Shop的时候,必然会有一个状态。添加店铺,添加成功,还是添加失败? 如果添加失败,失败是一个什么状态,这些都是要记录的,并且要返回controller层去处理。

1.1 店铺操作枚举类ShopStateEnum.java

package com.csj2018.o2o.enums;

public enum ShopStateEnum {
	CHECK(0,"审核中"),
	OFFLINE(-1,"非法店铺"),
	SUCCESS(1,"操作成功"),
	PASS(2,"通过认证"),
	INNER_ERROR(-1001,"内部系统错误"),
	NULL_SHOPID(-1002,"ShopId为空");
	private int state;
	private String stateInfo;
	private ShopStateEnum(int state,String stateInfo) {
		this.state = state;
		this.stateInfo = stateInfo;
	}
	public static ShopStateEnum stateOf(int state) {
		for(ShopStateEnum stateEnum:values()) { //values()包含了枚举对象所有的值
			if(stateEnum.getState() == state) {
				return stateEnum;
			}
		}
		return null;
	}
	public int getState() {
		return state;
	}
	public String getStateInfo() {
		return stateInfo;
	}
}

1.2 ShopExecution.java

package com.csj2018.o2o.dto;

import java.util.List;

import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.enums.ShopStateEnum;

public class ShopExecution {
	//结果状态
	private int state;
	//状态标识
	private String stateInfo;
	//店铺数量
	private int count;
	//操作的shop(增删改查店铺的时候用到)
	private Shop shop;
	//shop列表(查询店铺列表的时候使用)
	private List<Shop> shopList;
	
	public ShopExecution() {}
	//店铺操作失败时使用的构造器
	public ShopExecution(ShopStateEnum stateEnum){
		this.state = stateEnum.getState();
		this.stateInfo = stateEnum.getStateInfo();
	}
	//店铺操作成功时使用的构造器
	public ShopExecution(ShopStateEnum stateEnum,Shop shop){
		this.state = stateEnum.getState();
		this.stateInfo = stateEnum.getStateInfo();
		this.shop = shop;
	}
	//店铺操作成功时使用的构造器
	public ShopExecution(ShopStateEnum stateEnum,List<Shop> shopList){
		this.state = stateEnum.getState();
		this.stateInfo = stateEnum.getStateInfo();
		this.shopList = shopList;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public String getStateInfo() {
		return stateInfo;
	}
	public void setStateInfo(String stateInfo) {
		this.stateInfo = stateInfo;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public Shop getShop() {
		return shop;
	}
	public void setShop(Shop shop) {
		this.shop = shop;
	}
	public List<Shop> getShopList() {
		return shopList;
	}
	public void setShopList(List<Shop> shopList) {
		this.shopList = shopList;
	}
	
}
posted on 2019-09-25 16:06  singleSpace  阅读(409)  评论(0编辑  收藏  举报