com.nbsaas.boot.no.rest.generator.StepGenerator Maven / Gradle / Ivy
The newest version!
package com.nbsaas.boot.no.rest.generator;
import com.nbsaas.boot.no.api.apis.NoSequenceApi;
import com.nbsaas.boot.no.api.domain.field.NoSequenceField;
import com.nbsaas.boot.no.api.domain.request.NoSequenceRequest;
import com.nbsaas.boot.no.api.domain.response.NoSequenceResponse;
import com.nbsaas.boot.no.ext.apis.Generator;
import com.nbsaas.boot.rest.filter.Filter;
import java.util.Date;
// 步长生成器实现
public class StepGenerator implements Generator {
private int stepLength;
private String stepKey;
private NoSequenceApi noSequenceApi;
public StepGenerator(String stepKey, NoSequenceApi noSequenceApi, int stepLength) {
this.stepLength = stepLength;
this.noSequenceApi = noSequenceApi;
this.stepKey = stepKey;
}
@Override
public String generate() {
NoSequenceResponse no = noSequenceApi.oneData(Filter.eq(NoSequenceField.stepKey, stepKey));
if (no==null){
NoSequenceRequest req=new NoSequenceRequest();
req.setStep(1);
req.setStepKey(stepKey);
req.setCurStep(0L);
req.setAddDate(new Date());
req.setLastDate(new Date());
no = noSequenceApi.createData(req);
}
Long currentStep=no.getCurStep()+no.getStep();
String stepPart = String.format("%0" + stepLength + "d", currentStep);
NoSequenceRequest request=new NoSequenceRequest();
request.setId(no.getId());
request.setCurStep(currentStep);
request.setLastDate(new Date());
noSequenceApi.update(request);
return stepPart;
}
}