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
package biz.code.segment;
 
import biz.code.CodeRule;
import biz.code.SegmentCreator;
import foundation.persist.NamedSQL;
import foundation.persist.SQLRunner;
import foundation.util.ID;
import foundation.util.Util;
import foundation.variant.provider.IVariantsProvider;
 
 
public class NoSegment extends SegmentCreator {
 
    public static String SQLName_NextVal = "code_next_sequence";
    public static String SQLName_CurrVal = "code_curr_sequence";
    public static String SQLName_NextSync = "code_next_sync";
    private String ruleCode;
    private NamedSQL nextvalSQL;
    private NamedSQL currvalSQL;
    private NamedSQL nextsyncSQL;
    private String cachedValue;
 
    public static String SQL_GetPeriodSequence = "getPeriodSequence";
    public static String SQL_UpdatePeriodSequence = "updatePeriodSequence";
    public static String SQL_InsertPeriodSequence = "insertPeriodSequence";
    private NamedSQL getPeriodSequence;
    private NamedSQL updatePeriodSequence;
    private NamedSQL insertPeriodSequence;
    
    public NoSegment() {
 
    }
    
    @Override
    protected void init(CodeRule rule) {
        ruleCode = rule.getCode();
        
        try {
            nextvalSQL = NamedSQL.getInstance(SQLName_NextVal);
            nextvalSQL.setParam("code", ruleCode);
            
            nextsyncSQL = NamedSQL.getInstance(SQLName_NextSync);
            nextsyncSQL.setParam("code", ruleCode);
            
            currvalSQL = NamedSQL.getInstance(SQLName_CurrVal);
            currvalSQL.setParam("code", ruleCode);
            
            getPeriodSequence = NamedSQL.getInstance(SQL_GetPeriodSequence);
            getPeriodSequence.setParam("code", ruleCode);
            
            updatePeriodSequence = NamedSQL.getInstance(SQL_UpdatePeriodSequence);
            updatePeriodSequence.setParam("code", ruleCode);
            
            insertPeriodSequence = NamedSQL.getInstance(SQL_InsertPeriodSequence);
            insertPeriodSequence.setParam("code", ruleCode);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public String getNextSequenceValue(String dynamic, IVariantsProvider... entitys) {
        try {
            int value = 0 ;
            if(!Util.isEmpty(dynamic)){
                value = getPeriodNextSequenceNo(dynamic);
            }
            else {
                SQLRunner.execSQL(nextsyncSQL);
                value = SQLRunner.getInteger(currvalSQL);
            }
            
            
            String result = String.valueOf(value);
            result = format(result);
            
            cachedValue = result;
            return result;
        } 
        catch (Exception e) {
            return null;
        }
    }
 
    private int getPeriodNextSequenceNo(String dynamic) throws Exception {
        //1. 如果内存中没有记录,就获取一次,并插入一条记录
        NamedSQL namedSQL = getPeriodSequence.createInstance();
        namedSQL.setParam("periodNo", dynamic);
        namedSQL.setParam("code", ruleCode);
        int sequenceNo = SQLRunner.getInteger(namedSQL);
        
        //2. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
        if (sequenceNo == 0) {
            sequenceNo = 1;
            
            namedSQL = insertPeriodSequence.createInstance();
            namedSQL.setParam("id", ID.newValue());
            namedSQL.setParam("periodNo", dynamic);
            namedSQL.setParam("code", ruleCode);
            namedSQL.setParam("sequenceValue", sequenceNo);
            SQLRunner.execSQL(namedSQL);
        }
        
        //3. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
        else {
            sequenceNo = sequenceNo + 1;
            
            namedSQL = updatePeriodSequence.createInstance();
            namedSQL.setParam("value", sequenceNo);
            namedSQL.setParam("periodNo", dynamic);
            namedSQL.setParam("code", ruleCode);
            SQLRunner.execSQL(namedSQL);
        }
 
//        //4. 返回结果
//        String result = String.valueOf(sequenceNo);
//        result = format(result);
//        
//        cachedValue = result;
        return sequenceNo;
    }
 
    @Override
    public String getCurrSequenceValue(String dynamic, IVariantsProvider... entitys) {
        try {
            int value = SQLRunner.getInteger(nextvalSQL);
            String result = String.valueOf(value);
            result = format(result);
            
            cachedValue = result;
            return result;
        } 
        catch (Exception e) {
            return null;
        }
    }
 
    @Override
    public String getTempValue(String dynamic, IVariantsProvider... entitys) {
        String result = format("#");
        return result;
    }
    
    @Override
    protected void setCodeRule(CodeRule codeRule) {
        ruleCode = codeRule.getCode();
        super.setCodeRule(codeRule);
    }
 
    @Override
    public String toString() {
        return codeRule.getCode() + " : " + cachedValue;
    }
    
}