郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
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
package com.gk.firework.Scheduls.DL;
 
 
import com.gk.firework.Domain.Enterprise;
import com.gk.firework.Service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
@Component
@EnableScheduling
public class DeviceIdRobot {
 
    private static  final int deviceSize = 32;
    @Value("${com.gk.firework.schedules.single.switch}")
    private Boolean switchBtn;
    @Autowired
    private EnterpriseService enterpriseService;
 
 
 
    @Scheduled(cron = "0/3 * * * * ?") //每隔3秒执行一次
    public void deviceIdRobot(){
        if (!switchBtn) return;
        List<Enterprise> result =  enterpriseService.selectAllDlCompanyCodeIsNotNull();
        List<Enterprise> updates = null;
        if (result.size() > 0) {
            updates = new ArrayList<>();
            for (Enterprise e : result) {
                String enterprisenumber = e.getEnterprisenumber();
                String dlcompanycode = e.getDlcompanycode();
                String strs = dlcompanycode + enterprisenumber;
                int length = strs.length();
                if ( length < deviceSize) {
                    String deviceId = addZeroForNum(strs);
                    e.setDeviceid(deviceId);
                }else{
                    String deviceId = strs.substring(0, deviceSize);
                    e.setDeviceid(deviceId);
                }
                updates.add(e);
            }
        }
 
        if (updates != null && updates.size() > 0) {
            enterpriseService.updateBatchById(updates);
        }
    }
 
 
    private static String addZeroForNum(String str) {
        int strLen = str.length();
        if (strLen < DeviceIdRobot.deviceSize) {
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            while (sb.toString().length() < DeviceIdRobot.deviceSize) {
                sb.append("0");
                str = sb.toString();
            }
        }
        return str;
    }
}