package biz.target;
|
|
import java.math.BigDecimal;
|
|
import foundation.data.entity.Entity;
|
import foundation.data.object.DataObject;
|
import foundation.data.object.EntitySaver;
|
import foundation.engine.Engine;
|
import foundation.preload.Node;
|
import foundation.state.MachineBucket;
|
import foundation.state.State;
|
import foundation.state.StateMachine;
|
import foundation.state.StatePoint;
|
import foundation.token.IOnlineUser;
|
import foundation.util.MapList;
|
|
public class PositionTargetEngine extends Engine {
|
protected PositionTarget positionTarget;
|
protected IOnlineUser user;
|
protected TargetDBActions actions;
|
protected TargetType type;
|
protected Entity modifyEntity;
|
protected String key;
|
protected String dataName;
|
|
public PositionTargetEngine() {
|
actions = new TargetDBActions();
|
progressor.setDebug(true);
|
}
|
|
public void init(PositionTarget positionTarget, Entity entity) throws Exception {
|
this.positionTarget = positionTarget;
|
this.modifyEntity = entity;
|
|
String dataName = entity.getString("data_name");
|
type = TargetType.parse(dataName);
|
key = type.getKeyValue(entity);
|
}
|
|
public void init(PositionTarget positionTarget, String dataName) throws Exception {
|
this.positionTarget = positionTarget;
|
this.dataName = dataName;
|
}
|
|
@Override
|
protected void sameThreadRun(String operator, Object... args) throws Exception {
|
user = IOnlineUser.getInstance();
|
}
|
|
@Override
|
protected void newThreadRun(String operator, Object... args) {
|
try {
|
boolean isReset = false;
|
if ("issue".equals(operator)) {
|
issue();
|
return ;
|
}
|
else if ("confirm".equals(operator)) {
|
confirm();
|
return ;
|
}
|
if ("reset".equals(operator)){
|
isReset = true;
|
}
|
|
modify(isReset);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
private void modify(boolean isReset) {
|
try {
|
BigDecimal preQty = modifyEntity.getBigDecimal("pre_qty");
|
BigDecimal finalQty = modifyEntity.getBigDecimal("final_qty");
|
BigDecimal gapQty = finalQty.subtract(preQty);
|
BigDecimal preAmt = modifyEntity.getBigDecimal("pre_amt");
|
BigDecimal finalAmt = modifyEntity.getBigDecimal("final_amt");;
|
BigDecimal gapAmt = finalAmt.subtract(preAmt);
|
|
positionTarget.setGap(gapQty, gapAmt);
|
|
allocate(isReset);
|
agg();
|
logTgtRequestState(modifyEntity, true, false);
|
|
//3. 同步数据库
|
save();
|
logTgtRequestState(modifyEntity, true, true);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
private void confirm() throws Exception {
|
StateMachine machine = MachineBucket.getMachineByDataName(dataName);
|
StatePoint statePoint = machine.getStatePoint();
|
State from = statePoint.getFromState();
|
State end = statePoint.getEndState();
|
positionTarget.setState(end, this);
|
|
MapList<String, Node> children = positionTarget.getChildren();
|
for (Node child : children) {
|
PositionTarget target = (PositionTarget)child;
|
target.issue(from, this);
|
}
|
}
|
|
private void issue() throws Exception {
|
StateMachine machine = MachineBucket.getMachineByDataName(dataName);
|
StatePoint statePoint = machine.getStatePoint();
|
State from = statePoint.getFromState();
|
State end = statePoint.getEndState();
|
positionTarget.setState(end, this);
|
|
MapList<String, Node> children = positionTarget.getChildren();
|
for (Node child : children) {
|
PositionTarget target = (PositionTarget)child;
|
target.issue(from, this);
|
}
|
|
save();
|
}
|
|
private void allocate(boolean isReset) throws Exception {
|
positionTarget.activeLoad();
|
|
if (type == TargetType.Position) {
|
if (isReset) {
|
positionTarget.reset(this);
|
return ;
|
}
|
|
positionTarget.positionAllocate(key, this);
|
}
|
else if (type == TargetType.Customer) {
|
positionTarget.customerAllocate(key, this);
|
}
|
else if (type == TargetType.Product) {
|
positionTarget.productAllocate(key, this);
|
}
|
else if (type == TargetType.Sku) {
|
positionTarget.skuAllocate(key, this);
|
}
|
}
|
|
private void agg() throws Exception {
|
positionTarget.aggPosition(this);
|
|
positionTarget.aggPositionCustomer(this);
|
|
positionTarget.aggPositionProduct(this);
|
|
positionTarget.aggPositionSku(this);
|
}
|
|
public IOnlineUser getUser() {
|
return user;
|
}
|
|
private void save() throws Exception {
|
actions.execute();
|
}
|
|
private void logTgtRequestState(Entity entity, boolean syncMemory, boolean syncDB) throws Exception {
|
DataObject dataObject = DataObject.getInstance("tgt_log");
|
EntitySaver saver = dataObject.createEntitySaver(entity);
|
saver.set("is_sync_memory", syncMemory);
|
saver.set("is_sync_db", syncDB);
|
|
saver.update();
|
}
|
}
|