ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
package jnpf.base.util;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import jnpf.base.entity.BillNumEntity;
import jnpf.base.service.BillNumService;
import jnpf.constant.MsgCode;
import jnpf.exception.DataException;
import jnpf.model.visualJson.FieLdsModel;
import jnpf.model.visualJson.config.ConfigModel;
import jnpf.model.visualJson.config.PrefixSuffixModel;
import jnpf.model.visualJson.config.RuleConfig;
import jnpf.util.*;
import jnpf.util.visiual.JnpfKeyConsts;
import lombok.Synchronized;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
@Component
public class VisualBillUtil {
    @Autowired
    private BillNumService billNumService;
 
    @Synchronized
    @DS("")
    public Object getBillNumber(String visualId, FieLdsModel fieLdsModel, Map<String, Object> data, Object thisValue) {
        String jnpfKey = fieLdsModel.getConfig().getJnpfKey();
        if (JnpfKeyConsts.BILLRULE.equals(jnpfKey) && fieLdsModel.getConfig().getRuleType() != null
                && Objects.equals(fieLdsModel.getConfig().getRuleType(), 2) && ObjectUtil.isEmpty(thisValue)) {
            ConfigModel config = fieLdsModel.getConfig();
            RuleConfig ruleConfig = config.getRuleConfig();
            Integer type = ruleConfig.getType();
            String flowId = null;
            if (data.get(FlowFormConstant.FLOWID) != null && StringUtil.isNotEmpty(data.get(FlowFormConstant.FLOWID).toString())) {
                flowId = data.get(FlowFormConstant.FLOWID).toString();
            }
 
            StringBuilder strNumber = new StringBuilder();
            //前缀
            String preFixStr = setPreSuffFix(data, ruleConfig.getPrefixList());
            if (StringUtil.isNotEmpty(preFixStr) && preFixStr.length() > 100) {
                throw new DataException(MsgCode.VS027.get(config.getLabel()));
            }
            strNumber.append(preFixStr);
 
            String ruleId = config.getFormId();
            BillNumEntity billNum = billNumService.getBillNum(ruleId, visualId, flowId);
            switch (type) {
                case 2:
                    // 随机数编号
                    if (ObjectUtil.equal(ruleConfig.getRandomType(), 1)) {
                        strNumber.append(cn.hutool.core.util.RandomUtil.randomNumbers(ruleConfig.getRandomDigit()));
                    } else {
                        strNumber.append(cn.hutool.core.util.RandomUtil.randomStringUpper(ruleConfig.getRandomDigit()));
                    }
                    if (billNum != null) {
                        billNumService.removeByRuleId(ruleId, visualId, flowId);
                    }
                    break;
                case 3:
                    // UUID
                    strNumber.append(IdUtil.randomUUID().toUpperCase());
                    if (billNum != null) {
                        billNumService.removeByRuleId(ruleId, visualId, flowId);
                    }
                    break;
                default:
                    // 时间格式
                    RuleConfig rule = BeanUtil.copyProperties(ruleConfig, RuleConfig.class);
                    rule.setRandomDigit(null);
                    rule.setRandomType(null);
                    String ruleJosn = JsonUtil.getObjectToString(rule);
 
                    String dateFormat = getTimeFormat(ruleConfig.getDateFormat());
                    String dateValue = "no".equals(dateFormat) ? "" : DateUtil.dateNow(dateFormat);
                    //获取位数最大值
                    Integer digit = ruleConfig.getDigit();
                    StringBuilder maxStr = new StringBuilder();
                    for (int i = 0; i < digit; i++) {
                        maxStr.append("9");
                    }
                    Integer maxValue = Integer.parseInt(maxStr.toString());
                    //起始值
                    Integer startNumber = Integer.parseInt(ruleConfig.getStartNumber());
                    Integer thisNum = 0;
 
                    //处理流水号归0
                    if (billNum != null) {
                        if (ruleJosn.equals(billNum.getRuleConfig())) {
                            String dateValueOld = billNum.getDateValue();
                            //判断时间值是否一致,一致流水号递增,不一致则重置流水号
                            if (StringUtil.isEmpty(dateValueOld) || dateValueOld.equals(dateValue)) {
                                thisNum = billNum.getNum() + 1;
                                if (startNumber + thisNum > maxValue) {
                                    thisNum = 0;
                                }
                            }
                        }
                    } else {
                        billNum = new BillNumEntity();
                    }
                    billNum.setRuleId(ruleId);
                    billNum.setVisualId(visualId);
                    billNum.setFlowId(flowId);
                    billNum.setDateValue(dateValue);
                    billNum.setNum(thisNum);
                    billNum.setRuleConfig(ruleJosn);
                    billNumService.saveBillNum(billNum);
 
                    if (!"no".equals(dateValue)) {
                        strNumber.append(dateValue);
                    }
                    strNumber.append(PadUtil.padRight(String.valueOf(startNumber + thisNum), ruleConfig.getDigit(), '0'));
                    break;
            }
            //后缀
            String suffFixStr = setPreSuffFix(data, ruleConfig.getSuffixList());
            if (StringUtil.isNotEmpty(suffFixStr) && suffFixStr.length() > 100) {
                throw new DataException(MsgCode.VS027.get(config.getLabel()));
            }
            strNumber.append(suffFixStr);
            return strNumber.toString();
        } else {
            return thisValue;
        }
    }
 
    /**
     * 获取时间格式
     *
     * @param dateFor
     * @return
     */
    private static String getTimeFormat(String dateFor) {
        String dateForValue = "no";
        if (StringUtil.isEmpty(dateFor)) {
            return dateForValue;
        }
        switch (dateFor) {
            case "YYYY":
                dateForValue = "yyyy";
                break;
            case "YYYYMM":
                dateForValue = "yyyyMM";
                break;
            case "YYYYMMDD":
                dateForValue = "yyyyMMdd";
                break;
            case "YYYYMMDDHH":
                dateForValue = "yyyyMMddHH";
                break;
            case "YYYYMMDDHHmm":
                dateForValue = "yyyyMMddHHmm";
                break;
            case "YYYYMMDDHHmmss":
                dateForValue = "yyyyMMddHHmmss";
                break;
            case "YYYYMMDDHHmmssSSS":
                dateForValue = "yyyyMMddHHmmssSSS";
                break;
        }
        return dateForValue;
    }
 
    /**
     * 设置前后缀的值
     *
     * @param data
     * @param list
     */
    private static String setPreSuffFix(Map<String, Object> data, List<PrefixSuffixModel> list) {
        StringBuilder sb = new StringBuilder();
        if (CollectionUtils.isNotEmpty(list)) {
            for (PrefixSuffixModel prefix : list) {
                //sourtype = 2自定义,1表单字段
                if (Objects.equals(prefix.getSourceType(), 2)) {
                    sb.append(prefix.getRelationField());
                } else {
                    if (StringUtil.isNotEmpty(prefix.getRelationField())) {
                        if (data.get(prefix.getRelationField()) != null) {
                            sb.append(data.get(prefix.getRelationField()));
                        } else if (prefix.getRelationField().toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX)) {
                            String[] split = prefix.getRelationField().split("-");
                            if (split.length == 2 && data.get(split[1]) != null) {
                                sb.append(data.get(split[1]));
                            }
                        }
                    }
                }
            }
        }
        return sb.toString();
    }
}