JAVA 单步步骤数据处理

为了生成单步步骤条数据 且为了能更好的扩展

一个订单存在多个工种 每个工种存在多个工序
在统计显示各个工序的状态时 才有了以下的内容。

首先定义枚举类型 用于标识每个步骤节点的状态

public enum StepState {
	INIT(0, "未开始"), 
	LOADING(1, "进行中"), 
	FINISH(2, "已完成");
	
	@Getter
	Integer code;
	
	@Getter
	String name;

	StepState(Integer code, String name) {
		this.code = code;
		this.name = name;
	}
}

定义节点

public class Step{

	@Getter
	@Setter
	Consumer<Step> initCon;
	
	@Getter
	@Setter
	Predicate<Step> loadingPre;
	
	@Getter
	@Setter
	Predicate<Step> finishPre;

	Integer index;
	Integer state;

	@Getter
	StepState stepState;
	
	@Getter
	@Setter
	String name;

	@Getter
	@Setter
	Map data;
	
	Step(Integer index) {
		this.index = index;
		data = new HashMap();
	}
	
	public Integer getState() {
		return stepState.getCode();
	}
	
	public void setStepState(StepState stepState) {
		if (stepState == null) {
			return;
		}
		boolean flag = true;
		if (StepState.INIT.getCode().equals(stepState.getCode())) {
			if (initCon != null) {
				initCon.accept(this);
			}
		} else if (StepState.LOADING.getCode().equals(stepState.getCode())) {
			if (loadingPre != null) {
				flag = loadingPre.test(this);
			}
		} else if (StepState.FINISH.getCode().equals(stepState.getCode())) {
			if (finishPre != null) {
				flag = finishPre.test(this);
			}
		}
		if (flag) {
			this.stepState = stepState;
			this.state = stepState.getCode();
		}
	}	

}

定义步骤实体类

public class StepBean {

	// 当前到第几步
	@Getter
	private Integer index;
	// 总步记录
	@Getter
	private List<Step> value;
	
	public StepBean() {
		this(null);
	}
	
	public StepBean(String[] stepArr) {
		this(stepArr, null);
	}

	public StepBean(String[] stepArr, Consumer<Step> initCon) {
		this(stepArr, initCon, null, null);
	}

	public StepBean(String[] stepArr, Predicate<Step> loadingPre, Predicate<Step> finishPre) {
		this(stepArr, null, loadingPre, finishPre);
	}

    public StepBean(String[] stepArr, Consumer<Step> initCon, Predicate<Step> loadingPre, Predicate<Step> finishPre) {
        this.setStepArr(stepArr, initCon, loadingPre, finishPre);
    }
	
	/**
	 * 	
	 * @param stepArr 步数字符数组
	 * @param initCon 初始化事件
	 * @param loadingPre 进行中事件
	 * @param finishPre 完成事件
	 */
	public void setStepArr(String[] stepArr, Consumer<Step> initCon, Predicate<Step> loadingPre,
		Predicate<Step> finishPre) {
		value = new ArrayList<Step>();
		index = 0;
		if (stepArr != null && stepArr.length > 0) {
			for (int i = 0; i < stepArr.length; i++) {
				String stepName = stepArr[i];
				Step step = new Step(i);
				step.setInitCon(initCon);
				step.setLoadingPre(loadingPre);
				step.setFinishPre(finishPre);
				step.setName(stepName);
				step.setStepState(StepState.INIT);
				value.add(step);
			}
		}
	}
	
	private Step next() {
		if (value == null || index >= value.size()) {
			return null;
		}
		Step step = value.get(index);
		index++;
		return step;
	}
	
	private void finishPre(Integer index) {
		if (index == null || index <= 0 || index >= value.size()) {
			return;
		}
		for (int i = 0; i < index; i++) {
			Step otherStep = value.get(i);
			otherStep.setStepState(StepState.FINISH);
		}
	}

	public void loading() {
        this.loading(null);
    }	

	/**
	 * 	当前节点进行中
	 */
	public void loading(Map data) {
		Step step = this.next();
		if (step == null) {
			return;
		}
		step.setStepState(StepState.LOADING);
		step.setData(data);
		this.finishPre(getIndex() - 1);
	}

	public void finish() {
		this.finish(null);
	}

	/**
	 * 当前节点已完成
	 */
	public void finish(Map data) {
		Step step = this.next();
		if (step == null) {
			return;
		}
		step.setStepState(StepState.FINISH);
		step.setData(data);
		Integer curIndex = getIndex();
		this.finishPre(curIndex - 1);
		// 使下一个节点处于进行中状态
		if (curIndex >= value.size()) {
			return;
		}
		Step nextStep = value.get(curIndex);
		nextStep.setStepState(StepState.LOADING);
	}

	/**
	 * 当前节点不处理
	 */
	public void hold() {
        this.hold(null);
    }

    public void hold(Map data) {
        Step step = this.next();
        if (step == null) {
            return;
        }
        step.setData(data);
    }
}

测试

public static void main(String[] args) {
	String[] orderItems = { "下层机构", "上层机构"};
	String[] stepNames = { "售前合同签订", "销售订单生效", "配置上传" };
	List<Map> result = new ArrayList();
	for(String orderItem : orderItems ) {
		StepBean sb = new StepBean(stepNames);
		// 第一步
		sb.finish();
		// 第二步
		sb.loading();
		// 第三步

		Map m = new HashMap();
		m.put(orderItem, sb.getValue());
		result.add(m);
	}
	Gson gson = new Gson();
	System.out.println(gson.toJson(result));
}

生成的Json如下

[
	{"下层机构":
		[
			{
				"index":0,
				"state":2,
				"stepState":"FINISH",
				"name":"售前合同签订",
				"data":{}
			}, {
				"index":1,
				"state":1,
				"stepState":"LOADING",
				"name":"销售订单生效",
				"data":{}
			}, {
				"index":2,
				"state":0,
				"stepState":"INIT",
				"name":"配置上传",
				"data":{}
			}
		]
	}, {"上层机构":
		[
			{
				"index":0,
				"state":2,
				"stepState":"FINISH",
				"name":"售前合同签订",
				"data":{}
			}, {
				"index":1,
				"state":1,
				"stepState":"LOADING",
				"name":"销售订单生效",
				"data":{}
			},{
				"index":2,
				"state":0,
				"stepState":"INIT",
				"name":"配置上传",
				"data":{}
			}
		]
	}
]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值