P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
package foundation.code.segment;
 
import foundation.code.CodeRule;
import foundation.code.SegmentCreator;
import foundation.period.ActivePeriod;
import foundation.persist.NamedSQL;
import foundation.persist.SQLRunner;
import foundation.util.ID;
import foundation.variant.provider.IVariantsProvider;
 
 
public class YearlyNoSegment extends SegmentCreator {
 
    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;
    private String ruleCode;
    private String cachedValue;
    private Integer sequenceNo;
    
    public YearlyNoSegment() {
        sequenceNo = null;
    }
    
    @Override
    protected void init(CodeRule rule) {
        ruleCode = rule.getCode();
        
        try {
            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) {
        synchronized (this) {
            NamedSQL namedSQL = null;
            int yearNo = ActivePeriod.getYear();
            
            try {
                //1. 如果内存中没有记录,就获取一次,并插入一条记录
                if (sequenceNo == null) {
                    namedSQL = getPeriodSequence.createInstance();
                    namedSQL.setParam("periodNo", yearNo);
                    sequenceNo = SQLRunner.getInteger(namedSQL);
                }
                
                //2. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
                if (sequenceNo == null) {
                    sequenceNo = 1;
                    
                    namedSQL = insertPeriodSequence.createInstance();
                    namedSQL.setParam("id", ID.newValue());
                    namedSQL.setParam("code", ruleCode);
                    namedSQL.setParam("periodNo", yearNo);
                    namedSQL.setParam("sequenceValue", sequenceNo);
                    SQLRunner.execSQL(namedSQL);
                }
                
                //3. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
                else {
                    sequenceNo = sequenceNo + 1;
                    
                    namedSQL = updatePeriodSequence.createInstance();
                    namedSQL.setParam("sequenceValue", sequenceNo);
                    SQLRunner.execSQL(namedSQL);
                }
 
                //4. 返回结果
                String result = String.valueOf(sequenceNo);
                result = format(result);
                
                cachedValue = result;
                return result;
            } 
            catch (Exception e) {
                return null;
            }
        }
    }
 
    @Override
    public String getCurrSequenceValue(String dynamic, IVariantsProvider... entitys) {
        synchronized (this) {
            NamedSQL namedSQL = null;
            int yearNo = ActivePeriod.getYear();
            
            try {
                //1. 如果内存中没有记录,就获取一次,并插入一条记录
                if (sequenceNo == null) {
                    namedSQL = getPeriodSequence.createInstance();
                    namedSQL.setParam("periodNo", yearNo);
                    sequenceNo = SQLRunner.getInteger(namedSQL);
                }
                
                //2. 如果数据库中没有记录,就插入一条记录(只有第一次会运行)
                if (sequenceNo == null) {
                    sequenceNo = 1;
                    
                    namedSQL = insertPeriodSequence.createInstance();
                    namedSQL.setParam("id", ID.newValue());
                    namedSQL.setParam("code", ruleCode);
                    namedSQL.setParam("periodNo", yearNo);
                    namedSQL.setParam("sequenceValue", sequenceNo);
                    SQLRunner.execSQL(namedSQL);
                }
                
                //3. 返回结果
                String result = String.valueOf(sequenceNo);
                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;
    }
    
}