kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
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
package com.nanometer.smartlab.service;
 
import com.nanometer.smartlab.dao.SysSequenceDao;
import com.nanometer.smartlab.entity.SysSequence;
import com.nanometer.smartlab.util.Constants;
import com.nanometer.smartlab.util.Utils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
 
/**
 * Created by johnny on 17/12/13.
 */
@Service("sysSequenceService")
@Scope("singleton")
public class SysSequenceServiceImpl implements SysSequenceService {
 
    private static Logger logger = Logger.getLogger(SysSequenceService.class);
 
    @Resource(name = "sysSequenceDao")
    SysSequenceDao sysSequenceDao;
 
    public synchronized int getNextVal(String seqName) {
        int result = -1;
        try {
            if (seqName!=null) {
                SysSequence sequence = this.sysSequenceDao.getSysSequenceByName(seqName);
                int currentVal =0;
                if (sequence == null) {
                    sequence = new SysSequence();
                    sequence.setName(seqName);
                    sequence.setMinVal(0);
                    sequence.setMaxVal(99999);
                    sequence.setStep(1);
                    currentVal = sequence.getStep();
                    sequence.setCurrentVal(currentVal);
                    this.sysSequenceDao.insertSysSequence(sequence);
                }else{
                    currentVal = sequence.getCurrentVal() + sequence.getStep();
                    if (currentVal > sequence.getMaxVal()) {
                        currentVal = sequence.getMinVal();
                    }
                    sequence.setCurrentVal(currentVal);
                    this.sysSequenceDao.updateSysSequence(sequence);
                }
                result = currentVal;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = -1;
        }
 
        return result;
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public String getApplyCode() {
        StringBuffer result = new StringBuffer("");
        result.append("SG");
        result.append(Utils.formatDateToString(new Date(), "yyyyMMdd"));
 
        int seq = this.getNextVal(Constants.SEQ_APPLY_CODE);
        String seqStr = "";
        if (seq == -1) {
            seqStr = String.valueOf(RandomUtils.nextInt(5));
        } else {
            seqStr = String.valueOf(seq);
            if (seqStr.length() < 5) {
                seqStr = String.format("%05d", seq);
            }
        }
 
        result.append(seqStr);
 
        return result.toString();
    }
 
    public String getOrderCode() {
        StringBuffer result = new StringBuffer("");
        result.append("OD");
        result.append(Utils.formatDateToString(new Date(), "yyyyMMdd"));
 
        int seq = this.getNextVal(Constants.SEQ_ORDER_CODE);
        String seqStr = "";
        if (seq == -1) {
            seqStr = String.valueOf(RandomUtils.nextInt(5));
        } else {
            seqStr = String.valueOf(seq);
            if (seqStr.length() < 5) {
                seqStr = String.format("%05d", seq);
            }
        }
        result.append(seqStr);
        return result.toString();
    }
}