为了生成单步步骤条数据 且为了能更好的扩展
一个订单存在多个工种 每个工种存在多个工序
在统计显示各个工序的状态时 才有了以下的内容。
首先定义枚举类型 用于标识每个步骤节点的状态
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":{}
}
]
}
]