P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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();
    }
}