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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package biz.target;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;
 
import foundation.data.entity.Entity;
import foundation.data.entity.EntityNode;
import foundation.data.entity.EntitySet;
import foundation.data.entity.EntityTree;
import foundation.data.entity.Filter;
import foundation.data.entity.GroupBy;
import foundation.data.getter.EntityTreeGetter;
import foundation.data.object.DataObject;
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.util.Util;
 
public class PotentialInstance implements IJSONProvider {
    private static DataObject temTargetHospitalObject = DataObject.getInstance("temp_tgt_project_detail");
    private static DataObject targetHospitalObject = DataObject.getInstance("tgt_project_detail");
    private static DataObject temTargetPositionObject = DataObject.getInstance("temp_tgt_distribution_position");
    private Potential potential;
    
    private Date hospitalLastBuild;
    private Date positionLastBuild;
    private EntitySet hospitalSet;
    private EntityTree positionTree;
    private EntitySet positionSet;
    private boolean hospitalBuild;
    private boolean positionBuild;
    private boolean hospitalSyncDB;
    private boolean positionSyncDB;
 
    public PotentialInstance(Potential potential) {
        hospitalBuild = false;
        positionBuild = false;
        hospitalSyncDB = false;
        positionSyncDB = false;
        this.potential = potential;
        
        try {
            this.hospitalSet = temTargetHospitalObject.createTableEntitySet();
            this.positionSet = temTargetPositionObject.createTableEntitySet();
        }catch (Exception e) {
        }
    }
 
    public void saveHospitalToDB(String state) throws Exception {
        if (hospitalSyncDB || !hospitalBuild ) {
            return ;
        }
        
        //保存至数据库
        if (Util.isEmpty(state)) {
            return ;
        }
        else if ("预设".equals(state)) {
            temTargetHospitalObject.deleteEntity(new Filter("sort_id", potential.getId()));
            temTargetHospitalObject.batchInsertEntitySet(hospitalSet);
        }
        else if ("已下发".equals(state)) {
            targetHospitalObject.deleteEntity(new Filter("sort_id", potential.getId()));
            targetHospitalObject.batchInsertEntitySet(hospitalSet);
        }
        
        hospitalSyncDB = true;
    }
 
    public void buildPositionTree() throws Exception {
        String sum = "sum(target_increase_amt)/(sum(target_amt) -sum(target_increase_amt)) target_rate_cal, "
                + "sum(current_forecast_amt) current_forecast_amt, "
                + "sum(current_forecast_rate) current_forecast_rate, "
                + "sum(target_amt) target_amt_sum, "
                + "sum(target_rate_actual) target_rate_actual, "
                + "sum(target_amt_actual) target_amt_actual ";
        GroupBy groupBy = new GroupBy("position_id");
        EntitySet postionSet = temTargetHospitalObject.getSummary(sum, "", null, groupBy, null);
 
        EntityTreeGetter postionTreeGetter = new EntityTreeGetter(postionSet.getEntityMeta());
        postionTreeGetter.loadData(postionSet);
        
        positionTree = postionTreeGetter.getEntityTree();
        
        positionSet = temTargetPositionObject.createTableEntitySet();
        HashMap<String, BigDecimal> accumulateList = new HashMap<String, BigDecimal>();
        accumulateList.put("current_forecast_amt", BigDecimal.ZERO);
        accumulateList.put("current_forecast_rate", BigDecimal.ZERO);
        accumulateList.put("target_amt_sum", BigDecimal.ZERO);
        accumulateList.put("target_increase_amt", BigDecimal.ZERO);
        accumulateList.put("target_rate_cal", BigDecimal.ZERO);
        accumulateList.put("target_rate_actual", BigDecimal.ZERO);
        accumulateList.put("target_amt_actual", BigDecimal.ZERO);
        
        EntitySet rootSet = positionTree.getRootSet();
        int levelNo = 0;
        creatPositionNode(rootSet, levelNo, accumulateList);
        
        positionSyncDB = false;
        positionBuild = true;
        positionLastBuild = new Date();
    }
    
    public void creatPositionNode(EntitySet rootSet, int levelNo, HashMap<String, BigDecimal> accumulateList) throws Exception {
        for (Entity entity: rootSet) {
            EntityNode node = (EntityNode)entity;
            
            // 1. 计算节点汇总
            for (Entry<String, BigDecimal> entry : accumulateList.entrySet()) {
                String field = entry.getKey();
                BigDecimal accumulateValue = entry.getValue();
                BigDecimal value = entity.getBigDecimal(field);
                accumulateValue = accumulateValue.add(value);
                entry.setValue(accumulateValue);
                entity.set(field, accumulateValue);
            }
            
            levelNo = node.getLevel() + 1;
            entity.set("level_no", levelNo);
            positionSet.append(entity);
            
            
            // 2. 递归便利子节点
            EntitySet children = node.getChildren();
            
            if (children != null) {
                creatPositionNode(children, levelNo, accumulateList);
            }
        }
    }
    
    public void syncHospital() throws Exception {
        for (Entity hospital : hospitalSet) {
            BigDecimal potientialAmt = TargetOperator.TotalTarget.multiply(potential.getRate());
            BigDecimal currentForecastAmt = hospital.getBigDecimal("current_forecast_amt");
            hospital.set("potential_amt", potientialAmt);
            hospital.set("target_amt", potientialAmt.add(currentForecastAmt));
            hospital.set("target_increase_amt", potientialAmt);
            hospital.set("potential_rate", potential.getRate());
        }
        
        hospitalLastBuild = new Date();
        hospitalBuild = true;
    }
    
    public void savePositionTreeToDB() throws Exception {
        if (positionSyncDB) {
            return ;
        }
        
        if (!positionBuild) {
            buildPositionTree();
        }
        
        //保存至数据库
        temTargetPositionObject.emptyTable();
        temTargetPositionObject.batchInsertEntitySet(positionSet);
        
        positionSyncDB = true;
    }
    
    public void setPotentialRate(BigDecimal potentialRate) {
        potential.setRate(potentialRate);
        
        for (Entity entity : hospitalSet) {
            entity.set("potential_rate", potentialRate);
            BigDecimal currentForecastAmt = entity.getBigDecimal("current_forecast_amt", BigDecimal.ZERO);
            entity.set("potential_amt", currentForecastAmt.add(potentialRate.multiply(TargetOperator.TotalTarget)));
        }
 
        hospitalBuild = true;
        hospitalSyncDB = false;
    }
    
    public void loadOneHospital(Entity hospital) {
        hospitalSet.append(hospital);
    }
 
    public String getId() {
        return potential.getId();
    }
    
    public boolean isHospitalBuild() {
        return hospitalBuild;
    }
    
    public boolean isPositionBuild() {
        return positionBuild;
    }
    
    public EntitySet getHospitalSet() {
        return hospitalSet;
    }
    
    public void setHospitalSet(EntitySet hospitalSet) {
        this.hospitalSet = hospitalSet;
    }
    
    public EntityTree getPositionTree() {
        return positionTree;
    }
    
    public void setPositionTree(EntityTree positionTree) {
        this.positionTree = positionTree;
    }
 
    public String getName() {
        return potential.getRemark();
    }
 
    public BigDecimal getRate() {
        return potential.getRate();
    }
 
    public BigDecimal getAmt() {
        return TargetOperator.TotalTarget.multiply(potential.getRate());
    }
 
    public boolean isValid(BigDecimal[] values) {
        return potential.isVaild(values);
    }
 
    public TargetPotentialType getTargetType() {
        return potential.getTargetType();
    }
    
    public void setHospitalBuild(boolean hospitalBuild) {
        this.hospitalBuild = hospitalBuild;
    }
 
    public Date getHospitalLastBuild() {
        return hospitalLastBuild;
    }
    
    public void setPositionBuild(boolean positionBuild) {
        this.positionBuild = positionBuild;
    }
 
    public Date getPositionLastBuild() {
        return positionLastBuild;
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject();
        //1. potential
        potential.writeJSONBody(writer);
        
        //2. hospitals
        if (hospitalSet == null) {
            writer.writeNull("hospitals");
        }
        else {
            writer.beginArray("hospitals");
            hospitalSet.writeJSONBody(writer);
            writer.endArray();
        }
        
        //3. position
        if (positionTree == null) {
            writer.writeNull("positions");
        }
        else {    
            writer.beginArray("positions");
            positionTree.writeJSONBody(writer);
            writer.endArray();
        }
        
        writer.endObject();
    }
}